diff options
Diffstat (limited to 'sdc-distribution-client/src/test/java/org/onap/sdc/http')
5 files changed, 173 insertions, 191 deletions
diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpAsdcClientResponseTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpAsdcClientResponseTest.java index eb5c1eb..8a912d9 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpAsdcClientResponseTest.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpAsdcClientResponseTest.java @@ -19,44 +19,39 @@ */ package org.onap.sdc.http; -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpStatus; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpStatus; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.junit.jupiter.MockitoExtension; +@ExtendWith(MockitoExtension.class) +class HttpAsdcClientResponseTest { -@RunWith(value = Parameterized.class) -public class HttpAsdcClientResponseTest { - @Parameterized.Parameter - public int httpStatusCode; - - @Parameterized.Parameter(value = 1) - public String httpMessage; - - @Parameterized.Parameters(name = "{index}: test({0},{1}) = {0} {1}") public static Collection<Object[]> data() { return Arrays.asList(new Object[][]{ - {HttpStatus.SC_INTERNAL_SERVER_ERROR, "failed to send request"}, - {HttpStatus.SC_BAD_GATEWAY, "failed to connect"}, - {HttpStatus.SC_BAD_GATEWAY, "failed to send request "} + {HttpStatus.SC_INTERNAL_SERVER_ERROR, "failed to send request"}, + {HttpStatus.SC_BAD_GATEWAY, "failed to connect"}, + {HttpStatus.SC_BAD_GATEWAY, "failed to send request "} }); } - @Test - public void shouldCreateHttpResponse() throws IOException { + @ParameterizedTest + @MethodSource("data") + void shouldCreateHttpResponse(int httpStatusCode, String httpMessage) throws IOException { // when final HttpAsdcResponse response = HttpAsdcClient.createHttpResponse(httpStatusCode, httpMessage); // then - Assert.assertEquals(httpStatusCode, response.getStatus()); - Assert.assertEquals(httpMessage, getResponseMessage(response)); + assertEquals(httpStatusCode, response.getStatus()); + assertEquals(httpMessage, getResponseMessage(response)); } private String getResponseMessage(HttpAsdcResponse response) throws IOException { 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; } diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpClientFactoryTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpClientFactoryTest.java index 4594fb9..347b7f5 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpClientFactoryTest.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpClientFactoryTest.java @@ -19,68 +19,68 @@ */ package org.onap.sdc.http; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + import org.apache.http.client.CredentialsProvider; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.onap.sdc.utils.Pair; import org.onap.sdc.utils.TestConfiguration; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - - -public class HttpClientFactoryTest { +class HttpClientFactoryTest { @Test - public void shouldReturnSSLConnection(){ + void shouldReturnSSLConnection() { TestConfiguration config = spy(new TestConfiguration()); HttpClientFactory httpClientFactory = new HttpClientFactory(config); when(config.activateServerTLSAuth()).thenReturn(true); when(config.getKeyStorePath()).thenReturn("src/test/resources/asdc-client.jks"); when(config.getKeyStorePassword()).thenReturn("Aa123456"); - Pair<String, CloseableHttpClient> client = httpClientFactory.createInstance(); + Pair<String, CloseableHttpClient> client = httpClientFactory.createInstance(); SSLConnectionSocketFactory sslsf = spy(SSLConnectionSocketFactory.getSocketFactory()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); CloseableHttpClient expectedHttpClient = HttpClientBuilder.create(). - setDefaultCredentialsProvider(credsProvider). - setSSLSocketFactory(sslsf). - build(); + setDefaultCredentialsProvider(credsProvider). + setSSLSocketFactory(sslsf). + build(); Pair<String, CloseableHttpClient> expectedClient = new Pair<>("https://", expectedHttpClient); assertNotNull(client); assertEquals(expectedClient.getFirst(), client.getFirst()); } @Test - public void shouldReturnSSLConnectionWithHttp(){ + void shouldReturnSSLConnectionWithHttp() { TestConfiguration config = spy(new TestConfiguration()); HttpClientFactory httpClientFactory = new HttpClientFactory(config); when(config.activateServerTLSAuth()).thenReturn(false); when(config.isUseHttpsWithSDC()).thenReturn(false); - Pair<String, CloseableHttpClient> client = httpClientFactory.createInstance(); + Pair<String, CloseableHttpClient> client = httpClientFactory.createInstance(); SSLConnectionSocketFactory sslsf = spy(SSLConnectionSocketFactory.getSocketFactory()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); CloseableHttpClient expectedHttpClient = HttpClientBuilder.create(). - setDefaultCredentialsProvider(credsProvider). - setSSLSocketFactory(sslsf). - build(); + setDefaultCredentialsProvider(credsProvider). + setSSLSocketFactory(sslsf). + build(); Pair<String, CloseableHttpClient> expectedClient = new Pair<>("http://", expectedHttpClient); assertNotNull(client); assertEquals(expectedClient.getFirst(), client.getFirst()); } - @Test (expected = HttpAsdcClientException.class) - public void shouldReturnSSLConnectionError() throws HttpAsdcClientException{ + @Test + void shouldReturnSSLConnectionError() throws HttpAsdcClientException { TestConfiguration config = spy(new TestConfiguration()); HttpClientFactory httpClientFactory = new HttpClientFactory(config); when(config.activateServerTLSAuth()).thenReturn(true); when(config.getKeyStorePath()).thenReturn("src/test/resources/dummy.jks"); when(config.getKeyStorePassword()).thenReturn("Aa123456"); - httpClientFactory.createInstance(); + assertThrows(HttpAsdcClientException.class, () -> httpClientFactory.createInstance()); } -}
\ No newline at end of file +} diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpRequestFactoryTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpRequestFactoryTest.java index ba42703..c9eb4b3 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpRequestFactoryTest.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/http/HttpRequestFactoryTest.java @@ -20,20 +20,19 @@ package org.onap.sdc.http; -import org.apache.http.HttpEntity; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; - -public class HttpRequestFactoryTest { +class HttpRequestFactoryTest { private static final String URL = "https://127.0.0.1:8080/target"; private static final String EXPECTED_AUTHORIZATION_VALUE = "Basic dXNlcjpwYXNzd29yZA=="; @@ -41,7 +40,7 @@ public class HttpRequestFactoryTest { private static final String HEADER_KEY_2 = "key2"; private static final String HEADER_VALUE_1 = "value1"; private static final String HEADER_VALUE_2 = "value2"; - private static final Map<String, String> HEADERS = new HashMap<String, String>(){ + private static final Map<String, String> HEADERS = new HashMap<String, String>() { { put(HEADER_KEY_1, HEADER_VALUE_1); put(HEADER_KEY_2, HEADER_VALUE_2); @@ -51,22 +50,22 @@ public class HttpRequestFactoryTest { private HttpRequestFactory testObj = new HttpRequestFactory("user", "password"); @Test - public void shouldCreateValidGetHttpRequest() throws URISyntaxException { + void shouldCreateValidGetHttpRequest() throws URISyntaxException { // when final HttpGet httpGetRequest = testObj.createHttpGetRequest(URL, HEADERS); // then assertThat(httpGetRequest.getURI()).isEqualTo(new URI(URL)); assertThat(httpGetRequest.getFirstHeader(HEADER_KEY_1).getValue()) - .isEqualTo(HEADER_VALUE_1); + .isEqualTo(HEADER_VALUE_1); assertThat(httpGetRequest.getFirstHeader(HEADER_KEY_2).getValue()) - .isEqualTo(HEADER_VALUE_2); + .isEqualTo(HEADER_VALUE_2); assertThat(httpGetRequest.getFirstHeader(HttpRequestFactory.AUTHORIZATION).getValue()) - .isEqualTo(EXPECTED_AUTHORIZATION_VALUE); + .isEqualTo(EXPECTED_AUTHORIZATION_VALUE); } @Test - public void shouldCreateValidPostHttpRequest() throws URISyntaxException { + void shouldCreateValidPostHttpRequest() throws URISyntaxException { // given final HttpEntity httpEntity = mock(HttpEntity.class); @@ -77,10 +76,10 @@ public class HttpRequestFactoryTest { assertThat(httpPostRequest.getURI()).isEqualTo(new URI(URL)); assertThat(httpPostRequest.getEntity()).isEqualTo(httpEntity); assertThat(httpPostRequest.getFirstHeader(HEADER_KEY_1).getValue()) - .isEqualTo(HEADER_VALUE_1); + .isEqualTo(HEADER_VALUE_1); assertThat(httpPostRequest.getFirstHeader(HEADER_KEY_2).getValue()) - .isEqualTo(HEADER_VALUE_2); + .isEqualTo(HEADER_VALUE_2); assertThat(httpPostRequest.getFirstHeader(HttpRequestFactory.AUTHORIZATION).getValue()) - .isEqualTo(EXPECTED_AUTHORIZATION_VALUE); + .isEqualTo(EXPECTED_AUTHORIZATION_VALUE); } } diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java index 6ff0f9b..b09de78 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java @@ -21,20 +21,25 @@ package org.onap.sdc.http; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.att.nsa.apiClient.credentials.ApiCredential; import com.google.common.hash.Hashing; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import fj.data.Either; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -43,59 +48,48 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; - import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.methods.CloseableHttpResponse; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.mockito.Matchers; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import org.onap.sdc.api.asdc.RegistrationRequest; +import org.onap.sdc.api.consumer.IConfiguration; import org.onap.sdc.api.notification.IArtifactInfo; import org.onap.sdc.api.results.IDistributionClientResult; import org.onap.sdc.impl.DistributionClientResultImpl; -import org.onap.sdc.api.asdc.RegistrationRequest; -import org.onap.sdc.api.consumer.IConfiguration; import org.onap.sdc.utils.DistributionActionResultEnum; import org.onap.sdc.utils.Pair; -import com.att.nsa.apiClient.credentials.ApiCredential; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -import fj.data.Either; - -public class SdcConnectorClientTest { +class SdcConnectorClientTest { - private static Gson gson = new GsonBuilder().create(); private static final String MOCK_ENV = "MockEnv"; private static final String MOCK_API_KEY = "MockApikey"; + private static final String ARTIFACT_URL = "http://127.0.0.1/artifact/url"; + private static final String IT_JUST_DIDN_T_WORK = "It just didn't work"; + private static final List<String> ARTIFACT_TYPES = Arrays.asList("Service", "Resource", "VF", "VFC"); + private static final int PORT = 49512; + private static final byte[] BYTES = new byte[]{0xA, 0xB, 0xC, 0xD}; + private static Gson gson = new GsonBuilder().create(); + private static final String VALID_JSON_PAYLOAD = gson.toJson(ARTIFACT_TYPES); private static HttpAsdcClient httpClient = mock(HttpAsdcClient.class); private static IConfiguration configuration = mock(IConfiguration.class); private static ApiCredential apiCredential = mock(ApiCredential.class); private static HttpAsdcResponse httpAsdcResponse = mock(HttpAsdcResponse.class); @SuppressWarnings("unchecked") private static Either<TopicRegistrationResponse, DistributionClientResultImpl> mockResponse = - Mockito.mock(Either.class); + Mockito.mock(Either.class); private static Map<String, String> mockHeaders = new HashMap<>(); + private static SdcConnectorClient asdcClient; Pair<HttpAsdcResponse, CloseableHttpResponse> mockPair = new Pair<>(httpAsdcResponse, null); private HttpEntity lastHttpEntity = null; - private static SdcConnectorClient asdcClient; - - private static final String ARTIFACT_URL = "http://127.0.0.1/artifact/url"; - private static final String IT_JUST_DIDN_T_WORK = "It just didn't work"; - private static final List<String> ARTIFACT_TYPES = Arrays.asList("Service", "Resource", "VF", "VFC"); - private static final String VALID_JSON_PAYLOAD = gson.toJson(ARTIFACT_TYPES); - private static final int PORT = 49512; - private static final byte[] BYTES = new byte[] {0xA, 0xB, 0xC, 0xD}; - - - @BeforeClass + @BeforeAll public static void beforeClass() { asdcClient = Mockito.spy(new SdcConnectorClient(configuration, httpClient)); when(apiCredential.getApiKey()).thenReturn(MOCK_API_KEY); @@ -105,25 +99,23 @@ public class SdcConnectorClientTest { doReturn(mockResponse).when(asdcClient).parseRegistrationResponse(httpAsdcResponse); } - @Before + @BeforeEach public void beforeMethod() { Mockito.reset(configuration, httpClient); lastHttpEntity = null; when(configuration.getEnvironmentName()).thenReturn(MOCK_ENV); - doAnswer(new Answer<Pair<HttpAsdcResponse, CloseableHttpResponse>>() { @Override public Pair<HttpAsdcResponse, CloseableHttpResponse> answer(InvocationOnMock invocation) throws Throwable { lastHttpEntity = invocation.getArgument(1, HttpEntity.class); return mockPair; } - }).when(httpClient).postRequest(Mockito.eq(AsdcUrls.POST_FOR_TOPIC_REGISTRATION), Mockito.any(HttpEntity.class), - Mockito.eq(mockHeaders), Mockito.eq(false)); + }).when(httpClient).postRequest(eq(AsdcUrls.POST_FOR_TOPIC_REGISTRATION), any(HttpEntity.class), eq(mockHeaders), eq(false)); } - @Test(expected = IllegalStateException.class) - public void initAndCloseTest() { + @Test + void initAndCloseTest() { IConfiguration conf = Mockito.mock(IConfiguration.class); when(conf.getUser()).thenReturn("user"); when(conf.getPassword()).thenReturn("password"); @@ -134,56 +126,51 @@ public class SdcConnectorClientTest { SdcConnectorClient client = new SdcConnectorClient(conf, httpClient); client.close(); - //check if client is really closed - httpClient.getRequest(AsdcUrls.POST_FOR_TOPIC_REGISTRATION, new HashMap<>()); + assertThrows(IllegalStateException.class, () -> { + //check if client is really closed + httpClient.getRequest(AsdcUrls.POST_FOR_TOPIC_REGISTRATION, new HashMap<>()); + }); + } @Test - public void testConsumeProduceStatusTopicFalse() throws UnsupportedOperationException, IOException { - + void testConsumeProduceStatusTopicFalse() throws UnsupportedOperationException, IOException { testConsumeProduceStatusTopic(false); - } @Test - public void testConsumeProduceStatusTopicTrue() throws UnsupportedOperationException, IOException { - + void testConsumeProduceStatusTopicTrue() throws UnsupportedOperationException, IOException { testConsumeProduceStatusTopic(true); - } private void testConsumeProduceStatusTopic(final boolean isConsumeProduceStatusFlag) throws IOException { when(configuration.isConsumeProduceStatusTopic()).thenReturn(isConsumeProduceStatusFlag); asdcClient.registerAsdcTopics(apiCredential); - verify(httpClient, times(1)) - .postRequest(Mockito.eq(AsdcUrls.POST_FOR_TOPIC_REGISTRATION), any(HttpEntity.class), - Mockito.eq(mockHeaders), Mockito.eq(false)); + verify(httpClient, times(1)).postRequest(eq(AsdcUrls.POST_FOR_TOPIC_REGISTRATION), any(HttpEntity.class), eq(mockHeaders), eq(false)); assertNotNull(lastHttpEntity); - RegistrationRequest actualRegRequest = - gson.fromJson(IOUtils.toString(lastHttpEntity.getContent(), StandardCharsets.UTF_8), - RegistrationRequest.class); - RegistrationRequest expectedRegRequest = - gson.fromJson(excpectedStringBody(isConsumeProduceStatusFlag), RegistrationRequest.class); - - assertTrue(actualRegRequest.getApiPublicKey().equals(expectedRegRequest.getApiPublicKey())); - assertTrue(actualRegRequest.getDistrEnvName().equals(expectedRegRequest.getDistrEnvName())); - assertTrue(actualRegRequest.getIsConsumerToSdcDistrStatusTopic() - .equals(expectedRegRequest.getIsConsumerToSdcDistrStatusTopic())); + RegistrationRequest actualRegRequest + = gson.fromJson(IOUtils.toString(lastHttpEntity.getContent(), StandardCharsets.UTF_8), RegistrationRequest.class); + RegistrationRequest expectedRegRequest + = gson.fromJson(excpectedStringBody(isConsumeProduceStatusFlag), RegistrationRequest.class); + + assertEquals(expectedRegRequest.getApiPublicKey(), actualRegRequest.getApiPublicKey()); + assertEquals(expectedRegRequest.getDistrEnvName(), actualRegRequest.getDistrEnvName()); + assertEquals(expectedRegRequest.getIsConsumerToSdcDistrStatusTopic(), actualRegRequest.getIsConsumerToSdcDistrStatusTopic()); } @Test - public void getValidArtifactTypesListHappyScenarioTest() throws IOException { + void getValidArtifactTypesListHappyScenarioTest() throws IOException { HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class); CloseableHttpResponse closeableHttpResponseMock = mock(CloseableHttpResponse.class); HttpEntity messageMock = mock(HttpEntity.class); Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair = - new Pair<>(responseMock, closeableHttpResponseMock); + new Pair<>(responseMock, closeableHttpResponseMock); when(responseMock.getStatus()).thenReturn(HttpStatus.SC_OK); when(responseMock.getMessage()).thenReturn(messageMock); when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(VALID_JSON_PAYLOAD.getBytes())); - when(httpClient.getRequest(eq(AsdcUrls.GET_VALID_ARTIFACT_TYPES), Matchers.any(), eq(false))) - .thenReturn(responsePair); + when(httpClient.getRequest(eq(AsdcUrls.GET_VALID_ARTIFACT_TYPES), any(), eq(false))) + .thenReturn(responsePair); Either<List<String>, IDistributionClientResult> result = asdcClient.getValidArtifactTypesList(); assertTrue(result.isLeft()); @@ -192,7 +179,7 @@ public class SdcConnectorClientTest { } @Test - public void getValidArtifactTypesListErrorResponseScenarioTest() throws IOException { + void getValidArtifactTypesListErrorResponseScenarioTest() throws IOException { HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class); HttpEntity messageMock = mock(HttpEntity.class); Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair = new Pair<>(responseMock, null); @@ -200,30 +187,30 @@ public class SdcConnectorClientTest { when(responseMock.getStatus()).thenReturn(HttpStatus.SC_GATEWAY_TIMEOUT); when(responseMock.getMessage()).thenReturn(messageMock); when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(IT_JUST_DIDN_T_WORK.getBytes())); - when(httpClient.getRequest(eq(AsdcUrls.GET_VALID_ARTIFACT_TYPES), Matchers.any(), eq(false))) - .thenReturn(responsePair); + when(httpClient.getRequest(eq(AsdcUrls.GET_VALID_ARTIFACT_TYPES), any(), eq(false))) + .thenReturn(responsePair); Either<List<String>, IDistributionClientResult> result = asdcClient.getValidArtifactTypesList(); assertTrue(result.isRight()); IDistributionClientResult distributionClientResult = result.right().value(); assertEquals(DistributionActionResultEnum.ASDC_SERVER_TIMEOUT, - distributionClientResult.getDistributionActionResult()); + distributionClientResult.getDistributionActionResult()); } @Test - public void getValidArtifactTypesListExceptionDuringConnectionClosingTest() throws IOException { + void getValidArtifactTypesListExceptionDuringConnectionClosingTest() throws IOException { HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class); CloseableHttpResponse closeableHttpResponseMock = mock(CloseableHttpResponse.class); HttpEntity messageMock = mock(HttpEntity.class); Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair = - new Pair<>(responseMock, closeableHttpResponseMock); + new Pair<>(responseMock, closeableHttpResponseMock); when(responseMock.getStatus()).thenReturn(HttpStatus.SC_GATEWAY_TIMEOUT); when(responseMock.getMessage()).thenReturn(messageMock); when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(VALID_JSON_PAYLOAD.getBytes())); - when(httpClient.getRequest(eq(AsdcUrls.GET_VALID_ARTIFACT_TYPES), Matchers.any(), eq(false))) - .thenReturn(responsePair); + when(httpClient.getRequest(eq(AsdcUrls.GET_VALID_ARTIFACT_TYPES), any(), eq(false))) + .thenReturn(responsePair); doThrow(new IOException("Test exception")).when(closeableHttpResponseMock).close(); @@ -231,36 +218,36 @@ public class SdcConnectorClientTest { assertTrue(result.isRight()); IDistributionClientResult distributionClientResult = result.right().value(); assertEquals(DistributionActionResultEnum.ASDC_SERVER_TIMEOUT, - distributionClientResult.getDistributionActionResult()); + distributionClientResult.getDistributionActionResult()); } @Test - public void getValidArtifactTypesListParsingExceptionHandlingTest() throws IOException { + void getValidArtifactTypesListParsingExceptionHandlingTest() throws IOException { HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class); CloseableHttpResponse closeableHttpResponseMock = mock(CloseableHttpResponse.class); HttpEntity messageMock = mock(HttpEntity.class); Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair = - new Pair<>(responseMock, closeableHttpResponseMock); + new Pair<>(responseMock, closeableHttpResponseMock); when(responseMock.getStatus()).thenReturn(HttpStatus.SC_OK); when(responseMock.getMessage()).thenReturn(messageMock); when(messageMock.getContent()).thenReturn(new ThrowingInputStreamForTesting()); - when(httpClient.getRequest(eq(AsdcUrls.GET_VALID_ARTIFACT_TYPES), Matchers.any(), eq(false))) - .thenReturn(responsePair); + when(httpClient.getRequest(eq(AsdcUrls.GET_VALID_ARTIFACT_TYPES), any(), eq(false))) + .thenReturn(responsePair); Either<List<String>, IDistributionClientResult> result = asdcClient.getValidArtifactTypesList(); assertTrue(result.isRight()); IDistributionClientResult distributionClientResult = result.right().value(); assertEquals(DistributionActionResultEnum.GENERAL_ERROR, - distributionClientResult.getDistributionActionResult()); + distributionClientResult.getDistributionActionResult()); } @Test - public void unregisterTopicsErrorDuringProcessingTest() throws IOException { + void unregisterTopicsErrorDuringProcessingTest() throws IOException { when(configuration.getAsdcAddress()).thenReturn("127.0.0.1" + PORT); when(configuration.isConsumeProduceStatusTopic()).thenReturn(false); when(configuration.getMsgBusAddress()) - .thenReturn(Arrays.asList("http://127.0.0.1:45321/dmaap", "http://127.0.0.1:45321/dmaap")); + .thenReturn(Arrays.asList("http://127.0.0.1:45321/dmaap", "http://127.0.0.1:45321/dmaap")); String failMessage = "It just didn't work"; HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class); @@ -271,14 +258,14 @@ public class SdcConnectorClientTest { when(responseMock.getMessage()).thenReturn(messageMock); when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(failMessage.getBytes())); doReturn(responsePair).when(httpClient) - .postRequest(eq(AsdcUrls.POST_FOR_UNREGISTER), any(HttpEntity.class), any(), eq(false)); + .postRequest(eq(AsdcUrls.POST_FOR_UNREGISTER), any(HttpEntity.class), any(), eq(false)); IDistributionClientResult result = asdcClient.unregisterTopics(apiCredential); assertEquals(DistributionActionResultEnum.ASDC_CONNECTION_FAILED, result.getDistributionActionResult()); } @Test - public void unregisterTopicsHappyScenarioTest() throws IOException { + void unregisterTopicsHappyScenarioTest() throws IOException { when(configuration.getAsdcAddress()).thenReturn("127.0.0.1" + PORT); when(configuration.isConsumeProduceStatusTopic()).thenReturn(false); @@ -291,14 +278,14 @@ public class SdcConnectorClientTest { when(responseMock.getMessage()).thenReturn(messageMock); when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(failMessage.getBytes())); doReturn(responsePair).when(httpClient) - .postRequest(eq(AsdcUrls.POST_FOR_UNREGISTER), any(HttpEntity.class), any(), eq(false)); + .postRequest(eq(AsdcUrls.POST_FOR_UNREGISTER), any(HttpEntity.class), any(), eq(false)); IDistributionClientResult result = asdcClient.unregisterTopics(apiCredential); assertEquals(DistributionActionResultEnum.SUCCESS, result.getDistributionActionResult()); } @Test - public void downloadArtifactHappyScenarioTest() throws IOException { + void downloadArtifactHappyScenarioTest() throws IOException { Map<String, String> headers = new HashMap<>(); headers.put(asdcClient.CONTENT_DISPOSITION_HEADER, "SomeHeader"); @@ -321,7 +308,7 @@ public class SdcConnectorClientTest { } @Test - public void downloadArtifactDataIntegrityProblemTest() throws IOException { + void downloadArtifactDataIntegrityProblemTest() throws IOException { IArtifactInfo artifactInfo = mock(IArtifactInfo.class); when(artifactInfo.getArtifactURL()).thenReturn(ARTIFACT_URL); @@ -339,7 +326,7 @@ public class SdcConnectorClientTest { } @Test - public void downloadArtifactExceptionDuringDownloadHandlingTest() throws IOException { + void downloadArtifactExceptionDuringDownloadHandlingTest() throws IOException { IArtifactInfo artifactInfo = mock(IArtifactInfo.class); when(artifactInfo.getArtifactURL()).thenReturn(ARTIFACT_URL); @@ -357,7 +344,7 @@ public class SdcConnectorClientTest { } @Test - public void downloadArtifactHandleDownloadErrorTest() throws IOException { + void downloadArtifactHandleDownloadErrorTest() throws IOException { IArtifactInfo artifactInfo = mock(IArtifactInfo.class); when(artifactInfo.getArtifactURL()).thenReturn(ARTIFACT_URL); @@ -376,8 +363,8 @@ public class SdcConnectorClientTest { private String excpectedStringBody(boolean isConsumeProduceStatusTopic) { String stringBodyTemplate = - "{\r\n" + " \"apiPublicKey\": \"MockApikey\",\r\n" + " \"distrEnvName\": \"MockEnv\",\r\n" - + " \"isConsumerToSdcDistrStatusTopic\": %s\r\n" + "}"; + "{\r\n" + " \"apiPublicKey\": \"MockApikey\",\r\n" + " \"distrEnvName\": \"MockEnv\",\r\n" + + " \"isConsumerToSdcDistrStatusTopic\": %s\r\n" + "}"; return String.format(stringBodyTemplate, isConsumeProduceStatusTopic); } |