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.msgdata;
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertSame;
31 import static org.junit.Assert.assertTrue;
32 import static org.mockito.Matchers.any;
33 import static org.mockito.Matchers.eq;
34 import static org.mockito.Mockito.never;
35 import static org.mockito.Mockito.times;
36 import static org.mockito.Mockito.verify;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.models.pdp.concepts.PdpMessage;
41 import org.onap.policy.models.pdp.concepts.PdpStateChange;
42 import org.onap.policy.models.pdp.concepts.PdpStatus;
43 import org.onap.policy.models.pdp.concepts.PdpUpdate;
44 import org.onap.policy.pap.main.comm.CommonRequestBase;
45 import org.onap.policy.pap.main.comm.QueueToken;
46 import org.onap.policy.pap.main.parameters.RequestParams;
48 public class RequestImplTest extends CommonRequestBase {
49 private static final int MY_PRIORITY = 10;
51 private MyRequest req;
52 private PdpStatus response;
53 private PdpStateChange msg;
57 * @throws Exception if an error occurs
60 public void setUp() throws Exception {
63 response = new PdpStatus();
64 msg = new PdpStateChange();
66 response.setName(PDP1);
69 req = new MyRequest(reqParams, MY_REQ_NAME, msg);
70 req.setListener(listener);
74 public void testRequest_InvalidArgs() {
76 assertThatThrownBy(() -> new MyRequest(null, MY_REQ_NAME, msg)).isInstanceOf(NullPointerException.class);
79 assertThatThrownBy(() -> new MyRequest(reqParams, null, msg)).isInstanceOf(NullPointerException.class);
82 assertThatThrownBy(() -> new MyRequest(reqParams, MY_REQ_NAME, null)).isInstanceOf(NullPointerException.class);
85 assertThatIllegalArgumentException().isThrownBy(() -> new MyRequest(new RequestParams(), MY_REQ_NAME, msg));
89 public void testReconfigure_WrongMsgClass() {
90 assertThatIllegalArgumentException().isThrownBy(() -> req.reconfigure(new PdpUpdate(), null))
91 .withMessage("expecting PdpStateChange instead of PdpUpdate");
95 public void testReconfigure_NotPublishing() {
97 // replace the message with a new message
98 req.reconfigure(new PdpStateChange(), null);
100 // nothing should have been placed in the queue
101 assertNull(queue.poll());
105 public void testRequestImpl_testReconfigure_Publishing() {
106 req.startPublishing();
108 // replace the message with a new message
109 PdpStateChange msg2 = new PdpStateChange();
110 req.reconfigure(msg2, null);
112 // should have cancelled the first timer
113 verify(timer).cancel();
115 // should only be one token in the queue
116 QueueToken<PdpMessage> token = queue.poll();
117 assertNotNull(token);
118 assertSame(msg2, token.get());
120 verify(dispatcher).register(eq(msg.getRequestId()), any());
121 verify(timers).register(eq(msg.getRequestId()), any());
122 verify(publisher).enqueue(token);
124 verify(dispatcher).unregister(eq(msg.getRequestId()));
126 verify(dispatcher).register(eq(msg2.getRequestId()), any());
127 verify(timers).register(eq(msg2.getRequestId()), any());
128 verify(publisher).enqueue(any());
132 public void testReconfigure_PublishingNullToken() {
133 req.startPublishing();
135 // replace the message with a new message
136 PdpStateChange msg2 = new PdpStateChange();
137 req.reconfigure(msg2, null);
139 // should have cancelled the first timer
140 verify(timer).cancel();
142 // should only be one token in the queue
143 QueueToken<PdpMessage> token = queue.poll();
144 assertNotNull(token);
145 assertSame(msg2, token.get());
149 public void testReconfigure_PublishingNewToken() {
150 req.startPublishing();
152 // null out the original token so it isn't reused
153 QueueToken<PdpMessage> token = queue.poll();
154 assertNotNull(token);
155 token.replaceItem(null);
157 QueueToken<PdpMessage> token2 = new QueueToken<>(new PdpStateChange());
159 // replace the message with a new message
160 PdpStateChange msg2 = new PdpStateChange();
161 req.reconfigure(msg2, token2);
163 // new token should have the new message
164 token = queue.poll();
165 assertSame(msg2, token.get());
167 assertNull(queue.poll());
171 public void testIsPublishing() {
172 assertFalse(req.isPublishing());
174 req.startPublishing();
175 assertTrue(req.isPublishing());
177 req.stopPublishing();
178 assertFalse(req.isPublishing());
182 public void testStartPublishingQueueToken() {
183 req.startPublishing(null);
185 assertTrue(req.isPublishing());
187 verify(dispatcher).register(eq(msg.getRequestId()), any());
188 verify(timers).register(eq(msg.getRequestId()), any());
189 verify(publisher).enqueue(any());
191 QueueToken<PdpMessage> token = queue.poll();
192 assertNotNull(token);
193 assertSame(msg, token.get());
196 // invoking start() again has no effect - invocation counts remain the same
197 req.startPublishing(null);
198 verify(dispatcher, times(1)).register(any(), any());
199 verify(timers, times(1)).register(any(), any());
200 verify(publisher, times(1)).enqueue(any());
201 assertNull(queue.poll());
203 // should NOT have cancelled the timer
204 verify(timer, never()).cancel();
208 public void testStartPublishingQueueToken_NoListener() {
209 req.setListener(null);
210 assertThatIllegalStateException().isThrownBy(() -> req.startPublishing())
211 .withMessage("listener has not been set");
215 public void testStartPublishing() {
216 req.startPublishing();
218 assertTrue(req.isPublishing());
220 verify(dispatcher).register(eq(msg.getRequestId()), any());
221 verify(timers).register(eq(msg.getRequestId()), any());
222 verify(publisher).enqueue(any());
224 QueueToken<PdpMessage> token = queue.poll();
225 assertNotNull(token);
226 assertSame(msg, token.get());
229 // invoking start() again has no effect - invocation counts remain the same
230 req.startPublishing();
231 verify(dispatcher, times(1)).register(any(), any());
232 verify(timers, times(1)).register(any(), any());
233 verify(publisher, times(1)).enqueue(any());
234 assertNull(queue.poll());
238 public void testReplaceToken_NullNewToken() {
239 req.startPublishing(null);
240 assertSame(msg, queue.poll().get());
244 public void testReplaceToken_NullOldToken() {
245 QueueToken<PdpMessage> token = new QueueToken<>(new PdpStateChange());
247 req.startPublishing(token);
248 assertNull(queue.poll());
249 assertSame(msg, token.get());
253 public void testReplaceToken_SameToken() {
254 req.startPublishing();
256 QueueToken<PdpMessage> token = queue.poll();
257 req.startPublishing(token);
259 // nothing else should have been enqueued
260 assertNull(queue.poll());
262 assertSame(msg, token.get());
266 public void testReplaceToken_DifferentToken() {
267 req.startPublishing();
269 QueueToken<PdpMessage> token2 = new QueueToken<>(new PdpStateChange());
270 req.startPublishing(token2);
272 QueueToken<PdpMessage> token = queue.poll();
274 // old token should still have the message
275 assertSame(msg, token.get());
277 // should not have added new token to the queue
278 assertNull(queue.poll());
280 // new token should have been nulled out
281 assertNull(token2.get());
285 public void testStopPublishing() {
286 // not publishing yet
287 req.stopPublishing();
288 assertFalse(req.isPublishing());
291 req.startPublishing();
293 req.stopPublishing();
294 assertFalse(req.isPublishing());
296 // should only be one token in the queue - should be nulled out
297 QueueToken<PdpMessage> token = queue.poll();
298 assertNotNull(token);
299 assertNull(token.get());
301 verify(dispatcher).unregister(eq(msg.getRequestId()));
302 verify(timer).cancel();
306 public void testStopPublishingBoolean_NotPublishing() {
307 assertNull(req.stopPublishing(false));
311 public void testStopPublishingBoolean_TruePublishing() {
312 req.startPublishing();
314 assertNull(req.stopPublishing(true));
316 // should be nulled out
317 QueueToken<PdpMessage> token = queue.poll();
318 assertNotNull(token);
319 assertNull(token.get());
321 verify(dispatcher).unregister(eq(msg.getRequestId()));
322 verify(timer).cancel();
324 // if start publishing again - should use a new token
325 req.startPublishing();
326 QueueToken<PdpMessage> token2 = queue.poll();
327 assertNotNull(token2);
328 assertTrue(token2 != token);
329 assertSame(msg, token2.get());
333 public void testStopPublishingBoolean_FalsePublishing() {
334 req.startPublishing();
336 QueueToken<PdpMessage> token = req.stopPublishing(false);
337 assertNotNull(token);
338 assertSame(token, queue.poll());
340 // should not be nulled out
341 assertSame(msg, token.get());
343 verify(dispatcher).unregister(eq(msg.getRequestId()));
344 verify(timer).cancel();
346 // if start publishing again - should use a new token
347 req.startPublishing();
348 QueueToken<PdpMessage> token2 = queue.poll();
349 assertNotNull(token2);
350 assertTrue(token2 != token);
351 assertSame(msg, token2.get());
355 public void testEnqueue() {
356 req.startPublishing();
358 // replace the message with a new message
359 PdpStateChange msg2 = new PdpStateChange();
360 req.reconfigure(msg2, null);
362 // should still only be one token in the queue
363 QueueToken<PdpMessage> token = queue.poll();
364 assertNull(queue.poll());
365 assertNotNull(token);
366 assertSame(msg2, token.get());
368 // force the token to be nulled out
369 req.stopPublishing();
371 // enqueue a new message
372 PdpStateChange msg3 = new PdpStateChange();
373 req.reconfigure(msg3, null);
374 req.startPublishing();
376 // a new token should have been placed in the queue
377 QueueToken<PdpMessage> token2 = queue.poll();
378 assertTrue(token != token2);
379 assertNull(queue.poll());
380 assertNotNull(token2);
381 assertSame(msg3, token2.get());
385 public void testResetRetryCount_testBumpRetryCount() {
386 req = new MyRequest(new RequestParams().setMaxRetryCount(2).setModifyLock(lock).setPdpPublisher(publisher)
387 .setResponseDispatcher(dispatcher).setTimers(timers), MY_REQ_NAME, msg);
388 req.setListener(listener);
390 assertEquals(0, req.getRetryCount());
391 assertTrue(req.bumpRetryCount());
392 assertTrue(req.bumpRetryCount());
394 // limit should now be reached and it should go no further
395 assertFalse(req.bumpRetryCount());
396 assertFalse(req.bumpRetryCount());
398 assertEquals(2, req.getRetryCount());
400 req.resetRetryCount();
401 assertEquals(0, req.getRetryCount());
405 public void testProcessResponse() {
406 req.startPublishing();
408 invokeProcessResponse(response);
410 verify(listener).success(PDP1);
411 verify(listener, never()).failure(any(), any());
412 verify(timer).cancel();
416 public void testProcessResponse_NotPublishing() {
417 // force registration with the dispatcher - needed by invokeProcessResponse(response)
418 req.startPublishing();
419 req.stopPublishing();
421 invokeProcessResponse(response);
423 verify(listener, never()).success(any());
424 verify(listener, never()).failure(any(), any());
428 public void testProcessResponse_ResponseFailed() {
429 req.startPublishing();
431 response.setName(DIFFERENT);
433 invokeProcessResponse(response);
435 verify(listener, never()).success(any());
436 verify(listener).failure(DIFFERENT, "PDP name does not match");
437 verify(timer).cancel();
441 public void testHandleTimeout() {
442 req.startPublishing();
444 // remove it from the queue
445 queue.poll().replaceItem(null);
447 invokeTimeoutHandler();
449 // count should have been bumped
450 assertEquals(1, req.getRetryCount());
452 // should have invoked startPublishing() a second time
453 verify(dispatcher, times(2)).register(eq(msg.getRequestId()), any());
457 public void testHandleTimeout_NotPublishing() {
458 req.startPublishing();
460 req.stopPublishing();
462 invokeTimeoutHandler();
464 // should NOT have invoked startPublishing() a second time
465 verify(dispatcher, times(1)).register(eq(msg.getRequestId()), any());
466 verify(listener, never()).retryCountExhausted();
470 public void testHandleTimeout_RetryExhausted() {
471 req.startPublishing();
474 req.bumpRetryCount();
475 req.bumpRetryCount();
476 req.bumpRetryCount();
478 // remove it from the queue
479 queue.poll().replaceItem(null);
481 invokeTimeoutHandler();
483 // should NOT have invoked startPublishing() a second time
484 verify(dispatcher, times(1)).register(eq(msg.getRequestId()), any());
486 verify(listener).retryCountExhausted();
490 public void testCheckResponse_Matched() {
491 req.startPublishing();
493 invokeProcessResponse(response);
495 verify(listener).success(PDP1);
496 verify(listener, never()).failure(any(), any());
500 public void testCheckResponse_NullName() {
501 req.startPublishing();
503 response.setName(null);
505 invokeProcessResponse(response);
507 verify(listener, never()).success(any());
508 verify(listener).failure(null, "null PDP name");
512 public void testCheckResponse_MismatchedName() {
513 req.startPublishing();
515 response.setName(DIFFERENT);
517 invokeProcessResponse(response);
519 verify(listener, never()).success(any());
520 verify(listener).failure(DIFFERENT, "PDP name does not match");
524 public void testCheckResponse_MismatchedNameWithBroadcast() {
526 req.startPublishing();
528 response.setName(DIFFERENT);
530 invokeProcessResponse(response);
532 verify(listener).success(DIFFERENT);
533 verify(listener, never()).failure(any(), any());
537 public void testGetName() {
538 assertEquals(MY_REQ_NAME, req.getName());
542 public void testGetMessage() {
543 assertSame(msg, req.getMessage());
545 PdpStateChange msg2 = new PdpStateChange();
546 req.reconfigure(msg2, null);
547 assertSame(msg2, req.getMessage());
551 public void testGetParams() {
552 assertSame(reqParams, req.getParams());
555 private class MyRequest extends RequestImpl {
557 public MyRequest(RequestParams params, String name, PdpMessage message) {
558 super(params, name, message);
562 public int getPriority() {
567 public boolean isSameContent(Request other) {