2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020, 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.msgdata;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.junit.jupiter.api.Assertions.assertFalse;
29 import static org.junit.jupiter.api.Assertions.assertNotNull;
30 import static org.junit.jupiter.api.Assertions.assertNotSame;
31 import static org.junit.jupiter.api.Assertions.assertNull;
32 import static org.junit.jupiter.api.Assertions.assertSame;
33 import static org.junit.jupiter.api.Assertions.assertTrue;
34 import static org.mockito.ArgumentMatchers.any;
35 import static org.mockito.ArgumentMatchers.eq;
36 import static org.mockito.Mockito.never;
37 import static org.mockito.Mockito.times;
38 import static org.mockito.Mockito.verify;
40 import org.assertj.core.api.Assertions;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.onap.policy.models.pdp.concepts.PdpMessage;
44 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
45 import org.onap.policy.models.pdp.concepts.PdpStateChange;
46 import org.onap.policy.models.pdp.concepts.PdpStatus;
47 import org.onap.policy.models.pdp.concepts.PdpUpdate;
48 import org.onap.policy.pap.main.comm.CommonRequestBase;
49 import org.onap.policy.pap.main.comm.QueueToken;
50 import org.onap.policy.pap.main.parameters.RequestParams;
52 class RequestImplTest extends CommonRequestBase {
54 private MyRequest req;
55 private PdpStatus response;
56 private PdpStateChange msg;
60 * @throws Exception if an error occurs
64 public void setUp() throws Exception {
67 response = new PdpStatus();
68 msg = new PdpStateChange();
70 response.setName(PDP1);
71 response.setResponse(new PdpResponseDetails());
72 response.getResponse().setResponseTo(msg.getRequestId());
75 req = new MyRequest(reqParams, MY_REQ_NAME, msg);
76 req.setListener(listener);
80 void testRequest_InvalidArgs() {
82 assertThatThrownBy(() -> new MyRequest(null, MY_REQ_NAME, msg)).isInstanceOf(NullPointerException.class);
85 assertThatThrownBy(() -> new MyRequest(reqParams, null, msg)).isInstanceOf(NullPointerException.class);
88 assertThatThrownBy(() -> new MyRequest(reqParams, MY_REQ_NAME, null)).isInstanceOf(NullPointerException.class);
91 assertThatIllegalArgumentException().isThrownBy(() -> new MyRequest(new RequestParams(), MY_REQ_NAME, msg));
95 void testReconfigure2_WrongMsgClass() {
96 assertThatIllegalArgumentException().isThrownBy(() -> req.reconfigure(new PdpUpdate()))
97 .withMessage("expecting PdpStateChange instead of PdpUpdate");
101 void testReconfigure2_NotPublishing() {
103 // replace the message with a new message
104 req.reconfigure(new PdpStateChange());
106 // nothing should have been placed in the queue
107 assertNull(queue.poll());
111 void testRequestImpl_testReconfigure2_Publishing() {
112 req.startPublishing();
114 // replace the message with a new message
115 PdpStateChange msg2 = new PdpStateChange();
116 req.reconfigure(msg2);
118 // should have cancelled the first timer
119 verify(timer).cancel();
121 // should only be one token in the queue
122 QueueToken<PdpMessage> token = queue.poll();
123 org.junit.jupiter.api.Assertions.assertNotNull(token);
124 org.junit.jupiter.api.Assertions.assertSame(msg2, token.get());
126 verify(dispatcher).register(eq(msg.getRequestId()), any());
127 verify(timers).register(eq(msg.getRequestId()), any());
128 verify(publisher).enqueue(token);
130 verify(dispatcher).unregister(msg.getRequestId());
132 verify(dispatcher).register(eq(msg2.getRequestId()), any());
133 verify(timers).register(eq(msg2.getRequestId()), any());
134 verify(publisher).enqueue(any());
138 void testIsPublishing() {
139 assertFalse(req.isPublishing());
141 req.startPublishing();
142 assertTrue(req.isPublishing());
144 req.stopPublishing();
145 assertFalse(req.isPublishing());
149 void testStartPublishingQueueToken_NoListener() {
150 req.setListener(null);
151 assertThatIllegalStateException().isThrownBy(() -> req.startPublishing())
152 .withMessage("listener has not been set");
156 void testStartPublishing() {
157 req.startPublishing();
159 assertTrue(req.isPublishing());
161 verify(dispatcher).register(eq(msg.getRequestId()), any());
162 verify(timers).register(eq(msg.getRequestId()), any());
163 verify(publisher).enqueue(any());
165 QueueToken<PdpMessage> token = queue.poll();
166 assertNotNull(token);
167 assertSame(msg, token.get());
170 // invoking start() again has no effect - invocation counts remain the same
171 req.startPublishing();
172 verify(dispatcher, times(1)).register(any(), any());
173 verify(timers, times(1)).register(any(), any());
174 verify(publisher, times(1)).enqueue(any());
175 assertNull(queue.poll());
179 void testStopPublishing() {
180 // not publishing yet
181 req.stopPublishing();
182 assertFalse(req.isPublishing());
185 req.startPublishing();
187 req.stopPublishing();
188 assertFalse(req.isPublishing());
190 // should only be one token in the queue - should be nulled out
191 QueueToken<PdpMessage> token = queue.poll();
192 assertNotNull(token);
193 assertNull(token.get());
195 verify(dispatcher).unregister(msg.getRequestId());
196 verify(timer).cancel();
200 void testStopPublishingBoolean_NotPublishing() {
201 // should not throw an exception
202 Assertions.assertThatCode(() -> req.stopPublishing()).doesNotThrowAnyException();
206 void testStopPublishingBoolean_TruePublishing() {
207 req.startPublishing();
209 req.stopPublishing();
211 // should be nulled out
212 QueueToken<PdpMessage> token = queue.poll();
213 assertNotNull(token);
214 assertNull(token.get());
216 verify(dispatcher).unregister(msg.getRequestId());
217 verify(timer).cancel();
219 // if start publishing again - should use a new token
220 req.startPublishing();
221 QueueToken<PdpMessage> token2 = queue.poll();
222 assertNotNull(token2);
223 assertNotSame(token, token2);
224 assertSame(msg, token2.get());
229 req.startPublishing();
231 // replace the message with a new message
232 PdpStateChange msg2 = new PdpStateChange();
233 req.reconfigure(msg2);
235 // should still only be one token in the queue
236 QueueToken<PdpMessage> token = queue.poll();
237 assertNull(queue.poll());
238 assertNotNull(token);
239 assertSame(msg2, token.get());
241 // force the token to be nulled out
242 req.stopPublishing();
244 // enqueue a new message
245 PdpStateChange msg3 = new PdpStateChange();
246 req.reconfigure(msg3);
247 req.startPublishing();
249 // a new token should have been placed in the queue
250 QueueToken<PdpMessage> token2 = queue.poll();
251 assertNotSame(token, token2);
252 assertNull(queue.poll());
253 assertNotNull(token2);
254 assertSame(msg3, token2.get());
256 // zap the token, indicating it's been published
257 token2.replaceItem(null);
258 PdpStateChange msg4 = new PdpStateChange();
259 req.reconfigure(msg4);
261 // a new token should have been placed in the queue
262 QueueToken<PdpMessage> token3 = queue.poll();
263 assertNotSame(token2, token3);
264 assertNull(queue.poll());
265 assertNotNull(token3);
266 assertSame(msg4, token3.get());
270 void testResetRetryCount_testBumpRetryCount() {
271 req = new MyRequest(new RequestParams().setMaxRetryCount(2).setModifyLock(lock).setPdpPublisher(publisher)
272 .setResponseDispatcher(dispatcher).setTimers(timers), MY_REQ_NAME, msg);
273 req.setListener(listener);
275 assertEquals(0, req.getRetryCount());
276 assertTrue(req.bumpRetryCount());
277 assertTrue(req.bumpRetryCount());
279 // limit should now be reached and it should go no further
280 assertFalse(req.bumpRetryCount());
281 assertFalse(req.bumpRetryCount());
283 assertEquals(2, req.getRetryCount());
285 req.resetRetryCount();
286 assertEquals(0, req.getRetryCount());
290 void testProcessResponse() {
291 req.startPublishing();
293 invokeProcessResponse(response);
295 verify(listener).success(PDP1, response);
296 verify(listener, never()).failure(any(), any());
297 verify(timer).cancel();
301 void testProcessResponse_NotPublishing() {
302 // force registration with the dispatcher - needed by invokeProcessResponse(response)
303 req.startPublishing();
304 req.stopPublishing();
306 invokeProcessResponse(response);
308 verify(listener, never()).success(any(), any());
309 verify(listener, never()).failure(any(), any());
313 void testProcessResponse_WrongRequest() {
314 req.startPublishing();
316 response.getResponse().setResponseTo(DIFFERENT);
318 invokeProcessResponse(response);
320 verify(listener, never()).success(any(), any());
321 verify(listener, never()).failure(any(), any());
322 verify(timer, never()).cancel();
326 void testProcessResponse_ResponseFailed() {
327 req.startPublishing();
329 response.setName(DIFFERENT);
331 invokeProcessResponse(response);
333 verify(listener, never()).success(any(), any());
334 verify(listener).failure(DIFFERENT, "PDP name does not match");
335 verify(timer).cancel();
339 void testHandleTimeout() {
340 req.startPublishing();
342 // remove it from the queue
343 queue.poll().replaceItem(null);
345 invokeTimeoutHandler();
347 // count should have been bumped
348 assertEquals(1, req.getRetryCount());
350 // should have invoked startPublishing() a second time
351 verify(dispatcher, times(2)).register(eq(msg.getRequestId()), any());
355 void testHandleTimeout_NotPublishing() {
356 req.startPublishing();
358 req.stopPublishing();
360 invokeTimeoutHandler();
362 // should NOT have invoked startPublishing() a second time
363 verify(dispatcher, times(1)).register(eq(msg.getRequestId()), any());
364 verify(listener, never()).retryCountExhausted(any());
368 void testHandleTimeout_RetryExhausted() {
369 req.startPublishing();
372 req.bumpRetryCount();
373 req.bumpRetryCount();
374 req.bumpRetryCount();
376 // remove it from the queue
377 queue.poll().replaceItem(null);
379 invokeTimeoutHandler();
381 // should NOT have invoked startPublishing() a second time
382 verify(dispatcher, times(1)).register(eq(msg.getRequestId()), any());
384 verify(listener).retryCountExhausted(req);
388 void testCheckResponse_Matched() {
389 req.startPublishing();
391 invokeProcessResponse(response);
393 verify(listener).success(PDP1, response);
394 verify(listener, never()).failure(any(), any());
398 void testCheckResponse_NullName() {
399 req.startPublishing();
401 response.setName(null);
403 invokeProcessResponse(response);
405 verify(listener, never()).success(any(), any());
406 verify(listener).failure(null, "null PDP name");
410 void testCheckResponse_MismatchedName() {
411 req.startPublishing();
413 response.setName(DIFFERENT);
415 invokeProcessResponse(response);
417 verify(listener, never()).success(any(), any());
418 verify(listener).failure(DIFFERENT, "PDP name does not match");
422 void testCheckResponse_MismatchedNameWithBroadcast() {
424 req.startPublishing();
426 response.setName(DIFFERENT);
428 invokeProcessResponse(response);
430 verify(listener).success(DIFFERENT, response);
431 verify(listener, never()).failure(any(), any());
436 assertEquals(MY_REQ_NAME, req.getName());
440 void testGetMessage() {
441 assertSame(msg, req.getMessage());
443 PdpStateChange msg2 = new PdpStateChange();
444 req.reconfigure(msg2);
445 assertSame(msg2, req.getMessage());
449 void testGetParams() {
450 assertSame(reqParams, req.getParams());
454 void testGetUndeployPolicies() {
455 assertTrue(req.getUndeployPolicies().isEmpty());
458 private static class MyRequest extends RequestImpl {
460 public MyRequest(RequestParams params, String name, PdpMessage message) {
461 super(params, name, message);
465 public boolean reconfigure(PdpMessage newMessage) {
466 reconfigure2(newMessage);