aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgrabinsk <maciej.grabinski@nokia.com>2019-09-18 14:59:44 +0200
committergrabinsk <maciej.grabinski@nokia.com>2019-09-18 15:23:48 +0200
commite3b9b0d3162e8eba5df9903f03a4741ffb1115c7 (patch)
tree6dc981f2f563ac799dcbd405a4c67ea0c93f9a42
parente288bedf6a7b83c6127cbb1bc4281084cdbc218e (diff)
Prevent reading twice from ResponseEntity stream, since this caused IOErrors.
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>
-rw-r--r--bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/JsonUtilForPnfCorrelationId.java4
-rw-r--r--bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java26
-rw-r--r--bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/JsonUtilForPnfCorrelationIdTest.java9
-rw-r--r--bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClientTest.java8
4 files changed, 24 insertions, 23 deletions
diff --git a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/JsonUtilForPnfCorrelationId.java b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/JsonUtilForPnfCorrelationId.java
index 8010ce62ab..a932e4ac50 100644
--- a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/JsonUtilForPnfCorrelationId.java
+++ b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/JsonUtilForPnfCorrelationId.java
@@ -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<>();
diff --git a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java
index 02303a6b23..bd1a45c64d 100644
--- a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java
+++ b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClient.java
@@ -23,12 +23,14 @@
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) {
diff --git a/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/JsonUtilForPnfCorrelationIdTest.java b/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/JsonUtilForPnfCorrelationIdTest.java
index 8741208d26..4edee24531 100644
--- a/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/JsonUtilForPnfCorrelationIdTest.java
+++ b/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/JsonUtilForPnfCorrelationIdTest.java
@@ -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();
+ }
}
diff --git a/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClientTest.java b/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClientTest.java
index 19e08d9d59..cccfe0c762 100644
--- a/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClientTest.java
+++ b/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClientTest.java
@@ -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);