diff options
Diffstat (limited to 'prh-aai-client')
11 files changed, 293 insertions, 151 deletions
diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIClientConfiguration.java index f9cbeb19..60a022b4 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIClientConfiguration.java @@ -17,6 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.dcaegen2.services.config; @@ -30,7 +31,7 @@ import org.springframework.stereotype.Component; @Value.Immutable(prehash = true) @Value.Style(builder = "new") @Gson.TypeAdapters -public abstract class AAIHttpClientConfiguration implements Serializable { +public abstract class AAIClientConfiguration implements Serializable { private static final long serialVersionUID = 1L; diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClient.java index c60027c2..33a8d644 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClient.java @@ -23,7 +23,7 @@ package org.onap.dcaegen2.services.service; import org.apache.http.impl.client.CloseableHttpClient; @FunctionalInterface -public interface AAIHttpClient { +public interface AAIClient { CloseableHttpClient getAAIHttpClient(); } diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClientImpl.java index 90b551db..c2b40b9a 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClientImpl.java @@ -31,7 +31,7 @@ import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.TrustStrategy; -import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -40,14 +40,14 @@ import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; -public class AAIHttpClientImpl implements AAIHttpClient { +public class AAIClientImpl implements AAIClient { - Logger logger = LoggerFactory.getLogger(AAIHttpClientImpl.class); + Logger logger = LoggerFactory.getLogger(AAIClientImpl.class); - private AAIHttpClientConfiguration aaiHttpClientConfig; + private AAIClientConfiguration aaiHttpClientConfig; @Autowired - public AAIHttpClientImpl(AAIHttpClientConfiguration aaiHttpClientConfiguration) { + public AAIClientImpl(AAIClientConfiguration aaiHttpClientConfiguration) { this.aaiHttpClientConfig = aaiHttpClientConfiguration; } diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIConsumerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIConsumerClient.java new file mode 100644 index 00000000..24790be2 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIConsumerClient.java @@ -0,0 +1,133 @@ +/*- + * ============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.apache.http.HttpEntity; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; +import org.onap.dcaegen2.services.utils.HttpUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Optional; + +public class AAIConsumerClient implements AAIExtendedHttpClient { + + Logger logger = LoggerFactory.getLogger(AAIConsumerClient.class); + + private final CloseableHttpClient closeableHttpClient; + private final String aaiHost; + private final String aaiProtocol; + private final Integer aaiHostPortNumber; + + + public AAIConsumerClient(AAIClientConfiguration aaiHttpClientConfiguration) { + final AAIClient aaiClient = new AAIClientImpl(aaiHttpClientConfiguration); + closeableHttpClient = aaiClient.getAAIHttpClient(); + aaiHost = aaiHttpClientConfiguration.aaiHost(); + aaiProtocol = aaiHttpClientConfiguration.aaiProtocol(); + aaiHostPortNumber = aaiHttpClientConfiguration.aaiHostPortNumber(); + } + + @Override + public Optional<String> getHttpResponse(HttpRequestDetails requestDetails) { + + Optional<String> extendedDetails = Optional.empty(); + Optional<HttpRequestBase> request = createRequest(requestDetails); + + try { + extendedDetails = closeableHttpClient.execute(request.get(), aaiResponseHandler()); + } catch (IOException e) { + logger.error("Exception while executing HTTP request: {}", e); + } + + return extendedDetails; + } + + + private URI createAAIExtendedURI(final String path, String pnfName) { + + URI extendedURI = null; + + final URIBuilder uriBuilder = new URIBuilder() + .setScheme(aaiProtocol) + .setHost(aaiHost) + .setPort(aaiHostPortNumber) + .setPath(path + "/" + pnfName); + + try { + extendedURI = uriBuilder.build(); + logger.info("Building extended URI: {}", extendedURI); + } catch (URISyntaxException e) { + logger.error("Exception while building extended URI: {}", e); + } + + return extendedURI; + } + + private ResponseHandler<Optional<String>> aaiResponseHandler() { + return httpResponse -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + logger.info("Status code of operation: {}", responseCode); + final HttpEntity responseEntity = httpResponse.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode) ) { + logger.info("HTTP response successful."); + final String aaiResponse = EntityUtils.toString(responseEntity); + return Optional.of(aaiResponse); + } else { + String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.error("HTTP response not successful : {}", aaiResponse); + return Optional.of("" + responseCode); + } + }; + } + + private HttpRequestBase createHttpRequest(URI extendedURI) { + + if (isExtendedURINotNull(extendedURI)) { + return new HttpGet(extendedURI); + } else { + return null; + } + } + + private Boolean isExtendedURINotNull(URI extendedURI) { + return extendedURI != null; + } + + private Optional<HttpRequestBase> createRequest(HttpRequestDetails requestDetails) { + + final URI extendedURI = createAAIExtendedURI(requestDetails.aaiAPIPath(), requestDetails.pnfName()); + HttpRequestBase request = createHttpRequest(extendedURI); + requestDetails.headers().forEach(request::addHeader); + return Optional.of(request); + } +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIProducerClient.java index 20ae8d0b..0b82578a 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIProducerClient.java @@ -17,38 +17,32 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.dcaegen2.services.service; -import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPatch; -import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; -import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; import org.onap.dcaegen2.services.utils.HttpRequestDetails; import org.onap.dcaegen2.services.utils.HttpUtils; -import org.onap.dcaegen2.services.utils.RequestVerbs; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.lang.NonNull; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; -import java.util.Iterator; -import java.util.Map; import java.util.Optional; -public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { - - Logger logger = LoggerFactory.getLogger(AAIExtendedHttpClientImpl.class); +public class AAIProducerClient implements AAIExtendedHttpClient { + Logger logger = LoggerFactory.getLogger(AAIProducerClient.class); private final CloseableHttpClient closeableHttpClient; private final String aaiHost; @@ -56,8 +50,8 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { private final Integer aaiHostPortNumber; - public AAIExtendedHttpClientImpl (AAIHttpClientConfiguration aaiHttpClientConfiguration) { - final AAIHttpClient aaiHttpClient = new AAIHttpClientImpl(aaiHttpClientConfiguration); + public AAIProducerClient(AAIClientConfiguration aaiHttpClientConfiguration) { + final AAIClient aaiHttpClient = new AAIClientImpl(aaiHttpClientConfiguration); closeableHttpClient = aaiHttpClient.getAAIHttpClient(); aaiHost = aaiHttpClientConfiguration.aaiHost(); aaiProtocol = aaiHttpClientConfiguration.aaiProtocol(); @@ -79,18 +73,15 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { return extendedDetails; } - private URI createAAIExtendedURI(final String path, Map<String, String> queryParams) { + private URI createAAIExtendedURI(final String path, final String pnfName) { URI extendedURI = null; - final URIBuilder uriBuilder = new URIBuilder().setScheme(this.aaiProtocol).setHost(this.aaiHost) - .setPort(this.aaiHostPortNumber) - .setPath(path); - final String customQuery = createCustomQuery(queryParams); - - if (StringUtils.isNoneBlank(customQuery)) { - uriBuilder.setCustomQuery(customQuery); - } + final URIBuilder uriBuilder = new URIBuilder() + .setScheme(aaiProtocol) + .setHost(aaiHost) + .setPort(aaiHostPortNumber) + .setPath(path + "/" + pnfName); try { extendedURI = uriBuilder.build(); @@ -102,45 +93,25 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { return extendedURI; } - private String createCustomQuery(@NonNull final Map<String, String> queryParams) { - final StringBuilder queryStringBuilder = new StringBuilder(""); - final Iterator<Map.Entry<String, String>> queryParamIterator = queryParams.entrySet().iterator(); - - while (queryParamIterator.hasNext()) { - final Map.Entry<String, String> queryParamsEntry = queryParamIterator.next(); - queryStringBuilder.append(queryParamsEntry.getKey()).append("=").append(queryParamsEntry.getValue()); - if (queryParamIterator.hasNext()) { - queryStringBuilder.append("&"); - } - } - - return queryStringBuilder.toString(); - } - private ResponseHandler<Optional<String>> aaiResponseHandler() { - return httpResponse -> { + return (HttpResponse httpResponse) -> { final int responseCode = httpResponse.getStatusLine().getStatusCode(); + logger.info("Status code of operation: {}", responseCode); final HttpEntity responseEntity = httpResponse.getEntity(); - if (HttpUtils.isSuccessfulResponseCode(responseCode) && responseEntity != null) { + if (HttpUtils.isSuccessfulResponseCode(responseCode)) { logger.info("HTTP response successful."); - final String aaiResponse = EntityUtils.toString(responseEntity); - return Optional.of(aaiResponse); + return Optional.of("" + responseCode); } else { String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; logger.error("HTTP response not successful : {}", aaiResponse); - return Optional.empty(); + return Optional.of("" + responseCode); } }; } private HttpRequestBase createHttpRequest(URI extendedURI, HttpRequestDetails httpRequestDetails) { - if (isExtendedURINotNull(extendedURI) && (httpRequestDetails.requestVerb().equals(RequestVerbs.GET))) { - return new HttpGet(extendedURI); - } else if (isExtendedURINotNull(extendedURI) && (httpRequestDetails.requestVerb().equals(RequestVerbs.PUT))) { - return new HttpPut(extendedURI); - } else if (isExtendedURINotNull(extendedURI) && - isPatchRequestValid(httpRequestDetails.requestVerb(),httpRequestDetails.jsonBody())) { + if (isExtendedURINotNull(extendedURI) && httpRequestDetails.jsonBody().isPresent()) { return createHttpPatch(extendedURI, httpRequestDetails.jsonBody()); } else { return null; @@ -174,13 +145,9 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient { return stringEntity; } - private Boolean isPatchRequestValid(RequestVerbs requestVerb, Optional<String> jsonBody) { - return requestVerb == RequestVerbs.PATCH && jsonBody.isPresent(); - } - private Optional<HttpRequestBase> createRequest(HttpRequestDetails requestDetails) { - final URI extendedURI = createAAIExtendedURI(requestDetails.aaiAPIPath(), requestDetails.queryParameters()); + final URI extendedURI = createAAIExtendedURI(requestDetails.aaiAPIPath(), requestDetails.pnfName()); HttpRequestBase request = createHttpRequest(extendedURI, requestDetails); requestDetails.headers().forEach(request::addHeader); return Optional.of(request); diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java index 574cb22f..37e6b860 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java @@ -39,11 +39,8 @@ public abstract class HttpRequestDetails implements Serializable { public abstract Optional<String> jsonBody(); @Value.Parameter - public abstract Map<String,String> queryParameters(); + public abstract String pnfName(); @Value.Parameter public abstract Map<String,String> headers(); - - @Value.Parameter - public abstract RequestVerbs requestVerb(); } diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIConsumerClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIConsumerClientTest.java new file mode 100644 index 00000000..b95cc8d6 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIConsumerClientTest.java @@ -0,0 +1,92 @@ +/*- + * ============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.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.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; + +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 HttpRequestDetails httpRequestDetailsMock = mock(HttpRequestDetails.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\" }"; + + @BeforeAll + public static void setup() throws NoSuchFieldException, IllegalAccessException { + + Map<String, String> aaiHeaders = new HashMap<>(); + aaiHeaders.put("X-FromAppId", "prh"); + aaiHeaders.put("X-TransactionId", "9999"); + aaiHeaders.put("Accept", "application/json"); + aaiHeaders.put("authentication", "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(httpRequestDetailsMock.aaiAPIPath()).thenReturn("/aai/v11/network/pnfs/pnf"); + when(httpRequestDetailsMock.headers()).thenReturn(aaiHeaders); + when(httpRequestDetailsMock.pnfName()).thenReturn("pnf-nokia-45fsfcx"); + when(httpRequestDetailsMock.jsonBody()).thenReturn(Optional.of(JSON_MESSAGE)); + + 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<String> actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); + 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/service/AAIHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java index fd13f1e7..cfe1a7f6 100644 --- 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 @@ -21,7 +21,7 @@ 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 org.onap.dcaegen2.services.config.AAIClientConfiguration; import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.mock; @@ -30,13 +30,13 @@ import static org.mockito.Mockito.when; public class AAIHttpClientImplTest { - private static AAIHttpClientImpl testedObject; - private static AAIHttpClientConfiguration aaiHttpClientConfigurationMock; + private static AAIClientImpl testedObject; + private static AAIClientConfiguration aaiHttpClientConfigurationMock; @BeforeAll public static void setup() { - aaiHttpClientConfigurationMock = mock(AAIHttpClientConfiguration.class); + aaiHttpClientConfigurationMock = mock(AAIClientConfiguration.class); when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https"); when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234); @@ -44,7 +44,7 @@ public class AAIHttpClientImplTest { when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PNF"); when(aaiHttpClientConfigurationMock.aaiIgnoreSSLCertificateErrors()).thenReturn(true); - testedObject = new AAIHttpClientImpl(aaiHttpClientConfigurationMock); + testedObject = new AAIClientImpl(aaiHttpClientConfigurationMock); } @Test 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/AAIProducerClientTest.java index 9ff34b8e..7f3978bd 100644 --- 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/AAIProducerClientTest.java @@ -1,4 +1,4 @@ -/*- +/* * ============LICENSE_START======================================================= * PNF-REGISTRATION-HANDLER * ================================================================================ @@ -20,113 +20,69 @@ 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 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.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.utils.HttpRequestDetails; 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.HttpPatch; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.impl.client.CloseableHttpClient; -import org.immutables.value.internal.$processor$.meta.$GsonMirrors; -import org.junit.Ignore; -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 { +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class AAIProducerClientTest { - private static AAIExtendedHttpClientImpl testedObject; - private static AAIHttpClientConfiguration aaiHttpClientConfigurationMock = mock(AAIHttpClientConfiguration.class); + private static AAIProducerClient testedObject; + private static AAIClientConfiguration aaiHttpClientConfigurationMock = mock(AAIClientConfiguration.class); private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); private static HttpRequestDetails httpRequestDetailsMock = mock(HttpRequestDetails.class); - private static Optional<String> expectedResult = Optional.empty(); - private static final String JSON_MESSAGE = "{ \"ipaddress-v4-oam\": \"11.22.33.44\" }"; - private static final String PNF_ID = "NOKQTFCOC540002E"; + private static final String JsonBody = "{\"ipaddress-v4-oam\":\"11.22.33.155\"}"; + private static final String SUCCESS = "200"; + private static final String PNF_NAME = "nokia-pnf-nhfsadhff"; @BeforeAll public static void init() throws NoSuchFieldException, IllegalAccessException { - Map<String, String> queryParams = new HashMap<>(); - queryParams.put("pnf-id", PNF_ID); - Map<String, String> 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/json"); + aaiHeaders.put("Content-Type", "application/merge-patch+json"); - when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2"); + 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(httpRequestDetailsMock.aaiAPIPath()).thenReturn("/aai/v11/network/pnfs/pnf"); + when(httpRequestDetailsMock.headers()).thenReturn(aaiHeaders); - when(httpRequestDetailsMock.queryParameters()).thenReturn(queryParams); - when(httpRequestDetailsMock.jsonBody()).thenReturn(Optional.of(JSON_MESSAGE)); + when(httpRequestDetailsMock.pnfName()).thenReturn(PNF_NAME); + when(httpRequestDetailsMock.jsonBody()).thenReturn(Optional.of(JsonBody)); - testedObject = new AAIExtendedHttpClientImpl(aaiHttpClientConfigurationMock); + testedObject = new AAIProducerClient(aaiHttpClientConfigurationMock); setField(); } - @AfterAll - public static void teardown() { - testedObject = null; - aaiHttpClientConfigurationMock = null; - closeableHttpClientMock = null; - httpRequestDetailsMock = null; - expectedResult = null; - } - @Test public void getHttpResponsePatch_success() throws IOException { - when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.PATCH); - - expectedResult = Optional.of(JSON_MESSAGE); when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) - .thenReturn(expectedResult); + .thenReturn(Optional.of(SUCCESS)); Optional<String> 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<String> 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<String> actualResult = testedObject.getHttpResponse(httpRequestDetailsMock); - Assertions.assertEquals(Optional.empty(),actualResult); - } - - @Test - public void getHttpResponsePut_failure() { - when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.PUT); - + Assertions.assertEquals(SUCCESS, actualResult.get()); } private static void setField() throws NoSuchFieldException, IllegalAccessException { 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 index 76c2969a..29a00927 100644 --- 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 @@ -23,12 +23,12 @@ 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; +import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.config.ImmutableAAIClientConfiguration; public class AAIHttpClientConfigurationTest { - private static AAIHttpClientConfiguration client; + private static AAIClientConfiguration 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"; @@ -36,7 +36,7 @@ public class AAIHttpClientConfigurationTest { @BeforeAll public static void init() { - client = new ImmutableAAIHttpClientConfiguration.Builder() + client = new ImmutableAAIClientConfiguration.Builder() .aaiHost(AAI_HOST) .aaiHostPortNumber(PORT) .aaiProtocol(PROTOCOL) 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 index 563e6921..0deb8adb 100644 --- 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 @@ -36,19 +36,16 @@ public class HttpRequestDetailsTest { private static HttpRequestDetails testObject; private static final String AAI_PATH = "aaiPathTest"; - private static final RequestVerbs HTTP_VERB = RequestVerbs.PATCH; - 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"; private static final String JSON_MESSAGE = "{\"dare_to\": \"dream_big\"}"; + private static final String PNF_NAME = "pnf-nokia-5454885485"; @BeforeAll public static void init() { testObject = ImmutableHttpRequestDetails.builder() .aaiAPIPath(AAI_PATH) - .requestVerb(HTTP_VERB) - .putQueryParameters(QUERY_KEY1,QUERY_VALUE1) + .pnfName(PNF_NAME) .putHeaders(HEADERS_KEY1,HEADERS_VALUE1) .jsonBody(JSON_MESSAGE) .build(); @@ -58,8 +55,7 @@ public class HttpRequestDetailsTest { 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.PATCH, testObject.requestVerb()); + Assertions.assertEquals(PNF_NAME, testObject.pnfName()); Assertions.assertEquals(Optional.of(JSON_MESSAGE), testObject.jsonBody()); } } |