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 ------ .../services/prh/configuration/AppConfig.java | 2 +- .../services/prh/configuration/Config.java | 6 +- .../services/prh/configuration/PrhAppConfig.java | 6 +- .../services/prh/tasks/AAIConsumerTaskImpl.java | 2 +- .../services/prh/tasks/AAIProducerTaskImpl.java | 6 +- .../services/prh/tasks/DmaapConsumerTaskImpl.java | 4 +- .../services/prh/tasks/DmaapPublisherTaskImpl.java | 4 +- .../services/prh/tasks/AAIConsumerTaskSpy.java | 2 +- .../prh/tasks/AAIProducerTaskImplTest.java | 6 +- .../services/prh/tasks/AAIPublisherTaskSpy.java | 4 +- .../services/prh/tasks/DmaapConsumerTaskSpy.java | 2 +- .../services/prh/tasks/DmaapProducerTaskSpy.java | 2 +- .../config/DmaapConsumerConfiguration.java | 66 --------- .../services/config/DmaapCustomConfig.java | 70 ---------- .../config/DmaapPublisherConfiguration.java | 43 ------ .../prh/config/DmaapConsumerConfiguration.java | 66 +++++++++ .../services/prh/config/DmaapCustomConfig.java | 70 ++++++++++ .../prh/config/DmaapPublisherConfiguration.java | 43 ++++++ .../prh/response/DMaaPConsumerResponse.java | 32 +++++ .../prh/response/DMaaPConsumerResponseImpl.java | 72 ++++++++++ .../services/prh/response/DMaaPResponse.java | 32 +++++ .../services/prh/service/DmaapHttpClientImpl.java | 70 ++++++++++ .../dcaegen2/services/prh/service/HttpUtils.java | 31 +++++ .../ExtendedDmaapConsumerHttpClientImpl.java | 141 +++++++++++++++++++ .../ExtendedDmaapProducerHttpClientImpl.java | 149 ++++++++++++++++++++ .../services/response/DMaaPConsumerResponse.java | 32 ----- .../response/DMaaPConsumerResponseImpl.java | 72 ---------- .../dcaegen2/services/response/DMaaPResponse.java | 32 ----- .../services/service/DmaapHttpClientImpl.java | 70 ---------- .../onap/dcaegen2/services/service/HttpUtils.java | 31 ----- .../ExtendedDmaapConsumerHttpClientImpl.java | 141 ------------------- .../ExtendedDmaapProducerHttpClientImpl.java | 149 -------------------- .../ExtendedDmaapConsumerHttpClientImplTest.java | 96 +++++++++++++ .../ExtendedDmaapProducerHttpClientImplTest.java | 98 +++++++++++++ .../ExtendedDmaapConsumerHttpClientImplTest.java | 96 ------------- .../ExtendedDmaapProducerHttpClientImplTest.java | 98 ------------- 58 files changed, 1771 insertions(+), 1771 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 delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapConsumerConfiguration.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapCustomConfig.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapPublisherConfiguration.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapConsumerConfiguration.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapCustomConfig.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapPublisherConfiguration.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponse.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponseImpl.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPResponse.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapHttpClientImpl.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/HttpUtils.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponse.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponseImpl.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPResponse.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/DmaapHttpClientImpl.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/HttpUtils.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImpl.java create mode 100644 prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java create mode 100644 prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java delete mode 100644 prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java delete mode 100644 prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImplTest.java 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 diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java index a6014c4b..7f36a51b 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java @@ -19,7 +19,7 @@ */ package org.onap.dcaegen2.services.prh.configuration; -import org.onap.dcaegen2.services.config.*; +import org.onap.dcaegen2.services.prh.config.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/Config.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/Config.java index 605a1bd7..f3b1b557 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/Config.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/Config.java @@ -19,9 +19,9 @@ */ package org.onap.dcaegen2.services.prh.configuration; -import org.onap.dcaegen2.services.config.AAIClientConfiguration; -import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration; -import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration; +import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; +import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; /** * @author Przemysław Wąsala on 4/25/18 diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java index c4cb20de..ece1621d 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java @@ -20,9 +20,9 @@ package org.onap.dcaegen2.services.prh.configuration; import com.google.gson.*; -import org.onap.dcaegen2.services.config.AAIClientConfiguration; -import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration; -import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration; +import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; +import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.properties.ConfigurationProperties; diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImpl.java index 136f5b7f..cbe3d9aa 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImpl.java @@ -20,7 +20,7 @@ package org.onap.dcaegen2.services.prh.tasks; -import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.configuration.Config; import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java index b7f0ef34..24de3124 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java @@ -20,13 +20,13 @@ package org.onap.dcaegen2.services.prh.tasks; import java.io.IOException; -import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.configuration.Config; import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; -import org.onap.dcaegen2.services.service.AAIProducerClient; -import org.onap.dcaegen2.services.service.HttpUtils; +import org.onap.dcaegen2.services.prh.service.AAIProducerClient; +import org.onap.dcaegen2.services.prh.service.HttpUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java index 86940636..3b5a164e 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java @@ -20,14 +20,14 @@ package org.onap.dcaegen2.services.prh.tasks; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration; +import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.configuration.Config; import org.onap.dcaegen2.services.prh.exceptions.DmaapNotFoundException; import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; import org.onap.dcaegen2.services.prh.service.DmaapConsumerJsonParser; -import org.onap.dcaegen2.services.service.consumer.ExtendedDmaapConsumerHttpClientImpl; +import org.onap.dcaegen2.services.prh.service.consumer.ExtendedDmaapConsumerHttpClientImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java index 2ec38a34..77373e41 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java @@ -20,13 +20,13 @@ package org.onap.dcaegen2.services.prh.tasks; import com.google.gson.Gson; -import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration; +import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.configuration.Config; import org.onap.dcaegen2.services.prh.exceptions.DmaapNotFoundException; import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; -import org.onap.dcaegen2.services.service.producer.ExtendedDmaapProducerHttpClientImpl; +import org.onap.dcaegen2.services.prh.service.producer.ExtendedDmaapProducerHttpClientImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskSpy.java index 288a8961..e2b81373 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskSpy.java @@ -19,7 +19,7 @@ */ package org.onap.dcaegen2.services.prh.tasks; -import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.springframework.context.annotation.Bean; diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java index e5cc19da..203f349e 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java @@ -34,14 +34,14 @@ import java.util.Optional; 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; +import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.config.ImmutableAAIClientConfiguration; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; -import org.onap.dcaegen2.services.service.AAIProducerClient; +import org.onap.dcaegen2.services.prh.service.AAIProducerClient; /** * @author Przemysław Wąsala on 5/14/18 diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTaskSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTaskSpy.java index 1e843d96..762a251d 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTaskSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIPublisherTaskSpy.java @@ -19,9 +19,9 @@ */ package org.onap.dcaegen2.services.prh.tasks; -import org.onap.dcaegen2.services.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; -import org.onap.dcaegen2.services.service.AAIProducerClient; +import org.onap.dcaegen2.services.prh.service.AAIProducerClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskSpy.java index c2934d4e..1d86d24f 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskSpy.java @@ -23,7 +23,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; -import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration; +import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapProducerTaskSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapProducerTaskSpy.java index c36c84b0..10f4900b 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapProducerTaskSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapProducerTaskSpy.java @@ -23,7 +23,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; -import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration; +import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapConsumerConfiguration.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapConsumerConfiguration.java deleted file mode 100644 index 382dfcad..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapConsumerConfiguration.java +++ /dev/null @@ -1,66 +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 org.immutables.gson.Gson; -import org.immutables.value.Value; -import org.springframework.stereotype.Component; - -/** - * @author Przemysław Wąsala on 3/23/18 - */ -@Component -@Value.Immutable(prehash = true) -@Value.Style(builder = "new") -@Gson.TypeAdapters -public abstract class DmaapConsumerConfiguration implements DmaapCustomConfig { - - private static final long serialVersionUID = 1L; - - @Value.Parameter - public abstract String consumerId(); - - @Value.Parameter - public abstract String consumerGroup(); - - @Value.Parameter - public abstract Integer timeoutMS(); - - @Value.Parameter - public abstract Integer messageLimit(); - - - public interface Builder extends - DmaapCustomConfig.Builder { - - Builder consumerId(String consumerId); - - Builder consumerGroup(String consumerGroup); - - Builder timeoutMS(Integer timeoutMS); - - Builder messageLimit(Integer messageLimit); - } - - public static DmaapConsumerConfiguration.Builder builder() { - return ImmutableDmaapConsumerConfiguration.builder(); - } - -} \ No newline at end of file diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapCustomConfig.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapCustomConfig.java deleted file mode 100644 index 0c20989a..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapCustomConfig.java +++ /dev/null @@ -1,70 +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 org.immutables.value.Value; - -/** - * @author Przemysław Wąsala on 3/28/18 - */ -public interface DmaapCustomConfig extends Serializable { - - @Value.Parameter - String dmaapHostName(); - - @Value.Parameter - Integer dmaapPortNumber(); - - @Value.Parameter - String dmaapTopicName(); - - @Value.Parameter - String dmaapProtocol(); - - @Value.Parameter - String dmaapUserName(); - - @Value.Parameter - String dmaapUserPassword(); - - @Value.Parameter - String dmaapContentType(); - - - interface Builder> { - - B dmaapHostName(String dmaapHostName); - - B dmaapPortNumber(Integer dmaapPortNumber); - - B dmaapTopicName(String dmaapTopicName); - - B dmaapProtocol(String dmaapProtocol); - - B dmaapUserName(String dmaapUserName); - - B dmaapUserPassword(String dmaapUserPassword); - - B dmaapContentType(String dmaapContentType); - - T build(); - } -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapPublisherConfiguration.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapPublisherConfiguration.java deleted file mode 100644 index 6f5f17c4..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapPublisherConfiguration.java +++ /dev/null @@ -1,43 +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 org.immutables.gson.Gson; -import org.immutables.value.Value; - -/** - * @author Przemysław Wąsala on 3/23/18 - */ -@Value.Immutable(prehash = true) -@Value.Style(builder = "new") -@Gson.TypeAdapters -public abstract class DmaapPublisherConfiguration implements DmaapCustomConfig { - - private static final long serialVersionUID = 1L; - - interface Builder extends - DmaapCustomConfig.Builder { - - } - - public static DmaapPublisherConfiguration.Builder builder() { - return ImmutableDmaapPublisherConfiguration.builder(); - } -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapConsumerConfiguration.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapConsumerConfiguration.java new file mode 100644 index 00000000..89d3bd4c --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapConsumerConfiguration.java @@ -0,0 +1,66 @@ +/*- + * ============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 org.immutables.gson.Gson; +import org.immutables.value.Value; +import org.springframework.stereotype.Component; + +/** + * @author Przemysław Wąsala on 3/23/18 + */ +@Component +@Value.Immutable(prehash = true) +@Value.Style(builder = "new") +@Gson.TypeAdapters +public abstract class DmaapConsumerConfiguration implements DmaapCustomConfig { + + private static final long serialVersionUID = 1L; + + @Value.Parameter + public abstract String consumerId(); + + @Value.Parameter + public abstract String consumerGroup(); + + @Value.Parameter + public abstract Integer timeoutMS(); + + @Value.Parameter + public abstract Integer messageLimit(); + + + public interface Builder extends + DmaapCustomConfig.Builder { + + Builder consumerId(String consumerId); + + Builder consumerGroup(String consumerGroup); + + Builder timeoutMS(Integer timeoutMS); + + Builder messageLimit(Integer messageLimit); + } + + public static DmaapConsumerConfiguration.Builder builder() { + return ImmutableDmaapConsumerConfiguration.builder(); + } + +} \ No newline at end of file diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapCustomConfig.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapCustomConfig.java new file mode 100644 index 00000000..ecc9d211 --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapCustomConfig.java @@ -0,0 +1,70 @@ +/*- + * ============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 org.immutables.value.Value; + +/** + * @author Przemysław Wąsala on 3/28/18 + */ +public interface DmaapCustomConfig extends Serializable { + + @Value.Parameter + String dmaapHostName(); + + @Value.Parameter + Integer dmaapPortNumber(); + + @Value.Parameter + String dmaapTopicName(); + + @Value.Parameter + String dmaapProtocol(); + + @Value.Parameter + String dmaapUserName(); + + @Value.Parameter + String dmaapUserPassword(); + + @Value.Parameter + String dmaapContentType(); + + + interface Builder> { + + B dmaapHostName(String dmaapHostName); + + B dmaapPortNumber(Integer dmaapPortNumber); + + B dmaapTopicName(String dmaapTopicName); + + B dmaapProtocol(String dmaapProtocol); + + B dmaapUserName(String dmaapUserName); + + B dmaapUserPassword(String dmaapUserPassword); + + B dmaapContentType(String dmaapContentType); + + T build(); + } +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapPublisherConfiguration.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapPublisherConfiguration.java new file mode 100644 index 00000000..2e95463c --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/config/DmaapPublisherConfiguration.java @@ -0,0 +1,43 @@ +/* + * ============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 org.immutables.gson.Gson; +import org.immutables.value.Value; + +/** + * @author Przemysław Wąsala on 3/23/18 + */ +@Value.Immutable(prehash = true) +@Value.Style(builder = "new") +@Gson.TypeAdapters +public abstract class DmaapPublisherConfiguration implements DmaapCustomConfig { + + private static final long serialVersionUID = 1L; + + interface Builder extends + DmaapCustomConfig.Builder { + + } + + public static DmaapPublisherConfiguration.Builder builder() { + return ImmutableDmaapPublisherConfiguration.builder(); + } +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponse.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponse.java new file mode 100644 index 00000000..d2c83e73 --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponse.java @@ -0,0 +1,32 @@ +/* + * ============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.response; + +import java.util.List; + +/** + * @author Przemysław Wąsala on 4/17/18 + */ +public interface DMaaPConsumerResponse extends DMaaPResponse { + + + List getFetchedMessages(); + +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponseImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponseImpl.java new file mode 100644 index 00000000..ddac9cbf --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPConsumerResponseImpl.java @@ -0,0 +1,72 @@ +/* + * ============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.response; + +import static java.util.Collections.unmodifiableList; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.StringJoiner; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; + +/** + * @author Przemysław Wąsala on 4/17/18 + */ +public class DMaaPConsumerResponseImpl implements DMaaPConsumerResponse { + + + private final Integer responseCode; + private final String responseMessage; + private final List fetchedMessage; + + public DMaaPConsumerResponseImpl(@NonNull Integer responseCode, @NonNull String responseMessage, + @Nullable List fetchedMessage) { + this.responseCode = responseCode; + this.responseMessage = responseMessage; + this.fetchedMessage = Optional.ofNullable(fetchedMessage).orElse(Collections.emptyList()); + } + + + @Override + public String getResponseMessage() { + return responseMessage; + } + + @Override + public Integer getResponseCode() { + return responseCode; + } + + @Override + public List getFetchedMessages() { + return unmodifiableList(fetchedMessage); + } + + @Override + public String toString() { + return new StringJoiner(", ", this.getClass().getSimpleName() + "[", "]") + .add("responseCode=" + responseCode) + .add("responseMessage=" + responseMessage) + .add("fetchedMessage=" + fetchedMessage) + .toString(); + } +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPResponse.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPResponse.java new file mode 100644 index 00000000..5bf199c0 --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/response/DMaaPResponse.java @@ -0,0 +1,32 @@ +/* + * ============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.response; + +/** + * @author Przemysław Wąsala on 4/17/18 + */ +public interface DMaaPResponse { + + Integer getResponseCode(); + + + String getResponseMessage(); +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapHttpClientImpl.java new file mode 100644 index 00000000..59c4317b --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DmaapHttpClientImpl.java @@ -0,0 +1,70 @@ +/*- + * ============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.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.onap.dcaegen2.services.prh.config.DmaapCustomConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DmaapHttpClientImpl { + + private static Logger logger = LoggerFactory.getLogger(DmaapHttpClientImpl.class); + + private final String dmaapHostName; + private final Integer dmaapPortNumber; + private final String dmaapUserName; + private final String dmaapUserPassword; + + + public DmaapHttpClientImpl(DmaapCustomConfig configuration) { + this.dmaapHostName = configuration.dmaapHostName(); + this.dmaapPortNumber = configuration.dmaapPortNumber(); + this.dmaapUserName = configuration.dmaapUserName(); + this.dmaapUserPassword = configuration.dmaapUserPassword(); + } + + public CloseableHttpClient getHttpClient() { + + logger.info("Preparing closeable http client"); + + HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); + + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + + if (dmaapUserName != null) { + final AuthScope dmaapHostPortAuthScope = new AuthScope(dmaapHostName, dmaapPortNumber); + final Credentials dmaapCredentials = new UsernamePasswordCredentials(dmaapUserName, dmaapUserPassword); + credentialsProvider.setCredentials(dmaapHostPortAuthScope, dmaapCredentials); + } + + httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); + + return httpClientBuilder.build(); + } +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/HttpUtils.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/HttpUtils.java new file mode 100644 index 00000000..b96f80dd --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/HttpUtils.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.apache.http.HttpStatus; + +public final class HttpUtils implements HttpStatus { + + private HttpUtils() {} + + public static boolean isSuccessfulResponseCode(Integer statusCode) { + return statusCode >= 200 && statusCode < 300; + } +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java new file mode 100644 index 00000000..2b46f51b --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java @@ -0,0 +1,141 @@ +/*- + * ============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.consumer; + +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.DmaapConsumerConfiguration; +import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl; +import org.onap.dcaegen2.services.prh.service.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 ExtendedDmaapConsumerHttpClientImpl { + + private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapConsumerHttpClientImpl.class); + + private final CloseableHttpClient closeableHttpClient; + private final String dmaapHostName; + private final String dmaapProtocol; + private final Integer dmaapPortNumber; + private final String dmaapTopicName; + private final String consumerGroup; + private final String consumerId; + private final String dmaapContentType; + + + public ExtendedDmaapConsumerHttpClientImpl(DmaapConsumerConfiguration configuration) { + this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient(); + this.dmaapHostName = configuration.dmaapHostName(); + this.dmaapProtocol = configuration.dmaapProtocol(); + this.dmaapPortNumber = configuration.dmaapPortNumber(); + this.dmaapTopicName = configuration.dmaapTopicName(); + this.consumerGroup = configuration.consumerGroup(); + this.consumerId = configuration.consumerId(); + this.dmaapContentType = configuration.dmaapContentType(); + } + + public Optional getHttpConsumerResponse() { + + Optional extendedDetails = Optional.empty(); + Optional request = createRequest(); + + try { + extendedDetails = closeableHttpClient.execute(request.get(), dmaapConsumerResponseHandler()); + } catch (IOException | NullPointerException e) { + logger.error("Exception while executing HTTP request: {}", e); + } + + return extendedDetails; + } + + private static HttpRequestBase createHttpRequest(URI extendedURI) { + if (isExtendedURINotNull(extendedURI)) { + return new HttpGet(extendedURI); + } + + return null; + } + + private static Boolean isExtendedURINotNull(URI extendedURI) { + return extendedURI != null; + } + + private Optional createRequest() { + + Optional request = Optional.empty(); + final URI extendedURI = createDmaapConsumerExtendedURI(); + + if ("application/json".equals(dmaapContentType)) { + request = Optional.ofNullable(createHttpRequest(extendedURI)); + request.get().addHeader("Content-type", dmaapContentType); + } + + return request; + } + + private String createRequestPath() { + return dmaapTopicName + "/" + consumerGroup + "/" + consumerId; + } + + private URI createDmaapConsumerExtendedURI() { + try { + return new URIBuilder() + .setScheme(dmaapProtocol) + .setHost(dmaapHostName) + .setPort(dmaapPortNumber) + .setPath(createRequestPath()).build(); + } catch (URISyntaxException e) { + throw new RuntimeException("Exception while building extended URI: {}", e); + } + } + + private ResponseHandler> dmaapConsumerResponseHandler() { + 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 dmaapResponse = EntityUtils.toString(responseEntity); + return Optional.of(dmaapResponse); + } else { + String dmaapResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.error("HTTP response not successful : {}", dmaapResponse); + return Optional.of(String.valueOf(responseCode)); + } + }; + } +} + + diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java new file mode 100644 index 00000000..156c1891 --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java @@ -0,0 +1,149 @@ +/*- + * ============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.producer; + +import org.apache.http.HttpEntity; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpPost; +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.DmaapPublisherConfiguration; +import org.onap.dcaegen2.services.prh.model.CommonFunctions; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl; +import org.onap.dcaegen2.services.prh.service.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.Optional; + +public class ExtendedDmaapProducerHttpClientImpl { + + private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class); + + private final CloseableHttpClient closeableHttpClient; + private final String dmaapHostName; + private final String dmaapProtocol; + private final Integer dmaapPortNumber; + private final String dmaapTopicName; + private final String dmaapContentType; + + + public ExtendedDmaapProducerHttpClientImpl(DmaapPublisherConfiguration configuration) { + this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient(); + this.dmaapHostName = configuration.dmaapHostName(); + this.dmaapProtocol = configuration.dmaapProtocol(); + this.dmaapPortNumber = configuration.dmaapPortNumber(); + this.dmaapTopicName = configuration.dmaapTopicName(); + this.dmaapContentType = configuration.dmaapContentType(); + } + + public Optional getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) { + Optional extendedDetails = Optional.empty(); + Optional request = createRequest(consumerDmaapModel); + try { + extendedDetails = closeableHttpClient.execute(request.get(), dmaapProducerResponseHandler()); + } catch (IOException | NullPointerException e) { + logger.warn("Exception while executing HTTP request: ", e); + } + return extendedDetails; + } + + private Optional createStringEntity(Optional jsonBody) { + return Optional.of(parseJson(jsonBody).get()); + } + + 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) { + Optional request = Optional.empty(); + final URI extendedURI = createDmaapPublisherExtendedURI(); + + if ("application/json".equals(dmaapContentType)) { + request = Optional.ofNullable(createRequest(extendedURI, consumerDmaapModel)); + request.get().addHeader("Content-type", dmaapContentType); + } + + return request; + } + + private URI createDmaapPublisherExtendedURI() { + URI extendedURI = null; + final URIBuilder uriBuilder = new URIBuilder() + .setScheme(dmaapProtocol) + .setHost(dmaapHostName) + .setPort(dmaapPortNumber) + .setPath(dmaapTopicName); + try { + extendedURI = uriBuilder.build(); + logger.trace("Building extended URI: {}", extendedURI); + } catch (URISyntaxException e) { + logger.warn("Exception while building extended URI: ", e); + } + return extendedURI; + } + + private HttpRequestBase createRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { + if (extendedURI != null) { + return createHttpPost(extendedURI, Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel))); + } else { + return null; + } + } + + private HttpPost createHttpPost(URI extendedURI, Optional jsonBody) { + HttpPost post = new HttpPost(extendedURI); + Optional stringEntity = createStringEntity(jsonBody); + post.setEntity(stringEntity.get()); + return post; + } + + private ResponseHandler> dmaapProducerResponseHandler() { + return httpResponse -> { + final int responseCode = httpResponse.getStatusLine().getStatusCode(); + final HttpEntity responseEntity = httpResponse.getEntity(); + + if (HttpUtils.isSuccessfulResponseCode(responseCode)) { + logger.trace("HTTP response successful."); + return Optional.of("" + responseCode); + } else { + String response = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + logger.warn("HTTP response not successful : {}", response); + return Optional.of("" + responseCode); + } + }; + } +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponse.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponse.java deleted file mode 100644 index a9e5c526..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponse.java +++ /dev/null @@ -1,32 +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.response; - -import java.util.List; - -/** - * @author Przemysław Wąsala on 4/17/18 - */ -public interface DMaaPConsumerResponse extends DMaaPResponse { - - - List getFetchedMessages(); - -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponseImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponseImpl.java deleted file mode 100644 index f3565edf..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPConsumerResponseImpl.java +++ /dev/null @@ -1,72 +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.response; - -import static java.util.Collections.unmodifiableList; - -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.StringJoiner; -import org.springframework.lang.NonNull; -import org.springframework.lang.Nullable; - -/** - * @author Przemysław Wąsala on 4/17/18 - */ -public class DMaaPConsumerResponseImpl implements DMaaPConsumerResponse { - - - private final Integer responseCode; - private final String responseMessage; - private final List fetchedMessage; - - public DMaaPConsumerResponseImpl(@NonNull Integer responseCode, @NonNull String responseMessage, - @Nullable List fetchedMessage) { - this.responseCode = responseCode; - this.responseMessage = responseMessage; - this.fetchedMessage = Optional.ofNullable(fetchedMessage).orElse(Collections.emptyList()); - } - - - @Override - public String getResponseMessage() { - return responseMessage; - } - - @Override - public Integer getResponseCode() { - return responseCode; - } - - @Override - public List getFetchedMessages() { - return unmodifiableList(fetchedMessage); - } - - @Override - public String toString() { - return new StringJoiner(", ", this.getClass().getSimpleName() + "[", "]") - .add("responseCode=" + responseCode) - .add("responseMessage=" + responseMessage) - .add("fetchedMessage=" + fetchedMessage) - .toString(); - } -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPResponse.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPResponse.java deleted file mode 100644 index 6528eec8..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/response/DMaaPResponse.java +++ /dev/null @@ -1,32 +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.response; - -/** - * @author Przemysław Wąsala on 4/17/18 - */ -public interface DMaaPResponse { - - Integer getResponseCode(); - - - String getResponseMessage(); -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/DmaapHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/DmaapHttpClientImpl.java deleted file mode 100644 index f2917d02..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/DmaapHttpClientImpl.java +++ /dev/null @@ -1,70 +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.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.onap.dcaegen2.services.config.DmaapCustomConfig; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DmaapHttpClientImpl { - - private static Logger logger = LoggerFactory.getLogger(DmaapHttpClientImpl.class); - - private final String dmaapHostName; - private final Integer dmaapPortNumber; - private final String dmaapUserName; - private final String dmaapUserPassword; - - - public DmaapHttpClientImpl(DmaapCustomConfig configuration) { - this.dmaapHostName = configuration.dmaapHostName(); - this.dmaapPortNumber = configuration.dmaapPortNumber(); - this.dmaapUserName = configuration.dmaapUserName(); - this.dmaapUserPassword = configuration.dmaapUserPassword(); - } - - public CloseableHttpClient getHttpClient() { - - logger.info("Preparing closeable http client"); - - HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); - - final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - - if (dmaapUserName != null) { - final AuthScope dmaapHostPortAuthScope = new AuthScope(dmaapHostName, dmaapPortNumber); - final Credentials dmaapCredentials = new UsernamePasswordCredentials(dmaapUserName, dmaapUserPassword); - credentialsProvider.setCredentials(dmaapHostPortAuthScope, dmaapCredentials); - } - - httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); - - return httpClientBuilder.build(); - } -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/HttpUtils.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/HttpUtils.java deleted file mode 100644 index fb615bbf..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/HttpUtils.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.apache.http.HttpStatus; - -public final class HttpUtils implements HttpStatus { - - private HttpUtils() {} - - public static boolean isSuccessfulResponseCode(Integer statusCode) { - return statusCode >= 200 && statusCode < 300; - } -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java deleted file mode 100644 index 1772104a..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImpl.java +++ /dev/null @@ -1,141 +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.consumer; - -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.DmaapConsumerConfiguration; -import org.onap.dcaegen2.services.service.DmaapHttpClientImpl; -import org.onap.dcaegen2.services.service.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 ExtendedDmaapConsumerHttpClientImpl { - - private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapConsumerHttpClientImpl.class); - - private final CloseableHttpClient closeableHttpClient; - private final String dmaapHostName; - private final String dmaapProtocol; - private final Integer dmaapPortNumber; - private final String dmaapTopicName; - private final String consumerGroup; - private final String consumerId; - private final String dmaapContentType; - - - public ExtendedDmaapConsumerHttpClientImpl(DmaapConsumerConfiguration configuration) { - this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient(); - this.dmaapHostName = configuration.dmaapHostName(); - this.dmaapProtocol = configuration.dmaapProtocol(); - this.dmaapPortNumber = configuration.dmaapPortNumber(); - this.dmaapTopicName = configuration.dmaapTopicName(); - this.consumerGroup = configuration.consumerGroup(); - this.consumerId = configuration.consumerId(); - this.dmaapContentType = configuration.dmaapContentType(); - } - - public Optional getHttpConsumerResponse() { - - Optional extendedDetails = Optional.empty(); - Optional request = createRequest(); - - try { - extendedDetails = closeableHttpClient.execute(request.get(), dmaapConsumerResponseHandler()); - } catch (IOException | NullPointerException e) { - logger.error("Exception while executing HTTP request: {}", e); - } - - return extendedDetails; - } - - private static HttpRequestBase createHttpRequest(URI extendedURI) { - if (isExtendedURINotNull(extendedURI)) { - return new HttpGet(extendedURI); - } - - return null; - } - - private static Boolean isExtendedURINotNull(URI extendedURI) { - return extendedURI != null; - } - - private Optional createRequest() { - - Optional request = Optional.empty(); - final URI extendedURI = createDmaapConsumerExtendedURI(); - - if ("application/json".equals(dmaapContentType)) { - request = Optional.ofNullable(createHttpRequest(extendedURI)); - request.get().addHeader("Content-type", dmaapContentType); - } - - return request; - } - - private String createRequestPath() { - return dmaapTopicName + "/" + consumerGroup + "/" + consumerId; - } - - private URI createDmaapConsumerExtendedURI() { - try { - return new URIBuilder() - .setScheme(dmaapProtocol) - .setHost(dmaapHostName) - .setPort(dmaapPortNumber) - .setPath(createRequestPath()).build(); - } catch (URISyntaxException e) { - throw new RuntimeException("Exception while building extended URI: {}", e); - } - } - - private ResponseHandler> dmaapConsumerResponseHandler() { - 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 dmaapResponse = EntityUtils.toString(responseEntity); - return Optional.of(dmaapResponse); - } else { - String dmaapResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - logger.error("HTTP response not successful : {}", dmaapResponse); - return Optional.of(String.valueOf(responseCode)); - } - }; - } -} - - diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImpl.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImpl.java deleted file mode 100644 index 11d5a487..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImpl.java +++ /dev/null @@ -1,149 +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.producer; - -import org.apache.http.HttpEntity; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpPost; -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.DmaapPublisherConfiguration; -import org.onap.dcaegen2.services.prh.model.CommonFunctions; -import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import org.onap.dcaegen2.services.service.DmaapHttpClientImpl; -import org.onap.dcaegen2.services.service.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.Optional; - -public class ExtendedDmaapProducerHttpClientImpl { - - private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class); - - private final CloseableHttpClient closeableHttpClient; - private final String dmaapHostName; - private final String dmaapProtocol; - private final Integer dmaapPortNumber; - private final String dmaapTopicName; - private final String dmaapContentType; - - - public ExtendedDmaapProducerHttpClientImpl(DmaapPublisherConfiguration configuration) { - this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient(); - this.dmaapHostName = configuration.dmaapHostName(); - this.dmaapProtocol = configuration.dmaapProtocol(); - this.dmaapPortNumber = configuration.dmaapPortNumber(); - this.dmaapTopicName = configuration.dmaapTopicName(); - this.dmaapContentType = configuration.dmaapContentType(); - } - - public Optional getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) { - Optional extendedDetails = Optional.empty(); - Optional request = createRequest(consumerDmaapModel); - try { - extendedDetails = closeableHttpClient.execute(request.get(), dmaapProducerResponseHandler()); - } catch (IOException | NullPointerException e) { - logger.warn("Exception while executing HTTP request: ", e); - } - return extendedDetails; - } - - private Optional createStringEntity(Optional jsonBody) { - return Optional.of(parseJson(jsonBody).get()); - } - - 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) { - Optional request = Optional.empty(); - final URI extendedURI = createDmaapPublisherExtendedURI(); - - if ("application/json".equals(dmaapContentType)) { - request = Optional.ofNullable(createRequest(extendedURI, consumerDmaapModel)); - request.get().addHeader("Content-type", dmaapContentType); - } - - return request; - } - - private URI createDmaapPublisherExtendedURI() { - URI extendedURI = null; - final URIBuilder uriBuilder = new URIBuilder() - .setScheme(dmaapProtocol) - .setHost(dmaapHostName) - .setPort(dmaapPortNumber) - .setPath(dmaapTopicName); - try { - extendedURI = uriBuilder.build(); - logger.trace("Building extended URI: {}", extendedURI); - } catch (URISyntaxException e) { - logger.warn("Exception while building extended URI: ", e); - } - return extendedURI; - } - - private HttpRequestBase createRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { - if (extendedURI != null) { - return createHttpPost(extendedURI, Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel))); - } else { - return null; - } - } - - private HttpPost createHttpPost(URI extendedURI, Optional jsonBody) { - HttpPost post = new HttpPost(extendedURI); - Optional stringEntity = createStringEntity(jsonBody); - post.setEntity(stringEntity.get()); - return post; - } - - private ResponseHandler> dmaapProducerResponseHandler() { - return httpResponse -> { - final int responseCode = httpResponse.getStatusLine().getStatusCode(); - final HttpEntity responseEntity = httpResponse.getEntity(); - - if (HttpUtils.isSuccessfulResponseCode(responseCode)) { - logger.trace("HTTP response successful."); - return Optional.of("" + responseCode); - } else { - String response = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; - logger.warn("HTTP response not successful : {}", response); - return Optional.of("" + responseCode); - } - }; - } -} diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java new file mode 100644 index 00000000..39c10de5 --- /dev/null +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java @@ -0,0 +1,96 @@ +/*- + * ============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.consumer; + +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.DmaapConsumerConfiguration; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +public class ExtendedDmaapConsumerHttpClientImplTest { + + private static ExtendedDmaapConsumerHttpClientImpl objectUnderTest; + + private static DmaapConsumerConfiguration configurationMock = mock(DmaapConsumerConfiguration.class); + private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); + + private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\" }"; + + private static Optional expectedResult = Optional.empty(); + + @BeforeAll + public static void init() throws NoSuchFieldException, IllegalAccessException { + + when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2"); + when(configurationMock.dmaapProtocol()).thenReturn("https"); + when(configurationMock.dmaapPortNumber()).thenReturn(1234); + when(configurationMock.dmaapUserName()).thenReturn("PRH"); + when(configurationMock.dmaapUserPassword()).thenReturn("PRH"); + when(configurationMock.dmaapContentType()).thenReturn("application/json"); + when(configurationMock.dmaapTopicName()).thenReturn("unauthenticated.SEC_OTHER_OUTPUT"); + when(configurationMock.consumerGroup()).thenReturn("OpenDCAE-c12"); + when(configurationMock.consumerId()).thenReturn("c12"); + + objectUnderTest = new ExtendedDmaapConsumerHttpClientImpl(configurationMock); + + setField(); + } + + + @Test + public void getHttpResponseGet_success() throws IOException { + expectedResult = Optional.of(JSON_MESSAGE); + + when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))) + .thenReturn(expectedResult); + + Optional actualResult = objectUnderTest.getHttpConsumerResponse(); + + Assertions.assertEquals(expectedResult.get(), actualResult.get()); + } + + @Test + public void getExtendedDetails_returnsNull() throws IOException { + when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). + thenReturn(Optional.empty()); + Optional actualResult = objectUnderTest.getHttpConsumerResponse(); + Assertions.assertEquals(Optional.empty(),actualResult); + } + + + private static void setField() throws NoSuchFieldException, IllegalAccessException { + Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient"); + field.setAccessible(true); + field.set(objectUnderTest, closeableHttpClientMock); + } +} diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java new file mode 100644 index 00000000..3cb84206 --- /dev/null +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java @@ -0,0 +1,98 @@ +/*- + * ============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.producer; + +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpPost; +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.DmaapPublisherConfiguration; +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.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +public class ExtendedDmaapProducerHttpClientImplTest { + + private static ExtendedDmaapProducerHttpClientImpl objectUnderTest; + + private static DmaapPublisherConfiguration configurationMock = mock(DmaapPublisherConfiguration.class); + private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); + private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest(); + + private static Optional expectedResult = Optional.empty(); + private static final String RESPONSE_SUCCESS = "200"; + private static final String RESPONSE_FAILURE = "404"; + + @BeforeAll + public static void init() throws NoSuchFieldException, IllegalAccessException { + + when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2"); + when(configurationMock.dmaapProtocol()).thenReturn("https"); + when(configurationMock.dmaapPortNumber()).thenReturn(1234); + when(configurationMock.dmaapUserName()).thenReturn("PRH"); + when(configurationMock.dmaapUserPassword()).thenReturn("PRH"); + when(configurationMock.dmaapContentType()).thenReturn("application/json"); + when(configurationMock.dmaapTopicName()).thenReturn("pnfReady"); + + objectUnderTest = new ExtendedDmaapProducerHttpClientImpl(configurationMock); + + setField(); + } + + + @Test + public void getHttpResponsePost_success() throws IOException { + expectedResult = Optional.of(RESPONSE_SUCCESS); + + when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class))) + .thenReturn(expectedResult); + + Optional actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel); + + Assertions.assertEquals(expectedResult.get(), actualResult.get()); + } + + @Test + public void getExtendedDetails_returnsFailure() throws IOException { + expectedResult = Optional.of(RESPONSE_FAILURE); + when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class))) + .thenReturn(Optional.empty()); + Optional actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel); + Assertions.assertEquals(Optional.empty(), actualResult); + } + + + private static void setField() throws NoSuchFieldException, IllegalAccessException { + Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient"); + field.setAccessible(true); + field.set(objectUnderTest, closeableHttpClientMock); + } +} diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java deleted file mode 100644 index 1df025ca..00000000 --- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/consumer/ExtendedDmaapConsumerHttpClientImplTest.java +++ /dev/null @@ -1,96 +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.consumer; - -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.DmaapConsumerConfiguration; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.Optional; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - - -public class ExtendedDmaapConsumerHttpClientImplTest { - - private static ExtendedDmaapConsumerHttpClientImpl objectUnderTest; - - private static DmaapConsumerConfiguration configurationMock = mock(DmaapConsumerConfiguration.class); - private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); - - private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\" }"; - - private static Optional expectedResult = Optional.empty(); - - @BeforeAll - public static void init() throws NoSuchFieldException, IllegalAccessException { - - when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2"); - when(configurationMock.dmaapProtocol()).thenReturn("https"); - when(configurationMock.dmaapPortNumber()).thenReturn(1234); - when(configurationMock.dmaapUserName()).thenReturn("PRH"); - when(configurationMock.dmaapUserPassword()).thenReturn("PRH"); - when(configurationMock.dmaapContentType()).thenReturn("application/json"); - when(configurationMock.dmaapTopicName()).thenReturn("unauthenticated.SEC_OTHER_OUTPUT"); - when(configurationMock.consumerGroup()).thenReturn("OpenDCAE-c12"); - when(configurationMock.consumerId()).thenReturn("c12"); - - objectUnderTest = new ExtendedDmaapConsumerHttpClientImpl(configurationMock); - - setField(); - } - - - @Test - public void getHttpResponseGet_success() throws IOException { - expectedResult = Optional.of(JSON_MESSAGE); - - when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))) - .thenReturn(expectedResult); - - Optional actualResult = objectUnderTest.getHttpConsumerResponse(); - - Assertions.assertEquals(expectedResult.get(), actualResult.get()); - } - - @Test - public void getExtendedDetails_returnsNull() throws IOException { - when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))). - thenReturn(Optional.empty()); - Optional actualResult = objectUnderTest.getHttpConsumerResponse(); - Assertions.assertEquals(Optional.empty(),actualResult); - } - - - private static void setField() throws NoSuchFieldException, IllegalAccessException { - Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient"); - field.setAccessible(true); - field.set(objectUnderTest, closeableHttpClientMock); - } -} diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImplTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImplTest.java deleted file mode 100644 index 6e7d81f6..00000000 --- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/service/producer/ExtendedDmaapProducerHttpClientImplTest.java +++ /dev/null @@ -1,98 +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.producer; - -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpPost; -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.DmaapPublisherConfiguration; -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.Optional; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - - -public class ExtendedDmaapProducerHttpClientImplTest { - - private static ExtendedDmaapProducerHttpClientImpl objectUnderTest; - - private static DmaapPublisherConfiguration configurationMock = mock(DmaapPublisherConfiguration.class); - private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class); - private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest(); - - private static Optional expectedResult = Optional.empty(); - private static final String RESPONSE_SUCCESS = "200"; - private static final String RESPONSE_FAILURE = "404"; - - @BeforeAll - public static void init() throws NoSuchFieldException, IllegalAccessException { - - when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2"); - when(configurationMock.dmaapProtocol()).thenReturn("https"); - when(configurationMock.dmaapPortNumber()).thenReturn(1234); - when(configurationMock.dmaapUserName()).thenReturn("PRH"); - when(configurationMock.dmaapUserPassword()).thenReturn("PRH"); - when(configurationMock.dmaapContentType()).thenReturn("application/json"); - when(configurationMock.dmaapTopicName()).thenReturn("pnfReady"); - - objectUnderTest = new ExtendedDmaapProducerHttpClientImpl(configurationMock); - - setField(); - } - - - @Test - public void getHttpResponsePost_success() throws IOException { - expectedResult = Optional.of(RESPONSE_SUCCESS); - - when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class))) - .thenReturn(expectedResult); - - Optional actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel); - - Assertions.assertEquals(expectedResult.get(), actualResult.get()); - } - - @Test - public void getExtendedDetails_returnsFailure() throws IOException { - expectedResult = Optional.of(RESPONSE_FAILURE); - when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class))) - .thenReturn(Optional.empty()); - Optional actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel); - Assertions.assertEquals(Optional.empty(), actualResult); - } - - - private static void setField() throws NoSuchFieldException, IllegalAccessException { - Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient"); - field.setAccessible(true); - field.set(objectUnderTest, closeableHttpClientMock); - } -} -- cgit 1.2.3-korg