Prevent reading twice from ResponseEntity stream, since this caused IOErrors. 15/95915/1
authorgrabinsk <maciej.grabinski@nokia.com>
Wed, 18 Sep 2019 12:59:44 +0000 (14:59 +0200)
committergrabinsk <maciej.grabinski@nokia.com>
Wed, 18 Sep 2019 13:23:48 +0000 (15:23 +0200)
Also moving empty response checks to parseJsonToGelAllPnfCorrelationId

Issue-ID: SO-2349
Signed-off-by: grabinsk <maciej.grabinski@nokia.com>
Change-Id: I316856d56106a1ae715812628695406f4147765d
Signed-off-by: grabinsk <maciej.grabinski@nokia.com>
bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/JsonUtilForPnfCorrelationId.java
bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java
bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/JsonUtilForPnfCorrelationIdTest.java
bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClientTest.java

index 8010ce6..a932e4a 100644 (file)
@@ -28,6 +28,7 @@ import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
 import java.util.Spliterator;
@@ -41,6 +42,9 @@ public final class JsonUtilForPnfCorrelationId {
     }
 
     static List<String> parseJsonToGelAllPnfCorrelationId(String json) {
+        if (json == null || json.isEmpty()) {
+            return Collections.emptyList();
+        }
         JsonElement je = new JsonParser().parse(json);
         JsonArray array = je.getAsJsonArray();
         List<String> list = new ArrayList<>();
index 02303a6..bd1a45c 100644 (file)
 package org.onap.so.bpmn.infrastructure.pnf.dmaap;
 
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import javax.ws.rs.core.UriBuilder;
 import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.impl.client.HttpClientBuilder;
@@ -42,6 +44,7 @@ import org.onap.so.client.aai.entities.uri.AAIResourceUri;
 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
 import org.onap.so.client.aai.AAIResourcesClient;
 import org.onap.so.client.aai.AAIObjectType;
+import static org.onap.so.bpmn.infrastructure.pnf.dmaap.JsonUtilForPnfCorrelationId.*;
 
 @Component
 public class PnfEventReadyDmaapClient implements DmaapClient {
@@ -129,15 +132,10 @@ public class PnfEventReadyDmaapClient implements DmaapClient {
             try {
                 logger.debug("dmaap listener starts listening pnf ready dmaap topic");
                 HttpResponse response = httpClient.execute(getRequest);
-                List<String> idList = getPnfCorrelationIdListFromResponse(response);
-
-                // idList is never null
-                if (!idList.isEmpty()) {
-                    // send only body of response
-                    registerClientResponse(idList.get(0), EntityUtils.toString(response.getEntity(), "UTF-8"));
-                }
-
-                if (idList != null) {
+                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+                    String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
+                    List<String> idList = parseJsonToGelAllPnfCorrelationId(responseString);
+                    idList.stream().findFirst().ifPresent(id -> registerClientResponse(id, responseString));
                     idList.forEach(this::informAboutPnfReadyIfPnfCorrelationIdFound);
                 }
             } catch (IOException e) {
@@ -147,16 +145,6 @@ public class PnfEventReadyDmaapClient implements DmaapClient {
             }
         }
 
-        private List<String> getPnfCorrelationIdListFromResponse(HttpResponse response) throws IOException {
-            if (response.getStatusLine().getStatusCode() == 200) {
-                String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
-                if (responseString != null) {
-                    return JsonUtilForPnfCorrelationId.parseJsonToGelAllPnfCorrelationId(responseString);
-                }
-            }
-            return Collections.emptyList();
-        }
-
         private void informAboutPnfReadyIfPnfCorrelationIdFound(String pnfCorrelationId) {
             Runnable runnable = unregister(pnfCorrelationId);
             if (runnable != null) {
index 8741208..4edee24 100644 (file)
@@ -63,4 +63,13 @@ public class JsonUtilForPnfCorrelationIdTest {
         assertThat(expectedResult).isEmpty();
     }
 
+    @Test
+    public void shouldReturnEmptyListWhenInputIsNull() {
+        assertThat(JsonUtilForPnfCorrelationId.parseJsonToGelAllPnfCorrelationId(null)).isEmpty();
+    }
+
+    @Test
+    public void shouldReturnEmptyListWhenInputIsEmpty() {
+        assertThat(JsonUtilForPnfCorrelationId.parseJsonToGelAllPnfCorrelationId("")).isEmpty();
+    }
 }
index 19e08d9..cccfe0c 100644 (file)
@@ -30,8 +30,8 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyZeroInteractions;
 import static org.mockito.Mockito.when;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Field;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -41,7 +41,7 @@ import org.apache.http.HttpResponse;
 import org.apache.http.ProtocolVersion;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
-import org.apache.http.entity.StringEntity;
+import org.apache.http.entity.InputStreamEntity;
 import org.apache.http.message.BasicHttpResponse;
 import org.junit.Before;
 import org.junit.Test;
@@ -182,8 +182,8 @@ public class PnfEventReadyDmaapClientTest {
         threadRunFlag.setAccessible(false);
     }
 
-    private HttpResponse createResponse(String json) throws UnsupportedEncodingException {
-        HttpEntity entity = new StringEntity(json);
+    private HttpResponse createResponse(String json) {
+        HttpEntity entity = new InputStreamEntity(new ByteArrayInputStream(json.getBytes()));
         ProtocolVersion protocolVersion = new ProtocolVersion("", 1, 1);
         HttpResponse response = new BasicHttpResponse(protocolVersion, 1, "");
         response.setEntity(entity);