aboutsummaryrefslogtreecommitdiffstats
path: root/feature-simulators/src
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-08-14 17:31:50 -0400
committerJim Hahn <jrh3@att.com>2019-08-15 11:23:31 -0400
commit59e9b9a8b56d563814ef21a23716959f772f9194 (patch)
treef152aea1578a82737501f56916ca07d8e7889d18 /feature-simulators/src
parenta156cf3cbad6512510ae9a02a13c0408f901c734 (diff)
Fix more sonar issues in drools-pdp
Addressed issues of cyclomatic complexity and deep nesting by refactoring code into separate methods. In some cases, had to refactor the code into nested classes to avoid passing too many parameters to the newly extracted methods. Addressed issue "too many conditionals" by breaking conditionals apart. Addressed issue "Remove usage of generic wildcard type" by eliminating "? extends" from return values. Addressed issue "Remove this use of 'Thread.sleep()'" in junit tests by introducing latches or using Awaitility. Note: this won't build until ApiUtils has been merged. Change-Id: I0d5596b4cb918a36bc22f426f426bd238195b458 Issue-ID: POLICY-1968 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'feature-simulators/src')
-rw-r--r--feature-simulators/src/main/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRs.java7
-rw-r--r--feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRsTest.java14
2 files changed, 15 insertions, 6 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 8b653aae..dd0fb7b8 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
@@ -99,7 +99,7 @@ public class DMaaPSimulatorJaxRs {
return response;
}
- private String waitForNextMessageFromQueue(int timeout, String topicName) {
+ protected String waitForNextMessageFromQueue(int timeout, String topicName) {
try {
sleep(timeout);
if (queues.containsKey(topicName)) {
@@ -129,10 +129,7 @@ public class DMaaPSimulatorJaxRs {
@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);
- }
+ queue.offer(body);
return "";
}
diff --git a/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRsTest.java b/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRsTest.java
index 7200bdce..b1275004 100644
--- a/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRsTest.java
+++ b/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRsTest.java
@@ -21,6 +21,7 @@
package org.onap.policy.drools.simulators;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doThrow;
import java.io.IOException;
@@ -30,6 +31,7 @@ import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;
import org.junit.After;
import org.junit.Before;
@@ -130,6 +132,16 @@ public class DMaaPSimulatorJaxRsTest {
@Test
public void testWaitForNextMessageFromQueue() throws InterruptedException {
+ CountDownLatch waitCalled = new CountDownLatch(1);
+
+ sim = new DMaaPSimulatorJaxRs() {
+ @Override
+ protected String waitForNextMessageFromQueue(int timeout, String topicName) {
+ waitCalled.countDown();
+ return super.waitForNextMessageFromQueue(timeout, topicName);
+ }
+ };
+
BlockingQueue<String> queue = new LinkedBlockingQueue<>();
CountDownLatch latch1 = backgroundSubscribe(queue);
@@ -143,7 +155,7 @@ public class DMaaPSimulatorJaxRsTest {
* Must pause to prevent the topic from being created before subscribe() is
* invoked.
*/
- Thread.sleep(LONG_TIMEOUT_MS / 3);
+ assertTrue(waitCalled.await(1, TimeUnit.SECONDS));
// only publish one message
sim.publish(TOPIC, MESSAGE);