From 9725138e6882182c50cbac79ace5d66774a17619 Mon Sep 17 00:00:00 2001 From: pwielebs Date: Tue, 3 Apr 2018 12:12:43 +0200 Subject: Http put added Change-Id: Id21b799a35b531c30f0c1b5df5c6daac6fd56e92 Issue-ID: DCAEGEN2-407 Signed-off-by: pwielebs --- .../service/AAIExtendedHttpClientImplTest.java | 131 +++++++++++++++++++++ .../services/service/AAIHttpClientImplTest.java | 57 +++++++++ .../config/AAIHttpClientConfigurationTest.java | 58 +++++++++ .../service/utils/HttpRequestDetailsTest.java | 60 ++++++++++ .../services/service/utils/HttpUtilsTest.java | 42 +++++++ 5 files changed, 348 insertions(+) create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java (limited to 'prh-aai-client/src/test/java/org/onap') diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java new file mode 100644 index 00000000..e114bbfd --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java @@ -0,0 +1,131 @@ +/*- + * ============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.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.client.methods.HttpPut; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.jupiter.api.*; +import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.onap.dcaegen2.services.utils.RequestVerbs; + +public class AAIExtendedHttpClientImplTest { + + private static AAIExtendedHttpClientImpl testedObject; + private static AAIHttpClientConfiguration aaiHttpClientConfigurationMock = mock(AAIHttpClientConfiguration.class); + private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); + private static HttpRequestDetails httpRequestDetailsMock = mock(HttpRequestDetails.class); + private static Optional expectedResult = Optional.empty(); + + @BeforeAll + public static void init() throws NoSuchFieldException, IllegalAccessException { + + Map queryParams = new HashMap<>(); + queryParams.put("ipaddress-v4-oam", "11.22.33.44"); + + Map AAI_HEADERS = new HashMap<>(); + AAI_HEADERS.put("X-FromAppId", "prh"); + AAI_HEADERS.put("X-TransactionId", "vv-temp"); + AAI_HEADERS.put("Accept", "application/json"); + AAI_HEADERS.put("Real-Time", "true"); + AAI_HEADERS.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(httpRequestDetailsMock.aaiAPIPath()).thenReturn("/aai/v11/network/pnfs/pnf/NOKQTFCOC540002E"); + when(httpRequestDetailsMock.headers()).thenReturn(AAI_HEADERS); + when(httpRequestDetailsMock.queryParameters()).thenReturn(queryParams); + + testedObject = new AAIExtendedHttpClientImpl(aaiHttpClientConfigurationMock); + setField(); + } + + @AfterAll + public static void teardown() { + testedObject = null; + aaiHttpClientConfigurationMock = null; + closeableHttpClientMock = null; + httpRequestDetailsMock = null; + expectedResult = null; + } + + @Test + public void getHttpResponseGet_success() throws IOException { + when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.GET); + + expectedResult = Optional.of("getExtendedDetailsOK"); + + when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). + thenReturn(expectedResult); + Optional actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); + + Assertions.assertEquals(expectedResult.get(), actualResult.get()); + } + + @Test + public void getHttpResponsePut_success() throws IOException { + when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.PUT); + + expectedResult = Optional.of("getExtendedDetailsOK"); + + when(closeableHttpClientMock.execute(any(HttpPut.class), any(ResponseHandler.class))). + thenReturn(expectedResult); + Optional actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); + + Assertions.assertEquals(expectedResult.get(), actualResult.get()); + } + + @Test + public void getExtendedDetails_returnsNull() throws IOException { + when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.GET); + when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). + thenReturn(Optional.empty()); + Optional actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); + Assertions.assertEquals(Optional.empty(),actualResult); + } + + @Test + public void getHttpResponsePut_failure() { + when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.PUT); + + } + + 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/service/AAIHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java new file mode 100644 index 00000000..d26e6afa --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java @@ -0,0 +1,57 @@ +/*- + * ============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.service; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +public class AAIHttpClientImplTest { + + private static AAIHttpClientImpl testedObject; + private static AAIHttpClientConfiguration aaiHttpClientConfigurationMock; + + + @BeforeAll + public static void setup() { + aaiHttpClientConfigurationMock = mock(AAIHttpClientConfiguration.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 AAIHttpClientImpl(aaiHttpClientConfigurationMock); + } + + @Test + public void getAAIHttpClientObject_shouldNotBeNull() { + testedObject.getAAIHttpClient(); + assertNotNull(testedObject.getAAIHttpClient()); + } +} + diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java new file mode 100644 index 00000000..a759aca5 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java @@ -0,0 +1,58 @@ +/*- + * ============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.service.config; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.config.ImmutableAAIHttpClientConfiguration; + +public class AAIHttpClientConfigurationTest { + + private static AAIHttpClientConfiguration client; + private static final String AAI_HOST = "/aai/v11/network/pnfs/pnf/NOKQTFCOC540002E"; + private static final Integer PORT = 1234; + private static final String PROTOCOL = "https"; + private static final String USER_NAME_PASSWORD = "PRH"; + + @BeforeAll + public static void init() { + client = ImmutableAAIHttpClientConfiguration.builder() + .aaiHost(AAI_HOST) + .aaiHostPortNumber(PORT) + .aaiProtocol(PROTOCOL) + .aaiUserName(USER_NAME_PASSWORD) + .aaiUserPassword(USER_NAME_PASSWORD) + .aaiIgnoreSSLCertificateErrors(true) + .build(); + } + + @Test + public void testGetters_success() { + Assertions.assertEquals(AAI_HOST,client.aaiHost()); + Assertions.assertEquals(PORT, client.aaiHostPortNumber()); + Assertions.assertEquals(PROTOCOL,client.aaiProtocol()); + Assertions.assertEquals(USER_NAME_PASSWORD, client.aaiUserName()); + Assertions.assertEquals(USER_NAME_PASSWORD, client.aaiUserPassword()); + Assertions.assertEquals(true, client.aaiIgnoreSSLCertificateErrors()); + } +} diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java new file mode 100644 index 00000000..36105d05 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java @@ -0,0 +1,60 @@ +/*- + * ============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.service.utils; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.onap.dcaegen2.services.utils.ImmutableHttpRequestDetails; +import org.onap.dcaegen2.services.utils.RequestVerbs; + + +public class HttpRequestDetailsTest { + + private static HttpRequestDetails testObject; + + private static final String AAI_PATH = "aaiPathTest"; + private static final RequestVerbs HTTP_VERB = RequestVerbs.GET; + private static final String QUERY_KEY1 = "queryKey1"; + private static final String QUERY_VALUE1 = "queryValue1"; + private static final String HEADERS_KEY1 = "headersKey1"; + private static final String HEADERS_VALUE1 = "headersValue1"; + + @BeforeAll + public static void init() { + testObject = ImmutableHttpRequestDetails.builder() + .aaiAPIPath(AAI_PATH) + .requestVerb(HTTP_VERB) + .putQueryParameters(QUERY_KEY1,QUERY_VALUE1) + .putHeaders(HEADERS_KEY1,HEADERS_VALUE1) + .build(); + } + + @Test + public void testGetters_success() { + Assertions.assertEquals(AAI_PATH, testObject.aaiAPIPath()); + Assertions.assertEquals(HEADERS_VALUE1, testObject.headers().get(HEADERS_KEY1)); + Assertions.assertEquals(QUERY_VALUE1, testObject.queryParameters().get(QUERY_KEY1)); + Assertions.assertEquals(RequestVerbs.GET, testObject.requestVerb()); + } +} diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java new file mode 100644 index 00000000..53b31da2 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java @@ -0,0 +1,42 @@ +/*- + * ============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.service.utils; + +import org.apache.http.HttpStatus; +import org.junit.Test; +import org.onap.dcaegen2.services.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