From c1f0313e1f0085afb813d1ba3e2d7fcc71b5833d Mon Sep 17 00:00:00 2001 From: wasala Date: Tue, 10 Jul 2018 11:47:01 +0200 Subject: Checkstyle violations *Correction of violations without javadoc Change-Id: Ida177bf32a58605e74feae5fab22198228e970ce Issue-ID: DCAEGEN2-563 Signed-off-by: wasala --- .../prh/service/AAIConsumerClientTest.java | 93 ----------- .../prh/service/AAIHttpClientImplTest.java | 56 ------- .../prh/service/AAIProducerClientTest.java | 171 --------------------- .../prh/service/AaiConsumerClientTest.java | 94 +++++++++++ .../prh/service/AaiHttpClientImplTest.java | 54 +++++++ .../prh/service/AaiProducerClientTest.java | 166 ++++++++++++++++++++ 6 files changed, 314 insertions(+), 320 deletions(-) delete mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClientTest.java delete mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIHttpClientImplTest.java delete mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiConsumerClientTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiHttpClientImplTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiProducerClientTest.java (limited to 'prh-aai-client/src/test') diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClientTest.java deleted file mode 100644 index 6045c007..00000000 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClientTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.prh.service; - -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.CloseableHttpClient; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; -import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class AAIConsumerClientTest { - - private static AAIConsumerClient testedObject; - private static AAIClientConfiguration aaiHttpClientConfigurationMock = mock(AAIClientConfiguration.class); - private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); - private static final String JSON_MESSAGE = "{ \"pnf-id\": \"example-pnf-id-val-22343\", \"regional-resource-zone\":null, \"ipaddress-v4-oam\": \"11.22.33.44\" }"; - private static ConsumerDmaapModel consumerDmaapModelMock = mock(ConsumerDmaapModel.class); - private static final String PNF_NAME = "nokia-pnf-nhfsadhff"; - - @BeforeAll - public static void setup() throws NoSuchFieldException, IllegalAccessException { - - Map aaiHeaders = new HashMap<>(); - aaiHeaders.put("X-FromAppId", "prh"); - aaiHeaders.put("X-TransactionId", "9999"); - aaiHeaders.put("Accept", "application/json"); - aaiHeaders.put("Authorization", "Basic QUFJOkFBSQ=="); - aaiHeaders.put("Real-Time", "true"); - aaiHeaders.put("Content-Type", "application/json"); - - when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); - when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); - when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); - when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PRH"); - when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH"); - when(aaiHttpClientConfigurationMock.aaiBasePath()).thenReturn("/aai/v11"); - when(aaiHttpClientConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf"); - when(aaiHttpClientConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders); - - when(consumerDmaapModelMock.getPnfName()).thenReturn(PNF_NAME); - - testedObject = new AAIConsumerClient(aaiHttpClientConfigurationMock); - setField(); - } - - - @Test - public void getExtendedDetails_returnsSuccess() throws IOException { - - when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). - thenReturn(Optional.of(JSON_MESSAGE)); - Optional actualResult = testedObject.getHttpResponse(consumerDmaapModelMock); - Assertions.assertEquals(Optional.of(JSON_MESSAGE),actualResult); - } - - - private static void setField() throws NoSuchFieldException, IllegalAccessException { - Field field = testedObject.getClass().getDeclaredField("closeableHttpClient"); - field.setAccessible(true); - field.set(testedObject, closeableHttpClientMock); - } -} diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIHttpClientImplTest.java deleted file mode 100644 index 0e713856..00000000 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIHttpClientImplTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ -package org.onap.dcaegen2.services.prh.service; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; - -import static org.junit.Assert.assertNotNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - - -public class AAIHttpClientImplTest { - - private static AAIClientImpl testedObject; - private static AAIClientConfiguration aaiHttpClientConfigurationMock; - - - @BeforeAll - public static void setup() { - aaiHttpClientConfigurationMock = mock(AAIClientConfiguration.class); - when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); - when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); - when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); - when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PNF"); - when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PNF"); - when(aaiHttpClientConfigurationMock.aaiIgnoreSSLCertificateErrors()).thenReturn(true); - - testedObject = new AAIClientImpl(aaiHttpClientConfigurationMock); - } - - @Test - public void getAAIHttpClientObject_shouldNotBeNull() { - testedObject.getAAIHttpClient(); - assertNotNull(testedObject.getAAIHttpClient()); - } -} - diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java deleted file mode 100644 index b7515ad5..00000000 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.prh.service; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.StatusLine; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpPatch; -import org.apache.http.impl.client.CloseableHttpClient; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; -import org.onap.dcaegen2.services.prh.model.CommonFunctions; -import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest; - -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.lang.reflect.Field; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -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.when; - -class AAIProducerClientTest { - - private static final Integer SUCCESS = 200; - private static AAIProducerClient testedObject; - private static AAIClientConfiguration aaiHttpClientConfigurationMock = mock(AAIClientConfiguration.class); - private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); - private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest(); - - private final static HttpResponse httpResponseMock = mock(HttpResponse.class); - private final static HttpEntity httpEntityMock = mock(HttpEntity.class); - private final static StatusLine statusLineMock = mock(StatusLine.class); - - - - @BeforeAll - static void setup() throws NoSuchFieldException, IllegalAccessException { - when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("eucalyptus.es-si-eu-dhn-20.eecloud.nsn-net.net"); - when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); - when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); - when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PRH"); - when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH"); - when(aaiHttpClientConfigurationMock.aaiBasePath()).thenReturn("/aai/v11"); - when(aaiHttpClientConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf"); - when(aaiHttpClientConfigurationMock.aaiHeaders()).thenReturn(setupHeaders()); - - testedObject = new AAIProducerClient(aaiHttpClientConfigurationMock); - setField(); - } - - @Test - void getHttpResponse_shouldReturnSuccessStatusCode() throws IOException, URISyntaxException { - // when - when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) - .thenReturn(Optional.of(SUCCESS)); - Optional actualResult = testedObject.getHttpResponse(consumerDmaapModel); - // then - assertEquals(SUCCESS, actualResult.get()); - } - - @Test - void getHttpResponse_shouldHandleIOException() throws IOException, URISyntaxException { - // when - when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) - .thenThrow(new IOException("Error occur")); - - testedObject.getHttpResponse(consumerDmaapModel); - // then - assertNotNull(testedObject.getHttpResponse(consumerDmaapModel)); - } - - @Test - void createHttpRequest_shouldCatchUnsupportedEncodingException() throws URISyntaxException, IOException { - // when - when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) - .thenThrow(new UnsupportedEncodingException("A new Error")); - testedObject.getHttpResponse(consumerDmaapModel); - // then - assertNotNull(testedObject.getHttpResponse(consumerDmaapModel)); - } - - @Test - void encode_shouldCreateEncodedString_whenUserAndPasswordAreSet() throws UnsupportedEncodingException { - // given - String expected = "UFJIOlBSSA=="; - // when - String result = testedObject.encode(); - // then - assertNotNull(result); - assertEquals(expected, result); - } - - @Test - void createHttpPatch_shouldContainAuthorizationBasicValue() throws UnsupportedEncodingException { - // given - String expected = "Authorization: Basic UFJIOlBSSA=="; - // when - HttpPatch patch = testedObject.createHttpPatch(URI.create("localhost"), "{}"); - // then - assertNotNull(patch); - assertEquals(expected, patch.getLastHeader("Authorization").toString()); - } - - @Test - void handleResponse_shouldReturn200() throws IOException { - // When - when(httpResponseMock.getEntity()).thenReturn(httpEntityMock); - when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock); - when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_OK); - // Then - assertEquals(Optional.of(HttpStatus.SC_OK), testedObject.handleResponse(httpResponseMock)); - } - - @Test - void handleResponse_shouldReturn300() throws IOException { - // When - when(httpResponseMock.getEntity()).thenReturn(httpEntityMock); - when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock); - when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_BAD_REQUEST); - // Then - assertEquals(Optional.of(HttpStatus.SC_BAD_REQUEST), testedObject.handleResponse(httpResponseMock)); - } - - - private static void setField() throws NoSuchFieldException, IllegalAccessException { - Field field = testedObject.getClass().getDeclaredField("closeableHttpClient"); - field.setAccessible(true); - field.set(testedObject, closeableHttpClientMock); - } - - private static Map setupHeaders() { - Map aaiHeaders = new HashMap<>(); - aaiHeaders.put("X-FromAppId", "prh"); - aaiHeaders.put("X-TransactionId", "vv-temp"); - aaiHeaders.put("Accept", "application/json"); - aaiHeaders.put("Real-Time", "true"); - aaiHeaders.put("Content-Type", "application/merge-patch+json"); - return aaiHeaders; - - } -} diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiConsumerClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiConsumerClientTest.java new file mode 100644 index 00000000..8b4a2110 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiConsumerClientTest.java @@ -0,0 +1,94 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.service; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; + +class AaiConsumerClientTest { + + private static AaiConsumerClient testedObject; + private static AaiClientConfiguration aaiHttpClientConfigurationMock = mock(AaiClientConfiguration.class); + private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); + private static final String JSON_MESSAGE = "{ \"pnf-id\": \"example-pnf-id-val-22343\", " + + "\"regional-resource-zone\":null, \"ipaddress-v4-oam\": \"11.22.33.44\" }"; + private static ConsumerDmaapModel consumerDmaapModelMock = mock(ConsumerDmaapModel.class); + private static final String PNF_NAME = "nokia-pnf-nhfsadhff"; + + @BeforeAll + static void setup() throws NoSuchFieldException, IllegalAccessException { + + Map aaiHeaders = new HashMap<>(); + aaiHeaders.put("X-FromAppId", "prh"); + aaiHeaders.put("X-TransactionId", "9999"); + aaiHeaders.put("Accept", "application/json"); + aaiHeaders.put("Authorization", "Basic QUFJOkFBSQ=="); + aaiHeaders.put("Real-Time", "true"); + aaiHeaders.put("Content-Type", "application/json"); + + when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); + when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); + when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); + when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PRH"); + when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH"); + when(aaiHttpClientConfigurationMock.aaiBasePath()).thenReturn("/aai/v11"); + when(aaiHttpClientConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf"); + when(aaiHttpClientConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders); + + when(consumerDmaapModelMock.getPnfName()).thenReturn(PNF_NAME); + + testedObject = new AaiConsumerClient(aaiHttpClientConfigurationMock); + setField(); + } + + + @Test + void getExtendedDetails_returnsSuccess() throws IOException { + + when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))) + .thenReturn(Optional.of(JSON_MESSAGE)); + Optional actualResult = testedObject.getHttpResponse(consumerDmaapModelMock); + Assertions.assertEquals(Optional.of(JSON_MESSAGE), actualResult); + } + + + private static void setField() throws NoSuchFieldException, IllegalAccessException { + Field field = testedObject.getClass().getDeclaredField("closeableHttpClient"); + field.setAccessible(true); + field.set(testedObject, closeableHttpClientMock); + } +} diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiHttpClientImplTest.java new file mode 100644 index 00000000..419fe32b --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiHttpClientImplTest.java @@ -0,0 +1,54 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.service; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; + +class AaiHttpClientImplTest { + + private static AaiClientImpl testedObject; + + + @BeforeAll + public static void setup() { + AaiClientConfiguration aaiHttpClientConfigurationMock = mock(AaiClientConfiguration.class); + when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); + when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); + when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); + when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PNF"); + when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PNF"); + when(aaiHttpClientConfigurationMock.aaiIgnoreSslCertificateErrors()).thenReturn(true); + + testedObject = new AaiClientImpl(aaiHttpClientConfigurationMock); + } + + @Test + public void getAaiHttpClientObject_shouldNotBeNull() { + assertNotNull(testedObject.getAaiHttpClient()); + } +} + diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiProducerClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiProducerClientTest.java new file mode 100644 index 00000000..27cb374d --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AaiProducerClientTest.java @@ -0,0 +1,166 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.service; + +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.when; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Field; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.StatusLine; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpPatch; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest; + +class AaiProducerClientTest { + + private static final Integer SUCCESS = 200; + private static final HttpResponse httpResponseMock = mock(HttpResponse.class); + private static final HttpEntity httpEntityMock = mock(HttpEntity.class); + private static final StatusLine statusLineMock = mock(StatusLine.class); + + private static AaiProducerClient testedObject; + private static AaiClientConfiguration aaiHttpClientConfigurationMock = mock(AaiClientConfiguration.class); + private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); + private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest(); + + @BeforeAll + static void setup() throws NoSuchFieldException, IllegalAccessException { + when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("eucalyptus.es-si-eu-dhn-20.eecloud.nsn-net.net"); + when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); + when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); + when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PRH"); + when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH"); + when(aaiHttpClientConfigurationMock.aaiBasePath()).thenReturn("/aai/v11"); + when(aaiHttpClientConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf"); + when(aaiHttpClientConfigurationMock.aaiHeaders()).thenReturn(setupHeaders()); + + testedObject = new AaiProducerClient(aaiHttpClientConfigurationMock); + setField(); + } + + @Test + void getHttpResponse_shouldReturnSuccessStatusCode() throws IOException, URISyntaxException { + // when + when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) + .thenReturn(Optional.of(SUCCESS)); + Optional actualResult = testedObject.getHttpResponse(consumerDmaapModel); + // then + assertEquals(SUCCESS, actualResult.get()); + } + + @Test + void getHttpResponse_shouldHandleIoException() throws IOException, URISyntaxException { + // when + when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) + .thenThrow(new IOException("Error occur")); + + // then + assertNotNull(testedObject.getHttpResponse(consumerDmaapModel)); + } + + @Test + void createHttpRequest_shouldCatchUnsupportedEncodingException() throws URISyntaxException, IOException { + // when + when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) + .thenThrow(new UnsupportedEncodingException("A new Error")); + // then + assertNotNull(testedObject.getHttpResponse(consumerDmaapModel)); + } + + @Test + void encode_shouldCreateEncodedString_whenUserAndPasswordAreSet() throws UnsupportedEncodingException { + // given + String expected = "UFJIOlBSSA=="; + // when + String result = testedObject.encode(); + // then + assertNotNull(result); + assertEquals(expected, result); + } + + @Test + void createHttpPatch_shouldContainAuthorizationBasicValue() throws UnsupportedEncodingException { + // given + String expected = "Authorization: Basic UFJIOlBSSA=="; + // when + HttpPatch patch = testedObject.createHttpPatch(URI.create("localhost"), "{}"); + // then + assertNotNull(patch); + assertEquals(expected, patch.getLastHeader("Authorization").toString()); + } + + @Test + void handleResponse_shouldReturn200() throws IOException { + // When + when(httpResponseMock.getEntity()).thenReturn(httpEntityMock); + when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock); + when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_OK); + // Then + assertEquals(Optional.of(HttpStatus.SC_OK), testedObject.handleResponse(httpResponseMock)); + } + + @Test + void handleResponse_shouldReturn300() throws IOException { + // When + when(httpResponseMock.getEntity()).thenReturn(httpEntityMock); + when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock); + when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_BAD_REQUEST); + // Then + assertEquals(Optional.of(HttpStatus.SC_BAD_REQUEST), testedObject.handleResponse(httpResponseMock)); + } + + + private static void setField() throws NoSuchFieldException, IllegalAccessException { + Field field = testedObject.getClass().getDeclaredField("closeableHttpClient"); + field.setAccessible(true); + field.set(testedObject, closeableHttpClientMock); + } + + private static Map setupHeaders() { + Map aaiHeaders = new HashMap<>(); + aaiHeaders.put("X-FromAppId", "prh"); + aaiHeaders.put("X-TransactionId", "vv-temp"); + aaiHeaders.put("Accept", "application/json"); + aaiHeaders.put("Real-Time", "true"); + aaiHeaders.put("Content-Type", "application/merge-patch+json"); + return aaiHeaders; + + } +} -- cgit 1.2.3-korg