2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.comm;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
28 import java.util.Arrays;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.atomic.AtomicBoolean;
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
35 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
36 import org.onap.policy.models.pdp.concepts.PdpStatus;
38 public class MultiPdpStatusListenerTest {
39 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
40 private static final String TOPIC = "my-topic";
41 private static final String ID1 = "request-1";
42 private static final String ID2 = "request-2";
43 private static final List<String> ID_LIST = Arrays.asList(ID1, ID2);
45 private MultiPdpStatusListener listener;
46 private PdpStatus status;
49 public void testMultiPdpStatusListenerString() throws Exception {
50 listener = new MyListener(ID1);
51 assertEquals(Arrays.asList(ID1).toString(), listener.getUnseenIds().toString());
53 // an ID is in the queue - not done yet
54 assertFalse(doWait(0));
58 public void testMultiPdpStatusListenerCollectionOfString() throws Exception {
59 List<String> lst = ID_LIST;
61 listener = new MyListener(lst);
62 assertEquals(lst.toString(), listener.getUnseenIds().toString());
64 // an ID is in the queue - not done yet
65 assertFalse(doWait(0));
68 * Try with an empty list - should already be complete.
70 listener = new MyListener(new LinkedList<>());
71 assertTrue(listener.getUnseenIds().isEmpty());
72 assertTrue(doWait(0));
76 public void testGetUnseenIds() {
77 List<String> lst = ID_LIST;
79 listener = new MyListener(lst);
80 assertEquals(lst.toString(), listener.getUnseenIds().toString());
82 // receive message from one PDP
83 status = new PdpStatus();
84 status.setResponse(makeResponse(ID2));
85 listener.onTopicEvent(INFRA, TOPIC, status);
86 assertEquals(Arrays.asList(ID1).toString(), listener.getUnseenIds().toString());
88 // receive message from the other PDP
89 status = new PdpStatus();
90 status.setResponse(makeResponse(ID1));
91 listener.onTopicEvent(INFRA, TOPIC, status);
92 assertTrue(listener.getUnseenIds().isEmpty());
96 public void testAwait() throws Exception {
97 // try with an empty list - should already be complete
98 listener = new MyListener(new LinkedList<>());
99 assertTrue(doWait(0));
101 // try it with something in the list
102 listener = new MyListener(ID_LIST);
103 assertFalse(doWait(0));
105 // process a message from one PDP - wait should block the entire time
106 status = new PdpStatus();
107 status.setResponse(makeResponse(ID1));
108 listener.onTopicEvent(INFRA, TOPIC, status);
109 long tbeg = System.currentTimeMillis();
110 assertFalse(doWait(50));
111 assertTrue(System.currentTimeMillis() - tbeg >= 49);
113 // process a message from the other PDP - wait should NOT block
114 status = new PdpStatus();
115 status.setResponse(makeResponse(ID2));
116 listener.onTopicEvent(INFRA, TOPIC, status);
117 tbeg = System.currentTimeMillis();
118 assertTrue(doWait(4000));
119 assertTrue(System.currentTimeMillis() - tbeg < 3000);
123 public void testOnTopicEvent() throws Exception {
124 listener = new MyListener(ID_LIST);
127 assertFalse(doWait(0));
129 // process a message - still not done as have another ID to go
130 status = new PdpStatus();
131 status.setResponse(makeResponse(ID1));
132 listener.onTopicEvent(INFRA, TOPIC, status);
133 assertFalse(doWait(0));
135 // process a message from the same PDP - still not done
136 status = new PdpStatus();
137 status.setResponse(makeResponse(ID1));
138 listener.onTopicEvent(INFRA, TOPIC, status);
139 assertFalse(doWait(0));
141 // process another message - now we're done
142 status = new PdpStatus();
143 status.setResponse(makeResponse(ID2));
144 listener.onTopicEvent(INFRA, TOPIC, status);
145 assertTrue(doWait(0));
147 // handleEvent throws an exception - doWait does not return true
148 listener = new MyListener(ID1) {
150 protected String handleEvent(CommInfrastructure infra, String topic, PdpStatus message) {
151 throw new RuntimeException("expected exception");
154 status = new PdpStatus();
155 status.setResponse(makeResponse(ID1));
156 listener.onTopicEvent(INFRA, TOPIC, status);
157 assertFalse(doWait(0));
159 // handleEvent returns null - doWait does not return true
160 listener = new MyListener(ID1) {
162 protected String handleEvent(CommInfrastructure infra, String topic, PdpStatus message) {
166 status = new PdpStatus();
167 status.setResponse(makeResponse(ID1));
168 listener.onTopicEvent(INFRA, TOPIC, status);
169 assertFalse(doWait(0));
173 * Waits for the listener to complete. Spawns a background thread to do the waiting so
174 * we can limit how long we wait.
176 * @param millisec milliseconds to wait
177 * @return {@code true} if the wait completed successfully, {@code false} otherwise
178 * @throws InterruptedException if this thread is interrupted while waiting for the
179 * background thread to complete
181 private boolean doWait(long millisec) throws InterruptedException {
182 AtomicBoolean done = new AtomicBoolean(false);
184 Thread thread = new Thread() {
188 done.set(listener.await(millisec, TimeUnit.MILLISECONDS));
190 } catch (InterruptedException expected) {
204 * Makes a response for the given request ID.
206 * @param id ID of the request
207 * @return a new response
209 private PdpResponseDetails makeResponse(String id) {
210 PdpResponseDetails resp = new PdpResponseDetails();
211 resp.setResponseTo(id);
216 private static class MyListener extends MultiPdpStatusListener {
218 public MyListener(String id) {
222 public MyListener(List<String> lst) {
227 protected String handleEvent(CommInfrastructure infra, String topic, PdpStatus message) {
228 return (message.getResponse().getResponseTo());