Generate notifications when policies change
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / PdpRequestsTest.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.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.models.pdp.concepts.PdpMessage;
36 import org.onap.policy.pap.main.comm.msgdata.StateChangeReq;
37 import org.onap.policy.pap.main.comm.msgdata.UpdateReq;
38
39 public class PdpRequestsTest extends CommonRequestBase {
40
41     private PdpRequests data;
42     private UpdateReq update;
43     private StateChangeReq change;
44
45     /**
46      * Sets up.
47      */
48     @Before
49     public void setUp() {
50         update = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
51         change = makeStateChangeReq(PDP1, MY_STATE);
52
53         data = new PdpRequests(PDP1, notifier);
54     }
55
56     @Test
57     public void testPdpRequests_testGetLastGroupName() {
58         assertEquals(PDP1, data.getPdpName());
59     }
60
61     @Test
62     public void testAddSingleton() {
63         data.addSingleton(update);
64
65         verify(update).setNotifier(notifier);
66         verify(update).startPublishing(any());
67     }
68
69     @Test
70     public void testAddSingleton_SameAsExisting() {
71         data.addSingleton(update);
72
73         // add duplicate update
74         UpdateReq req2 = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
75         data.addSingleton(req2);
76
77         // should not publish duplicate
78         verify(req2, never()).startPublishing(any());
79     }
80
81     @Test
82     public void testAddSingleton_LowerPriority() {
83         data.addSingleton(update);
84
85         // add lower priority request
86         data.addSingleton(change);
87
88         // should not publish lower priority request
89         verify(change, never()).startPublishing(any());
90     }
91
92     @Test
93     public void testAddSingleton_HigherPriority() {
94         data.addSingleton(change);
95
96         QueueToken<PdpMessage> token = new QueueToken<>(change.getMessage());
97         when(change.stopPublishing(false)).thenReturn(token);
98
99         // add higher priority request
100         data.addSingleton(update);
101
102         // should stop publishing lower priority request
103         verify(change).stopPublishing(false);
104
105         // should start publishing higher priority request
106         verify(update).startPublishing(token);
107     }
108
109     @Test
110     public void testAddSingleton_Broadcast() {
111         UpdateReq req = makeUpdateReq(null, MY_GROUP, MY_SUBGROUP);
112         assertThatIllegalArgumentException().isThrownBy(() -> data.addSingleton(req))
113                         .withMessage("unexpected broadcast for pdp_1");
114     }
115
116     @Test
117     public void testCheckExistingSingleton_DoesNotExist() {
118         data.addSingleton(update);
119         verify(update).startPublishing(any());
120     }
121
122     @Test
123     public void testCheckExistingSingleton_SameContent() {
124         data.addSingleton(update);
125
126         // add duplicate update
127         UpdateReq req2 = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
128         when(update.isSameContent(req2)).thenReturn(true);
129         data.addSingleton(req2);
130
131         // should not publish duplicate
132         verify(req2, never()).startPublishing(any());
133     }
134
135     @Test
136     public void testCheckExistingSingleton_DifferentContent() {
137         data.addSingleton(update);
138
139         // add different update
140         UpdateReq req2 = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
141         when(req2.isSameContent(update)).thenReturn(false);
142         data.addSingleton(req2);
143
144         // should not publish duplicate
145         verify(req2, never()).startPublishing(any());
146
147         // should have re-configured the original
148         verify(update).reconfigure(req2.getMessage(), null);
149
150         // should not have started publishing again
151         verify(update).startPublishing(any());
152     }
153
154     @Test
155     public void testStopPublishing() {
156         data.addSingleton(update);
157         data.addSingleton(change);
158
159         data.stopPublishing();
160
161         verify(update).stopPublishing();
162         verify(change).stopPublishing();
163
164         // repeat, but with only one request in the queue
165         data.addSingleton(update);
166         data.stopPublishing();
167         verify(update, times(2)).stopPublishing();
168
169         // should not have been invoked again
170         verify(change).stopPublishing();
171     }
172
173     @Test
174     public void testStopPublishingLowerPriority() {
175         data.addSingleton(change);
176
177         QueueToken<PdpMessage> token = new QueueToken<>(change.getMessage());
178         when(change.stopPublishing(false)).thenReturn(token);
179
180         // add higher priority request
181         data.addSingleton(update);
182
183         // should stop publishing lower priority request
184         verify(change).stopPublishing(false);
185
186         // should start publishing higher priority request, with the old token
187         verify(update).startPublishing(token);
188     }
189
190     @Test
191     public void testStopPublishingLowerPriority_NothingPublishing() {
192         data.addSingleton(change);
193
194         // change will return a null token when stopPublishing(false) is called
195
196         data.addSingleton(update);
197
198         // should stop publishing lower priority request
199         verify(change).stopPublishing(false);
200
201         // should start publishing higher priority request
202         verify(update).startPublishing(null);
203     }
204
205     @Test
206     public void testStartNextRequest_NothingToStart() {
207         assertFalse(data.startNextRequest(update));
208     }
209
210     @Test
211     public void testStartNextRequest_ZapCurrent() {
212         data.addSingleton(update);
213         assertFalse(data.startNextRequest(update));
214
215         // invoke again
216         assertFalse(data.startNextRequest(update));
217     }
218
219     @Test
220     public void testStartNextRequest_ZapOther() {
221         data.addSingleton(update);
222         data.addSingleton(change);
223
224         // update is still active - should return true
225         assertTrue(data.startNextRequest(change));
226
227         // invoke again
228         assertTrue(data.startNextRequest(change));
229
230         // nothing more once update completes
231         assertFalse(data.startNextRequest(update));
232
233         assertFalse(data.startNextRequest(change));
234     }
235
236     @Test
237     public void testStartNextRequest_StartOther() {
238         data.addSingleton(update);
239         data.addSingleton(change);
240
241         assertTrue(data.startNextRequest(change));
242
243         // should have published update twice, with and without a token
244         verify(update).startPublishing(any());
245         verify(update).startPublishing();
246     }
247
248     @Test
249     public void testStartNextRequest_NoOther() {
250         data.addSingleton(update);
251
252         // nothing else to start
253         assertFalse(data.startNextRequest(update));
254
255         verify(update).startPublishing(any());
256         verify(update, never()).startPublishing();
257     }
258
259     @Test
260     public void testHigherPrioritySingleton_True() {
261         data.addSingleton(update);
262         data.addSingleton(change);
263
264         verify(update).startPublishing(any());
265
266         verify(update, never()).startPublishing();
267         verify(change, never()).startPublishing();
268         verify(change, never()).startPublishing(any());
269     }
270
271     @Test
272     public void testHigherPrioritySingleton_FalseWithUpdate() {
273         data.addSingleton(update);
274
275         verify(update).startPublishing(any());
276         verify(update, never()).startPublishing();
277     }
278
279     @Test
280     public void testHigherPrioritySingleton_FalseWithStateChange() {
281         data.addSingleton(change);
282
283         verify(change).startPublishing(any());
284         verify(change, never()).startPublishing();
285     }
286
287     @Test
288     public void testGetPdpName() {
289         assertEquals(PDP1, data.getPdpName());
290     }
291 }