Http put added 89/40989/5
authorpwielebs <piotr.wielebski@nokia.com>
Tue, 3 Apr 2018 10:12:43 +0000 (12:12 +0200)
committerpwielebs <piotr.wielebski@nokia.com>
Mon, 9 Apr 2018 13:01:40 +0000 (15:01 +0200)
Change-Id: Id21b799a35b531c30f0c1b5df5c6daac6fd56e92
Issue-ID: DCAEGEN2-407
Signed-off-by: pwielebs <piotr.wielebski@nokia.com>
16 files changed:
prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java [moved from prh-aai-client/src/main/java/services/config/AAIHttpClientConfiguration.java with 97% similarity]
prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java [new file with mode: 0644]
prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImpl.java [moved from prh-aai-client/src/main/java/services/service/AAIExtendedHttpClientImpl.java with 64% similarity]
prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClient.java [moved from prh-aai-client/src/main/java/services/service/AAIHttpClient.java with 94% similarity]
prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIHttpClientImpl.java [moved from prh-aai-client/src/main/java/services/service/AAIHttpClientImpl.java with 90% similarity]
prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java [new file with mode: 0644]
prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpUtils.java [moved from prh-aai-client/src/main/java/services/utils/HttpUtils.java with 93% similarity]
prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/RequestVerbs.java [moved from prh-aai-client/src/main/java/services/service/AAIExtendedHttpClient.java with 83% similarity]
prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java [new file with mode: 0644]
prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java [new file with mode: 0644]
prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java [new file with mode: 0644]
prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java [new file with mode: 0644]
prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpUtilsTest.java [moved from prh-aai-client/src/test/java/services/service/utils/HttpUtilsTest.java with 93% similarity]
prh-aai-client/src/test/java/services/service/AAIExtendedHttpClientImplTest.java [deleted file]
prh-aai-client/src/test/java/services/service/AAIHttpClientImplTest.java [deleted file]
prh-aai-client/src/test/java/services/service/utils/HTTPConfiguration.java [deleted file]

  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
+package org.onap.dcaegen2.services.config;
 
-package services.config;
 
-
-import java.io.Serializable;
 import org.immutables.value.Value;
 import org.springframework.stereotype.Component;
 
+import java.io.Serializable;
+
+
 @Component
 @Value.Immutable(prehash = true)
 @Value.Style(stagedBuilder = true)
diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClient.java
new file mode 100644 (file)
index 0000000..24149e6
--- /dev/null
@@ -0,0 +1,30 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * PNF-REGISTRATION-HANDLER
+ * ================================================================================
+ * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dcaegen2.services.service;
+
+import org.onap.dcaegen2.services.utils.HttpRequestDetails;
+
+import java.util.Optional;
+
+
+@FunctionalInterface
+public interface AAIExtendedHttpClient {
+    Optional<String> getHttpResponse(HttpRequestDetails httpRequestDetails);
+}
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
-package services.service;
+package org.onap.dcaegen2.services.service;
 
-import java.util.Optional;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.http.HttpEntity;
 import org.apache.http.client.ResponseHandler;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.client.utils.URIBuilder;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.util.EntityUtils;
+import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration;
+import org.onap.dcaegen2.services.utils.HttpRequestDetails;
+import org.onap.dcaegen2.services.utils.HttpUtils;
+import org.onap.dcaegen2.services.utils.RequestVerbs;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 
-import services.config.AAIHttpClientConfiguration;
-import services.utils.HttpUtils;
-
 import javax.annotation.Nonnull;
 import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Optional;
 
 public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient {
 
+    Logger logger = LoggerFactory.getLogger(AAIExtendedHttpClientImpl.class);
+
     private final CloseableHttpClient closeableHttpClient;
     private final String aaiHost;
     private final String aaiProtocol;
@@ -56,53 +63,52 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient {
     }
 
     @Override
-    public String getExtendedDetails(final String aaiAPIPath, final Map<String, String> queryParams,
-                                       final Map<String, String> headers) {
-        final URI extendedURI =
-                createAAIExtendedURI(aaiProtocol, aaiHost, aaiHostPortNumber, aaiAPIPath, queryParams);
+    public Optional<String> getHttpResponse(HttpRequestDetails httpRequestDetails) {
 
-        if (extendedURI == null) {
-            return null;
-        }
+        Optional<String> extendedDetails = Optional.empty();
 
-        final HttpGet getRequest = new HttpGet(extendedURI);
+        final URI extendedURI = createAAIExtendedURI(httpRequestDetails.aaiAPIPath(),
+                httpRequestDetails.queryParameters());
+        final HttpRequestBase request = createHttpRequest(extendedURI, httpRequestDetails);
 
-        for (Map.Entry<String, String> headersEntry : headers.entrySet()) {
-            getRequest.addHeader(headersEntry.getKey(), headersEntry.getValue());
+        if (request == null) {
+            return Optional.empty();
         }
 
-        Optional<String> extendedDetails = Optional.empty();
+        for (Map.Entry<String, String> headersEntry : httpRequestDetails.headers().entrySet()) {
+            request.addHeader(headersEntry.getKey(), headersEntry.getValue());
+        }
 
         try {
-            extendedDetails = closeableHttpClient.execute(getRequest, aaiResponseHandler());
-        } catch (IOException ex) {
-            //ToDo loging
+            extendedDetails = closeableHttpClient.execute(request, aaiResponseHandler());
+        } catch (IOException e) {
+            logger.error("Exception while executing HTTP request: {}", e);
         }
 
-        // return response
         if (extendedDetails.isPresent()) {
-            return extendedDetails.get();
+            return extendedDetails;
         } else {
-            return null;
+            return Optional.empty();
         }
     }
 
-    private URI createAAIExtendedURI(final String protocol, final String hostName,  final Integer portNumber,
-                                     final String path, Map<String, String> queryParams) {
-        final URIBuilder uriBuilder = new URIBuilder().setScheme(protocol).setHost(hostName).setPort(portNumber)
-                .setPath(path);
+    private URI createAAIExtendedURI(final String path, Map<String, String> queryParams) {
+        URI extendedURI = null;
 
+        final URIBuilder uriBuilder = new URIBuilder().setScheme(this.aaiProtocol).setHost(this.aaiHost)
+                .setPort(this.aaiHostPortNumber)
+                .setPath(path);
         final String customQuery = createCustomQuery(queryParams);
+
         if (StringUtils.isNoneBlank(customQuery)) {
             uriBuilder.setCustomQuery(customQuery);
         }
 
-        URI extendedURI = null;
-
         try {
+            logger.info("Building extended URI");
             extendedURI = uriBuilder.build();
         } catch (URISyntaxException e) {
-            // ToDo loging
+            logger.error("Exception while building extended URI: {}", e);
         }
 
         return extendedURI;
@@ -111,15 +117,15 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient {
     private String createCustomQuery(@Nonnull final Map<String, String> queryParams) {
         final StringBuilder queryStringBuilder = new StringBuilder("");
         final Iterator<Map.Entry<String, String>> queryParamIterator = queryParams.entrySet().iterator();
+
         while (queryParamIterator.hasNext()) {
             final Map.Entry<String, String> queryParamsEntry = queryParamIterator.next();
-            queryStringBuilder.append(queryParamsEntry.getKey());
-            queryStringBuilder.append("=");
-            queryStringBuilder.append(queryParamsEntry.getValue());
+            queryStringBuilder.append(queryParamsEntry.getKey()).append("=").append(queryParamsEntry.getValue());
             if (queryParamIterator.hasNext()) {
                 queryStringBuilder.append("&");
             }
         }
+
         return queryStringBuilder.toString();
     }
 
@@ -128,15 +134,29 @@ public class AAIExtendedHttpClientImpl implements AAIExtendedHttpClient {
             final int responseCode = httpResponse.getStatusLine().getStatusCode();
             final HttpEntity responseEntity = httpResponse.getEntity();
 
-            if (HttpUtils.isSuccessfulResponseCode(responseCode) && null != responseEntity) {
+            if (HttpUtils.isSuccessfulResponseCode(responseCode) && responseEntity != null) {
+                logger.info("HTTP response successful.");
                 final String aaiResponse = EntityUtils.toString(responseEntity);
                 return Optional.of(aaiResponse);
             } else {
                 String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
-                //ToDo loging
+                logger.error("HTTP response not successful : {}", aaiResponse);
                 return Optional.empty();
             }
         };
     }
 
+    private HttpRequestBase createHttpRequest(URI extendedURI, HttpRequestDetails httpRequestDetails) {
+        if (isExtendedURINotNull(extendedURI) && (httpRequestDetails.requestVerb().equals(RequestVerbs.GET))) {
+            return new HttpGet(extendedURI);
+        } else if (isExtendedURINotNull(extendedURI) && (httpRequestDetails.requestVerb().equals(RequestVerbs.PUT))) {
+            return new HttpPut(extendedURI);
+        } else {
+            return null;
+        }
+    }
+
+    private Boolean isExtendedURINotNull(URI extendedURI) {
+        return extendedURI != null ? true : false;
+    }
 }
  * ============LICENSE_END=========================================================
  */
 
-package services.service;
+package org.onap.dcaegen2.services.service;
 
 import org.apache.http.impl.client.CloseableHttpClient;
 
+@FunctionalInterface
 public interface AAIHttpClient {
     CloseableHttpClient getAAIHttpClient();
 }
@@ -18,7 +18,7 @@
  * ============LICENSE_END=========================================================
  */
 
-package services.service;
+package org.onap.dcaegen2.services.service;
 
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.Credentials;
@@ -31,11 +31,10 @@ import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.ssl.SSLContextBuilder;
 import org.apache.http.ssl.TrustStrategy;
+import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import services.config.AAIHttpClientConfiguration;
-
 
 import java.security.KeyManagementException;
 import java.security.KeyStoreException;
@@ -62,12 +61,13 @@ public class AAIHttpClientImpl implements AAIHttpClient {
 
         if (aaiIgnoreSSLCertificateErrors) {
             try {
-                SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
-                sslContextBuilder.loadTrustMaterial(null, acceptingTrustStrategy);
-                httpClientBuilder.setSSLContext(sslContextBuilder.build());
+                logger.info("Setting SSL Context for AAI HTTP Client");
+                httpClientBuilder.setSSLContext(new SSLContextBuilder()
+                        .loadTrustMaterial(null, acceptingTrustStrategy)
+                        .build());
 
             } catch (NoSuchAlgorithmException | KeyStoreException |  KeyManagementException e )  {
-                logger.error("Exception while setting SSL Context for AAI HTTP Client.");
+                logger.error("Exception while setting SSL Context for AAI HTTP Client: {}", e);
             }
 
             httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/utils/HttpRequestDetails.java
new file mode 100644 (file)
index 0000000..896e306
--- /dev/null
@@ -0,0 +1,45 @@
+/*-
+ * ============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.utils;
+
+
+import org.immutables.value.Value;
+
+import java.io.Serializable;
+import java.util.Map;
+
+@Value.Immutable(prehash = true)
+@Value.Style(stagedBuilder = true)
+public abstract class HttpRequestDetails implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Value.Parameter
+    public abstract String aaiAPIPath();
+
+    @Value.Parameter
+    public abstract Map<String,String> queryParameters();
+
+    @Value.Parameter
+    public abstract Map<String,String> headers();
+
+    @Value.Parameter
+    public abstract RequestVerbs requestVerb();
+}
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
-package services.utils;
+package org.onap.dcaegen2.services.utils;
 
 import org.apache.http.HttpStatus;
 
-public class HttpUtils implements HttpStatus {
-
-    private HttpUtils() {}
+public final class HttpUtils implements HttpStatus {
 
     public static final String JSON_APPLICATION_TYPE = "application/json";
 
+    private HttpUtils() {}
+
     public static boolean isSuccessfulResponseCode(Integer statusCode) {
         return statusCode >= 200 && statusCode < 300;
     }
  * ============LICENSE_END=========================================================
  */
 
-package services.service;
+package org.onap.dcaegen2.services.utils;
 
-import java.util.Map;
+public enum RequestVerbs {
+    GET,
+    PUT;
 
-public interface AAIExtendedHttpClient {
-    String getExtendedDetails(String aaiAPIPath, Map<String, String> queryParams, Map<String, String> headers);
+    private RequestVerbs() {};
 }
diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIExtendedHttpClientImplTest.java
new file mode 100644 (file)
index 0000000..e114bbf
--- /dev/null
@@ -0,0 +1,131 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * PNF-REGISTRATION-HANDLER
+ * ================================================================================
+ * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.services.service;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.http.client.ResponseHandler;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.junit.jupiter.api.*;
+import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration;
+import org.onap.dcaegen2.services.utils.HttpRequestDetails;
+import org.onap.dcaegen2.services.utils.RequestVerbs;
+
+public class AAIExtendedHttpClientImplTest {
+
+    private static AAIExtendedHttpClientImpl testedObject;
+    private static AAIHttpClientConfiguration aaiHttpClientConfigurationMock = mock(AAIHttpClientConfiguration.class);
+    private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class);
+    private static HttpRequestDetails httpRequestDetailsMock = mock(HttpRequestDetails.class);
+    private static Optional<String> expectedResult = Optional.empty();
+
+    @BeforeAll
+    public static void init() throws NoSuchFieldException, IllegalAccessException {
+
+        Map<String, String> queryParams = new HashMap<>();
+        queryParams.put("ipaddress-v4-oam", "11.22.33.44");
+
+        Map<String, String> AAI_HEADERS = new HashMap<>();
+        AAI_HEADERS.put("X-FromAppId", "prh");
+        AAI_HEADERS.put("X-TransactionId", "vv-temp");
+        AAI_HEADERS.put("Accept", "application/json");
+        AAI_HEADERS.put("Real-Time", "true");
+        AAI_HEADERS.put("Content-Type", "application/json");
+
+        when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2");
+        when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https");
+        when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234);
+        when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PRH");
+        when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PRH");
+
+        when(httpRequestDetailsMock.aaiAPIPath()).thenReturn("/aai/v11/network/pnfs/pnf/NOKQTFCOC540002E");
+        when(httpRequestDetailsMock.headers()).thenReturn(AAI_HEADERS);
+        when(httpRequestDetailsMock.queryParameters()).thenReturn(queryParams);
+
+        testedObject = new AAIExtendedHttpClientImpl(aaiHttpClientConfigurationMock);
+        setField();
+    }
+
+    @AfterAll
+    public static void teardown() {
+        testedObject = null;
+        aaiHttpClientConfigurationMock = null;
+        closeableHttpClientMock = null;
+        httpRequestDetailsMock = null;
+        expectedResult = null;
+    }
+
+    @Test
+    public void getHttpResponseGet_success() throws IOException {
+        when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.GET);
+
+        expectedResult = Optional.of("getExtendedDetailsOK");
+
+        when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))).
+                thenReturn(expectedResult);
+        Optional<String> actualResult = testedObject.getHttpResponse(httpRequestDetailsMock);
+
+        Assertions.assertEquals(expectedResult.get(), actualResult.get());
+    }
+
+    @Test
+    public void getHttpResponsePut_success() throws IOException {
+        when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.PUT);
+
+        expectedResult = Optional.of("getExtendedDetailsOK");
+
+        when(closeableHttpClientMock.execute(any(HttpPut.class), any(ResponseHandler.class))).
+                thenReturn(expectedResult);
+        Optional<String>  actualResult = testedObject.getHttpResponse(httpRequestDetailsMock);
+
+        Assertions.assertEquals(expectedResult.get(), actualResult.get());
+    }
+
+    @Test
+    public void getExtendedDetails_returnsNull() throws IOException {
+        when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.GET);
+        when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))).
+                thenReturn(Optional.empty());
+        Optional<String>  actualResult = testedObject.getHttpResponse(httpRequestDetailsMock);
+        Assertions.assertEquals(Optional.empty(),actualResult);
+    }
+
+    @Test
+    public void getHttpResponsePut_failure() {
+        when(httpRequestDetailsMock.requestVerb()).thenReturn(RequestVerbs.PUT);
+
+    }
+
+    private static void setField() throws NoSuchFieldException, IllegalAccessException {
+        Field field = testedObject.getClass().getDeclaredField("closeableHttpClient");
+        field.setAccessible(true);
+        field.set(testedObject, closeableHttpClientMock);
+    }
+}
diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/AAIHttpClientImplTest.java
new file mode 100644 (file)
index 0000000..d26e6af
--- /dev/null
@@ -0,0 +1,57 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * PNF-REGISTRATION-HANDLER
+ * ================================================================================
+ * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dcaegen2.services.service;
+
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+
+public class AAIHttpClientImplTest {
+
+    private static AAIHttpClientImpl testedObject;
+    private static AAIHttpClientConfiguration aaiHttpClientConfigurationMock;
+
+
+    @BeforeAll
+    public static void setup() {
+        aaiHttpClientConfigurationMock = mock(AAIHttpClientConfiguration.class);
+        when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2");
+        when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https");
+        when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234);
+        when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("PNF");
+        when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("PNF");
+        when(aaiHttpClientConfigurationMock.aaiIgnoreSSLCertificateErrors()).thenReturn(true);
+
+
+        testedObject  = new AAIHttpClientImpl(aaiHttpClientConfigurationMock);
+    }
+
+    @Test
+    public void getAAIHttpClientObject_shouldNotBeNull() {
+        testedObject.getAAIHttpClient();
+        assertNotNull(testedObject.getAAIHttpClient());
+    }
+}
+
diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java
new file mode 100644 (file)
index 0000000..a759aca
--- /dev/null
@@ -0,0 +1,58 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * PNF-REGISTRATION-HANDLER
+ * ================================================================================
+ * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.services.service.config;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration;
+import org.onap.dcaegen2.services.config.ImmutableAAIHttpClientConfiguration;
+
+public class AAIHttpClientConfigurationTest {
+
+    private static AAIHttpClientConfiguration client;
+    private static final String AAI_HOST = "/aai/v11/network/pnfs/pnf/NOKQTFCOC540002E";
+    private static final Integer PORT = 1234;
+    private static final String PROTOCOL = "https";
+    private static final String USER_NAME_PASSWORD = "PRH";
+
+    @BeforeAll
+    public static void init() {
+        client = ImmutableAAIHttpClientConfiguration.builder()
+                .aaiHost(AAI_HOST)
+                .aaiHostPortNumber(PORT)
+                .aaiProtocol(PROTOCOL)
+                .aaiUserName(USER_NAME_PASSWORD)
+                .aaiUserPassword(USER_NAME_PASSWORD)
+                .aaiIgnoreSSLCertificateErrors(true)
+                .build();
+    }
+
+    @Test
+    public void testGetters_success() {
+        Assertions.assertEquals(AAI_HOST,client.aaiHost());
+        Assertions.assertEquals(PORT, client.aaiHostPortNumber());
+        Assertions.assertEquals(PROTOCOL,client.aaiProtocol());
+        Assertions.assertEquals(USER_NAME_PASSWORD, client.aaiUserName());
+        Assertions.assertEquals(USER_NAME_PASSWORD, client.aaiUserPassword());
+        Assertions.assertEquals(true, client.aaiIgnoreSSLCertificateErrors());
+    }
+}
diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/utils/HttpRequestDetailsTest.java
new file mode 100644 (file)
index 0000000..36105d0
--- /dev/null
@@ -0,0 +1,60 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * PNF-REGISTRATION-HANDLER
+ * ================================================================================
+ * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.services.service.utils;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import org.onap.dcaegen2.services.utils.HttpRequestDetails;
+import org.onap.dcaegen2.services.utils.ImmutableHttpRequestDetails;
+import org.onap.dcaegen2.services.utils.RequestVerbs;
+
+
+public class HttpRequestDetailsTest {
+
+    private static HttpRequestDetails testObject;
+
+    private static final String AAI_PATH = "aaiPathTest";
+    private static final RequestVerbs HTTP_VERB = RequestVerbs.GET;
+    private static final String QUERY_KEY1 = "queryKey1";
+    private static final String QUERY_VALUE1 = "queryValue1";
+    private static final String HEADERS_KEY1 = "headersKey1";
+    private static final String HEADERS_VALUE1 = "headersValue1";
+
+    @BeforeAll
+    public static void init() {
+        testObject = ImmutableHttpRequestDetails.builder()
+                .aaiAPIPath(AAI_PATH)
+                .requestVerb(HTTP_VERB)
+                .putQueryParameters(QUERY_KEY1,QUERY_VALUE1)
+                .putHeaders(HEADERS_KEY1,HEADERS_VALUE1)
+                .build();
+    }
+
+    @Test
+    public void testGetters_success() {
+        Assertions.assertEquals(AAI_PATH, testObject.aaiAPIPath());
+        Assertions.assertEquals(HEADERS_VALUE1, testObject.headers().get(HEADERS_KEY1));
+        Assertions.assertEquals(QUERY_VALUE1, testObject.queryParameters().get(QUERY_KEY1));
+        Assertions.assertEquals(RequestVerbs.GET, testObject.requestVerb());
+    }
+}
  * ============LICENSE_END=========================================================
  */
 
-package services.service.utils;
+package org.onap.dcaegen2.services.service.utils;
 
 import org.apache.http.HttpStatus;
 import org.junit.Test;
-import services.utils.HttpUtils;
+import org.onap.dcaegen2.services.utils.HttpUtils;
 
 import static junit.framework.TestCase.assertFalse;
 import static junit.framework.TestCase.assertTrue;
diff --git a/prh-aai-client/src/test/java/services/service/AAIExtendedHttpClientImplTest.java b/prh-aai-client/src/test/java/services/service/AAIExtendedHttpClientImplTest.java
deleted file mode 100644 (file)
index c46e034..0000000
+++ /dev/null
@@ -1,84 +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 services.service;
-
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import java.io.IOException;
-import java.lang.reflect.Field;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Optional;
-import org.apache.http.client.ResponseHandler;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import services.config.AAIHttpClientConfiguration;
-
-public class AAIExtendedHttpClientImplTest {
-
-    private AAIExtendedHttpClientImpl testedObject;
-    private AAIHttpClientConfiguration aaiHttpClientConfigurationMock = mock(AAIHttpClientConfiguration.class);
-    private CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class);
-
-    @Before
-    public void init() throws NoSuchFieldException, IllegalAccessException {
-        when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("hostTest");
-        when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https");
-        when(aaiHttpClientConfigurationMock.aaiHostPortNumber()).thenReturn(1234);
-        testedObject = new AAIExtendedHttpClientImpl(aaiHttpClientConfigurationMock);
-        setField();
-    }
-
-    @Test
-    public void getExtendedDetails_success() throws IOException {
-        String expectedResult = "getExtendedDetailsOK";
-        Map<String, String> queryParams = new HashMap<>();
-        queryParams.put("key1", "value1");
-        Map<String, String> headers = new HashMap<>();
-        headers.put("headerKey", "headerValue");
-
-        when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))).
-                thenReturn(Optional.of(expectedResult));
-        String actualResult = testedObject.getExtendedDetails("testPath", queryParams, headers);
-        Assert.assertEquals(expectedResult, actualResult);
-    }
-
-    @Test
-    public void getExtendedDetails_returnsNull() throws IOException {
-        when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))).
-                thenReturn(Optional.empty());
-        String actualResult = testedObject.getExtendedDetails("testPath", new HashMap<>(), new HashMap<>());
-        Assert.assertNull(actualResult);
-    }
-
-    private void setField() throws NoSuchFieldException, IllegalAccessException {
-        Field field = testedObject.getClass().getDeclaredField("closeableHttpClient");
-        field.setAccessible(true);
-        field.set(testedObject, closeableHttpClientMock);
-    }
-
-
-}
diff --git a/prh-aai-client/src/test/java/services/service/AAIHttpClientImplTest.java b/prh-aai-client/src/test/java/services/service/AAIHttpClientImplTest.java
deleted file mode 100644 (file)
index 242e56d..0000000
+++ /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 services.service;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import services.config.AAIHttpClientConfiguration;
-import services.service.utils.HTTPConfiguration;
-
-import static org.junit.Assert.assertNotNull;
-
-
-public class AAIHttpClientImplTest {
-
-    private AAIHttpClientImpl aaiHttpClientImpl;
-    private AAIHttpClientConfiguration aaiHttpClientConfiguration;
-
-
-    @Before
-    public void setup() {
-        aaiHttpClientConfiguration = new HTTPConfiguration();
-        aaiHttpClientImpl  = new AAIHttpClientImpl(aaiHttpClientConfiguration);
-    }
-
-    @After
-    public void teaDown() {
-        aaiHttpClientImpl = null;
-    }
-
-    @Test
-    public void getAAIHttpClientObject_shouldNotBeNull() {
-        aaiHttpClientImpl.getAAIHttpClient();
-        assertNotNull(aaiHttpClientImpl.getAAIHttpClient());
-    }
-
-}
-
diff --git a/prh-aai-client/src/test/java/services/service/utils/HTTPConfiguration.java b/prh-aai-client/src/test/java/services/service/utils/HTTPConfiguration.java
deleted file mode 100644 (file)
index 82f656d..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * PNF-REGISTRATION-HANDLER
- * ================================================================================
- * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package services.service.utils;
-
-import services.config.AAIHttpClientConfiguration;
-
-public class HTTPConfiguration extends AAIHttpClientConfiguration {
-
-    private static final String AAI_HOST_NAME = "1.2.3.4";
-    private static final Integer AAI_HOST_PORT_NUMBER = 1234;
-    private static final String AAI_HOST_PROTOCOL = "https";
-    private static final String AAI_USER_NAME = "PRH";
-    private static final String AAI_USER_PASS = "PRH";
-
-    @Override
-    public String aaiHost() {
-        return AAI_HOST_NAME;
-    }
-
-    @Override
-    public Integer aaiHostPortNumber() {
-        return AAI_HOST_PORT_NUMBER;
-    }
-
-    @Override
-    public String aaiProtocol() {
-        return AAI_HOST_PROTOCOL;
-    }
-
-    @Override
-    public String aaiUserName() {
-        return AAI_USER_NAME;
-    }
-
-    @Override
-    public String aaiUserPassword() {
-        return AAI_USER_PASS;
-    }
-
-    @Override
-    public Boolean aaiIgnoreSSLCertificateErrors() {
-        return true;
-    }
-}
\ No newline at end of file