diff options
author | Marcin Migdal <marcin.migdal@nokia.com> | 2019-03-07 15:02:24 +0100 |
---|---|---|
committer | Marcin Migdal <marcin.migdal@nokia.com> | 2019-03-08 12:47:13 +0100 |
commit | 522a1c9dde168f0baa32a52a80a1b85b0b4812f9 (patch) | |
tree | 55f72ca7984183f2ae9072bd69935805a7eec492 /rest-services | |
parent | 3666e89b3411e84b138279cccc8130df60c81f4c (diff) |
DMaaP client remove Spring
Change-Id: I9e0206d4b3c737df8a80944f0050871c1ad307c6
Issue-ID: DCAEGEN2-1310
Signed-off-by: Marcin Migdal <marcin.migdal@nokia.com>
Diffstat (limited to 'rest-services')
16 files changed, 247 insertions, 214 deletions
diff --git a/rest-services/dmaap-client/pom.xml b/rest-services/dmaap-client/pom.xml index cedf02c7..52e9f0ac 100644 --- a/rest-services/dmaap-client/pom.xml +++ b/rest-services/dmaap-client/pom.xml @@ -24,22 +24,6 @@ <version>${project.version}</version> </dependency> <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-context</artifactId> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-webflux</artifactId> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-reactor-netty</artifactId> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-web</artifactId> - </dependency> - <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/config/DmaapConsumerConfiguration.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/config/DmaapConsumerConfiguration.java index f7107f7c..75816ea4 100644 --- a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/config/DmaapConsumerConfiguration.java +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/config/DmaapConsumerConfiguration.java @@ -22,12 +22,10 @@ package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config; import org.immutables.gson.Gson; import org.immutables.value.Value; -import org.springframework.stereotype.Component; /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18 */ -@Component @Value.Immutable(prehash = true) @Value.Style(builder = "new") @Gson.TypeAdapters diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/DMaaPAbstractReactiveHttpClient.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/DMaaPAbstractReactiveHttpClient.java new file mode 100644 index 00000000..55fa7fb7 --- /dev/null +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/DMaaPAbstractReactiveHttpClient.java @@ -0,0 +1,37 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * 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.sdk.rest.services.dmaap.client.service; + +import java.util.UUID; +import org.onap.dcaegen2.services.sdk.rest.services.model.logging.ImmutableRequestDiagnosticContext; +import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; + +public abstract class DMaaPAbstractReactiveHttpClient { + + protected final static String SLASH = "/"; + + protected RequestDiagnosticContext getRequestDiagnosticContext() { + return ImmutableRequestDiagnosticContext.builder() + .invocationId(UUID.randomUUID()).requestId(UUID.randomUUID()).build(); + } + + +} diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/DMaaPClientServiceUtils.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/DMaaPClientServiceUtils.java new file mode 100644 index 00000000..3876b527 --- /dev/null +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/DMaaPClientServiceUtils.java @@ -0,0 +1,49 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * 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.sdk.rest.services.dmaap.client.service; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author <a href="mailto:marcin.wmigdal@nokia.com">Marcin Migdal</a> on 3/8/2019 + */ +public final class DMaaPClientServiceUtils { + + public final static String CONTENT_TYPE = "Content-Type"; + + private DMaaPClientServiceUtils() { + + } + + /** + * Method generates headers for HTTP requests + * @param contentType contentType to be used in HTTP header* + * @return Map of Headers + * */ + public static Map<String, String> getHeaders(String contentType) { + Map<String, String> header = new HashMap<>(); + header.put(CONTENT_TYPE, contentType); + return header; + } + + +} diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/DMaaPConsumerReactiveHttpClient.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/DMaaPConsumerReactiveHttpClient.java index 99f70209..d0f95f6e 100644 --- a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/DMaaPConsumerReactiveHttpClient.java +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/DMaaPConsumerReactiveHttpClient.java @@ -21,12 +21,13 @@ package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer; import java.net.URI; -import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.UUID; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPAbstractReactiveHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPClientServiceUtils; import org.onap.dcaegen2.services.sdk.rest.services.model.logging.ImmutableRequestDiagnosticContext; import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; import org.onap.dcaegen2.services.sdk.rest.services.uri.URI.URIBuilder; @@ -36,17 +37,9 @@ import reactor.core.publisher.Mono; /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/26/18 */ -public class DMaaPConsumerReactiveHttpClient { +public class DMaaPConsumerReactiveHttpClient extends DMaaPAbstractReactiveHttpClient { - private final static String SLASH = "/"; - private final static String CONTENT_TYPE = "Content-Type"; - 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 contentType; + private final DmaapConsumerConfiguration consumerConfiguration; private final CloudHttpClient cloudHttpClient; /** @@ -57,13 +50,7 @@ public class DMaaPConsumerReactiveHttpClient { public DMaaPConsumerReactiveHttpClient(DmaapConsumerConfiguration consumerConfiguration, CloudHttpClient cloudHttpClient) { - this.dmaapHostName = consumerConfiguration.dmaapHostName(); - this.dmaapProtocol = consumerConfiguration.dmaapProtocol(); - this.dmaapPortNumber = consumerConfiguration.dmaapPortNumber(); - this.dmaapTopicName = consumerConfiguration.dmaapTopicName(); - this.consumerGroup = consumerConfiguration.consumerGroup(); - this.consumerId = consumerConfiguration.consumerId(); - this.contentType = consumerConfiguration.dmaapContentType(); + this.consumerConfiguration = consumerConfiguration; this.cloudHttpClient = cloudHttpClient; } @@ -73,10 +60,10 @@ public class DMaaPConsumerReactiveHttpClient { * @return reactive response from DMaaP in string format */ public Mono<String> getDMaaPConsumerResponse(Optional<RequestDiagnosticContext> requestDiagnosticContextOptional) { - Map<String,String> headers = new HashMap<>(); - headers.put(CONTENT_TYPE,contentType); + Map<String,String> headers = DMaaPClientServiceUtils.getHeaders(consumerConfiguration.dmaapContentType()); if (requestDiagnosticContextOptional.isPresent()) { - return cloudHttpClient.get(getUri().toString(), requestDiagnosticContextOptional.get(),headers, String.class); + return cloudHttpClient + .get(getUri().toString(), requestDiagnosticContextOptional.get(), headers, String.class); } RequestDiagnosticContext requestDiagnosticContext = ImmutableRequestDiagnosticContext.builder() .invocationId(UUID.randomUUID()).requestId(UUID.randomUUID()).build(); @@ -85,12 +72,14 @@ public class DMaaPConsumerReactiveHttpClient { URI getUri() { return URI.create( - new URIBuilder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber).path(createRequestPath()) + new URIBuilder().scheme(consumerConfiguration.dmaapProtocol()).host(consumerConfiguration.dmaapHostName()) + .port(consumerConfiguration.dmaapPortNumber()).path(createRequestPath()) .build().toString()); } private String createRequestPath() { - return new StringBuilder().append(SLASH).append(dmaapTopicName).append(SLASH).append(consumerGroup) - .append(SLASH).append(consumerId).toString(); + return new StringBuilder().append(SLASH).append(consumerConfiguration.dmaapTopicName()).append(SLASH) + .append(consumerConfiguration.consumerGroup()) + .append(SLASH).append(consumerConfiguration.consumerId()).toString(); } } diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DMaaPPublisherReactiveHttpClient.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DMaaPPublisherReactiveHttpClient.java index 39fdb32c..6314bcba 100644 --- a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DMaaPPublisherReactiveHttpClient.java +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DMaaPPublisherReactiveHttpClient.java @@ -21,55 +21,39 @@ package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer; -import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.REQUEST_ID; -import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.X_INVOCATION_ID; -import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.X_ONAP_REQUEST_ID; - import java.net.URI; -import java.util.UUID; +import java.util.Map; +import java.util.Optional; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPAbstractReactiveHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPClientServiceUtils; import org.onap.dcaegen2.services.sdk.rest.services.model.DmaapModel; import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder; +import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; import org.onap.dcaegen2.services.sdk.rest.services.uri.URI.URIBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; import reactor.core.publisher.Mono; /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18 */ -public class DMaaPPublisherReactiveHttpClient { +public class DMaaPPublisherReactiveHttpClient extends DMaaPAbstractReactiveHttpClient { - private final static String SLASH = "/"; - private final Logger logger = LoggerFactory.getLogger(DMaaPPublisherReactiveHttpClient.class); - private final String dmaapHostName; - private final Integer dmaapPortNumber; - private final String dmaapProtocol; - private final String dmaapTopicName; - private final String dmaapContentType; - private final Mono<RestTemplate> restTemplateMono; + private final DmaapPublisherConfiguration dmaapPublisherConfiguration; private final JsonBodyBuilder jsonBodyBuilder; + private final CloudHttpClient cloudHttpClient; /** * Constructor DMaaPPublisherReactiveHttpClient. * * @param dmaapPublisherConfiguration - DMaaP producer configuration object + * @param cloudHttpClient - cloudHttpClient sending http requests */ DMaaPPublisherReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration, - Mono<RestTemplate> restTemplateMono, JsonBodyBuilder jsonBodyBuilder) { - this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName(); - this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol(); - this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber(); - this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName(); - this.dmaapContentType = dmaapPublisherConfiguration.dmaapContentType(); - this.restTemplateMono = restTemplateMono; + CloudHttpClient cloudHttpClient, JsonBodyBuilder jsonBodyBuilder) { + this.dmaapPublisherConfiguration = dmaapPublisherConfiguration; + this.cloudHttpClient = cloudHttpClient; this.jsonBodyBuilder = jsonBodyBuilder; } @@ -80,32 +64,31 @@ public class DMaaPPublisherReactiveHttpClient { * @return status code of operation */ - public Mono<ResponseEntity<String>> getDMaaPProducerResponse(DmaapModel dmaapModel) { + public Mono<Integer> getDMaaPProducerResponse(DmaapModel dmaapModel, + Optional<RequestDiagnosticContext> requestDiagnosticContextOptional) { return Mono.defer(() -> { - HttpEntity<String> request = new HttpEntity<>(jsonBodyBuilder.createJsonBody(dmaapModel), getAllHeaders()); - logger.info("Request: {} {}", getUri(), request); - return restTemplateMono.map( - restTemplate -> restTemplate.exchange(getUri(), HttpMethod.POST, request, String.class)); + Map<String, String> headers = DMaaPClientServiceUtils.getHeaders(dmaapPublisherConfiguration.dmaapContentType()); + if (requestDiagnosticContextOptional.isPresent()) { + cloudHttpClient + .post(getUri().toString(), requestDiagnosticContextOptional.get(), headers, jsonBodyBuilder, + dmaapModel); + } + return cloudHttpClient + .post(getUri().toString(), getRequestDiagnosticContext(), headers, jsonBodyBuilder, dmaapModel); }); } - private HttpHeaders getAllHeaders() { - HttpHeaders headers = new HttpHeaders(); - headers.set(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID)); - headers.set(X_INVOCATION_ID, UUID.randomUUID().toString()); - headers.set(HttpHeaders.CONTENT_TYPE, dmaapContentType); - return headers; - - } URI getUri() { return URI.create( - new URIBuilder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber).path(createRequestPath()) + new URIBuilder().scheme(dmaapPublisherConfiguration.dmaapProtocol()) + .host(dmaapPublisherConfiguration.dmaapHostName()).port(dmaapPublisherConfiguration.dmaapPortNumber()) + .path(createRequestPath()) .build().toString()); } private String createRequestPath() { - return new StringBuilder().append(SLASH).append(dmaapTopicName).toString(); + return new StringBuilder().append(SLASH).append(dmaapPublisherConfiguration.dmaapTopicName()).toString(); } } diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DmaaPRestTemplateFactory.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DmaaPRestTemplateFactory.java index fe2b2c10..765c64b7 100644 --- a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DmaaPRestTemplateFactory.java +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DmaaPRestTemplateFactory.java @@ -20,96 +20,42 @@ package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.security.GeneralSecurityException; -import java.security.KeyStore; -import javax.net.ssl.SSLContext; -import org.apache.http.client.HttpClient; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.ssl.SSLContextBuilder; +import io.netty.handler.ssl.SslContext; +import javax.net.ssl.SSLException; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration; -import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.springframework.web.client.RestTemplate; -import reactor.core.publisher.Mono; +import org.onap.dcaegen2.services.sdk.rest.services.ssl.SslFactory; public class DmaaPRestTemplateFactory { + private SslFactory sslFactory; + + public DmaaPRestTemplateFactory() { + this(new SslFactory()); + } + + DmaaPRestTemplateFactory(SslFactory sslFactory) { + this.sslFactory = sslFactory; + } + /** * Function for creating RestTemplate object. * * @param publisherConfiguration - DMaaP publisher configuration object * @return RestTemplate with correct ssl configuration */ - public Mono<RestTemplate> build(DmaapPublisherConfiguration publisherConfiguration) { - if (publisherConfiguration.enableDmaapCertAuth()) { - return createRestTemplateWithSslSetup(publisherConfiguration); - } - - return Mono.just(new RestTemplate()); + public CloudHttpClient build(DmaapPublisherConfiguration publisherConfiguration) throws SSLException { + SslContext sslContext = createSslContext(publisherConfiguration); + return new CloudHttpClient(sslContext); } - private Mono<RestTemplate> createRestTemplateWithSslSetup(DmaapPublisherConfiguration publisherConfiguration) { - try { - RestTemplateBuilder builder = new RestTemplateBuilder(); - - SSLContext sslContext = createSslContext(publisherConfiguration, - loadPasswordFromFile(publisherConfiguration.keyStorePasswordPath()), - loadPasswordFromFile(publisherConfiguration.trustStorePasswordPath())); - - return Mono.just(builder - .requestFactory(() -> createRequestFactory(sslContext)).build()); - - } catch (GeneralSecurityException | IOException e) { - return Mono.error(e); + private SslContext createSslContext(DmaapPublisherConfiguration consumerConfiguration) throws SSLException { + if (consumerConfiguration.enableDmaapCertAuth()) { + return sslFactory.createSecureContext( + consumerConfiguration.keyStorePath(), consumerConfiguration.keyStorePasswordPath(), + consumerConfiguration.trustStorePath(), consumerConfiguration.trustStorePasswordPath() + ); } + return sslFactory.createInsecureContext(); } - - private SSLContext createSslContext(DmaapPublisherConfiguration publisherConfiguration, - String keyStorePassword, String trustStorePassword) - throws IOException, GeneralSecurityException { - return new SSLContextBuilder() - .loadKeyMaterial( - keyStore(publisherConfiguration.keyStorePath(), keyStorePassword), - keyStorePassword.toCharArray()) - .loadTrustMaterial( - getFile(publisherConfiguration.trustStorePath()), trustStorePassword.toCharArray()) - .build(); - } - - private HttpComponentsClientHttpRequestFactory createRequestFactory(SSLContext sslContext) { - SSLConnectionSocketFactory socketFactory = - new SSLConnectionSocketFactory(sslContext); - HttpClient httpClient = HttpClients.custom() - .setSSLSocketFactory(socketFactory).build(); - - return new HttpComponentsClientHttpRequestFactory(httpClient); - } - - private KeyStore keyStore(String keyStoreFile, String keyStorePassword) - throws GeneralSecurityException, IOException { - KeyStore ks = KeyStore.getInstance("jks"); - ks.load(getResource(keyStoreFile), keyStorePassword.toCharArray()); - return ks; - } - - private File getFile(String fileName) { - return new File(fileName); - } - - private InputStream getResource(String fileName) throws FileNotFoundException { - return new FileInputStream(fileName); - } - - private String loadPasswordFromFile(String path) throws IOException { - return new String(Files.readAllBytes(Paths.get(path))); - } - } diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/PublisherReactiveHttpClientFactory.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/PublisherReactiveHttpClientFactory.java index ff574f46..1cd3544e 100644 --- a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/PublisherReactiveHttpClientFactory.java +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/PublisherReactiveHttpClientFactory.java @@ -20,23 +20,25 @@ package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer; +import javax.net.ssl.SSLException; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration; import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder; - public class PublisherReactiveHttpClientFactory { private final DmaaPRestTemplateFactory restTemplateFactory; private final JsonBodyBuilder jsonBodyBuilder; - public PublisherReactiveHttpClientFactory(DmaaPRestTemplateFactory restTemplateFactory, JsonBodyBuilder jsonBodyBuilder) { + public PublisherReactiveHttpClientFactory(DmaaPRestTemplateFactory restTemplateFactory, + JsonBodyBuilder jsonBodyBuilder) { this.restTemplateFactory = restTemplateFactory; this.jsonBodyBuilder = jsonBodyBuilder; } - public DMaaPPublisherReactiveHttpClient create(DmaapPublisherConfiguration publisherConfiguration) { + public DMaaPPublisherReactiveHttpClient create(DmaapPublisherConfiguration publisherConfiguration) + throws SSLException { return new DMaaPPublisherReactiveHttpClient(publisherConfiguration, - restTemplateFactory.build(publisherConfiguration), jsonBodyBuilder); + restTemplateFactory.build(publisherConfiguration), jsonBodyBuilder); } } diff --git a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/config/DmaapConsumerConfigurationTest.java b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/config/DmaapConsumerConfigurationTest.java index cf0be692..f7940c8f 100644 --- a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/config/DmaapConsumerConfigurationTest.java +++ b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/config/DmaapConsumerConfigurationTest.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.config; +package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/config/DmaapPublisherConfigurationTest.java b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/config/DmaapPublisherConfigurationTest.java index 7128777a..d7f06d1b 100644 --- a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/config/DmaapPublisherConfigurationTest.java +++ b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/config/DmaapPublisherConfigurationTest.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.config; +package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/DMaaPClientServiceUtilsTest.java b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/DMaaPClientServiceUtilsTest.java new file mode 100644 index 00000000..58f23b35 --- /dev/null +++ b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/DMaaPClientServiceUtilsTest.java @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * 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.sdk.rest.services.dmaap.client.service; + +import java.util.Map; +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPClientServiceUtils; + +public class DMaaPClientServiceUtilsTest { + + + private final static String contentType = "myType"; + + @Test + public void getHeaders(){ + + Map<String,String> headres = DMaaPClientServiceUtils.getHeaders(contentType); + assertEquals(1,headres.size()); + assertEquals(contentType,headres.get(DMaaPClientServiceUtils.CONTENT_TYPE)); + + } + + +} diff --git a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/ConsumerReactiveHttpClientFactoryTest.java b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/ConsumerReactiveHttpClientFactoryTest.java index 75eafcb6..e356f179 100644 --- a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/ConsumerReactiveHttpClientFactoryTest.java +++ b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/ConsumerReactiveHttpClientFactoryTest.java @@ -32,7 +32,7 @@ class ConsumerReactiveHttpClientFactoryTest { private DmaapConsumerConfiguration dmaapConsumerConfiguration = mock(DmaapConsumerConfiguration.class); private DMaaPReactiveWebClientFactory reactiveWebClientFactory = mock(DMaaPReactiveWebClientFactory.class); private ConsumerReactiveHttpClientFactory httpClientFactory = - new ConsumerReactiveHttpClientFactory(reactiveWebClientFactory); + new ConsumerReactiveHttpClientFactory(reactiveWebClientFactory); @Test void create_shouldReturnNotNullFactoryInstance() throws Exception { diff --git a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/DMaaPConsumerReactiveHttpClientTest.java b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/DMaaPConsumerReactiveHttpClientTest.java index 4c789019..d2ca5d12 100644 --- a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/DMaaPConsumerReactiveHttpClientTest.java +++ b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/DMaaPConsumerReactiveHttpClientTest.java @@ -24,8 +24,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.net.URI; -import java.util.HashMap; -import java.util.Map; import java.util.Optional; import org.apache.http.entity.ContentType; import org.junit.jupiter.api.Assertions; @@ -33,6 +31,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPClientServiceUtils; import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -69,7 +68,7 @@ class DMaaPConsumerReactiveHttpClientTest { void getHttpResponse_Success() { //given expectedResult = Mono.just(JSON_MESSAGE); - when(httpClient.get(exampleTestUri.toString(), requestDiagnosticContext, getCustomHeaders(), String.class)) + when(httpClient.get(exampleTestUri.toString(), requestDiagnosticContext, DMaaPClientServiceUtils.getHeaders(ContentType.APPLICATION_JSON.getMimeType()), String.class)) .thenReturn(expectedResult); //when Mono<String> response = dmaapConsumerReactiveHttpClient @@ -87,10 +86,5 @@ class DMaaPConsumerReactiveHttpClientTest { Assertions.assertEquals(dmaapConsumerReactiveHttpClient.getUri(), exampleTestUri); } - private Map<String, String> getCustomHeaders() { - Map<String, String> customHeaders = new HashMap<>(); - customHeaders.put("Content-Type", ContentType.APPLICATION_JSON.getMimeType()); - return customHeaders; - } }
\ No newline at end of file diff --git a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DMaaPPublisherReactiveHttpClientTest.java b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DMaaPPublisherReactiveHttpClientTest.java index c84ca7d2..0ff1e207 100644 --- a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DMaaPPublisherReactiveHttpClientTest.java +++ b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DMaaPPublisherReactiveHttpClientTest.java @@ -20,24 +20,22 @@ package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.net.URI; +import java.util.Optional; +import org.apache.http.entity.ContentType; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration; - +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.DMaaPClientServiceUtils; +import org.onap.dcaegen2.services.sdk.rest.services.model.ClientModel; import org.onap.dcaegen2.services.sdk.rest.services.model.DmaapModel; import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; +import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -49,12 +47,11 @@ class DMaaPPublisherReactiveHttpClientTest { private DMaaPPublisherReactiveHttpClient dmaapPublisherReactiveHttpClient; private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class); - - private RestTemplate restTemplate = mock(RestTemplate.class); - - private DmaapModel dmaapModel = mock(DmaapModel.class); - private JsonBodyBuilder<DmaapModel> jsonBodyBuilder = mock(JsonBodyBuilder.class); - + private CloudHttpClient cloudHttpClientMock = mock(CloudHttpClient.class); + private DmaapModel dmaapModelMock = mock(DmaapModel.class); + private JsonBodyBuilder<DmaapModel> jsonBodyBuilderMock = mock(JsonBodyBuilder.class); + private Optional<RequestDiagnosticContext> requestDiagnosticContextOptionalMock = Optional + .of(mock(RequestDiagnosticContext.class)); @BeforeEach void setUp() { @@ -66,33 +63,43 @@ class DMaaPPublisherReactiveHttpClientTest { when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn("application/json"); when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn("unauthenticated.PNF_READY"); - when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn( - "{\"correlationId\":\"NOKnhfsadhff\"," + - "\"ipaddress-v4\":\"256.22.33.155\", " + - "\"ipaddress-v6\":\"200J:0db8:85a3:0000:0000:8a2e:0370:7334\"}"); + when(jsonBodyBuilderMock.createJsonBody(dmaapModelMock)).thenReturn( + "{\"correlationId\":\"NOKnhfsadhff\"," + + "\"ipaddress-v4\":\"256.22.33.155\", " + + "\"ipaddress-v6\":\"200J:0db8:85a3:0000:0000:8a2e:0370:7334\"}"); dmaapPublisherReactiveHttpClient = - new DMaaPPublisherReactiveHttpClient(dmaapPublisherConfigurationMock, Mono.just(restTemplate),jsonBodyBuilder); + new DMaaPPublisherReactiveHttpClient(dmaapPublisherConfigurationMock, cloudHttpClientMock, + jsonBodyBuilderMock); } @Test void getHttpResponse_Success() { //given - int responseSuccess = 200; - ResponseEntity<String> mockedResponseEntity = mock(ResponseEntity.class); + Mono<Integer> expectedResult = Mono.just(Integer.valueOf(200)); //when - when(mockedResponseEntity.getStatusCode()).thenReturn(HttpStatus.valueOf(responseSuccess)); - doReturn(mockedResponseEntity).when(restTemplate) - .exchange(any(URI.class), any(HttpMethod.class), any(HttpEntity.class), (Class<Object>) any()); - + when( + cloudHttpClientMock + .post(getUri().toString(), requestDiagnosticContextOptionalMock.get(), + DMaaPClientServiceUtils.getHeaders(ContentType.APPLICATION_JSON.getMimeType()), + jsonBodyBuilderMock, + mock(ClientModel.class))) + .thenReturn(Mono.just(Integer.valueOf(200))); //then - StepVerifier.create(dmaapPublisherReactiveHttpClient.getDMaaPProducerResponse(dmaapModel)) - .expectSubscription().expectNext(mockedResponseEntity).verifyComplete(); + StepVerifier.create(expectedResult).expectSubscription() + .expectNextMatches(results -> { + Assertions.assertEquals(results, expectedResult.block()); + return true; + }).verifyComplete(); } @Test void getAppropriateUri_whenPassingCorrectedPathForPnf() { Assertions.assertEquals(dmaapPublisherReactiveHttpClient.getUri(), - URI.create("https://54.45.33.2:1234/unauthenticated.PNF_READY")); + getUri()); + } + + private URI getUri() { + return URI.create("https://54.45.33.2:1234/unauthenticated.PNF_READY"); } }
\ No newline at end of file diff --git a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DmaaPRestTemplateFactoryTest.java b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DmaaPRestTemplateFactoryTest.java index bd3b07e9..cc239fa4 100644 --- a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DmaaPRestTemplateFactoryTest.java +++ b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DmaaPRestTemplateFactoryTest.java @@ -23,6 +23,7 @@ package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.produc import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import javax.net.ssl.SSLException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration; @@ -38,21 +39,21 @@ class DmaaPRestTemplateFactoryTest { private DmaaPRestTemplateFactory factory = new DmaaPRestTemplateFactory(); @Test - void build_shouldCreateRestTemplateWithoutSslConfiguration() { + void build_shouldCreateRestTemplateWithoutSslConfiguration() throws SSLException { when(publisherConfiguration.enableDmaapCertAuth()).thenReturn(false); - Assertions.assertNotNull(factory.build(publisherConfiguration).block()); + Assertions.assertNotNull(factory.build(publisherConfiguration)); } @Test - void build_shouldCreateRestTemplateWithSslConfiguration() { + void build_shouldCreateRestTemplateWithSslConfiguration() throws SSLException { when(publisherConfiguration.enableDmaapCertAuth()).thenReturn(true); when(publisherConfiguration.keyStorePath()).thenReturn(getPath(KEY_STORE)); when(publisherConfiguration.keyStorePasswordPath()).thenReturn(getPath(KEYSTORE_PASSWORD)); when(publisherConfiguration.trustStorePath()).thenReturn(getPath(TRUST_STORE)); when(publisherConfiguration.trustStorePasswordPath()).thenReturn(getPath(TRUSTSTORE_PASSWORD)); - Assertions.assertNotNull(factory.build(publisherConfiguration).block()); + Assertions.assertNotNull(factory.build(publisherConfiguration)); } private String getPath(String fileName) { diff --git a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/PublisherReactiveHttpClientFactoryTest.java b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/PublisherReactiveHttpClientFactoryTest.java index 07b35673..380f8b15 100644 --- a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/PublisherReactiveHttpClientFactoryTest.java +++ b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/PublisherReactiveHttpClientFactoryTest.java @@ -22,6 +22,7 @@ package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.produc import static org.mockito.Mockito.mock; +import javax.net.ssl.SSLException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration; @@ -35,12 +36,11 @@ class PublisherReactiveHttpClientFactoryTest { private DmaapPublisherConfiguration dmaapPublisherConfiguration = mock(DmaapPublisherConfiguration.class); private JsonBodyBuilder<DmaapModel> jsonBodyBuilder = mock(JsonBodyBuilder.class); - private PublisherReactiveHttpClientFactory httpClientFactory = - new PublisherReactiveHttpClientFactory(restTemplateFactory, jsonBodyBuilder); + new PublisherReactiveHttpClientFactory(restTemplateFactory, jsonBodyBuilder); @Test - void create_shouldReturnNotNullFactoryInstance() { + void create_shouldReturnNotNullFactoryInstance() throws SSLException { Assertions.assertNotNull(httpClientFactory.create(dmaapPublisherConfiguration)); } }
\ No newline at end of file |