Skip to content

Commit

Permalink
Fix sonar coverage (#306)
Browse files Browse the repository at this point in the history
* add test for sonar coverage

* removed unused imports

* renamed test case

* CCD-5367 add coverage tests

* CCD-5367 add coverage tests

* CCD-5367 fix sonar issues

* CCD-5367 fix sonar issues

---------

Co-authored-by: Dinesh Patel <[email protected]>
  • Loading branch information
markdathornehmcts and dinesh1patel committed Sep 4, 2024
1 parent 6575482 commit e0d0496
Showing 1 changed file with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
Expand All @@ -19,17 +21,21 @@

import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpResponse;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -71,6 +77,7 @@ void shouldReturnStatusOK_ForwardGetRequests() throws IOException, InterruptedEx
assertThat(responseEntityReturned.getStatusCode(), is(HttpStatus.OK));
}

@SuppressWarnings("unchecked")
@Test
@DisplayName("Test for forwardGetRequests() with query parameter status OK")
void shouldReturnStatusOK_ForwardGetRequestsWhenQueryParametersPresent()
Expand All @@ -79,8 +86,8 @@ void shouldReturnStatusOK_ForwardGetRequestsWhenQueryParametersPresent()
String[] value = {"1"};
when(mockRequest.getParameterMap()).thenReturn(Map.of("id", value));

HttpResponse mockResponse = mock(HttpResponse.class);
Mockito.doReturn(mockResponse).when(mockHttpClient).send(any(), any());
HttpResponse<String> mockResponse = mock(HttpResponse.class);
when(mockHttpClient.<String>send(any(), any())).thenReturn(mockResponse);
when(mockResponse.body()).thenReturn("MOCK BODY");
when(mockResponse.statusCode()).thenReturn(200);

Expand All @@ -106,28 +113,41 @@ void shouldThrowException_ForwardGetRequests() throws IOException, InterruptedEx
/**
* Forward POST requests unit test with status OK.
*/
@SuppressWarnings("unchecked")
@Test
@DisplayName("Test for forwardPostRequests() status OK")
void shouldReturnStatusOK_ForwardPostRequests() throws IOException, InterruptedException {
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
HttpResponse mockResponse = mock(HttpResponse.class);
Mockito.doReturn(mockResponse).when(mockHttpClient).send(any(), any());
stubResponseController = new StubResponseController(mockHttpClient, mockHttpServer, mapper);
HttpHeaders headers = HttpHeaders.of(
Map.of("Header-Name", List.of("Header-Value")), (s1, s2) -> true);
HttpResponse<String> mockResponse = mock(HttpResponse.class);
when(mockHttpClient.<String>send(any(), any())).thenReturn(mockResponse);
when(mockResponse.body()).thenReturn("MOCK BODY");
when(mockResponse.statusCode()).thenReturn(200);
when(mockResponse.headers()).thenReturn(headers);

HttpServletRequest mockRequest = mock(HttpServletRequest.class);
ResponseEntity<Object> responseEntityReturned = stubResponseController.forwardPostRequests(mockRequest);
assertNotNull(responseEntityReturned);
assertThat(responseEntityReturned.getStatusCode(), is(HttpStatus.OK));
}


static Stream<Map<String, List<String>>> headersProvider() {
return Stream.of(
Collections.emptyMap(),
Map.of("Header-Name-1", List.of("Header-Value1"), "Client-Context", List.of("Header-Value2"))
);
}

/**
* Forward POST requests unit test with status OK.
*/
@Test
@DisplayName("Test for forwardPostRequests() status OK - empty custom headers")
void shouldReturnStatusOK_ForwardPostRequestsForEmptyCustomHeaders() throws IOException, InterruptedException {
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
HttpResponse mockResponse = mock(HttpResponse.class);
stubResponseController = new StubResponseController(mockHttpClient, mockHttpServer, mapper);
Mockito.doReturn(mockResponse).when(mockHttpClient).send(any(), any());
when(mockResponse.body()).thenReturn("MOCK BODY");

Expand All @@ -136,6 +156,51 @@ void shouldReturnStatusOK_ForwardPostRequestsForEmptyCustomHeaders() throws IOEx

when(mockResponse.statusCode()).thenReturn(200);

HttpServletRequest mockRequest = mock(HttpServletRequest.class);
ResponseEntity<Object> responseEntityReturned = stubResponseController.forwardPostRequests(mockRequest);
assertNotNull(responseEntityReturned);
assertThat(responseEntityReturned.getStatusCode(), is(HttpStatus.OK));
}

@ParameterizedTest
@MethodSource("headersProvider")
@DisplayName("Test for forwardPostRequests() status OK - with custom header")
void shouldReturnStatusOK_ForwardPostRequestsForCustomHeaders(Map<String, List<String>> headersMap)
throws IOException, InterruptedException {
HttpResponse mockResponse = mock(HttpResponse.class);
stubResponseController = new StubResponseController(mockHttpClient, mockHttpServer, mapper);
Mockito.doReturn(mockResponse).when(mockHttpClient).send(any(), any());
when(mockResponse.body()).thenReturn("MOCK BODY");

HeadersProvider mockHeadersProvider = mock(HeadersProvider.class);
when(mockHeadersProvider.getHeaders()).thenReturn(headersMap);

when(mockResponse.statusCode()).thenReturn(200);

HttpServletRequest mockRequest = mock(HttpServletRequest.class);
ResponseEntity<Object> responseEntityReturned = stubResponseController.forwardPostRequests(mockRequest);
assertNotNull(responseEntityReturned);
assertThat(responseEntityReturned.getStatusCode(), is(HttpStatus.OK));
}

/**
* Forward POST requests unit test with status OK.
*/
@Test
@DisplayName("Test for forwardPostRequests() status OK - with custom header")
void shouldReturnStatusOK_ForwardPostRequestsForCustomHeaders() throws IOException, InterruptedException {
HttpResponse mockResponse = mock(HttpResponse.class);
stubResponseController = new StubResponseController(mockHttpClient, mockHttpServer, mapper);
Mockito.doReturn(mockResponse).when(mockHttpClient).send(any(), any());
when(mockResponse.body()).thenReturn("MOCK BODY");

HttpHeaders headers = HttpHeaders.of(
Map.of("Header-Name-1", List.of("Header-Value1"),
"Client-Context", List.of("Header-Value2")), (s1, s2) -> true);
when(mockResponse.headers()).thenReturn(headers);
when(mockResponse.statusCode()).thenReturn(200);

HttpServletRequest mockRequest = mock(HttpServletRequest.class);
ResponseEntity<Object> responseEntityReturned = stubResponseController.forwardPostRequests(mockRequest);
assertNotNull(responseEntityReturned);
assertThat(responseEntityReturned.getStatusCode(), is(HttpStatus.OK));
Expand Down Expand Up @@ -269,4 +334,20 @@ void shouldReturnUIParams() {
assertThat(queryParameters, hasItem("clientid"));
assertThat(queryParameters, hasItem("http://localhost:5555/o"));
}

@ParameterizedTest
@MethodSource("redirectToOauth2InvalidUriProvider")
@DisplayName("redirectToOauth2 should handle invalid URIs")
void redirectToOauth2ShouldHandleInvalidURIs(String redirectUri) {
assertThrows(URISyntaxException.class, () -> {
stubResponseController.redirectToOauth2(redirectUri, "scope", "state", "clientId");
});
}

static Stream<String> redirectToOauth2InvalidUriProvider() {
return Stream.of(
"http://localhost:8080/callback with spaces",
"http://localhost:8080/callback?query=<invalid>"
);
}
}

0 comments on commit e0d0496

Please sign in to comment.