handleResponse refactor 15/51115/3
authorpwielebs <piotr.wielebski@nokia.com>
Mon, 11 Jun 2018 11:39:31 +0000 (13:39 +0200)
committerwasala <przemyslaw.wasala@nokia.com>
Tue, 12 Jun 2018 06:24:43 +0000 (08:24 +0200)
Due to logs consistency handleResponse method
was incorporated to the each of http producers.

Change-Id: I621d1795c376161273270f07ff3dd6caca2fc1d6
Issue-ID: DCAEGEN2-451
Signed-off-by: pwielebs <piotr.wielebski@nokia.com>
prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java
prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java
prh-commons/src/main/java/org/onap/dcaegen2/services/prh/model/CommonFunctions.java
prh-commons/src/test/java/org/onap/dcaegen2/services/prh/model/CommonFunctionsTest.java
prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImpl.java
prh-dmaap-client/src/test/java/org/onap/dcaegen2/services/prh/service/producer/ExtendedDmaapProducerHttpClientImplTest.java

index f9c2708..f85ac8a 100644 (file)
 
 package org.onap.dcaegen2.services.prh.service;
 
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpPatch;
 import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.client.utils.URIBuilder;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.util.EntityUtils;
 import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration;
 import org.onap.dcaegen2.services.prh.model.CommonFunctions;
 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
+import org.onap.dcaegen2.services.prh.model.utils.HttpUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -70,7 +74,7 @@ public class AAIProducerClient implements AAIExtendedHttpClient {
     public Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws URISyntaxException {
         return createRequest(consumerDmaapModel).flatMap(httpRequestBase -> {
             try {
-                return closeableHttpClient.execute(httpRequestBase, CommonFunctions::handleResponse);
+                return closeableHttpClient.execute(httpRequestBase, this::handleResponse);
             } catch (IOException e) {
                 logger.warn(EXCEPTION_MESSAGE, e);
                 return Optional.empty();
@@ -115,4 +119,20 @@ public class AAIProducerClient implements AAIExtendedHttpClient {
         return Base64.getEncoder().encodeToString((this.aaiUserName + ":" + this.aaiUserPassword)
                 .getBytes("UTF-8"));
     }
+
+    Optional<Integer> handleResponse(HttpResponse response) throws IOException {
+
+        final Integer responseCode = response.getStatusLine().getStatusCode();
+        logger.info("Status code of operation: {}", responseCode);
+        final HttpEntity responseEntity = response.getEntity();
+
+        if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
+            logger.trace("HTTP response successful.");
+            return Optional.of(responseCode);
+        } else {
+            String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
+            logger.warn("HTTP response not successful : {}", aaiResponse);
+            return Optional.of(responseCode);
+        }
+    }
 }
index 1ef9018..b7515ad 100644 (file)
 
 package org.onap.dcaegen2.services.prh.service;
 
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.StatusLine;
 import org.apache.http.client.ResponseHandler;
 import org.apache.http.client.methods.HttpPatch;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration;
+import org.onap.dcaegen2.services.prh.model.CommonFunctions;
 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest;
 
@@ -44,7 +49,7 @@ import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-public class AAIProducerClientTest {
+class AAIProducerClientTest {
 
     private static final Integer SUCCESS = 200;
     private static AAIProducerClient testedObject;
@@ -52,6 +57,11 @@ public class AAIProducerClientTest {
     private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class);
     private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
 
+    private final static HttpResponse httpResponseMock = mock(HttpResponse.class);
+    private final static HttpEntity httpEntityMock = mock(HttpEntity.class);
+    private final static StatusLine statusLineMock = mock(StatusLine.class);
+
+
 
     @BeforeAll
     static void setup() throws NoSuchFieldException, IllegalAccessException {
@@ -121,6 +131,26 @@ public class AAIProducerClientTest {
         assertEquals(expected, patch.getLastHeader("Authorization").toString());
     }
 
+    @Test
+    void handleResponse_shouldReturn200() throws IOException {
+        // When
+        when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
+        when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
+        when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_OK);
+        // Then
+        assertEquals(Optional.of(HttpStatus.SC_OK), testedObject.handleResponse(httpResponseMock));
+    }
+
+    @Test
+    void handleResponse_shouldReturn300() throws IOException {
+        // When
+        when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
+        when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
+        when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_BAD_REQUEST);
+        // Then
+        assertEquals(Optional.of(HttpStatus.SC_BAD_REQUEST), testedObject.handleResponse(httpResponseMock));
+    }
+
 
     private static void setField() throws NoSuchFieldException, IllegalAccessException {
         Field field = testedObject.getClass().getDeclaredField("closeableHttpClient");
index 634cfa9..c78146f 100644 (file)
@@ -22,41 +22,15 @@ package org.onap.dcaegen2.services.prh.model;
 
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.util.EntityUtils;
-import org.onap.dcaegen2.services.prh.model.utils.HttpUtils;
-import org.slf4j.LoggerFactory;
-import org.slf4j.Logger;
-import java.io.IOException;
-import java.util.Optional;
 
 
 public class CommonFunctions {
 
-    private static Logger logger = LoggerFactory.getLogger(CommonFunctions.class);
-
     private static Gson gson = new GsonBuilder().create();
 
-
     private CommonFunctions() {}
 
     public static String createJsonBody(ConsumerDmaapModel consumerDmaapModel) {
         return gson.toJson(consumerDmaapModel);
     }
-
-    public static Optional<Integer> handleResponse(HttpResponse response) throws IOException {
-        final Integer responseCode = response.getStatusLine().getStatusCode();
-        logger.info("Status code of operation: {}", responseCode);
-        final HttpEntity responseEntity = response.getEntity();
-
-        if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
-            logger.trace("HTTP response successful.");
-            return Optional.of(responseCode);
-        } else {
-            String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
-            logger.warn("HTTP response not successful : {}", aaiResponse);
-            return Optional.of(responseCode);
-        }
-    }
 }
index a2b077e..49b97ab 100644 (file)
@@ -37,11 +37,10 @@ import static org.mockito.Mockito.when;
 class CommonFunctionsTest {
     // Given
     private ConsumerDmaapModel model = new ConsumerDmaapModelForUnitTest();
-    private String expectedResult = "{\"pnfName\":\"NOKnhfsadhff\",\"ipv4\":\"256.22.33.155\",\"ipv6\":\"2001:0db8:85a3:0000:0000:8a2e:0370:7334\"}";
 
-    final static HttpResponse httpResponseMock = mock(HttpResponse.class);
-    final static HttpEntity httpEntityMock = mock(HttpEntity.class);
-    final static StatusLine statusLineMock = mock(StatusLine.class);
+    private final static HttpResponse httpResponseMock = mock(HttpResponse.class);
+    private final static HttpEntity httpEntityMock = mock(HttpEntity.class);
+    private final static StatusLine statusLineMock = mock(StatusLine.class);
 
     @BeforeAll
     static void setup() {
@@ -51,22 +50,7 @@ class CommonFunctionsTest {
 
     @Test
     void createJsonBody_shouldReturnJsonInString() {
+        String expectedResult = "{\"pnfName\":\"NOKnhfsadhff\",\"ipv4\":\"256.22.33.155\",\"ipv6\":\"2001:0db8:85a3:0000:0000:8a2e:0370:7334\"}";
         assertEquals(expectedResult, CommonFunctions.createJsonBody(model));
     }
-
-    @Test
-    void handleResponse_shouldReturn200() throws IOException {
-        // When
-        when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_OK);
-        // Then
-        assertEquals(Optional.of(HttpStatus.SC_OK), CommonFunctions.handleResponse(httpResponseMock));
-    }
-
-    @Test
-    void handleResponse_shouldReturn300() throws IOException {
-        // When
-        when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_BAD_REQUEST);
-        // Then
-        assertEquals(Optional.of(HttpStatus.SC_BAD_REQUEST), CommonFunctions.handleResponse(httpResponseMock));
-    }
 }
index b93c9c6..5704cd1 100644 (file)
 
 package org.onap.dcaegen2.services.prh.service.producer;
 
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.client.utils.URIBuilder;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.util.EntityUtils;
 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
 import org.onap.dcaegen2.services.prh.model.CommonFunctions;
 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
+import org.onap.dcaegen2.services.prh.model.utils.HttpUtils;
 import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -73,7 +77,7 @@ public class ExtendedDmaapProducerHttpClientImpl {
 
     private Optional<Integer> executeHttpClient(HttpRequestBase httpRequestBase) {
         try {
-            return closeableHttpClient.execute(httpRequestBase, CommonFunctions::handleResponse);
+            return closeableHttpClient.execute(httpRequestBase, this::handleResponse);
         } catch (IOException e) {
             logger.warn("Exception while executing HTTP request: ", e);
         }
@@ -109,4 +113,20 @@ public class ExtendedDmaapProducerHttpClientImpl {
         }
         return Optional.empty();
     }
+
+    Optional<Integer> handleResponse(HttpResponse response) throws IOException {
+
+        final Integer responseCode = response.getStatusLine().getStatusCode();
+        logger.info("Status code of operation: {}", responseCode);
+        final HttpEntity responseEntity = response.getEntity();
+
+        if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
+            logger.trace("HTTP response successful.");
+            return Optional.of(responseCode);
+        } else {
+            String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
+            logger.warn("HTTP response not successful : {}", aaiResponse);
+            return Optional.of(responseCode);
+        }
+    }
 }
\ No newline at end of file
index aa6810e..d9e7426 100644 (file)
 
 package org.onap.dcaegen2.services.prh.service.producer;
 
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.StatusLine;
 import org.apache.http.client.ResponseHandler;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.impl.client.CloseableHttpClient;
@@ -34,12 +38,13 @@ import java.io.IOException;
 import java.lang.reflect.Field;
 import java.util.Optional;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 
-public class ExtendedDmaapProducerHttpClientImplTest {
+class ExtendedDmaapProducerHttpClientImplTest {
 
     private static ExtendedDmaapProducerHttpClientImpl objectUnderTest;
     private static DmaapPublisherConfiguration configurationMock = mock(DmaapPublisherConfiguration.class);
@@ -48,9 +53,13 @@ public class ExtendedDmaapProducerHttpClientImplTest {
     private static Integer expectedResult;
     private static final Integer RESPONSE_SUCCESS = 200;
     private static final Integer RESPONSE_FAILURE = 404;
+    private final static HttpResponse httpResponseMock = mock(HttpResponse.class);
+    private final static HttpEntity httpEntityMock = mock(HttpEntity.class);
+    private final static StatusLine statusLineMock = mock(StatusLine.class);
+
 
     @BeforeAll
-    public static void init() throws NoSuchFieldException, IllegalAccessException {
+    static void init() throws NoSuchFieldException, IllegalAccessException {
         when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2");
         when(configurationMock.dmaapProtocol()).thenReturn("https");
         when(configurationMock.dmaapPortNumber()).thenReturn(1234);
@@ -64,7 +73,7 @@ public class ExtendedDmaapProducerHttpClientImplTest {
 
 
     @Test
-    public void getHttpResponsePost_success() throws IOException {
+    void getHttpResponsePost_success() throws IOException {
         expectedResult = RESPONSE_SUCCESS;
         when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class)))
             .thenReturn(Optional.of(expectedResult));
@@ -73,7 +82,7 @@ public class ExtendedDmaapProducerHttpClientImplTest {
     }
 
     @Test
-    public void getExtendedDetails_returnsFailure() throws IOException {
+    void getExtendedDetails_returnsFailure() throws IOException {
         expectedResult = RESPONSE_FAILURE;
         when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class)))
             .thenReturn(Optional.of(expectedResult));
@@ -81,6 +90,26 @@ public class ExtendedDmaapProducerHttpClientImplTest {
         Assertions.assertEquals(expectedResult, actualResult.get());
     }
 
+    @Test
+    void handleResponse_shouldReturn200() throws IOException {
+        // When
+        when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
+        when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
+        when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_OK);
+        // Then
+        assertEquals(Optional.of(HttpStatus.SC_OK), objectUnderTest.handleResponse(httpResponseMock));
+    }
+
+    @Test
+    void handleResponse_shouldReturn300() throws IOException {
+        // When
+        when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
+        when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
+        when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_BAD_REQUEST);
+        // Then
+        assertEquals(Optional.of(HttpStatus.SC_BAD_REQUEST), objectUnderTest.handleResponse(httpResponseMock));
+    }
+
     private static void setField() throws NoSuchFieldException, IllegalAccessException {
         Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient");
         field.setAccessible(true);