4f276a90601db6376259466839bd597512c25352
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / MultiPdpStatusListenerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.pap.main.comm;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27
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;
37
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);
44
45     private MultiPdpStatusListener listener;
46     private PdpStatus status;
47
48     @Test
49     public void testMultiPdpStatusListenerString() throws Exception {
50         listener = new MyListener(ID1);
51         assertEquals(Arrays.asList(ID1).toString(), listener.getUnseenIds().toString());
52
53         // an ID is in the queue - not done yet
54         assertFalse(doWait(0));
55     }
56
57     @Test
58     public void testMultiPdpStatusListenerCollectionOfString() throws Exception {
59         List<String> lst = ID_LIST;
60
61         listener = new MyListener(lst);
62         assertEquals(lst.toString(), listener.getUnseenIds().toString());
63
64         // an ID is in the queue - not done yet
65         assertFalse(doWait(0));
66
67         /*
68          * Try with an empty list - should already be complete.
69          */
70         listener = new MyListener(new LinkedList<>());
71         assertTrue(listener.getUnseenIds().isEmpty());
72         assertTrue(doWait(0));
73     }
74
75     @Test
76     public void testGetUnseenIds() {
77         List<String> lst = ID_LIST;
78
79         listener = new MyListener(lst);
80         assertEquals(lst.toString(), listener.getUnseenIds().toString());
81
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());
87
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());
93     }
94
95     @Test
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));
100
101         // try it with something in the list
102         listener = new MyListener(ID_LIST);
103         assertFalse(doWait(0));
104
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);
112
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);
120     }
121
122     @Test
123     public void testOnTopicEvent() throws Exception {
124         listener = new MyListener(ID_LIST);
125
126         // not done yet
127         assertFalse(doWait(0));
128
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));
134
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));
140
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));
146
147         // handleEvent throws an exception - doWait does not return true
148         listener = new MyListener(ID1) {
149             @Override
150             protected String handleEvent(CommInfrastructure infra, String topic, PdpStatus message) {
151                 throw new RuntimeException("expected exception");
152             }
153         };
154         status = new PdpStatus();
155         status.setResponse(makeResponse(ID1));
156         listener.onTopicEvent(INFRA, TOPIC, status);
157         assertFalse(doWait(0));
158
159         // handleEvent returns null - doWait does not return true
160         listener = new MyListener(ID1) {
161             @Override
162             protected String handleEvent(CommInfrastructure infra, String topic, PdpStatus message) {
163                 return null;
164             }
165         };
166         status = new PdpStatus();
167         status.setResponse(makeResponse(ID1));
168         listener.onTopicEvent(INFRA, TOPIC, status);
169         assertFalse(doWait(0));
170     }
171
172     /**
173      * Waits for the listener to complete. Spawns a background thread to do the waiting so
174      * we can limit how long we wait.
175      *
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
180      */
181     private boolean doWait(long millisec) throws InterruptedException {
182         AtomicBoolean done = new AtomicBoolean(false);
183
184         Thread thread = new Thread() {
185             @Override
186             public void run() {
187                 try {
188                     done.set(listener.await(millisec, TimeUnit.MILLISECONDS));
189
190                 } catch (InterruptedException expected) {
191                     return;
192                 }
193             }
194         };
195
196         thread.start();
197         thread.join(5000);
198         thread.interrupt();
199
200         return done.get();
201     }
202
203     /**
204      * Makes a response for the given request ID.
205      *
206      * @param id ID of the request
207      * @return a new response
208      */
209     private PdpResponseDetails makeResponse(String id) {
210         PdpResponseDetails resp = new PdpResponseDetails();
211         resp.setResponseTo(id);
212
213         return resp;
214     }
215
216     private static class MyListener extends MultiPdpStatusListener {
217
218         public MyListener(String id) {
219             super(id);
220         }
221
222         public MyListener(List<String> lst) {
223             super(lst);
224         }
225
226         @Override
227         protected String handleEvent(CommInfrastructure infra, String topic, PdpStatus message) {
228             return (message.getResponse().getResponseTo());
229         }
230     }
231 }