From 3e559e25389b24d1cacedc8983334054e33faf14 Mon Sep 17 00:00:00 2001 From: wasala Date: Thu, 17 May 2018 08:16:09 +0200 Subject: Change package in prh-dmaap-client module Move package org.onap.dcaegen2.services to org.onap.dcaegen2.services.prh Change-Id: Idef63833dcbc9cd28dda6b893245fcd16df4d97d Issue-ID: DCAEGEN2-396 Signed-off-by: wasala --- .../services/config/AAIClientConfiguration.java | 67 --------- .../prh/config/AAIClientConfiguration.java | 67 +++++++++ .../dcaegen2/services/prh/service/AAIClient.java | 29 ++++ .../services/prh/service/AAIClientImpl.java | 92 ++++++++++++ .../services/prh/service/AAIConsumerClient.java | 127 +++++++++++++++++ .../prh/service/AAIExtendedHttpClient.java | 31 +++++ .../services/prh/service/AAIProducerClient.java | 155 +++++++++++++++++++++ .../onap/dcaegen2/services/service/AAIClient.java | 29 ---- .../dcaegen2/services/service/AAIClientImpl.java | 92 ------------ .../services/service/AAIConsumerClient.java | 127 ----------------- .../services/service/AAIExtendedHttpClient.java | 31 ----- .../services/service/AAIProducerClient.java | 155 --------------------- .../prh/service/AAIConsumerClientTest.java | 93 +++++++++++++ .../prh/service/AAIHttpClientImplTest.java | 56 ++++++++ .../prh/service/AAIProducerClientTest.java | 94 +++++++++++++ .../config/AAIHttpClientConfigurationTest.java | 62 +++++++++ .../services/prh/service/utils/HttpUtilsTest.java | 42 ++++++ .../services/service/AAIConsumerClientTest.java | 93 ------------- .../services/service/AAIHttpClientImplTest.java | 56 -------- .../services/service/AAIProducerClientTest.java | 94 ------------- .../config/AAIHttpClientConfigurationTest.java | 62 --------- .../services/service/utils/HttpUtilsTest.java | 42 ------ 22 files changed, 848 insertions(+), 848 deletions(-) delete mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIClientConfiguration.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/config/AAIClientConfiguration.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIClient.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIClientImpl.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java create mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java delete mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClient.java delete mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClientImpl.java delete mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIConsumerClient.java delete mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java delete mode 100644 prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIProducerClient.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 create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/config/AAIHttpClientConfigurationTest.java create mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/utils/HttpUtilsTest.java delete mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIConsumerClientTest.java delete mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java delete mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIProducerClientTest.java delete mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java delete mode 100644 prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java (limited to 'prh-aai-client') diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIClientConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIClientConfiguration.java deleted file mode 100644 index 5a3bffc6..00000000 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIClientConfiguration.java +++ /dev/null @@ -1,67 +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.config; - - -import java.io.Serializable; -import java.util.Map; - -import org.immutables.gson.Gson; -import org.immutables.value.Value; -import org.springframework.stereotype.Component; - - -@Component -@Value.Immutable(prehash = true) -@Value.Style(builder = "new") -@Gson.TypeAdapters -public abstract class AAIClientConfiguration implements Serializable { - - private static final long serialVersionUID = 1L; - - @Value.Parameter - public abstract String aaiHost(); - - @Value.Parameter - public abstract Integer aaiHostPortNumber(); - - @Value.Parameter - public abstract String aaiProtocol(); - - @Value.Parameter - public abstract String aaiUserName(); - - @Value.Parameter - public abstract String aaiUserPassword(); - - @Value.Parameter - public abstract Boolean aaiIgnoreSSLCertificateErrors(); - - @Value.Parameter - public abstract String aaiBasePath(); - - @Value.Parameter - public abstract String aaiPnfPath(); - - @Value.Parameter - public abstract Map aaiHeaders(); - -} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/config/AAIClientConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/config/AAIClientConfiguration.java new file mode 100644 index 00000000..d652ccb7 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/config/AAIClientConfiguration.java @@ -0,0 +1,67 @@ +/*- + * ============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.config; + + +import java.io.Serializable; +import java.util.Map; + +import org.immutables.gson.Gson; +import org.immutables.value.Value; +import org.springframework.stereotype.Component; + + +@Component +@Value.Immutable(prehash = true) +@Value.Style(builder = "new") +@Gson.TypeAdapters +public abstract class AAIClientConfiguration implements Serializable { + + private static final long serialVersionUID = 1L; + + @Value.Parameter + public abstract String aaiHost(); + + @Value.Parameter + public abstract Integer aaiHostPortNumber(); + + @Value.Parameter + public abstract String aaiProtocol(); + + @Value.Parameter + public abstract String aaiUserName(); + + @Value.Parameter + public abstract String aaiUserPassword(); + + @Value.Parameter + public abstract Boolean aaiIgnoreSSLCertificateErrors(); + + @Value.Parameter + public abstract String aaiBasePath(); + + @Value.Parameter + public abstract String aaiPnfPath(); + + @Value.Parameter + public abstract Map aaiHeaders(); + +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIClient.java new file mode 100644 index 00000000..09d7f6ae --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIClient.java @@ -0,0 +1,29 @@ +/*- + * ============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.impl.client.CloseableHttpClient; + +@FunctionalInterface +public interface AAIClient { + CloseableHttpClient getAAIHttpClient(); +} + diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIClientImpl.java new file mode 100644 index 00000000..5e938528 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIClientImpl.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.prh.service; + +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +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.prh.config.AAIClientConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + +public class AAIClientImpl implements AAIClient { + + private Logger logger = LoggerFactory.getLogger(AAIClientImpl.class); + + private AAIClientConfiguration aaiClientConfig; + + + public AAIClientImpl(AAIClientConfiguration aaiClientConfiguration) { + this.aaiClientConfig = aaiClientConfiguration; + } + + @Override + public CloseableHttpClient getAAIHttpClient() { + + final HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); + final boolean aaiIgnoreSSLCertificateErrors = aaiClientConfig.aaiIgnoreSSLCertificateErrors(); + + TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; + + if (aaiIgnoreSSLCertificateErrors) { + try { + logger.info("Setting SSL Context for AAI HTTP Client"); + httpClientBuilder.setSSLContext(new SSLContextBuilder() + .loadTrustMaterial(null, acceptingTrustStrategy) + .build()); + + } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e ) { + logger.error("Exception while setting SSL Context for AAI HTTP Client: {}", e); + } + + httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); + } + + final String aaiUserName = aaiClientConfig.aaiUserName(); + + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + + if (aaiUserName != null) { + final String aaiHost = aaiClientConfig.aaiHost(); + final Integer aaiHostPortNumber = aaiClientConfig.aaiHostPortNumber(); + final String aaiUserPassword = aaiClientConfig.aaiUserPassword(); + final AuthScope aaiHostPortAuthScope = new AuthScope(aaiHost, aaiHostPortNumber); + final Credentials aaiCredentials = new UsernamePasswordCredentials(aaiUserName, aaiUserPassword); + credentialsProvider.setCredentials(aaiHostPortAuthScope, aaiCredentials); + } + + httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); + + return httpClientBuilder.build(); + } +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java new file mode 100644 index 00000000..7836a75d --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java @@ -0,0 +1,127 @@ +/*- + * ============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.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.prh.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +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.Map; +import java.util.Objects; +import java.util.Optional; + +public class AAIConsumerClient { + + Logger logger = LoggerFactory.getLogger(AAIConsumerClient.class); + + private final CloseableHttpClient closeableHttpClient; + private final String aaiHost; + private final String aaiProtocol; + private final Integer aaiHostPortNumber; + private final String aaiPath; + private final Map aaiHeaders; + + + public AAIConsumerClient(AAIClientConfiguration aaiClientConfiguration) { + closeableHttpClient = new AAIClientImpl(aaiClientConfiguration).getAAIHttpClient(); + aaiHost = aaiClientConfiguration.aaiHost(); + aaiProtocol = aaiClientConfiguration.aaiProtocol(); + aaiHostPortNumber = aaiClientConfiguration.aaiHostPortNumber(); + aaiPath = aaiClientConfiguration.aaiBasePath() + aaiClientConfiguration.aaiPnfPath(); + aaiHeaders = aaiClientConfiguration.aaiHeaders(); + } + + public Optional getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException { + Optional request = createRequest(consumerDmaapModel); + try { + return closeableHttpClient.execute(request.get(), aaiResponseHandler()); + } catch (IOException e) { + logger.warn("Exception while executing http client: ", e); + throw new IOException(); + } + } + + private URI createAAIExtendedURI(String pnfName) { + + URI extendedURI = null; + + final URIBuilder uriBuilder = new URIBuilder() + .setScheme(aaiProtocol) + .setHost(aaiHost) + .setPort(aaiHostPortNumber) + .setPath(aaiPath + "/" + pnfName); + + try { + extendedURI = uriBuilder.build(); + logger.trace("Building extended URI: {}", extendedURI); + } catch (URISyntaxException e) { + logger.warn("Exception while building extended URI: {}", e); + } + + return extendedURI; + } + + private ResponseHandler> aaiResponseHandler() { + return httpResponse -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + logger.trace("Status code of operation: {}", responseCode); + final HttpEntity responseEntity = httpResponse.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode) ) { + logger.trace("HTTP response successful."); + final String aaiResponse = EntityUtils.toString(responseEntity); + return Optional.of(aaiResponse); + } else { + String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.warn("HTTP response not successful : {}", aaiResponse); + return Optional.of(String.valueOf(responseCode)); + } + }; + } + + private HttpRequestBase createHttpRequest(URI extendedURI) { + return isExtendedURINotNull(extendedURI) ? new HttpGet(extendedURI) : null; + } + + private Boolean isExtendedURINotNull(URI extendedURI) { + return extendedURI != null; + } + + private Optional createRequest(ConsumerDmaapModel consumerDmaapModel) { + final URI extendedURI = createAAIExtendedURI(consumerDmaapModel.getPnfName()); + HttpRequestBase request = createHttpRequest(extendedURI); + aaiHeaders.forEach(Objects.requireNonNull(request)::addHeader); + Objects.requireNonNull(request).addHeader("Content-Type", "application/json"); + return Optional.of(request); + } +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java new file mode 100644 index 00000000..cb884aed --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java @@ -0,0 +1,31 @@ +/*- + * ============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.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; + +import java.io.IOException; +import java.util.Optional; + + +@FunctionalInterface +public interface AAIExtendedHttpClient { + Optional getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException; +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java new file mode 100644 index 00000000..0db9b114 --- /dev/null +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java @@ -0,0 +1,155 @@ +/*- + * ============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.client.ResponseHandler; +import org.apache.http.client.methods.HttpPatch; +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.prh.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.model.CommonFunctions; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.utils.HttpUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +public class AAIProducerClient implements AAIExtendedHttpClient { + Logger logger = LoggerFactory.getLogger(AAIProducerClient.class); + + private final CloseableHttpClient closeableHttpClient; + private final String aaiHost; + private final String aaiProtocol; + private final Integer aaiHostPortNumber; + private final String aaiPath; + private final Map aaiHeaders; + + + public AAIProducerClient(AAIClientConfiguration aaiClientConfiguration) { + closeableHttpClient = new AAIClientImpl(aaiClientConfiguration).getAAIHttpClient(); + aaiHost = aaiClientConfiguration.aaiHost(); + aaiProtocol = aaiClientConfiguration.aaiProtocol(); + aaiHostPortNumber = aaiClientConfiguration.aaiHostPortNumber(); + aaiPath = aaiClientConfiguration.aaiBasePath() + aaiClientConfiguration.aaiPnfPath(); + aaiHeaders = aaiClientConfiguration.aaiHeaders(); + } + + + @Override + public Optional getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException { + Optional request = createRequest(consumerDmaapModel); + try { + return closeableHttpClient.execute(request.get(), aaiResponseHandler()); + } catch (IOException e) { + logger.warn("Exception while executing http client: ", e); + throw new IOException(); + } + } + + private URI createAAIExtendedURI(final String pnfName) { + URI extendedURI = null; + final URIBuilder uriBuilder = new URIBuilder() + .setScheme(aaiProtocol) + .setHost(aaiHost) + .setPort(aaiHostPortNumber) + .setPath(aaiPath + "/" + pnfName); + try { + extendedURI = uriBuilder.build(); + logger.trace("Building extended URI: {}", extendedURI); + } catch (URISyntaxException e) { + logger.warn("Exception while building extended URI: ", e); + } + return extendedURI; + } + + private ResponseHandler> aaiResponseHandler() { + return (HttpResponse httpResponse) -> { + final Integer responseCode = httpResponse.getStatusLine().getStatusCode(); + logger.trace("Status code of operation: {}", responseCode); + final HttpEntity responseEntity = httpResponse.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode)) { + logger.trace("HTTP response successful."); + return Optional.of(responseCode); + } else { + String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.warn("HTTP response not successful : {}", aaiResponse); + return Optional.of(responseCode); + } + }; + } + + private HttpRequestBase createHttpRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { + String jsonBody = CommonFunctions.createJsonBody(consumerDmaapModel); + + if (isExtendedURINotNull(extendedURI) && jsonBody != null && !"".equals(jsonBody)) { + return createHttpPatch(extendedURI, Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel))); + } else { + return null; + } + } + + private Boolean isExtendedURINotNull(URI extendedURI) { + return extendedURI != null; + } + + + private Optional createStringEntity(Optional jsonBody) { + return Optional.of(parseJson(jsonBody).get()); + } + + private HttpPatch createHttpPatch(URI extendedURI, Optional jsonBody) { + HttpPatch httpPatch = new HttpPatch(extendedURI); + Optional stringEntity = createStringEntity(jsonBody); + httpPatch.setEntity(stringEntity.get()); + return httpPatch; + } + + private Optional parseJson(Optional jsonBody) { + Optional stringEntity = Optional.empty(); + try { + stringEntity = Optional.of(new StringEntity(jsonBody.get())); + } catch (UnsupportedEncodingException e) { + logger.warn("Exception while parsing JSON: ", e); + } + return stringEntity; + } + + private Optional createRequest(ConsumerDmaapModel consumerDmaapModel) { + final URI extendedURI = createAAIExtendedURI(consumerDmaapModel.getPnfName()); + HttpRequestBase request = createHttpRequest(extendedURI, consumerDmaapModel); + aaiHeaders.forEach(Objects.requireNonNull(request)::addHeader); + Objects.requireNonNull(request).addHeader("Content-Type", "application/merge-patch+json"); + return Optional.of(request); + } +} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClient.java deleted file mode 100644 index 33a8d644..00000000 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClient.java +++ /dev/null @@ -1,29 +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.service; - -import org.apache.http.impl.client.CloseableHttpClient; - -@FunctionalInterface -public interface AAIClient { - CloseableHttpClient getAAIHttpClient(); -} - diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClientImpl.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClientImpl.java deleted file mode 100644 index 785ff972..00000000 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIClientImpl.java +++ /dev/null @@ -1,92 +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.service; - -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.Credentials; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.conn.ssl.NoopHostnameVerifier; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.CloseableHttpClient; -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.AAIClientConfiguration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; - -public class AAIClientImpl implements AAIClient { - - private Logger logger = LoggerFactory.getLogger(AAIClientImpl.class); - - private AAIClientConfiguration aaiClientConfig; - - - public AAIClientImpl(AAIClientConfiguration aaiClientConfiguration) { - this.aaiClientConfig = aaiClientConfiguration; - } - - @Override - public CloseableHttpClient getAAIHttpClient() { - - final HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); - final boolean aaiIgnoreSSLCertificateErrors = aaiClientConfig.aaiIgnoreSSLCertificateErrors(); - - TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; - - if (aaiIgnoreSSLCertificateErrors) { - try { - logger.info("Setting SSL Context for AAI HTTP Client"); - httpClientBuilder.setSSLContext(new SSLContextBuilder() - .loadTrustMaterial(null, acceptingTrustStrategy) - .build()); - - } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e ) { - logger.error("Exception while setting SSL Context for AAI HTTP Client: {}", e); - } - - httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); - } - - final String aaiUserName = aaiClientConfig.aaiUserName(); - - final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - - if (aaiUserName != null) { - final String aaiHost = aaiClientConfig.aaiHost(); - final Integer aaiHostPortNumber = aaiClientConfig.aaiHostPortNumber(); - final String aaiUserPassword = aaiClientConfig.aaiUserPassword(); - final AuthScope aaiHostPortAuthScope = new AuthScope(aaiHost, aaiHostPortNumber); - final Credentials aaiCredentials = new UsernamePasswordCredentials(aaiUserName, aaiUserPassword); - credentialsProvider.setCredentials(aaiHostPortAuthScope, aaiCredentials); - } - - httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); - - return httpClientBuilder.build(); - } -} 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 deleted file mode 100644 index edf2492f..00000000 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIConsumerClient.java +++ /dev/null @@ -1,127 +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.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.prh.model.ConsumerDmaapModel; -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.Map; -import java.util.Objects; -import java.util.Optional; - -public class AAIConsumerClient { - - Logger logger = LoggerFactory.getLogger(AAIConsumerClient.class); - - private final CloseableHttpClient closeableHttpClient; - private final String aaiHost; - private final String aaiProtocol; - private final Integer aaiHostPortNumber; - private final String aaiPath; - private final Map aaiHeaders; - - - public AAIConsumerClient(AAIClientConfiguration aaiClientConfiguration) { - closeableHttpClient = new AAIClientImpl(aaiClientConfiguration).getAAIHttpClient(); - aaiHost = aaiClientConfiguration.aaiHost(); - aaiProtocol = aaiClientConfiguration.aaiProtocol(); - aaiHostPortNumber = aaiClientConfiguration.aaiHostPortNumber(); - aaiPath = aaiClientConfiguration.aaiBasePath() + aaiClientConfiguration.aaiPnfPath(); - aaiHeaders = aaiClientConfiguration.aaiHeaders(); - } - - public Optional getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException { - Optional request = createRequest(consumerDmaapModel); - try { - return closeableHttpClient.execute(request.get(), aaiResponseHandler()); - } catch (IOException e) { - logger.warn("Exception while executing http client: ", e); - throw new IOException(); - } - } - - private URI createAAIExtendedURI(String pnfName) { - - URI extendedURI = null; - - final URIBuilder uriBuilder = new URIBuilder() - .setScheme(aaiProtocol) - .setHost(aaiHost) - .setPort(aaiHostPortNumber) - .setPath(aaiPath + "/" + pnfName); - - try { - extendedURI = uriBuilder.build(); - logger.trace("Building extended URI: {}", extendedURI); - } catch (URISyntaxException e) { - logger.warn("Exception while building extended URI: {}", e); - } - - return extendedURI; - } - - private ResponseHandler> aaiResponseHandler() { - return httpResponse -> { - final int responseCode = httpResponse.getStatusLine().getStatusCode(); - logger.trace("Status code of operation: {}", responseCode); - final HttpEntity responseEntity = httpResponse.getEntity(); - - if (HttpUtils.isSuccessfulResponseCode(responseCode) ) { - logger.trace("HTTP response successful."); - final String aaiResponse = EntityUtils.toString(responseEntity); - return Optional.of(aaiResponse); - } else { - String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - logger.warn("HTTP response not successful : {}", aaiResponse); - return Optional.of(String.valueOf(responseCode)); - } - }; - } - - private HttpRequestBase createHttpRequest(URI extendedURI) { - return isExtendedURINotNull(extendedURI) ? new HttpGet(extendedURI) : null; - } - - private Boolean isExtendedURINotNull(URI extendedURI) { - return extendedURI != null; - } - - private Optional createRequest(ConsumerDmaapModel consumerDmaapModel) { - final URI extendedURI = createAAIExtendedURI(consumerDmaapModel.getPnfName()); - HttpRequestBase request = createHttpRequest(extendedURI); - aaiHeaders.forEach(Objects.requireNonNull(request)::addHeader); - Objects.requireNonNull(request).addHeader("Content-Type", "application/json"); - return Optional.of(request); - } -} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java deleted file mode 100644 index 690300dd..00000000 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java +++ /dev/null @@ -1,31 +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.service; - -import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; - -import java.io.IOException; -import java.util.Optional; - - -@FunctionalInterface -public interface AAIExtendedHttpClient { - Optional getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException; -} diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIProducerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIProducerClient.java deleted file mode 100644 index 4c459bc4..00000000 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIProducerClient.java +++ /dev/null @@ -1,155 +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.service; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpPatch; -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.AAIClientConfiguration; -import org.onap.dcaegen2.services.prh.model.CommonFunctions; -import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import org.onap.dcaegen2.services.utils.HttpUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; - -public class AAIProducerClient implements AAIExtendedHttpClient { - Logger logger = LoggerFactory.getLogger(AAIProducerClient.class); - - private final CloseableHttpClient closeableHttpClient; - private final String aaiHost; - private final String aaiProtocol; - private final Integer aaiHostPortNumber; - private final String aaiPath; - private final Map aaiHeaders; - - - public AAIProducerClient(AAIClientConfiguration aaiClientConfiguration) { - closeableHttpClient = new AAIClientImpl(aaiClientConfiguration).getAAIHttpClient(); - aaiHost = aaiClientConfiguration.aaiHost(); - aaiProtocol = aaiClientConfiguration.aaiProtocol(); - aaiHostPortNumber = aaiClientConfiguration.aaiHostPortNumber(); - aaiPath = aaiClientConfiguration.aaiBasePath() + aaiClientConfiguration.aaiPnfPath(); - aaiHeaders = aaiClientConfiguration.aaiHeaders(); - } - - - @Override - public Optional getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException { - Optional request = createRequest(consumerDmaapModel); - try { - return closeableHttpClient.execute(request.get(), aaiResponseHandler()); - } catch (IOException e) { - logger.warn("Exception while executing http client: ", e); - throw new IOException(); - } - } - - private URI createAAIExtendedURI(final String pnfName) { - URI extendedURI = null; - final URIBuilder uriBuilder = new URIBuilder() - .setScheme(aaiProtocol) - .setHost(aaiHost) - .setPort(aaiHostPortNumber) - .setPath(aaiPath + "/" + pnfName); - try { - extendedURI = uriBuilder.build(); - logger.trace("Building extended URI: {}", extendedURI); - } catch (URISyntaxException e) { - logger.warn("Exception while building extended URI: ", e); - } - return extendedURI; - } - - private ResponseHandler> aaiResponseHandler() { - return (HttpResponse httpResponse) -> { - final Integer responseCode = httpResponse.getStatusLine().getStatusCode(); - logger.trace("Status code of operation: {}", responseCode); - final HttpEntity responseEntity = httpResponse.getEntity(); - - if (HttpUtils.isSuccessfulResponseCode(responseCode)) { - logger.trace("HTTP response successful."); - return Optional.of(responseCode); - } else { - String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - logger.warn("HTTP response not successful : {}", aaiResponse); - return Optional.of(responseCode); - } - }; - } - - private HttpRequestBase createHttpRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { - String jsonBody = CommonFunctions.createJsonBody(consumerDmaapModel); - - if (isExtendedURINotNull(extendedURI) && jsonBody != null && !"".equals(jsonBody)) { - return createHttpPatch(extendedURI, Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel))); - } else { - return null; - } - } - - private Boolean isExtendedURINotNull(URI extendedURI) { - return extendedURI != null; - } - - - private Optional createStringEntity(Optional jsonBody) { - return Optional.of(parseJson(jsonBody).get()); - } - - private HttpPatch createHttpPatch(URI extendedURI, Optional jsonBody) { - HttpPatch httpPatch = new HttpPatch(extendedURI); - Optional stringEntity = createStringEntity(jsonBody); - httpPatch.setEntity(stringEntity.get()); - return httpPatch; - } - - private Optional parseJson(Optional jsonBody) { - Optional stringEntity = Optional.empty(); - try { - stringEntity = Optional.of(new StringEntity(jsonBody.get())); - } catch (UnsupportedEncodingException e) { - logger.warn("Exception while parsing JSON: ", e); - } - return stringEntity; - } - - private Optional createRequest(ConsumerDmaapModel consumerDmaapModel) { - final URI extendedURI = createAAIExtendedURI(consumerDmaapModel.getPnfName()); - HttpRequestBase request = createHttpRequest(extendedURI, consumerDmaapModel); - aaiHeaders.forEach(Objects.requireNonNull(request)::addHeader); - Objects.requireNonNull(request).addHeader("Content-Type", "application/merge-patch+json"); - return Optional.of(request); - } -} 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..6045c007 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClientTest.java @@ -0,0 +1,93 @@ +/*- + * ============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 new file mode 100644 index 00000000..0e713856 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIHttpClientImplTest.java @@ -0,0 +1,56 @@ +/*- + * ============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 new file mode 100644 index 00000000..dbe857e6 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.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 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.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 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(); + + + @BeforeAll + public static void init() throws 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"); + + //when + 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(aaiHeaders); + + testedObject = new AAIProducerClient(aaiHttpClientConfigurationMock); + setField(); + } + + @Test + public void getHttpResponsePatch_shouldReturnSuccessStatusCode() throws IOException { + //when + when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) + .thenReturn(Optional.of(SUCCESS)); + Optional actualResult = testedObject.getHttpResponse(consumerDmaapModel); + + //then + Assertions.assertEquals(SUCCESS, actualResult.get()); + } + + 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/config/AAIHttpClientConfigurationTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/config/AAIHttpClientConfigurationTest.java new file mode 100644 index 00000000..929d3f18 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/config/AAIHttpClientConfigurationTest.java @@ -0,0 +1,62 @@ +/*- + * ============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.config; + +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.config.ImmutableAAIClientConfiguration; + +public class AAIHttpClientConfigurationTest { + + 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"; + private static final String USER_NAME_PASSWORD = "PRH"; + private static final String BASE_PATH = "/aai/v11"; + private static final String PNF_PATH = "/network/pnfs/pnf"; + + @BeforeAll + public static void init() { + client = new ImmutableAAIClientConfiguration.Builder() + .aaiHost(AAI_HOST) + .aaiHostPortNumber(PORT) + .aaiProtocol(PROTOCOL) + .aaiUserName(USER_NAME_PASSWORD) + .aaiUserPassword(USER_NAME_PASSWORD) + .aaiIgnoreSSLCertificateErrors(true) + .aaiBasePath(BASE_PATH) + .aaiPnfPath(PNF_PATH) + .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/prh/service/utils/HttpUtilsTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/utils/HttpUtilsTest.java new file mode 100644 index 00000000..98e50cd7 --- /dev/null +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/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.prh.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 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 deleted file mode 100644 index 375d7ee4..00000000 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/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.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.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/service/AAIHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java deleted file mode 100644 index cfe1a7f6..00000000 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/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.service; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.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/service/AAIProducerClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIProducerClientTest.java deleted file mode 100644 index a83dbefe..00000000 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIProducerClientTest.java +++ /dev/null @@ -1,94 +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.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.config.AAIClientConfiguration; -import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest; - - -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 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(); - - - @BeforeAll - public static void init() throws 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"); - - //when - 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(aaiHeaders); - - testedObject = new AAIProducerClient(aaiHttpClientConfigurationMock); - setField(); - } - - @Test - public void getHttpResponsePatch_shouldReturnSuccessStatusCode() throws IOException { - //when - when(closeableHttpClientMock.execute(any(HttpPatch.class), any(ResponseHandler.class))) - .thenReturn(Optional.of(SUCCESS)); - Optional actualResult = testedObject.getHttpResponse(consumerDmaapModel); - - //then - Assertions.assertEquals(SUCCESS, actualResult.get()); - } - - 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/config/AAIHttpClientConfigurationTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java deleted file mode 100644 index 447e0ecc..00000000 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java +++ /dev/null @@ -1,62 +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.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.AAIClientConfiguration; -import org.onap.dcaegen2.services.config.ImmutableAAIClientConfiguration; - -public class AAIHttpClientConfigurationTest { - - 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"; - private static final String USER_NAME_PASSWORD = "PRH"; - private static final String BASE_PATH = "/aai/v11"; - private static final String PNF_PATH = "/network/pnfs/pnf"; - - @BeforeAll - public static void init() { - client = new ImmutableAAIClientConfiguration.Builder() - .aaiHost(AAI_HOST) - .aaiHostPortNumber(PORT) - .aaiProtocol(PROTOCOL) - .aaiUserName(USER_NAME_PASSWORD) - .aaiUserPassword(USER_NAME_PASSWORD) - .aaiIgnoreSSLCertificateErrors(true) - .aaiBasePath(BASE_PATH) - .aaiPnfPath(PNF_PATH) - .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/HttpUtilsTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java deleted file mode 100644 index 53b31da2..00000000 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/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.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