Receive PDP status messages
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.comm;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.Arrays;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.atomic.AtomicBoolean;
32 import org.junit.Test;
33 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
34 import org.onap.policy.pap.main.comm.MultiPdpStatusListener;
35 import org.onap.policy.pdp.common.models.PdpStatus;
36
37 public class MultiPdpStatusListenerTest {
38     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
39     private static final String TOPIC = "my-topic";
40     private static final String NAME1 = "pdp_1";
41     private static final String NAME2 = "pdp_2";
42     private static final List<String> NAME_LIST = Arrays.asList(NAME1, NAME2);
43
44     private MultiPdpStatusListener listener;
45     private PdpStatus status;
46
47     @Test
48     public void testMultiPdpStatusListenerString() throws Exception {
49         listener = new MultiPdpStatusListener(NAME1);
50         assertEquals(Arrays.asList(NAME1).toString(), listener.getUnseenPdpNames().toString());
51
52         // a name is in the queue - not done yet
53         assertFalse(doWait(0));
54     }
55
56     @Test
57     public void testMultiPdpStatusListenerCollectionOfString() throws Exception {
58         List<String> lst = NAME_LIST;
59
60         listener = new MultiPdpStatusListener(lst);
61         assertEquals(lst.toString(), listener.getUnseenPdpNames().toString());
62
63         // a name is in the queue - not done yet
64         assertFalse(doWait(0));
65
66         /*
67          * Try with an empty list - should already be complete.
68          */
69         listener = new MultiPdpStatusListener(new LinkedList<>());
70         assertTrue(listener.getUnseenPdpNames().isEmpty());
71         assertTrue(doWait(0));
72     }
73
74     @Test
75     public void testGetUnseenPdpNames() {
76         List<String> lst = NAME_LIST;
77
78         listener = new MultiPdpStatusListener(lst);
79         assertEquals(lst.toString(), listener.getUnseenPdpNames().toString());
80
81         // receive message from one PDP
82         status = new PdpStatus();
83         status.setName(NAME2);
84         listener.onTopicEvent(INFRA, TOPIC, status);
85         assertEquals(Arrays.asList(NAME1).toString(), listener.getUnseenPdpNames().toString());
86
87         // receive message from the other PDP
88         status = new PdpStatus();
89         status.setName(NAME1);
90         listener.onTopicEvent(INFRA, TOPIC, status);
91         assertTrue(listener.getUnseenPdpNames().isEmpty());
92     }
93
94     @Test
95     public void testAwait() throws Exception {
96         // try with an empty list - should already be complete
97         listener = new MultiPdpStatusListener(new LinkedList<>());
98         assertTrue(doWait(0));
99
100         // try it with something in the list
101         listener = new MultiPdpStatusListener(NAME_LIST);
102         assertFalse(doWait(0));
103
104         // process a message from one PDP - wait should block the entire time
105         status = new PdpStatus();
106         status.setName(NAME1);
107         listener.onTopicEvent(INFRA, TOPIC, status);
108         long tbeg = System.currentTimeMillis();
109         assertFalse(doWait(50));
110         assertTrue(System.currentTimeMillis() - tbeg >= 49);
111
112         // process a message from the other PDP - wait should NOT block
113         status = new PdpStatus();
114         status.setName(NAME2);
115         listener.onTopicEvent(INFRA, TOPIC, status);
116         tbeg = System.currentTimeMillis();
117         assertTrue(doWait(4000));
118         assertTrue(System.currentTimeMillis() - tbeg < 3000);
119     }
120
121     @Test
122     public void testOnTopicEvent() throws Exception {
123         listener = new MultiPdpStatusListener(NAME_LIST);
124
125         // not done yet
126         assertFalse(doWait(0));
127
128         // process a message - still not done as have another name to go
129         status = new PdpStatus();
130         status.setName(NAME1);
131         listener.onTopicEvent(INFRA, TOPIC, status);
132         assertFalse(doWait(0));
133
134         // process a message from the same PDP - still not done
135         status = new PdpStatus();
136         status.setName(NAME1);
137         listener.onTopicEvent(INFRA, TOPIC, status);
138         assertFalse(doWait(0));
139
140         // process another message - now we're done
141         status = new PdpStatus();
142         status.setName(NAME2);
143         listener.onTopicEvent(INFRA, TOPIC, status);
144         assertTrue(doWait(0));
145     }
146
147     /**
148      * Waits for the listener to complete. Spawns a background thread to do the waiting so
149      * we can limit how long we wait.
150      *
151      * @param millisec milliseconds to wait
152      * @return {@code true} if the wait completed successfully, {@code false} otherwise
153      * @throws InterruptedException if this thread is interrupted while waiting for the
154      *         background thread to complete
155      */
156     private boolean doWait(long millisec) throws InterruptedException {
157         AtomicBoolean done = new AtomicBoolean(false);
158
159         Thread thread = new Thread() {
160             @Override
161             public void run() {
162                 try {
163                     done.set(listener.await(millisec, TimeUnit.MILLISECONDS));
164
165                 } catch (InterruptedException expected) {
166                     return;
167                 }
168             }
169         };
170
171         thread.start();
172         thread.join(5000);
173         thread.interrupt();
174
175         return done.get();
176     }
177 }