Skip to content

Commit 3e275a1

Browse files
committed
PLUGINAPI-42 Add unit tests
1 parent 8c10020 commit 3e275a1

File tree

5 files changed

+260
-1
lines changed

5 files changed

+260
-1
lines changed

plugin-api/src/main/java/org/sonar/api/server/http/HttpRequest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
*/
2929
public interface HttpRequest {
3030

31-
3231
/**
3332
* Returns the port number to which the request was sent.
3433
*/
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-2023 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.server.http;
21+
22+
import jakarta.servlet.http.HttpServletRequest;
23+
import java.util.Enumeration;
24+
import org.junit.Test;
25+
26+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
27+
import static org.mockito.Mockito.mock;
28+
import static org.mockito.Mockito.when;
29+
30+
public class JakartaHttpRequestTest {
31+
32+
@Test
33+
public void initRequest() {
34+
HttpServletRequest requestMock = mock(HttpServletRequest.class);
35+
when(requestMock.getServerPort()).thenReturn(80);
36+
when(requestMock.isSecure()).thenReturn(true);
37+
when(requestMock.getScheme()).thenReturn("https");
38+
when(requestMock.getServerName()).thenReturn("hostname");
39+
when(requestMock.getRequestURL()).thenReturn(new StringBuffer("https://hostname:80/path"));
40+
when(requestMock.getRequestURI()).thenReturn("/path");
41+
when(requestMock.getQueryString()).thenReturn("param1=value1");
42+
when(requestMock.getContextPath()).thenReturn("/path");
43+
when(requestMock.getMethod()).thenReturn("POST");
44+
when(requestMock.getParameter("param1")).thenReturn("value1");
45+
when(requestMock.getParameterValues("param1")).thenReturn(new String[]{"value1"});
46+
when(requestMock.getHeader("header1")).thenReturn("hvalue1");
47+
Enumeration<String> headers = mock(Enumeration.class);
48+
when(requestMock.getHeaders("header1")).thenReturn(headers);
49+
50+
JakartaHttpRequest request = new JakartaHttpRequest(requestMock);
51+
52+
assertThat(request.getRawRequest()).isSameAs(requestMock);
53+
assertThat(request.getServerPort()).isEqualTo(80);
54+
assertThat(request.isSecure()).isTrue();
55+
assertThat(request.getScheme()).isEqualTo("https");
56+
assertThat(request.getServerName()).isEqualTo("hostname");
57+
assertThat(request.getRequestURL()).isEqualTo("https://hostname:80/path");
58+
assertThat(request.getRequestURI()).isEqualTo("/path");
59+
assertThat(request.getQueryString()).isEqualTo("param1=value1");
60+
assertThat(request.getContextPath()).isEqualTo("/path");
61+
assertThat(request.getMethod()).isEqualTo("POST");
62+
assertThat(request.getParameter("param1")).isEqualTo("value1");
63+
assertThat(request.getParameterValues("param1")).containsExactly("value1");
64+
assertThat(request.getHeader("header1")).isEqualTo("hvalue1");
65+
assertThat(request.getHeaders("header1")).isEqualTo(headers);
66+
}
67+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-2023 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.server.http;
21+
22+
import jakarta.servlet.http.HttpServletResponse;
23+
import java.io.IOException;
24+
import java.io.PrintWriter;
25+
import java.util.List;
26+
import org.junit.Test;
27+
28+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
29+
import static org.mockito.Mockito.mock;
30+
import static org.mockito.Mockito.verify;
31+
import static org.mockito.Mockito.when;
32+
33+
public class JakartaHttpResponseTest {
34+
35+
@Test
36+
public void initResponse() throws IOException {
37+
HttpServletResponse responseMock = mock(HttpServletResponse.class);
38+
when(responseMock.getHeader("h1")).thenReturn("hvalue1");
39+
when(responseMock.getHeaders("h1")).thenReturn(List.of("hvalue1"));
40+
when(responseMock.getStatus()).thenReturn(200);
41+
PrintWriter writer = mock(PrintWriter.class);
42+
when(responseMock.getWriter()).thenReturn(writer);
43+
44+
JakartaHttpResponse response = new JakartaHttpResponse(responseMock);
45+
46+
assertThat(response.getRawResponse()).isSameAs(responseMock);
47+
assertThat(response.getHeader("h1")).isEqualTo("hvalue1");
48+
assertThat(response.getHeaders("h1")).asList().containsExactly("hvalue1");
49+
assertThat(response.getStatus()).isEqualTo(200);
50+
assertThat(response.getWriter()).isEqualTo(writer);
51+
52+
response.addHeader("h2", "hvalue2");
53+
response.setHeader("h3", "hvalue3");
54+
response.setStatus(201);
55+
response.setContentType("text/plain");
56+
response.sendRedirect("http://redirect");
57+
verify(responseMock).addHeader("h2", "hvalue2");
58+
verify(responseMock).setHeader("h3", "hvalue3");
59+
verify(responseMock).setStatus(201);
60+
verify(responseMock).setContentType("text/plain");
61+
verify(responseMock).sendRedirect("http://redirect");
62+
}
63+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-2023 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.server.http;
21+
22+
import java.util.Enumeration;
23+
import javax.servlet.http.HttpServletRequest;
24+
import org.junit.Test;
25+
26+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
27+
import static org.mockito.Mockito.mock;
28+
import static org.mockito.Mockito.when;
29+
30+
public class JavaxHttpRequestTest {
31+
32+
@Test
33+
public void initRequest() {
34+
HttpServletRequest requestMock = mock(HttpServletRequest.class);
35+
when(requestMock.getServerPort()).thenReturn(80);
36+
when(requestMock.isSecure()).thenReturn(true);
37+
when(requestMock.getScheme()).thenReturn("https");
38+
when(requestMock.getServerName()).thenReturn("hostname");
39+
when(requestMock.getRequestURL()).thenReturn(new StringBuffer("https://hostname:80/path"));
40+
when(requestMock.getRequestURI()).thenReturn("/path");
41+
when(requestMock.getQueryString()).thenReturn("param1=value1");
42+
when(requestMock.getContextPath()).thenReturn("/path");
43+
when(requestMock.getMethod()).thenReturn("POST");
44+
when(requestMock.getParameter("param1")).thenReturn("value1");
45+
when(requestMock.getParameterValues("param1")).thenReturn(new String[]{"value1"});
46+
when(requestMock.getHeader("header1")).thenReturn("hvalue1");
47+
Enumeration<String> headers = mock(Enumeration.class);
48+
when(requestMock.getHeaders("header1")).thenReturn(headers);
49+
50+
JavaxHttpRequest request = new JavaxHttpRequest(requestMock);
51+
52+
assertThat(request.getRawRequest()).isSameAs(requestMock);
53+
assertThat(request.getServerPort()).isEqualTo(80);
54+
assertThat(request.isSecure()).isTrue();
55+
assertThat(request.getScheme()).isEqualTo("https");
56+
assertThat(request.getServerName()).isEqualTo("hostname");
57+
assertThat(request.getRequestURL()).isEqualTo("https://hostname:80/path");
58+
assertThat(request.getRequestURI()).isEqualTo("/path");
59+
assertThat(request.getQueryString()).isEqualTo("param1=value1");
60+
assertThat(request.getContextPath()).isEqualTo("/path");
61+
assertThat(request.getMethod()).isEqualTo("POST");
62+
assertThat(request.getParameter("param1")).isEqualTo("value1");
63+
assertThat(request.getParameterValues("param1")).containsExactly("value1");
64+
assertThat(request.getHeader("header1")).isEqualTo("hvalue1");
65+
assertThat(request.getHeaders("header1")).isEqualTo(headers);
66+
}
67+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-2023 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.server.http;
21+
22+
import java.io.IOException;
23+
import java.io.PrintWriter;
24+
import java.util.List;
25+
import javax.servlet.http.HttpServletResponse;
26+
import org.junit.Test;
27+
28+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
29+
import static org.mockito.Mockito.mock;
30+
import static org.mockito.Mockito.verify;
31+
import static org.mockito.Mockito.when;
32+
33+
public class JavaxHttpResponseTest {
34+
35+
@Test
36+
public void initResponse() throws IOException {
37+
HttpServletResponse responseMock = mock(HttpServletResponse.class);
38+
when(responseMock.getHeader("h1")).thenReturn("hvalue1");
39+
when(responseMock.getHeaders("h1")).thenReturn(List.of("hvalue1"));
40+
when(responseMock.getStatus()).thenReturn(200);
41+
PrintWriter writer = mock(PrintWriter.class);
42+
when(responseMock.getWriter()).thenReturn(writer);
43+
44+
JavaxHttpResponse response = new JavaxHttpResponse(responseMock);
45+
46+
assertThat(response.getRawResponse()).isSameAs(responseMock);
47+
assertThat(response.getHeader("h1")).isEqualTo("hvalue1");
48+
assertThat(response.getHeaders("h1")).asList().containsExactly("hvalue1");
49+
assertThat(response.getStatus()).isEqualTo(200);
50+
assertThat(response.getWriter()).isEqualTo(writer);
51+
52+
response.addHeader("h2", "hvalue2");
53+
response.setHeader("h3", "hvalue3");
54+
response.setStatus(201);
55+
response.setContentType("text/plain");
56+
response.sendRedirect("http://redirect");
57+
verify(responseMock).addHeader("h2", "hvalue2");
58+
verify(responseMock).setHeader("h3", "hvalue3");
59+
verify(responseMock).setStatus(201);
60+
verify(responseMock).setContentType("text/plain");
61+
verify(responseMock).sendRedirect("http://redirect");
62+
}
63+
}

0 commit comments

Comments
 (0)