From 7ddaf390698fe5ae9143d91e7011059b3973f8ce Mon Sep 17 00:00:00 2001 From: pwielebs Date: Wed, 23 May 2018 15:31:11 +0200 Subject: Refactor of prh-aai-client Change-Id: Idbca6fe4c050c789f4479164846437039d3b549d Issue-ID: DCAEGEN2-451 Signed-off-by: pwielebs --- .../prh/service/AAIProducerClientTest.java | 61 ++++++++++++++++------ .../services/prh/service/utils/HttpUtilsTest.java | 42 --------------- 2 files changed, 45 insertions(+), 58 deletions(-) delete mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/utils/HttpUtilsTest.java (limited to 'prh-aai-client/src/test/java') 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 index ec926294..594df662 100644 --- 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 @@ -23,19 +23,22 @@ package org.onap.dcaegen2.services.prh.service; 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.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 org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; 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; @@ -49,18 +52,8 @@ public class AAIProducerClientTest { private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest(); - @Test - public void getHttpResponse_shouldReturnSuccessStatusCode() - throws IOException, URISyntaxException, NoSuchFieldException, IllegalAccessException { - - //given - 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"); - + @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); @@ -68,16 +61,41 @@ public class AAIProducerClientTest { when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH"); when(aaiHttpClientConfigurationMock.aaiBasePath()).thenReturn("/aai/v11"); when(aaiHttpClientConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf"); - when(aaiHttpClientConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders); + 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()); + } - //then - Assertions.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)); } private static void setField() throws NoSuchFieldException, IllegalAccessException { @@ -85,4 +103,15 @@ public class AAIProducerClientTest { 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/utils/HttpUtilsTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/utils/HttpUtilsTest.java deleted file mode 100644 index 87a14799..00000000 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/utils/HttpUtilsTest.java +++ /dev/null @@ -1,42 +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.utils; - -import org.apache.http.HttpStatus; -import org.junit.Test; -import org.onap.dcaegen2.services.prh.utils.HttpUtils; - -import static junit.framework.TestCase.assertFalse; -import static junit.framework.TestCase.assertTrue; - - -public class HttpUtilsTest { - - @Test - public void isSuccessfulResponseCode_shouldReturnTrue() { - assertTrue(HttpUtils.isSuccessfulResponseCode(HttpUtils.SC_ACCEPTED)); - } - - @Test - public void isSuccessfulResponseCode_shouldReturnFalse() { - assertFalse(HttpUtils.isSuccessfulResponseCode(HttpStatus.SC_BAD_GATEWAY)); - } -} \ No newline at end of file -- cgit 1.2.3-korg