From e8a80102a45458b3f1d15e07dc0a63e1370c44a7 Mon Sep 17 00:00:00 2001 From: pkaras Date: Mon, 5 Nov 2018 16:04:54 +0100 Subject: DmaaP Publisher and consumer interfaces ssl setup Change-Id: I971d0fb222c6e8e15de4fc4a4d9eeb4ef3a99f44 Issue-ID: DCAEGEN2-944 Signed-off-by: piotr.karas --- .../services/prh/tasks/DmaapConsumerTask.java | 3 - .../services/prh/tasks/DmaapConsumerTaskImpl.java | 26 ++++---- .../services/prh/tasks/DmaapPublisherTask.java | 2 - .../services/prh/tasks/DmaapPublisherTaskImpl.java | 19 +++--- .../prh/tasks/DmaapConsumerTaskImplTest.java | 9 ++- .../prh/tasks/DmaapPublisherTaskImplTest.java | 6 +- .../prh/service/DMaaPReactiveWebClient.java | 72 ---------------------- .../ConsumerReactiveHttpClientFactory.java | 37 +++++++++++ .../consumer/DMaaPConsumerReactiveHttpClient.java | 9 +-- .../service/consumer/DMaaPReactiveWebClient.java | 72 ++++++++++++++++++++++ .../producer/DMaaPPublisherReactiveHttpClient.java | 12 ++-- .../PublisherReactiveHttpClientFactory.java | 32 ++++++++++ .../prh/service/DMaaPReactiveWebClientTest.java | 56 ----------------- .../ConsumerReactiveHttpClientFactoryTest.java | 42 +++++++++++++ .../DMaaPConsumerReactiveHttpClientTest.java | 3 +- .../consumer/DMaaPReactiveWebClientTest.java | 56 +++++++++++++++++ .../DMaaPPublisherReactiveHttpClientTest.java | 17 +++-- .../PublisherReactiveHttpClientFactoryTest.java | 39 ++++++++++++ 18 files changed, 321 insertions(+), 191 deletions(-) delete mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClient.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ConsumerReactiveHttpClientFactory.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPReactiveWebClient.java create mode 100644 prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/PublisherReactiveHttpClientFactory.java delete mode 100644 prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClientTest.java create mode 100644 prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/ConsumerReactiveHttpClientFactoryTest.java create mode 100644 prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPReactiveWebClientTest.java create mode 100644 prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/PublisherReactiveHttpClientFactoryTest.java diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTask.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTask.java index 2f947d47..58b29d2e 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTask.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTask.java @@ -23,7 +23,6 @@ package org.onap.dcaegen2.services.prh.tasks; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.service.consumer.DMaaPConsumerReactiveHttpClient; import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; /** * @author Przemysław Wąsala on 4/13/18 @@ -34,7 +33,5 @@ interface DmaapConsumerTask { Flux execute(String object); - Flux consume(Mono message); - DMaaPConsumerReactiveHttpClient resolveClient(); } 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 c4d9c44a..a52163b7 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 @@ -22,15 +22,15 @@ package org.onap.dcaegen2.services.prh.tasks; import org.onap.dcaegen2.services.prh.configuration.Config; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import org.onap.dcaegen2.services.prh.service.DMaaPReactiveWebClient; import org.onap.dcaegen2.services.prh.service.DmaapConsumerJsonParser; +import org.onap.dcaegen2.services.prh.service.consumer.ConsumerReactiveHttpClientFactory; import org.onap.dcaegen2.services.prh.service.consumer.DMaaPConsumerReactiveHttpClient; +import org.onap.dcaegen2.services.prh.service.consumer.DMaaPReactiveWebClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; /** * @author Przemysław Wąsala on 3/23/18 @@ -41,18 +41,20 @@ public class DmaapConsumerTaskImpl implements DmaapConsumerTask { private static final Logger LOGGER = LoggerFactory.getLogger(DmaapConsumerTaskImpl.class); private final Config config; private final DmaapConsumerJsonParser dmaapConsumerJsonParser; - private final DMaaPReactiveWebClient dmaapReactiveWebClient; + private final ConsumerReactiveHttpClientFactory httpClientFactory; @Autowired public DmaapConsumerTaskImpl(Config config) { - this(config, new DmaapConsumerJsonParser(), new DMaaPReactiveWebClient()); + this(config, new DmaapConsumerJsonParser(), + new ConsumerReactiveHttpClientFactory(new DMaaPReactiveWebClient())); } - DmaapConsumerTaskImpl(Config prhAppConfig, DmaapConsumerJsonParser dmaapConsumerJsonParser, - DMaaPReactiveWebClient dmaapReactiveWebClient) { + DmaapConsumerTaskImpl(Config prhAppConfig, + DmaapConsumerJsonParser dmaapConsumerJsonParser, + ConsumerReactiveHttpClientFactory httpClientFactory) { this.config = prhAppConfig; this.dmaapConsumerJsonParser = dmaapConsumerJsonParser; - this.dmaapReactiveWebClient = dmaapReactiveWebClient; + this.httpClientFactory = httpClientFactory; } @Override @@ -64,17 +66,11 @@ public class DmaapConsumerTaskImpl implements DmaapConsumerTask { public Flux execute(String object) { DMaaPConsumerReactiveHttpClient dmaaPConsumerReactiveHttpClient = resolveClient(); LOGGER.debug("Method called with arg {}", object); - return consume(dmaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse()); - } - - @Override - public Flux consume(Mono message) { - return dmaapConsumerJsonParser.getJsonObject(message); + return dmaapConsumerJsonParser.getJsonObject(dmaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse()); } @Override public DMaaPConsumerReactiveHttpClient resolveClient() { - return new DMaaPConsumerReactiveHttpClient( - config.getDmaapConsumerConfiguration()).createDMaaPWebClient(dmaapReactiveWebClient.build()); + return httpClientFactory.create(config.getDmaapConsumerConfiguration()); } } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTask.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTask.java index dc0a4488..3f59815b 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTask.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTask.java @@ -33,7 +33,5 @@ interface DmaapPublisherTask { Mono> execute(ConsumerDmaapModel consumerDmaapModel) throws PrhTaskException; - Mono> publish(ConsumerDmaapModel consumerDmaapModel) throws PrhTaskException; - DMaaPPublisherReactiveHttpClient resolveClient(); } 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 fdc5e625..76c1bb56 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 @@ -24,12 +24,12 @@ import org.onap.dcaegen2.services.prh.configuration.Config; import org.onap.dcaegen2.services.prh.exceptions.DmaapNotFoundException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.service.producer.DMaaPPublisherReactiveHttpClient; +import org.onap.dcaegen2.services.prh.service.producer.PublisherReactiveHttpClientFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; import reactor.core.publisher.Mono; /** @@ -40,11 +40,16 @@ public class DmaapPublisherTaskImpl implements DmaapPublisherTask { private static final Logger LOGGER = LoggerFactory.getLogger(DmaapPublisherTaskImpl.class); private final Config config; - private DMaaPPublisherReactiveHttpClient dmaapPublisherReactiveHttpClient; + private final PublisherReactiveHttpClientFactory httpClientFactory; @Autowired public DmaapPublisherTaskImpl(Config config) { + this(config, new PublisherReactiveHttpClientFactory()); + } + + DmaapPublisherTaskImpl(Config config, PublisherReactiveHttpClientFactory httpClientFactory) { this.config = config; + this.httpClientFactory = httpClientFactory; } @Override @@ -52,19 +57,13 @@ public class DmaapPublisherTaskImpl implements DmaapPublisherTask { if (consumerDmaapModel == null) { throw new DmaapNotFoundException("Invoked null object to DMaaP task"); } - dmaapPublisherReactiveHttpClient = resolveClient(); + DMaaPPublisherReactiveHttpClient dmaapPublisherReactiveHttpClient = resolveClient(); LOGGER.info("Method called with arg {}", consumerDmaapModel); - return publish(consumerDmaapModel); - } - - @Override - public Mono> publish(ConsumerDmaapModel consumerDmaapModel) { return dmaapPublisherReactiveHttpClient.getDMaaPProducerResponse(consumerDmaapModel); } @Override public DMaaPPublisherReactiveHttpClient resolveClient() { - return new DMaaPPublisherReactiveHttpClient(config.getDmaapPublisherConfiguration()) - .createDMaaPWebClient(new RestTemplate()); + return httpClientFactory.create(config.getDmaapPublisherConfiguration()); } } \ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImplTest.java index fe9f6f78..1a858917 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImplTest.java @@ -36,8 +36,8 @@ import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel; -import org.onap.dcaegen2.services.prh.service.DMaaPReactiveWebClient; import org.onap.dcaegen2.services.prh.service.DmaapConsumerJsonParser; +import org.onap.dcaegen2.services.prh.service.consumer.ConsumerReactiveHttpClientFactory; import org.onap.dcaegen2.services.prh.service.consumer.DMaaPConsumerReactiveHttpClient; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -120,9 +120,8 @@ class DmaapConsumerTaskImplTest { dMaaPConsumerReactiveHttpClient = mock(DMaaPConsumerReactiveHttpClient.class); when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse()).thenReturn(Mono.just(message.orElse(""))); when(appConfig.getDmaapConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration); - DMaaPReactiveWebClient dmaapReactiveWebClient = mock(DMaaPReactiveWebClient.class); - dmaapConsumerTask = - spy(new DmaapConsumerTaskImpl(appConfig, new DmaapConsumerJsonParser(), dmaapReactiveWebClient)); - doReturn(dMaaPConsumerReactiveHttpClient).when(dmaapConsumerTask).resolveClient(); + ConsumerReactiveHttpClientFactory httpClientFactory = mock(ConsumerReactiveHttpClientFactory.class); + doReturn(dMaaPConsumerReactiveHttpClient).when(httpClientFactory).create(dmaapConsumerConfiguration); + dmaapConsumerTask = new DmaapConsumerTaskImpl(appConfig, new DmaapConsumerJsonParser(), httpClientFactory); } } \ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java index e9eed698..5bdcba52 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java @@ -41,6 +41,7 @@ import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel; import org.onap.dcaegen2.services.prh.service.producer.DMaaPPublisherReactiveHttpClient; +import org.onap.dcaegen2.services.prh.service.producer.PublisherReactiveHttpClientFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import reactor.core.publisher.Mono; @@ -119,8 +120,9 @@ class DmaapPublisherTaskImplTest { dMaaPPublisherReactiveHttpClient = mock(DMaaPPublisherReactiveHttpClient.class); when(dMaaPPublisherReactiveHttpClient.getDMaaPProducerResponse(any())) .thenReturn(Mono.just(responseEntity)); - dmaapPublisherTask = spy(new DmaapPublisherTaskImpl(appConfig)); - doReturn(dMaaPPublisherReactiveHttpClient).when(dmaapPublisherTask).resolveClient(); + PublisherReactiveHttpClientFactory httpClientFactory = mock(PublisherReactiveHttpClientFactory.class); + doReturn(dMaaPPublisherReactiveHttpClient).when(httpClientFactory).create(dmaapPublisherConfiguration); + dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig, httpClientFactory); return responseEntity; } } \ No newline at end of file diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClient.java deleted file mode 100644 index 242a4251..00000000 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClient.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.prh.service; - -import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.RESPONSE_CODE; -import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.SERVICE_NAME; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.springframework.web.reactive.function.client.ExchangeFilterFunction; -import org.springframework.web.reactive.function.client.WebClient; -import reactor.core.publisher.Mono; - -/** - * @author Przemysław Wąsala on 7/4/18 - */ -public class DMaaPReactiveWebClient { - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - /** - * Construct Reactive WebClient with appropriate settings. - * - * @return WebClient - */ - public WebClient build() { - return WebClient.builder() - .filter(logRequest()) - .filter(logResponse()) - .build(); - } - - private ExchangeFilterFunction logResponse() { - return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { - MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode())); - logger.info("Response Status {}", clientResponse.statusCode()); - MDC.remove(RESPONSE_CODE); - return Mono.just(clientResponse); - }); - } - - private ExchangeFilterFunction logRequest() { - return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { - MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url())); - logger.info("Request: {} {}", clientRequest.method(), clientRequest.url()); - clientRequest.headers() - .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value))); - MDC.remove(SERVICE_NAME); - return Mono.just(clientRequest); - }); - } - -} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ConsumerReactiveHttpClientFactory.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ConsumerReactiveHttpClientFactory.java new file mode 100644 index 00000000..951e0b0b --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/ConsumerReactiveHttpClientFactory.java @@ -0,0 +1,37 @@ +/* + * ============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.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; + +public class ConsumerReactiveHttpClientFactory { + + private final DMaaPReactiveWebClient reactiveWebClient; + + public ConsumerReactiveHttpClientFactory(DMaaPReactiveWebClient reactiveWebClient) { + this.reactiveWebClient = reactiveWebClient; + } + + public DMaaPConsumerReactiveHttpClient create(DmaapConsumerConfiguration consumerConfiguration) { + return new DMaaPConsumerReactiveHttpClient(consumerConfiguration, reactiveWebClient.build()); + } + +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java index 02c6ba96..bed3b0a8 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClient.java @@ -47,14 +47,14 @@ public class DMaaPConsumerReactiveHttpClient { private final String consumerGroup; private final String consumerId; private final String contentType; - private WebClient webClient; + private final WebClient webClient; /** * Constructor of DMaaPConsumerReactiveHttpClient. * * @param consumerConfiguration - DMaaP consumer configuration object */ - public DMaaPConsumerReactiveHttpClient(DmaapConsumerConfiguration consumerConfiguration) { + DMaaPConsumerReactiveHttpClient(DmaapConsumerConfiguration consumerConfiguration, WebClient webClient) { this.dmaapHostName = consumerConfiguration.dmaapHostName(); this.dmaapProtocol = consumerConfiguration.dmaapProtocol(); this.dmaapPortNumber = consumerConfiguration.dmaapPortNumber(); @@ -62,6 +62,7 @@ public class DMaaPConsumerReactiveHttpClient { this.consumerGroup = consumerConfiguration.consumerGroup(); this.consumerId = consumerConfiguration.consumerId(); this.contentType = consumerConfiguration.dmaapContentType(); + this.webClient = webClient; } /** @@ -95,10 +96,6 @@ public class DMaaPConsumerReactiveHttpClient { return dmaapTopicName + "/" + consumerGroup + "/" + consumerId; } - public DMaaPConsumerReactiveHttpClient createDMaaPWebClient(WebClient webClient) { - this.webClient = webClient; - return this; - } URI getUri() { return new DefaultUriBuilderFactory().builder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber) diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPReactiveWebClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPReactiveWebClient.java new file mode 100644 index 00000000..9eb6ee62 --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPReactiveWebClient.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.service.consumer; + +import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.RESPONSE_CODE; +import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.SERVICE_NAME; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.springframework.web.reactive.function.client.ExchangeFilterFunction; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; + +/** + * @author Przemysław Wąsala on 7/4/18 + */ +public class DMaaPReactiveWebClient { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + /** + * Construct Reactive WebClient with appropriate settings. + * + * @return WebClient + */ + public WebClient build() { + return WebClient.builder() + .filter(logRequest()) + .filter(logResponse()) + .build(); + } + + private ExchangeFilterFunction logResponse() { + return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { + MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode())); + logger.info("Response Status {}", clientResponse.statusCode()); + MDC.remove(RESPONSE_CODE); + return Mono.just(clientResponse); + }); + } + + private ExchangeFilterFunction logRequest() { + return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { + MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url())); + logger.info("Request: {} {}", clientRequest.method(), clientRequest.url()); + clientRequest.headers() + .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value))); + MDC.remove(SERVICE_NAME); + return Mono.just(clientRequest); + }); + } + +} diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPPublisherReactiveHttpClient.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPPublisherReactiveHttpClient.java index 6919487a..d4687c51 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPPublisherReactiveHttpClient.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPPublisherReactiveHttpClient.java @@ -50,20 +50,21 @@ public class DMaaPPublisherReactiveHttpClient { private final String dmaapProtocol; private final String dmaapTopicName; private final String dmaapContentType; - - private RestTemplate restTemplate; + private final RestTemplate restTemplate; /** * Constructor DMaaPPublisherReactiveHttpClient. * * @param dmaapPublisherConfiguration - DMaaP producer configuration object */ - public DMaaPPublisherReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) { + DMaaPPublisherReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration, + RestTemplate restTemplate) { this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName(); this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol(); this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber(); this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName(); this.dmaapContentType = dmaapPublisherConfiguration.dmaapContentType(); + this.restTemplate = restTemplate; } /** @@ -90,11 +91,6 @@ public class DMaaPPublisherReactiveHttpClient { } - public DMaaPPublisherReactiveHttpClient createDMaaPWebClient(RestTemplate restTemplate) { - this.restTemplate = restTemplate; - return this; - } - URI getUri() { return new DefaultUriBuilderFactory().builder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber) .path(dmaapTopicName).build(); diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/PublisherReactiveHttpClientFactory.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/PublisherReactiveHttpClientFactory.java new file mode 100644 index 00000000..0fc8f16a --- /dev/null +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/PublisherReactiveHttpClientFactory.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.service.producer; + +import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; +import org.springframework.web.client.RestTemplate; + +public class PublisherReactiveHttpClientFactory { + + public DMaaPPublisherReactiveHttpClient create(DmaapPublisherConfiguration publisherConfiguration) { + return new DMaaPPublisherReactiveHttpClient(publisherConfiguration, new RestTemplate()); + } + +} diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClientTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClientTest.java deleted file mode 100644 index 4ca5ceb6..00000000 --- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/DMaaPReactiveWebClientTest.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.prh.service; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; -import org.springframework.web.reactive.function.client.WebClient; - -/** - * @author Przemysław Wąsala on 7/5/18 - */ -class DMaaPReactiveWebClientTest { - - - @Test - void builder_shouldBuildDMaaPReactiveWebClient() { - //given - DmaapConsumerConfiguration dmaapConsumerConfiguration = mock(DmaapConsumerConfiguration.class); - String dmaaPContentType = "*/*"; - String dmaaPUserName = "DMaaP"; - String dmaaPUserPassword = "DMaaP"; - - //when - when(dmaapConsumerConfiguration.dmaapContentType()).thenReturn(dmaaPContentType); - when(dmaapConsumerConfiguration.dmaapUserName()).thenReturn(dmaaPUserName); - when(dmaapConsumerConfiguration.dmaapUserPassword()).thenReturn(dmaaPUserPassword); - WebClient dmaapreactiveWebClient = new DMaaPReactiveWebClient() - .build(); - - //then - Assertions.assertNotNull(dmaapreactiveWebClient); - - } -} \ No newline at end of file diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/ConsumerReactiveHttpClientFactoryTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/ConsumerReactiveHttpClientFactoryTest.java new file mode 100644 index 00000000..6e864432 --- /dev/null +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/ConsumerReactiveHttpClientFactoryTest.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.consumer; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; + +class ConsumerReactiveHttpClientFactoryTest { + + private DmaapConsumerConfiguration dmaapConsumerConfiguration = mock(DmaapConsumerConfiguration.class); + private DMaaPReactiveWebClient reactiveWebClient = mock(DMaaPReactiveWebClient.class); + private ConsumerReactiveHttpClientFactory httpClientFactory = + new ConsumerReactiveHttpClientFactory(reactiveWebClient); + + @Test + void create_shouldReturnNotNullFactoryInstance() { + Assertions.assertNotNull(httpClientFactory.create(dmaapConsumerConfiguration)); + verify(reactiveWebClient).build(); + } +} \ No newline at end of file diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClientTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClientTest.java index c8ffd12f..595fd6d4 100644 --- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClientTest.java +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPConsumerReactiveHttpClientTest.java @@ -66,12 +66,12 @@ class DMaaPConsumerReactiveHttpClientTest { when(consumerConfigurationMock.consumerGroup()).thenReturn("OpenDCAE-c12"); when(consumerConfigurationMock.consumerId()).thenReturn("c12"); - dmaapConsumerReactiveHttpClient = new DMaaPConsumerReactiveHttpClient(consumerConfigurationMock); webClient = spy(WebClient.builder() .defaultHeader(HttpHeaders.CONTENT_TYPE, consumerConfigurationMock.dmaapContentType()) .filter(basicAuthentication(consumerConfigurationMock.dmaapUserName(), consumerConfigurationMock.dmaapUserPassword())) .build()); + dmaapConsumerReactiveHttpClient = new DMaaPConsumerReactiveHttpClient(consumerConfigurationMock, webClient); requestHeadersSpec = mock(RequestHeadersUriSpec.class); responseSpec = mock(ResponseSpec.class); } @@ -85,7 +85,6 @@ class DMaaPConsumerReactiveHttpClientTest { //when mockDependantObjects(); doReturn(expectedResult).when(responseSpec).bodyToMono(String.class); - dmaapConsumerReactiveHttpClient.createDMaaPWebClient(webClient); Mono response = dmaapConsumerReactiveHttpClient.getDMaaPConsumerResponse(); //then diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPReactiveWebClientTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPReactiveWebClientTest.java new file mode 100644 index 00000000..6b06c06a --- /dev/null +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/consumer/DMaaPReactiveWebClientTest.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.consumer; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; +import org.springframework.web.reactive.function.client.WebClient; + +/** + * @author Przemysław Wąsala on 7/5/18 + */ +class DMaaPReactiveWebClientTest { + + + @Test + void builder_shouldBuildDMaaPReactiveWebClient() { + //given + DmaapConsumerConfiguration dmaapConsumerConfiguration = mock(DmaapConsumerConfiguration.class); + String dmaaPContentType = "*/*"; + String dmaaPUserName = "DMaaP"; + String dmaaPUserPassword = "DMaaP"; + + //when + when(dmaapConsumerConfiguration.dmaapContentType()).thenReturn(dmaaPContentType); + when(dmaapConsumerConfiguration.dmaapUserName()).thenReturn(dmaaPUserName); + when(dmaapConsumerConfiguration.dmaapUserPassword()).thenReturn(dmaaPUserPassword); + WebClient dmaapreactiveWebClient = new DMaaPReactiveWebClient() + .build(); + + //then + Assertions.assertNotNull(dmaapreactiveWebClient); + + } +} \ No newline at end of file diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPPublisherReactiveHttpClientTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPPublisherReactiveHttpClientTest.java index db3d515c..ce2f7f36 100644 --- a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPPublisherReactiveHttpClientTest.java +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/DMaaPPublisherReactiveHttpClientTest.java @@ -39,8 +39,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import reactor.test.StepVerifier; - - /** * @author Przemysław Wąsala on 7/4/18 */ @@ -48,10 +46,10 @@ import reactor.test.StepVerifier; class DMaaPPublisherReactiveHttpClientTest { private DMaaPPublisherReactiveHttpClient dmaapPublisherReactiveHttpClient; - private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock( - DmaapPublisherConfiguration.class); + DmaapPublisherConfiguration.class); private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest(); + private RestTemplate restTemplate = mock(RestTemplate.class); @BeforeEach @@ -63,7 +61,8 @@ class DMaaPPublisherReactiveHttpClientTest { when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("PRH"); when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn("application/json"); when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn("unauthenticated.PNF_READY"); - dmaapPublisherReactiveHttpClient = new DMaaPPublisherReactiveHttpClient(dmaapPublisherConfigurationMock); + dmaapPublisherReactiveHttpClient = + new DMaaPPublisherReactiveHttpClient(dmaapPublisherConfigurationMock, restTemplate); } @@ -72,21 +71,19 @@ class DMaaPPublisherReactiveHttpClientTest { //given int responseSuccess = 200; ResponseEntity mockedResponseEntity = mock(ResponseEntity.class); - RestTemplate restTemplate = mock(RestTemplate.class); //when when(mockedResponseEntity.getStatusCode()).thenReturn(HttpStatus.valueOf(responseSuccess)); doReturn(mockedResponseEntity).when(restTemplate) - .exchange(any(URI.class), any(HttpMethod.class), any(HttpEntity.class), (Class) any()); - dmaapPublisherReactiveHttpClient.createDMaaPWebClient(restTemplate); + .exchange(any(URI.class), any(HttpMethod.class), any(HttpEntity.class), (Class) any()); //then StepVerifier.create(dmaapPublisherReactiveHttpClient.getDMaaPProducerResponse(consumerDmaapModel)) - .expectSubscription().expectNext(mockedResponseEntity).verifyComplete(); + .expectSubscription().expectNext(mockedResponseEntity).verifyComplete(); } @Test void getAppropriateUri_whenPassingCorrectedPathForPnf() { Assertions.assertEquals(dmaapPublisherReactiveHttpClient.getUri(), - URI.create("https://54.45.33.2:1234/unauthenticated.PNF_READY")); + URI.create("https://54.45.33.2:1234/unauthenticated.PNF_READY")); } } \ No newline at end of file diff --git a/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/PublisherReactiveHttpClientFactoryTest.java b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/PublisherReactiveHttpClientFactoryTest.java new file mode 100644 index 00000000..3acfde9d --- /dev/null +++ b/prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/PublisherReactiveHttpClientFactoryTest.java @@ -0,0 +1,39 @@ +/* + * ============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 static org.mockito.Mockito.mock; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; + + +class PublisherReactiveHttpClientFactoryTest { + + private DmaapPublisherConfiguration dmaapPublisherConfiguration = mock(DmaapPublisherConfiguration.class); + private PublisherReactiveHttpClientFactory httpClientFactory = new PublisherReactiveHttpClientFactory(); + + @Test + void create_shouldReturnNotNullFactoryInstance() { + Assertions.assertNotNull(httpClientFactory.create(dmaapPublisherConfiguration)); + } +} \ No newline at end of file -- cgit 1.2.3-korg