diff options
Diffstat (limited to 'sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpAsdcClientTest.java')
-rw-r--r-- | sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpAsdcClientTest.java | 85 |
1 files changed, 43 insertions, 42 deletions
diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpAsdcClientTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpAsdcClientTest.java index 4e74080..2dcfd5d 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpAsdcClientTest.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpAsdcClientTest.java @@ -19,32 +19,31 @@ */ package org.onap.sdc.http; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.HashMap; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.StatusLine; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.message.BasicHeader; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.onap.sdc.utils.Pair; import org.onap.sdc.utils.TestConfiguration; -import java.io.IOException; -import java.util.HashMap; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class HttpAsdcClientTest { +@ExtendWith(MockitoExtension.class) +class HttpAsdcClientTest { private static final String URL = "http://127.0.0.1:8080/target"; private static final int HTTP_OK = 200; @@ -63,19 +62,19 @@ public class HttpAsdcClientTest { private HttpEntity httpEntity; @Test - public void shouldCreateInitializedHttpClient() { + void shouldCreateInitializedHttpClient() { // given TestConfiguration configuration = new TestConfiguration(); configuration.setUseHttpsWithSDC(false); // when final HttpRequestFactory httpRequestFactory = new HttpRequestFactory( - configuration.getUser(), - configuration.getPassword()); + configuration.getUser(), + configuration.getPassword()); final HttpAsdcClient httpAsdcClient = new HttpAsdcClient( - configuration.getAsdcAddress(), - new HttpClientFactory(configuration), - httpRequestFactory); + configuration.getAsdcAddress(), + new HttpClientFactory(configuration), + httpRequestFactory); // then assertNotNull(httpAsdcClient); @@ -83,19 +82,19 @@ public class HttpAsdcClientTest { } @Test - public void shouldCreateInitializedHttpsClient() { + void shouldCreateInitializedHttpsClient() { // given TestConfiguration configuration = new TestConfiguration(); configuration.setUseHttpsWithSDC(true); // when final HttpRequestFactory httpRequestFactory = new HttpRequestFactory( - configuration.getUser(), - configuration.getPassword()); + configuration.getUser(), + configuration.getPassword()); final HttpAsdcClient httpAsdcClient = new HttpAsdcClient( - configuration.getAsdcAddress(), - new HttpClientFactory(configuration), - httpRequestFactory); + configuration.getAsdcAddress(), + new HttpClientFactory(configuration), + httpRequestFactory); // then assertNotNull(httpAsdcClient); @@ -103,11 +102,11 @@ public class HttpAsdcClientTest { } @Test - public void shouldSendGetRequestWithoutAnyError() throws IOException { + void shouldSendGetRequestWithoutAnyError() throws IOException { // given TestConfiguration configuration = givenHttpConfiguration(); final HttpAsdcClient httpAsdcClient = createTestObj(HttpClientFactory.HTTP, configuration, httpClient); - CloseableHttpResponse httpResponse = givenHttpResponse(); + CloseableHttpResponse httpResponse = givenHttpResponse(true); // when final HttpAsdcResponse response = httpAsdcClient.getRequest(URL, HEADERS_MAP); @@ -125,14 +124,14 @@ public class HttpAsdcClientTest { } @Test - public void shouldSendPostRequestWithoutAnyError() throws IOException { + void shouldSendPostRequestWithoutAnyError() throws IOException { // given TestConfiguration configuration = givenHttpConfiguration(); final HttpAsdcClient httpAsdcClient = createTestObj(HttpClientFactory.HTTP, configuration, httpClient); - CloseableHttpResponse httpResponse = givenHttpResponse(); + CloseableHttpResponse httpResponse = givenHttpResponse(false); // when - final HttpAsdcResponse response = httpAsdcClient.postRequest(URL,httpEntity, HEADERS_MAP); + final HttpAsdcResponse response = httpAsdcClient.postRequest(URL, httpEntity, HEADERS_MAP); // then assertThat(response).isNotNull(); @@ -144,23 +143,25 @@ public class HttpAsdcClientTest { private HttpAsdcClient createTestObj(String httpProtocol, TestConfiguration configuration, CloseableHttpClient httpClient) { final HttpRequestFactory httpRequestFactory = new HttpRequestFactory( - configuration.getUser(), - configuration.getPassword()); + configuration.getUser(), + configuration.getPassword()); HttpClientFactory httpClientFactory = mock(HttpClientFactory.class); when(httpClientFactory.createInstance()).thenReturn(new Pair<>(httpProtocol, httpClient)); final HttpAsdcClient httpAsdcClient = new HttpAsdcClient( - configuration.getAsdcAddress(), - httpClientFactory, - httpRequestFactory); + configuration.getAsdcAddress(), + httpClientFactory, + httpRequestFactory); return httpAsdcClient; } - private CloseableHttpResponse givenHttpResponse(HttpEntity httpEntity, Header[] headers) { + private CloseableHttpResponse givenHttpResponse(HttpEntity httpEntity, Header[] headers, boolean includeGetAllHeaders) { CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class); StatusLine statusLine = mock(StatusLine.class); when(statusLine.getStatusCode()).thenReturn(HTTP_OK); when(httpResponse.getStatusLine()).thenReturn(statusLine); - when(httpResponse.getAllHeaders()).thenReturn(headers); + if (includeGetAllHeaders) { + when(httpResponse.getAllHeaders()).thenReturn(headers); + } when(httpResponse.getEntity()).thenReturn(httpEntity); return httpResponse; } @@ -171,8 +172,8 @@ public class HttpAsdcClientTest { return configuration; } - private CloseableHttpResponse givenHttpResponse() throws IOException { - CloseableHttpResponse httpResponse = givenHttpResponse(httpEntity, HEADERS); + private CloseableHttpResponse givenHttpResponse(boolean includeGetAllHeaders) throws IOException { + CloseableHttpResponse httpResponse = givenHttpResponse(httpEntity, HEADERS, includeGetAllHeaders); when(httpClient.execute(any())).thenReturn(httpResponse); return httpResponse; } |