summaryrefslogtreecommitdiffstats
path: root/feature-simulators
diff options
context:
space:
mode:
Diffstat (limited to 'feature-simulators')
-rw-r--r--feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java184
-rw-r--r--feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java111
2 files changed, 156 insertions, 139 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 44700890..5e7861d2 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
@@ -22,11 +22,10 @@ package org.onap.policy.drools.simulators;
import java.io.IOException;
import java.util.Map;
+import java.util.concurrent.BlockingQueue;
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;
import javax.ws.rs.Consumes;
@@ -44,96 +43,111 @@ 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;
+ 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 consumer ID.
+ *
+ * @param timeout timeout value
+ * @param topicName the dmaap topic
+ * @param httpResponse http response object
+ * @return topic or error message
+ */
+ @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";
+ }
- @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;
+ }
- if (currentRespCode < 200 || currentRespCode >= 300)
- {
- return "You got response code: " + currentRespCode;
- }
+ if (queues.containsKey(topicName)) {
+ return getNextMessageFromQueue(timeout, topicName);
+ }
+ else if (timeout > 0) {
+ return waitForNextMessageFromQueue(timeout, topicName);
+ }
+ return "No topic";
+ }
- if (queues.containsKey(topicName)) {
- return getNextMessageFromQueue(timeout, topicName);
- }
- else if (timeout > 0) {
- 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 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;
+ }
+ return response;
+ }
+ } catch (InterruptedException e) {
+ logger.debug("error in DMaaP simulator", e);
+ Thread.currentThread().interrupt();
+ }
+ return "No topic";
+ }
- 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;
- }
- return response;
- }
- } catch (InterruptedException e) {
- logger.debug("error in DMaaP simulator", e);
- Thread.currentThread().interrupt();
- }
- return "No topic";
- }
+ /**
+ * Post to a topic.
+ *
+ * @param topicName name of the topic
+ * @param body message
+ * @return empty string
+ */
+ @POST
+ @Path("/{topicName}")
+ @Consumes(MediaType.TEXT_PLAIN)
+ public String publish(@PathParam("topicName") String topicName, String body) {
+ BlockingQueue<String> queue = queues.computeIfAbsent(topicName, entry -> new LinkedBlockingQueue<>());
- @POST
- @Path("/{topicName}")
- @Consumes(MediaType.TEXT_PLAIN)
- 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);
- }
+ if (!queue.offer(body)) {
+ logger.warn("error on topic {}, failed to place body {} on queue", topicName, body);
+ }
- return "";
- }
+ return "";
+ }
- @POST
- @Path("/setStatus")
- public String setStatus(@QueryParam("statusCode") int statusCode) {
- setResponseCode(statusCode);
- return "Status code set";
- }
+ @POST
+ @Path("/setStatus")
+ public String setStatus(@QueryParam("statusCode") int statusCode) {
+ 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;
- }
+ /**
+ * Static method to set static response code, synchronized for multiple possible uses.
+ *
+ * @param incomingResponseCode the response code to set
+ */
+ private static synchronized void setResponseCode(final int incomingResponseCode) {
+ responseCode = incomingResponseCode;
+ }
}
diff --git a/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java b/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java
index 1a19284b..2a849525 100644
--- a/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java
+++ b/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java
@@ -44,6 +44,9 @@ public class DMaaPSimulatorTest {
private static final int DMAAPSIM_SERVER_PORT = 6670;
+ /**
+ * Setup the simulator.
+ */
@BeforeClass
public static void setUpSimulator() {
LoggerUtil.setLevel("ROOT", "INFO");
@@ -71,8 +74,8 @@ public class DMaaPSimulatorTest {
int timeout = 1000;
Pair<Integer, String> response = dmaapGet("myTopicNoData", timeout);
assertNotNull(response);
- assertNotNull(response.a);
- assertEquals("No topic", response.b);
+ assertNotNull(response.first);
+ assertEquals("No topic", response.second);
}
@Test
@@ -81,13 +84,13 @@ public class DMaaPSimulatorTest {
String testData = "This is some test data";
Pair<Integer, String> response = dmaapPost(myTopic, testData);
assertNotNull(response);
- assertNotNull(response.a);
- assertNotNull(response.b);
+ assertNotNull(response.first);
+ assertNotNull(response.second);
response = dmaapGet(myTopic, 1000);
assertNotNull(response);
- assertNotNull(response.a);
- assertEquals(testData, response.b);
+ assertNotNull(response.first);
+ assertEquals(testData, response.second);
}
@Test
@@ -96,118 +99,118 @@ public class DMaaPSimulatorTest {
String myTopic = "myTopicMultiPost";
Pair<Integer, String> response = dmaapPost(myTopic, data[0]);
assertNotNull(response);
- assertNotNull(response.a);
- assertNotNull(response.b);
+ assertNotNull(response.first);
+ assertNotNull(response.second);
response = dmaapPost(myTopic, data[1]);
assertNotNull(response);
- assertNotNull(response.a);
- assertNotNull(response.b);
+ assertNotNull(response.first);
+ assertNotNull(response.second);
response = dmaapPost(myTopic, data[2]);
assertNotNull(response);
- assertNotNull(response.a);
- assertNotNull(response.b);
+ assertNotNull(response.first);
+ assertNotNull(response.second);
response = dmaapGet(myTopic, 1000);
assertNotNull(response);
- assertNotNull(response.a);
- assertEquals(data[0], response.b);
+ assertNotNull(response.first);
+ assertEquals(data[0], response.second);
response = dmaapGet(myTopic, 1000);
assertNotNull(response);
- assertNotNull(response.a);
- assertEquals(data[1], response.b);
+ assertNotNull(response.first);
+ assertEquals(data[1], response.second);
response = dmaapGet(myTopic, 1000);
assertNotNull(response);
- assertNotNull(response.a);
- assertEquals(data[2], response.b);
+ assertNotNull(response.first);
+ assertEquals(data[2], response.second);
}
@Test
public void testMultiTopic() {
String[][] data = {{"Topic one message one", "Topic one message two"},
- {"Topic two message one", "Topic two message two"}};
+ {"Topic two message one", "Topic two message two"}};
String[] topics = {"topic1", "topic2"};
Pair<Integer, String> response = dmaapPost(topics[0], data[0][0]);
assertNotNull(response);
- assertNotNull(response.a);
- assertNotNull(response.b);
+ assertNotNull(response.first);
+ assertNotNull(response.second);
response = dmaapGet(topics[0], 1000);
assertNotNull(response);
- assertNotNull(response.a);
- assertEquals(data[0][0], response.b);
+ assertNotNull(response.first);
+ assertEquals(data[0][0], response.second);
response = dmaapGet(topics[1], 1000);
assertNotNull(response);
- assertNotNull(response.a);
- assertEquals("No topic", response.b);
+ assertNotNull(response.first);
+ assertEquals("No topic", response.second);
response = dmaapPost(topics[1], data[1][0]);
assertNotNull(response);
- assertNotNull(response.a);
- assertNotNull(response.b);
+ assertNotNull(response.first);
+ assertNotNull(response.second);
response = dmaapPost(topics[1], data[1][1]);
assertNotNull(response);
- assertNotNull(response.a);
- assertNotNull(response.b);
+ assertNotNull(response.first);
+ assertNotNull(response.second);
response = dmaapPost(topics[0], data[0][1]);
assertNotNull(response);
- assertNotNull(response.a);
- assertNotNull(response.b);
+ assertNotNull(response.first);
+ assertNotNull(response.second);
response = dmaapGet(topics[1], 1000);
assertNotNull(response);
- assertNotNull(response.a);
- assertEquals(data[1][0], response.b);
+ assertNotNull(response.first);
+ assertEquals(data[1][0], response.second);
response = dmaapGet(topics[0], 1000);
assertNotNull(response);
- assertNotNull(response.a);
- assertEquals(data[0][1], response.b);
+ assertNotNull(response.first);
+ assertEquals(data[0][1], response.second);
response = dmaapGet(topics[1], 1000);
assertNotNull(response);
- assertNotNull(response.a);
- assertEquals(data[1][1], response.b);
+ assertNotNull(response.first);
+ assertEquals(data[1][1], response.second);
response = dmaapGet(topics[0], 1000);
assertNotNull(response);
- assertNotNull(response.a);
- assertEquals("No Data", response.b);
+ assertNotNull(response.first);
+ assertEquals("No Data", response.second);
}
@Test
public void testResponseCode() {
Pair<Integer, String> response = dmaapPost("myTopic", "myTopicData");
assertNotNull(response);
- assertNotNull(response.a);
- assertNotNull(response.b);
+ assertNotNull(response.first);
+ assertNotNull(response.second);
response = setStatus(503);
assertNotNull(response);
- assertNotNull(response.a);
- assertNotNull(response.b);
+ assertNotNull(response.first);
+ assertNotNull(response.second);
response = dmaapGet("myTopic", 500);
assertNotNull(response);
- assertEquals(503, response.a.intValue());
- assertEquals("You got response code: 503", response.b);
+ assertEquals(503, response.first.intValue());
+ assertEquals("You got response code: 503", response.second);
response = setStatus(202);
assertNotNull(response);
- assertNotNull(response.a);
- assertNotNull(response.b);
+ assertNotNull(response.first);
+ assertNotNull(response.second);
response = dmaapGet("myTopic", 500);
assertNotNull(response);
- assertEquals(202, response.a.intValue());
- assertEquals("myTopicData", response.b);
+ assertEquals(202, response.first.intValue());
+ assertEquals("myTopicData", response.second);
}
private static Pair<Integer, String> dmaapGet(String topic, int timeout) {
@@ -345,12 +348,12 @@ public class DMaaPSimulatorTest {
}
private static class Pair<A, B> {
- public final A a;
- public final B b;
+ public final A first;
+ public final B second;
- public Pair(A a, B b) {
- this.a = a;
- this.b = b;
+ public Pair(A first, B second) {
+ this.first = first;
+ this.second = second;
}
}
}