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.assertSame;
27 import static org.junit.Assert.assertTrue;
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.pap.main.comm.msgdata.StateChangeReq;
36 import org.onap.policy.pap.main.comm.msgdata.UpdateReq;
38 public class PdpRequestsTest extends CommonRequestBase {
40 private PdpRequests data;
41 private UpdateReq update;
42 private StateChangeReq change;
49 update = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
50 change = makeStateChangeReq(PDP1, MY_STATE);
52 data = new PdpRequests(PDP1, notifier);
56 public void testPdpRequests_testGetLastGroupName() {
57 assertEquals(PDP1, data.getPdpName());
58 assertSame(notifier, data.getNotifier());
62 public void testAddSingleton() {
63 data.addSingleton(update);
65 verify(update).setNotifier(notifier);
66 verify(update).startPublishing();
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();
80 // should not re-publish original
81 verify(update, times(1)).startPublishing();
85 public void testAddSingleton_AnotherRequest() {
86 data.addSingleton(update);
88 // add another request
89 data.addSingleton(change);
91 // add duplicate requests
92 StateChangeReq change2 = makeStateChangeReq(PDP1, MY_STATE);
93 when(change.reconfigure(change2.getMessage())).thenReturn(true);
94 data.addSingleton(change2);
96 UpdateReq update2 = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
97 when(update.reconfigure(update2.getMessage())).thenReturn(true);
98 data.addSingleton(update2);
100 // should still only be publishing the first request
101 verify(update).startPublishing();
103 verify(change, never()).startPublishing();
104 verify(change2, never()).startPublishing();
105 verify(update2, never()).startPublishing();
109 public void testAddSingleton_Broadcast() {
110 UpdateReq req = makeUpdateReq(null, MY_GROUP, MY_SUBGROUP);
111 assertThatIllegalArgumentException().isThrownBy(() -> data.addSingleton(req))
112 .withMessage("unexpected broadcast for pdp_1");
116 public void testStopPublishing() {
117 // nothing in the queue - nothing should happen
118 data.stopPublishing();
120 data.addSingleton(update);
121 data.addSingleton(change);
123 data.stopPublishing();
125 verify(update).stopPublishing();
126 verify(change, never()).stopPublishing();
128 // repeat, but with only one request in the queue
129 data.addSingleton(update);
130 data.stopPublishing();
131 verify(update, times(2)).stopPublishing();
132 verify(change, never()).stopPublishing();
136 public void testStartNextRequest_NothingToStart() {
137 assertFalse(data.startNextRequest(update));
139 // should not have published it
140 verify(update, never()).startPublishing();
144 * Tests addSingleton() when only one request is in the queue.
147 public void testStartNextRequest_OneRequest() {
148 data.addSingleton(update);
149 assertFalse(data.startNextRequest(update));
152 assertFalse(data.startNextRequest(update));
154 // should have only been published once
155 verify(update, times(1)).startPublishing();
159 * Tests addSingleton() when more than one request is in the queue.
162 public void testStartNextRequest_MultipleRequests() {
163 data.addSingleton(update);
164 data.addSingleton(change);
166 // update is still active - should return true
167 assertTrue(data.startNextRequest(change));
170 assertTrue(data.startNextRequest(change));
172 // should not have published yet
173 verify(change, never()).startPublishing();
175 // should publish "change" once update completes
176 assertTrue(data.startNextRequest(update));
177 verify(change).startPublishing();
179 // nothing more in the queue once it completes
180 assertFalse(data.startNextRequest(change));
184 public void testIsFirstInQueue() {
185 // test with empty queue
186 assertFalse(data.isFirstInQueue(update));
188 data.addSingleton(update);
189 assertTrue(data.isFirstInQueue(update));
191 data.addSingleton(change);
192 assertTrue(data.isFirstInQueue(update));
193 assertFalse(data.isFirstInQueue(change));
197 public void testGetPdpName() {
198 assertEquals(PDP1, data.getPdpName());