2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.main.comm;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertSame;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.pap.main.comm.msgdata.StateChangeReq;
37 import org.onap.policy.pap.main.comm.msgdata.UpdateReq;
39 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 void testPdpRequests_testGetLastGroupName() {
58 assertEquals(PDP1, data.getPdpName());
59 assertSame(notifier, data.getNotifier());
63 void testAddSingleton() {
64 data.addSingleton(update);
66 verify(update).setNotifier(notifier);
67 verify(update).startPublishing();
71 void testAddSingleton_SameAsExisting() {
72 data.addSingleton(update);
74 // add duplicate update
75 UpdateReq req2 = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
76 data.addSingleton(req2);
78 // should not publish duplicate
79 verify(req2, never()).startPublishing();
81 // should not re-publish original
82 verify(update, times(1)).startPublishing();
86 void testAddSingleton_AnotherRequest() {
87 data.addSingleton(update);
89 // add another request
90 data.addSingleton(change);
92 // add duplicate requests
93 StateChangeReq change2 = makeStateChangeReq(PDP1, MY_STATE);
94 when(change.reconfigure(change2.getMessage())).thenReturn(true);
95 data.addSingleton(change2);
97 UpdateReq update2 = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP);
98 when(update.reconfigure(update2.getMessage())).thenReturn(true);
99 data.addSingleton(update2);
101 // should still only be publishing the first request
102 verify(update).startPublishing();
104 verify(change, never()).startPublishing();
105 verify(change2, never()).startPublishing();
106 verify(update2, never()).startPublishing();
110 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 void testStopPublishing() {
118 // nothing in the queue - nothing should happen
119 data.stopPublishing();
121 data.addSingleton(update);
122 data.addSingleton(change);
124 data.stopPublishing();
126 verify(update).stopPublishing();
127 verify(change, never()).stopPublishing();
129 // repeat, but with only one request in the queue
130 data.addSingleton(update);
131 data.stopPublishing();
132 verify(update, times(2)).stopPublishing();
133 verify(change, never()).stopPublishing();
137 void testStartNextRequest_NothingToStart() {
138 assertFalse(data.startNextRequest(update));
140 // should not have published it
141 verify(update, never()).startPublishing();
145 * Tests addSingleton() when only one request is in the queue.
148 void testStartNextRequest_OneRequest() {
149 data.addSingleton(update);
150 assertFalse(data.startNextRequest(update));
153 assertFalse(data.startNextRequest(update));
155 // should have only been published once
156 verify(update, times(1)).startPublishing();
160 * Tests addSingleton() when more than one request is in the queue.
163 void testStartNextRequest_MultipleRequests() {
164 data.addSingleton(update);
165 data.addSingleton(change);
167 // update is still active - should return true
168 assertTrue(data.startNextRequest(change));
171 assertTrue(data.startNextRequest(change));
173 // should not have published yet
174 verify(change, never()).startPublishing();
176 // should publish "change" once update completes
177 assertTrue(data.startNextRequest(update));
178 verify(change).startPublishing();
180 // nothing more in the queue once it completes
181 assertFalse(data.startNextRequest(change));
185 void testIsFirstInQueue() {
186 // test with empty queue
187 assertFalse(data.isFirstInQueue(update));
189 data.addSingleton(update);
190 assertTrue(data.isFirstInQueue(update));
192 data.addSingleton(change);
193 assertTrue(data.isFirstInQueue(update));
194 assertFalse(data.isFirstInQueue(change));
198 void testGetPdpName() {
199 assertEquals(PDP1, data.getPdpName());