DMaaP client remove Spring 09/79909/2
authorMarcin Migdal <marcin.migdal@nokia.com>
Thu, 7 Mar 2019 14:02:24 +0000 (15:02 +0100)
committerMarcin Migdal <marcin.migdal@nokia.com>
Fri, 8 Mar 2019 11:47:13 +0000 (12:47 +0100)
Change-Id: I9e0206d4b3c737df8a80944f0050871c1ad307c6
Issue-ID: DCAEGEN2-1310
Signed-off-by: Marcin Migdal <marcin.migdal@nokia.com>
16 files changed:
rest-services/dmaap-client/pom.xml
rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/config/DmaapConsumerConfiguration.java
rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/DMaaPAbstractReactiveHttpClient.java [new file with mode: 0644]
rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/DMaaPClientServiceUtils.java [new file with mode: 0644]
rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/DMaaPConsumerReactiveHttpClient.java
rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DMaaPPublisherReactiveHttpClient.java
rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DmaaPRestTemplateFactory.java
rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/PublisherReactiveHttpClientFactory.java
rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/config/DmaapConsumerConfigurationTest.java [moved from rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/config/DmaapConsumerConfigurationTest.java with 99% similarity]
rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/config/DmaapPublisherConfigurationTest.java [moved from rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/config/DmaapPublisherConfigurationTest.java with 99% similarity]
rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/DMaaPClientServiceUtilsTest.java [new file with mode: 0644]
rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/ConsumerReactiveHttpClientFactoryTest.java
rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/consumer/DMaaPConsumerReactiveHttpClientTest.java
rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DMaaPPublisherReactiveHttpClientTest.java
rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/DmaaPRestTemplateFactoryTest.java
rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/service/producer/PublisherReactiveHttpClientFactoryTest.java

index cedf02c..52e9f0a 100644 (file)
       <artifactId>common-dependency</artifactId>
       <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>
index f7107f7..75816ea 100644 (file)
@@ -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 (file)
index 0000000..55fa7fb
--- /dev/null
@@ -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 (file)
index 0000000..3876b52
--- /dev/null
@@ -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;
+    }
+
+
+}
index 99f7020..d0f95f6 100644 (file)
 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();
     }
 }
index 39fdb32..6314bcb 100644 (file)
 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();
     }
 
 }
index fe2b2c1..765c64b 100644 (file)
 
 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)));
-    }
-
 }
index ff574f4..1cd3544 100644 (file)
 
 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/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 (file)
index 0000000..58f23b3
--- /dev/null
@@ -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));
+
+    }
+
+
+}
index 75eafcb..e356f17 100644 (file)
@@ -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 {
index 4c78901..d2ca5d1 100644 (file)
@@ -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
index c84ca7d..0ff1e20 100644 (file)
 
 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
index bd3b07e..cc239fa 100644 (file)
@@ -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) {
index 07b3567..380f8b1 100644 (file)
@@ -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