summaryrefslogtreecommitdiffstats
path: root/feature-simulators/src/main
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@ericsson.com>2018-03-20 17:28:29 +0000
committerliamfallon <liam.fallon@ericsson.com>2018-03-21 14:29:10 +0000
commit5c205833e4b2e8f6cfe938641cdbceaf680da92b (patch)
treee36d7973d7571d87bad00bb59e68d21d3ab6e26e /feature-simulators/src/main
parent0f981f6ceee254df09688b451bd32a2da6d1a700 (diff)
Fix bugs identified by Sonar on drools-pdp
Three bugs fixed: 1. Set of static variable in DMaaPSimulatorJaxR 2. Use of opssibly null variable "output" in RepositoryAudit 3. Unreachable statement error in RepositoryAudit Change-Id: I72e028cfc51a82250afd02fb4109d3dea08072dc Issue-ID: POLICY-691 Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'feature-simulators/src/main')
-rw-r--r--feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java122
1 files changed, 68 insertions, 54 deletions
diff --git a/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java b/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java
index 2513a040..44700890 100644
--- a/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java
+++ b/feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java
@@ -25,6 +25,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
import java.util.concurrent.BlockingQueue;
import javax.servlet.http.HttpServletResponse;
@@ -43,83 +44,96 @@ import org.slf4j.LoggerFactory;
@Path("/events")
public class DMaaPSimulatorJaxRs {
-
private static final String NO_DATA_MSG = "No Data";
private static final Map<String, BlockingQueue<String>> queues = new ConcurrentHashMap<>();
private static final Logger logger = LoggerFactory.getLogger(DMaaPSimulatorJaxRs.class);
private static int responseCode = 200;
-
+
@GET
@Path("/{topicName}/{consumeGroup}/{consumerId}")
public String subscribe(@DefaultValue("0") @QueryParam("timeout") int timeout, @PathParam("topicName") String topicName,
- @Context final HttpServletResponse httpResponse) {
- int currentRespCode = responseCode;
- httpResponse.setStatus(currentRespCode);
- try {
- httpResponse.flushBuffer();
- } catch (IOException e) {
- logger.error("flushBuffer threw: ", e);
- return "Got an error";
- }
-
- if (currentRespCode < 200 || currentRespCode >= 300)
- {
- return "You got response code: " + currentRespCode;
- }
+ @Context final HttpServletResponse httpResponse) {
+ int currentRespCode = responseCode;
+ httpResponse.setStatus(currentRespCode);
+ try {
+ httpResponse.flushBuffer();
+ } catch (IOException e) {
+ logger.error("flushBuffer threw: ", e);
+ return "Got an error";
+ }
+
+ if (currentRespCode < 200 || currentRespCode >= 300)
+ {
+ return "You got response code: " + currentRespCode;
+ }
+
if (queues.containsKey(topicName)) {
- BlockingQueue<String> queue = queues.get(topicName);
- String response = NO_DATA_MSG;
- try {
- response = queue.poll(timeout, TimeUnit.MILLISECONDS);
- } catch (InterruptedException e) {
- logger.debug("error in DMaaP simulator", e);
- Thread.currentThread().interrupt();
- }
- if (response == null) {
- response = NO_DATA_MSG;
- }
- return response;
+ return getNextMessageFromQueue(timeout, topicName);
}
else if (timeout > 0) {
- try {
- Thread.sleep(timeout);
- if (queues.containsKey(topicName)) {
- BlockingQueue<String> queue = queues.get(topicName);
- String response = queue.poll();
- if (response == null) {
- response = NO_DATA_MSG;
- }
- return response;
+ return waitForNextMessageFromQueue(timeout, topicName);
+ }
+ return "No topic";
+ }
+
+ private String getNextMessageFromQueue(final int timeout, final String topicName) {
+ BlockingQueue<String> queue = queues.get(topicName);
+ String response = NO_DATA_MSG;
+ try {
+ response = queue.poll(timeout, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ logger.debug("error in DMaaP simulator", e);
+ Thread.currentThread().interrupt();
+ }
+ if (response == null) {
+ response = NO_DATA_MSG;
+ }
+ return response;
+ }
+
+ private String waitForNextMessageFromQueue(int timeout, String topicName) {
+ try {
+ Thread.sleep(timeout);
+ if (queues.containsKey(topicName)) {
+ BlockingQueue<String> queue = queues.get(topicName);
+ String response = queue.poll();
+ if (response == null) {
+ response = NO_DATA_MSG;
}
- } catch (InterruptedException e) {
- logger.debug("error in DMaaP simulator", e);
- Thread.currentThread().interrupt();
+ return response;
}
+ } catch (InterruptedException e) {
+ logger.debug("error in DMaaP simulator", e);
+ Thread.currentThread().interrupt();
}
return "No topic";
}
-
+
@POST
@Path("/{topicName}")
@Consumes(MediaType.TEXT_PLAIN)
- public String publish(@PathParam("topicName") String topicName, String body) {
- if (queues.containsKey(topicName)) {
- BlockingQueue<String> queue = queues.get(topicName);
- queue.offer(body);
- }
- else {
- BlockingQueue<String> queue = new LinkedBlockingQueue<>();
- queue.offer(body);
- queues.put(topicName, queue);
- }
+ public String publish(@PathParam("topicName") String topicName, String body) {
+ BlockingQueue<String> queue = queues.computeIfAbsent(topicName, entry -> new LinkedBlockingQueue<>());
+ if (!queue.offer(body)) {
+ logger.warn("error on topic {}, failed to place body {} on queue", topicName, body);
+ }
+
return "";
}
-
+
@POST
@Path("/setStatus")
public String setStatus(@QueryParam("statusCode") int statusCode) {
- responseCode = statusCode;
- return "Status code set";
+ setResponseCode(statusCode);
+ return "Status code set";
+ }
+
+ /**
+ * Static method to set static response code, synchronized for multiple possible uses
+ * @param incomingResponseCode
+ */
+ private static synchronized void setResponseCode(final int incomingResponseCode) {
+ responseCode = incomingResponseCode;
}
}