2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.main.comm;
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;
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;
39 public class PdpRequestsTest extends CommonRequestBase {
41 private PdpRequests data;
42 private UpdateReq update;
43 private StateChangeReq change;
50 update = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
51 change = makeStateChangeReq(PDP1, MY_STATE);
53 data = new PdpRequests(PDP1, notifier);
57 public void testPdpRequests_testGetLastGroupName() {
58 assertEquals(PDP1, data.getPdpName());
62 public void testAddSingleton() {
63 data.addSingleton(update);
65 verify(update).setNotifier(notifier);
66 verify(update).startPublishing(any());
70 public void testAddSingleton_SameAsExisting() {
71 data.addSingleton(update);
73 // add duplicate update
74 UpdateReq req2 = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
75 data.addSingleton(req2);
77 // should not publish duplicate
78 verify(req2, never()).startPublishing(any());
82 public void testAddSingleton_LowerPriority() {
83 data.addSingleton(update);
85 // add lower priority request
86 data.addSingleton(change);
88 // should not publish lower priority request
89 verify(change, never()).startPublishing(any());
93 public void testAddSingleton_HigherPriority() {
94 data.addSingleton(change);
96 QueueToken<PdpMessage> token = new QueueToken<>(change.getMessage());
97 when(change.stopPublishing(false)).thenReturn(token);
99 // add higher priority request
100 data.addSingleton(update);
102 // should stop publishing lower priority request
103 verify(change).stopPublishing(false);
105 // should start publishing higher priority request
106 verify(update).startPublishing(token);
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");
117 public void testCheckExistingSingleton_DoesNotExist() {
118 data.addSingleton(update);
119 verify(update).startPublishing(any());
123 public void testCheckExistingSingleton_SameContent() {
124 data.addSingleton(update);
126 // add duplicate update
127 UpdateReq req2 = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
128 when(update.isSameContent(req2)).thenReturn(true);
129 data.addSingleton(req2);
131 // should not publish duplicate
132 verify(req2, never()).startPublishing(any());
136 public void testCheckExistingSingleton_DifferentContent() {
137 data.addSingleton(update);
139 // add different update
140 UpdateReq req2 = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
141 when(req2.isSameContent(update)).thenReturn(false);
142 data.addSingleton(req2);
144 // should not publish duplicate
145 verify(req2, never()).startPublishing(any());
147 // should have re-configured the original
148 verify(update).reconfigure(req2.getMessage(), null);
150 // should not have started publishing again
151 verify(update).startPublishing(any());
155 public void testStopPublishing() {
156 data.addSingleton(update);
157 data.addSingleton(change);
159 data.stopPublishing();
161 verify(update).stopPublishing();
162 verify(change).stopPublishing();
164 // repeat, but with only one request in the queue
165 data.addSingleton(update);
166 data.stopPublishing();
167 verify(update, times(2)).stopPublishing();
169 // should not have been invoked again
170 verify(change).stopPublishing();
174 public void testStopPublishingLowerPriority() {
175 data.addSingleton(change);
177 QueueToken<PdpMessage> token = new QueueToken<>(change.getMessage());
178 when(change.stopPublishing(false)).thenReturn(token);
180 // add higher priority request
181 data.addSingleton(update);
183 // should stop publishing lower priority request
184 verify(change).stopPublishing(false);
186 // should start publishing higher priority request, with the old token
187 verify(update).startPublishing(token);
191 public void testStopPublishingLowerPriority_NothingPublishing() {
192 data.addSingleton(change);
194 // change will return a null token when stopPublishing(false) is called
196 data.addSingleton(update);
198 // should stop publishing lower priority request
199 verify(change).stopPublishing(false);
201 // should start publishing higher priority request
202 verify(update).startPublishing(null);
206 public void testStartNextRequest_NothingToStart() {
207 assertFalse(data.startNextRequest(update));
211 public void testStartNextRequest_ZapCurrent() {
212 data.addSingleton(update);
213 assertFalse(data.startNextRequest(update));
216 assertFalse(data.startNextRequest(update));
220 public void testStartNextRequest_ZapOther() {
221 data.addSingleton(update);
222 data.addSingleton(change);
224 // update is still active - should return true
225 assertTrue(data.startNextRequest(change));
228 assertTrue(data.startNextRequest(change));
230 // nothing more once update completes
231 assertFalse(data.startNextRequest(update));
233 assertFalse(data.startNextRequest(change));
237 public void testStartNextRequest_StartOther() {
238 data.addSingleton(update);
239 data.addSingleton(change);
241 assertTrue(data.startNextRequest(change));
243 // should have published update twice, with and without a token
244 verify(update).startPublishing(any());
245 verify(update).startPublishing();
249 public void testStartNextRequest_NoOther() {
250 data.addSingleton(update);
252 // nothing else to start
253 assertFalse(data.startNextRequest(update));
255 verify(update).startPublishing(any());
256 verify(update, never()).startPublishing();
260 public void testHigherPrioritySingleton_True() {
261 data.addSingleton(update);
262 data.addSingleton(change);
264 verify(update).startPublishing(any());
266 verify(update, never()).startPublishing();
267 verify(change, never()).startPublishing();
268 verify(change, never()).startPublishing(any());
272 public void testHigherPrioritySingleton_FalseWithUpdate() {
273 data.addSingleton(update);
275 verify(update).startPublishing(any());
276 verify(update, never()).startPublishing();
280 public void testHigherPrioritySingleton_FalseWithStateChange() {
281 data.addSingleton(change);
283 verify(change).startPublishing(any());
284 verify(change, never()).startPublishing();
288 public void testGetPdpName() {
289 assertEquals(PDP1, data.getPdpName());