aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-03-05 17:19:33 -0500
committerJim Hahn <jrh3@att.com>2019-03-06 08:23:39 -0500
commit6fce4b823e85c8c454a5db4da76c5834fa010239 (patch)
treeff7ae68f4053e489757b8be4d205b7b90a684143 /main/src/test/java
parenta950874eb1ab8560fc86a50890c4bc8b84650d9e (diff)
Use request id instead of PDP name in listener
Modified the listener that receives messages from the PDPs to use the request id instead of the PDP name for tracking purposes. Modified the listener to make it more generic so any arbitrary ID can be used, depending on the context. Combined extractId into handleEvent. Corrected a comment. Change-Id: I6b7097c67d73f51b78ae4063341bcb118eb14bbc Issue-ID: POLICY-1542 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main/src/test/java')
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/comm/MultiPdpStatusListenerTest.java112
1 files changed, 83 insertions, 29 deletions
diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/MultiPdpStatusListenerTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/MultiPdpStatusListenerTest.java
index 2dc1aa2c..d0dba808 100644
--- a/main/src/test/java/org/onap/policy/pap/main/comm/MultiPdpStatusListenerTest.java
+++ b/main/src/test/java/org/onap/policy/pap/main/comm/MultiPdpStatusListenerTest.java
@@ -32,78 +32,79 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test;
import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
import org.onap.policy.pap.main.comm.MultiPdpStatusListener;
+import org.onap.policy.pdp.common.models.PdpResponseDetails;
import org.onap.policy.pdp.common.models.PdpStatus;
public class MultiPdpStatusListenerTest {
private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
private static final String TOPIC = "my-topic";
- private static final String NAME1 = "pdp_1";
- private static final String NAME2 = "pdp_2";
- private static final List<String> NAME_LIST = Arrays.asList(NAME1, NAME2);
+ private static final String ID1 = "request-1";
+ private static final String ID2 = "request-2";
+ private static final List<String> ID_LIST = Arrays.asList(ID1, ID2);
private MultiPdpStatusListener listener;
private PdpStatus status;
@Test
public void testMultiPdpStatusListenerString() throws Exception {
- listener = new MultiPdpStatusListener(NAME1);
- assertEquals(Arrays.asList(NAME1).toString(), listener.getUnseenPdpNames().toString());
+ listener = new MyListener(ID1);
+ assertEquals(Arrays.asList(ID1).toString(), listener.getUnseenIds().toString());
- // a name is in the queue - not done yet
+ // an ID is in the queue - not done yet
assertFalse(doWait(0));
}
@Test
public void testMultiPdpStatusListenerCollectionOfString() throws Exception {
- List<String> lst = NAME_LIST;
+ List<String> lst = ID_LIST;
- listener = new MultiPdpStatusListener(lst);
- assertEquals(lst.toString(), listener.getUnseenPdpNames().toString());
+ listener = new MyListener(lst);
+ assertEquals(lst.toString(), listener.getUnseenIds().toString());
- // a name is in the queue - not done yet
+ // an ID is in the queue - not done yet
assertFalse(doWait(0));
/*
* Try with an empty list - should already be complete.
*/
- listener = new MultiPdpStatusListener(new LinkedList<>());
- assertTrue(listener.getUnseenPdpNames().isEmpty());
+ listener = new MyListener(new LinkedList<>());
+ assertTrue(listener.getUnseenIds().isEmpty());
assertTrue(doWait(0));
}
@Test
- public void testGetUnseenPdpNames() {
- List<String> lst = NAME_LIST;
+ public void testGetUnseenIds() {
+ List<String> lst = ID_LIST;
- listener = new MultiPdpStatusListener(lst);
- assertEquals(lst.toString(), listener.getUnseenPdpNames().toString());
+ listener = new MyListener(lst);
+ assertEquals(lst.toString(), listener.getUnseenIds().toString());
// receive message from one PDP
status = new PdpStatus();
- status.setName(NAME2);
+ status.setResponse(makeResponse(ID2));
listener.onTopicEvent(INFRA, TOPIC, status);
- assertEquals(Arrays.asList(NAME1).toString(), listener.getUnseenPdpNames().toString());
+ assertEquals(Arrays.asList(ID1).toString(), listener.getUnseenIds().toString());
// receive message from the other PDP
status = new PdpStatus();
- status.setName(NAME1);
+ status.setResponse(makeResponse(ID1));
listener.onTopicEvent(INFRA, TOPIC, status);
- assertTrue(listener.getUnseenPdpNames().isEmpty());
+ assertTrue(listener.getUnseenIds().isEmpty());
}
@Test
public void testAwait() throws Exception {
// try with an empty list - should already be complete
- listener = new MultiPdpStatusListener(new LinkedList<>());
+ listener = new MyListener(new LinkedList<>());
assertTrue(doWait(0));
// try it with something in the list
- listener = new MultiPdpStatusListener(NAME_LIST);
+ listener = new MyListener(ID_LIST);
assertFalse(doWait(0));
// process a message from one PDP - wait should block the entire time
status = new PdpStatus();
- status.setName(NAME1);
+ status.setResponse(makeResponse(ID1));
listener.onTopicEvent(INFRA, TOPIC, status);
long tbeg = System.currentTimeMillis();
assertFalse(doWait(50));
@@ -111,7 +112,7 @@ public class MultiPdpStatusListenerTest {
// process a message from the other PDP - wait should NOT block
status = new PdpStatus();
- status.setName(NAME2);
+ status.setResponse(makeResponse(ID2));
listener.onTopicEvent(INFRA, TOPIC, status);
tbeg = System.currentTimeMillis();
assertTrue(doWait(4000));
@@ -120,28 +121,52 @@ public class MultiPdpStatusListenerTest {
@Test
public void testOnTopicEvent() throws Exception {
- listener = new MultiPdpStatusListener(NAME_LIST);
+ listener = new MyListener(ID_LIST);
// not done yet
assertFalse(doWait(0));
- // process a message - still not done as have another name to go
+ // process a message - still not done as have another ID to go
status = new PdpStatus();
- status.setName(NAME1);
+ status.setResponse(makeResponse(ID1));
listener.onTopicEvent(INFRA, TOPIC, status);
assertFalse(doWait(0));
// process a message from the same PDP - still not done
status = new PdpStatus();
- status.setName(NAME1);
+ status.setResponse(makeResponse(ID1));
listener.onTopicEvent(INFRA, TOPIC, status);
assertFalse(doWait(0));
// process another message - now we're done
status = new PdpStatus();
- status.setName(NAME2);
+ status.setResponse(makeResponse(ID2));
listener.onTopicEvent(INFRA, TOPIC, status);
assertTrue(doWait(0));
+
+ // handleEvent throws an exception - doWait does not return true
+ listener = new MyListener(ID1) {
+ @Override
+ protected String handleEvent(CommInfrastructure infra, String topic, PdpStatus message) {
+ throw new RuntimeException("expected exception");
+ }
+ };
+ status = new PdpStatus();
+ status.setResponse(makeResponse(ID1));
+ listener.onTopicEvent(INFRA, TOPIC, status);
+ assertFalse(doWait(0));
+
+ // handleEvent returns null - doWait does not return true
+ listener = new MyListener(ID1) {
+ @Override
+ protected String handleEvent(CommInfrastructure infra, String topic, PdpStatus message) {
+ return null;
+ }
+ };
+ status = new PdpStatus();
+ status.setResponse(makeResponse(ID1));
+ listener.onTopicEvent(INFRA, TOPIC, status);
+ assertFalse(doWait(0));
}
/**
@@ -174,4 +199,33 @@ public class MultiPdpStatusListenerTest {
return done.get();
}
+
+ /**
+ * Makes a response for the given request ID.
+ *
+ * @param id ID of the request
+ * @return a new response
+ */
+ private PdpResponseDetails makeResponse(String id) {
+ PdpResponseDetails resp = new PdpResponseDetails();
+ resp.setResponseTo(id);
+
+ return resp;
+ }
+
+ private static class MyListener extends MultiPdpStatusListener {
+
+ public MyListener(String id) {
+ super(id);
+ }
+
+ public MyListener(List<String> lst) {
+ super(lst);
+ }
+
+ @Override
+ protected String handleEvent(CommInfrastructure infra, String topic, PdpStatus message) {
+ return (message.getResponse().getResponseTo());
+ }
+ }
}