From 314bba3a72c1b1122668950005a2e754f8c5c5cc Mon Sep 17 00:00:00 2001 From: "waqas.ikram" Date: Fri, 22 Mar 2019 14:39:44 +0000 Subject: Created new BB for so-etsi Change-Id: I9bf6b4019c280b816925ee5e0d826bff69cb1583 Issue-ID: SO-1621 Signed-off-by: waqas.ikram --- .../rest/BasicHttpHeadersProviderTest.java | 58 +++++ .../HttpComponentsClientConfigurationTest.java | 45 ++++ .../service/HttpRestServiceProviderImplTest.java | 252 +++++++++++++++++++++ 3 files changed, 355 insertions(+) create mode 100644 common/src/test/java/org/onap/so/configuration/rest/BasicHttpHeadersProviderTest.java create mode 100644 common/src/test/java/org/onap/so/configuration/rest/HttpComponentsClientConfigurationTest.java create mode 100644 common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java (limited to 'common/src/test') diff --git a/common/src/test/java/org/onap/so/configuration/rest/BasicHttpHeadersProviderTest.java b/common/src/test/java/org/onap/so/configuration/rest/BasicHttpHeadersProviderTest.java new file mode 100644 index 0000000000..3e762cfbe7 --- /dev/null +++ b/common/src/test/java/org/onap/so/configuration/rest/BasicHttpHeadersProviderTest.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.so.configuration.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.onap.so.configuration.rest.BasicHttpHeadersProvider.AUTHORIZATION_HEADER; + +import java.util.Arrays; + +import org.junit.Test; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; + + +/** + * @author waqas.ikram@est.tech + * + */ +public class BasicHttpHeadersProviderTest { + + private static final String BASIC_AUTH_VALUE = "Basic AuthValue"; + + @Test + public void test_getHttpHeaders_ContentTypeIsJson() { + final HttpHeadersProvider objUnderTest = new BasicHttpHeadersProvider(); + final HttpHeaders actualHttpHeaders = objUnderTest.getHttpHeaders(); + assertNull(actualHttpHeaders.get(AUTHORIZATION_HEADER)); + assertEquals(MediaType.APPLICATION_JSON, actualHttpHeaders.getContentType()); + } + + @Test + public void test_getHttpHeaders_ContainAuthorizationHeader() { + final HttpHeadersProvider objUnderTest = new BasicHttpHeadersProvider(BASIC_AUTH_VALUE); + final HttpHeaders actualHttpHeaders = objUnderTest.getHttpHeaders(); + assertEquals(Arrays.asList(BASIC_AUTH_VALUE), actualHttpHeaders.get(AUTHORIZATION_HEADER)); + assertEquals(MediaType.APPLICATION_JSON, actualHttpHeaders.getContentType()); + } + +} diff --git a/common/src/test/java/org/onap/so/configuration/rest/HttpComponentsClientConfigurationTest.java b/common/src/test/java/org/onap/so/configuration/rest/HttpComponentsClientConfigurationTest.java new file mode 100644 index 0000000000..5084acc934 --- /dev/null +++ b/common/src/test/java/org/onap/so/configuration/rest/HttpComponentsClientConfigurationTest.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.so.configuration.rest; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; + +/** + * @author waqas.ikram@est.tech + */ +public class HttpComponentsClientConfigurationTest { + + @Test + public void test_httpComponentsClientHttpRequestFactory_HttpComponentsClientHttpRequestFactoryNotNull() { + final HttpClientConnectionConfiguration clientConnectionConfiguration = new HttpClientConnectionConfiguration(); + final HttpComponentsClientConfiguration objUnderTest = + new HttpComponentsClientConfiguration(clientConnectionConfiguration); + + final HttpComponentsClientHttpRequestFactory factory = objUnderTest.httpComponentsClientHttpRequestFactory(); + assertNotNull(factory); + assertNotNull(factory.getHttpClient()); + + } + +} diff --git a/common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java b/common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java new file mode 100644 index 0000000000..a738afe565 --- /dev/null +++ b/common/src/test/java/org/onap/so/rest/service/HttpRestServiceProviderImplTest.java @@ -0,0 +1,252 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.so.rest.service; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.rest.exceptions.InvalidRestRequestException; +import org.onap.so.rest.exceptions.RestProcessingException; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; + +import com.google.common.base.Optional; + + +/** + * @author waqas.ikram@est.tech + */ +@RunWith(MockitoJUnitRunner.class) +public class HttpRestServiceProviderImplTest { + + private static final String BODY = "{}"; + private static final String DUMMY_URL = "http://localhost:9000/dummy/url"; + + @Mock + private RestTemplate mockRestTemplate; + + @Mock + private ResponseEntity mockEntity; + + @Test + public void test_get_returnOptionalPresentIfResponseIsOKAndHasBody() { + + final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate); + + when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class))) + .thenReturn(mockEntity); + + when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK); + when(mockEntity.hasBody()).thenReturn(true); + when(mockEntity.getBody()).thenReturn(BODY); + + final Optional actual = objUnderTest.get(DUMMY_URL, String.class); + + assertTrue(actual.isPresent()); + verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), + eq(String.class)); + } + + @Test + public void test_get_returnOptionalPresentIfResponseIsNotOK() { + final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate); + + when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class))) + .thenReturn(mockEntity); + + when(mockEntity.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR); + + final Optional actual = objUnderTest.get(DUMMY_URL, String.class); + + assertFalse(actual.isPresent()); + verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), + eq(String.class)); + } + + @Test + public void test_get_returnOptionalPresentIfResponseIsOKAndNoBody() { + final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate); + + when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class))) + .thenReturn(mockEntity); + + when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK); + when(mockEntity.hasBody()).thenReturn(false); + + final Optional actual = objUnderTest.get(DUMMY_URL, String.class); + + assertFalse(actual.isPresent()); + verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), + eq(String.class)); + } + + @Test(expected = InvalidRestRequestException.class) + public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusBadRequest() { + assertGetErrorScenario(HttpStatus.BAD_REQUEST); + + } + + @Test(expected = InvalidRestRequestException.class) + public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusNotFoundHttpStatus() { + assertGetErrorScenario(HttpStatus.NOT_FOUND); + } + + @Test(expected = RestProcessingException.class) + public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionOccured() { + final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate); + + when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class))) + .thenThrow(HttpClientErrorException.class); + + objUnderTest.get(DUMMY_URL, String.class); + } + + @Test(expected = RestProcessingException.class) + public void test_get_ThrowsInvalidRestRequestExceptionifRestProcessingExceptionOccured() { + final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate); + + when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class))) + .thenThrow(RestClientException.class); + + objUnderTest.get(DUMMY_URL, String.class); + } + + @Test + public void test_post_returnOptionalPresentIfResponseIsOKAndHasBody() { + + final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate); + + when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))) + .thenReturn(mockEntity); + + when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK); + when(mockEntity.hasBody()).thenReturn(true); + when(mockEntity.getBody()).thenReturn(BODY); + + final Optional actual = objUnderTest.post(BODY, DUMMY_URL, String.class); + + assertTrue(actual.isPresent()); + verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), + eq(String.class)); + } + + @Test + public void test_post_returnOptionalPresentIfResponseIsOKAndHasNoBody() { + + final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate); + + when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))) + .thenReturn(mockEntity); + + when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK); + when(mockEntity.hasBody()).thenReturn(false); + + final Optional actual = objUnderTest.post(BODY, DUMMY_URL, String.class); + + assertFalse(actual.isPresent()); + verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), + eq(String.class)); + } + + + @Test + public void test_post_returnOptionalPresentIfResponseIsNotOKAndHasBody() { + + final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate); + + when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))) + .thenReturn(mockEntity); + + when(mockEntity.getStatusCode()).thenReturn(HttpStatus.PARTIAL_CONTENT); + + final Optional actual = objUnderTest.post(BODY, DUMMY_URL, String.class); + + assertFalse(actual.isPresent()); + verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), + eq(String.class)); + } + + @Test(expected = InvalidRestRequestException.class) + public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusBadRequest() { + assertPostErrorScenario(HttpStatus.BAD_REQUEST); + + } + + @Test(expected = InvalidRestRequestException.class) + public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusNotFoundHttpStatus() { + assertPostErrorScenario(HttpStatus.NOT_FOUND); + } + + @Test(expected = RestProcessingException.class) + public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionOccured() { + final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate); + + when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))) + .thenThrow(HttpClientErrorException.class); + + objUnderTest.post(BODY, DUMMY_URL, String.class); + } + + @Test(expected = RestProcessingException.class) + public void test_post_ThrowsInvalidRestRequestExceptionifRestProcessingExceptionOccured() { + final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate); + + when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))) + .thenThrow(RestClientException.class); + + objUnderTest.post(BODY, DUMMY_URL, String.class); + } + + private void assertPostErrorScenario(final HttpStatus status) { + final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate); + + final HttpClientErrorException errorException = new HttpClientErrorException(status); + when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))) + .thenThrow(errorException); + + objUnderTest.post(BODY, DUMMY_URL, String.class); + } + + private void assertGetErrorScenario(final HttpStatus status) { + final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate); + + final HttpClientErrorException errorException = new HttpClientErrorException(status); + when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class))) + .thenThrow(errorException); + + objUnderTest.get(DUMMY_URL, String.class); + } + +} -- cgit 1.2.3-korg