Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / msgdata / RequestImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.pap.main.comm.msgdata;
23
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;
39
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;
51
52 class RequestImplTest extends CommonRequestBase {
53
54     private MyRequest req;
55     private PdpStatus response;
56     private PdpStateChange msg;
57
58     /**
59      * Sets up.
60      * @throws Exception if an error occurs
61      */
62     @BeforeEach
63     @Override
64     public void setUp() throws Exception {
65         super.setUp();
66
67         response = new PdpStatus();
68         msg = new PdpStateChange();
69
70         response.setName(PDP1);
71         response.setResponse(new PdpResponseDetails());
72         response.getResponse().setResponseTo(msg.getRequestId());
73         msg.setName(PDP1);
74
75         req = new MyRequest(reqParams, MY_REQ_NAME, msg);
76         req.setListener(listener);
77     }
78
79     @Test
80     public void testRequest_InvalidArgs() {
81         // null params
82         assertThatThrownBy(() -> new MyRequest(null, MY_REQ_NAME, msg)).isInstanceOf(NullPointerException.class);
83
84         // null name
85         assertThatThrownBy(() -> new MyRequest(reqParams, null, msg)).isInstanceOf(NullPointerException.class);
86
87         // null message
88         assertThatThrownBy(() -> new MyRequest(reqParams, MY_REQ_NAME, null)).isInstanceOf(NullPointerException.class);
89
90         // invalid params
91         assertThatIllegalArgumentException().isThrownBy(() -> new MyRequest(new RequestParams(), MY_REQ_NAME, msg));
92     }
93
94     @Test
95     public void testReconfigure2_WrongMsgClass() {
96         assertThatIllegalArgumentException().isThrownBy(() -> req.reconfigure(new PdpUpdate()))
97                         .withMessage("expecting PdpStateChange instead of PdpUpdate");
98     }
99
100     @Test
101     public void testReconfigure2_NotPublishing() {
102
103         // replace the message with a new message
104         req.reconfigure(new PdpStateChange());
105
106         // nothing should have been placed in the queue
107         assertNull(queue.poll());
108     }
109
110     @Test
111     public void testRequestImpl_testReconfigure2_Publishing() {
112         req.startPublishing();
113
114         // replace the message with a new message
115         PdpStateChange msg2 = new PdpStateChange();
116         req.reconfigure(msg2);
117
118         // should have cancelled the first timer
119         verify(timer).cancel();
120
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());
125
126         verify(dispatcher).register(eq(msg.getRequestId()), any());
127         verify(timers).register(eq(msg.getRequestId()), any());
128         verify(publisher).enqueue(token);
129
130         verify(dispatcher).unregister(msg.getRequestId());
131
132         verify(dispatcher).register(eq(msg2.getRequestId()), any());
133         verify(timers).register(eq(msg2.getRequestId()), any());
134         verify(publisher).enqueue(any());
135     }
136
137     @Test
138     public void testIsPublishing() {
139         assertFalse(req.isPublishing());
140
141         req.startPublishing();
142         assertTrue(req.isPublishing());
143
144         req.stopPublishing();
145         assertFalse(req.isPublishing());
146     }
147
148     @Test
149     public void testStartPublishingQueueToken_NoListener() {
150         req.setListener(null);
151         assertThatIllegalStateException().isThrownBy(() -> req.startPublishing())
152                         .withMessage("listener has not been set");
153     }
154
155     @Test
156     public void testStartPublishing() {
157         req.startPublishing();
158
159         assertTrue(req.isPublishing());
160
161         verify(dispatcher).register(eq(msg.getRequestId()), any());
162         verify(timers).register(eq(msg.getRequestId()), any());
163         verify(publisher).enqueue(any());
164
165         QueueToken<PdpMessage> token = queue.poll();
166         assertNotNull(token);
167         assertSame(msg, token.get());
168
169
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());
176     }
177
178     @Test
179     public void testStopPublishing() {
180         // not publishing yet
181         req.stopPublishing();
182         assertFalse(req.isPublishing());
183
184         // now we'll publish
185         req.startPublishing();
186
187         req.stopPublishing();
188         assertFalse(req.isPublishing());
189
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());
194
195         verify(dispatcher).unregister(msg.getRequestId());
196         verify(timer).cancel();
197     }
198
199     @Test
200     public void testStopPublishingBoolean_NotPublishing() {
201         // should not throw an exception
202         Assertions.assertThatCode(() -> req.stopPublishing()).doesNotThrowAnyException();
203     }
204
205     @Test
206     public void testStopPublishingBoolean_TruePublishing() {
207         req.startPublishing();
208
209         req.stopPublishing();
210
211         // should be nulled out
212         QueueToken<PdpMessage> token = queue.poll();
213         assertNotNull(token);
214         assertNull(token.get());
215
216         verify(dispatcher).unregister(msg.getRequestId());
217         verify(timer).cancel();
218
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());
225     }
226
227     @Test
228     public void testEnqueue() {
229         req.startPublishing();
230
231         // replace the message with a new message
232         PdpStateChange msg2 = new PdpStateChange();
233         req.reconfigure(msg2);
234
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());
240
241         // force the token to be nulled out
242         req.stopPublishing();
243
244         // enqueue a new message
245         PdpStateChange msg3 = new PdpStateChange();
246         req.reconfigure(msg3);
247         req.startPublishing();
248
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());
255
256         // zap the token, indicating it's been published
257         token2.replaceItem(null);
258         PdpStateChange msg4 = new PdpStateChange();
259         req.reconfigure(msg4);
260
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());
267     }
268
269     @Test
270     public 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);
274
275         assertEquals(0, req.getRetryCount());
276         assertTrue(req.bumpRetryCount());
277         assertTrue(req.bumpRetryCount());
278
279         // limit should now be reached and it should go no further
280         assertFalse(req.bumpRetryCount());
281         assertFalse(req.bumpRetryCount());
282
283         assertEquals(2, req.getRetryCount());
284
285         req.resetRetryCount();
286         assertEquals(0, req.getRetryCount());
287     }
288
289     @Test
290     public void testProcessResponse() {
291         req.startPublishing();
292
293         invokeProcessResponse(response);
294
295         verify(listener).success(PDP1, response);
296         verify(listener, never()).failure(any(), any());
297         verify(timer).cancel();
298     }
299
300     @Test
301     public void testProcessResponse_NotPublishing() {
302         // force registration with the dispatcher - needed by invokeProcessResponse(response)
303         req.startPublishing();
304         req.stopPublishing();
305
306         invokeProcessResponse(response);
307
308         verify(listener, never()).success(any(), any());
309         verify(listener, never()).failure(any(), any());
310     }
311
312     @Test
313     public void testProcessResponse_WrongRequest() {
314         req.startPublishing();
315
316         response.getResponse().setResponseTo(DIFFERENT);
317
318         invokeProcessResponse(response);
319
320         verify(listener, never()).success(any(), any());
321         verify(listener, never()).failure(any(), any());
322         verify(timer, never()).cancel();
323     }
324
325     @Test
326     public void testProcessResponse_ResponseFailed() {
327         req.startPublishing();
328
329         response.setName(DIFFERENT);
330
331         invokeProcessResponse(response);
332
333         verify(listener, never()).success(any(), any());
334         verify(listener).failure(DIFFERENT, "PDP name does not match");
335         verify(timer).cancel();
336     }
337
338     @Test
339     public void testHandleTimeout() {
340         req.startPublishing();
341
342         // remove it from the queue
343         queue.poll().replaceItem(null);
344
345         invokeTimeoutHandler();
346
347         // count should have been bumped
348         assertEquals(1, req.getRetryCount());
349
350         // should have invoked startPublishing() a second time
351         verify(dispatcher, times(2)).register(eq(msg.getRequestId()), any());
352     }
353
354     @Test
355     public void testHandleTimeout_NotPublishing() {
356         req.startPublishing();
357
358         req.stopPublishing();
359
360         invokeTimeoutHandler();
361
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());
365     }
366
367     @Test
368     public void testHandleTimeout_RetryExhausted() {
369         req.startPublishing();
370
371         // exhaust the count
372         req.bumpRetryCount();
373         req.bumpRetryCount();
374         req.bumpRetryCount();
375
376         // remove it from the queue
377         queue.poll().replaceItem(null);
378
379         invokeTimeoutHandler();
380
381         // should NOT have invoked startPublishing() a second time
382         verify(dispatcher, times(1)).register(eq(msg.getRequestId()), any());
383
384         verify(listener).retryCountExhausted(req);
385     }
386
387     @Test
388     public void testCheckResponse_Matched() {
389         req.startPublishing();
390
391         invokeProcessResponse(response);
392
393         verify(listener).success(PDP1, response);
394         verify(listener, never()).failure(any(), any());
395     }
396
397     @Test
398     public void testCheckResponse_NullName() {
399         req.startPublishing();
400
401         response.setName(null);
402
403         invokeProcessResponse(response);
404
405         verify(listener, never()).success(any(), any());
406         verify(listener).failure(null, "null PDP name");
407     }
408
409     @Test
410     public void testCheckResponse_MismatchedName() {
411         req.startPublishing();
412
413         response.setName(DIFFERENT);
414
415         invokeProcessResponse(response);
416
417         verify(listener, never()).success(any(), any());
418         verify(listener).failure(DIFFERENT, "PDP name does not match");
419     }
420
421     @Test
422     public void testCheckResponse_MismatchedNameWithBroadcast() {
423         msg.setName(null);
424         req.startPublishing();
425
426         response.setName(DIFFERENT);
427
428         invokeProcessResponse(response);
429
430         verify(listener).success(DIFFERENT, response);
431         verify(listener, never()).failure(any(), any());
432     }
433
434     @Test
435     public void testGetName() {
436         assertEquals(MY_REQ_NAME, req.getName());
437     }
438
439     @Test
440     public void testGetMessage() {
441         assertSame(msg, req.getMessage());
442
443         PdpStateChange msg2 = new PdpStateChange();
444         req.reconfigure(msg2);
445         assertSame(msg2, req.getMessage());
446     }
447
448     @Test
449     public void testGetParams() {
450         assertSame(reqParams, req.getParams());
451     }
452
453     @Test
454     public void testGetUndeployPolicies() {
455         assertTrue(req.getUndeployPolicies().isEmpty());
456     }
457
458     private static class MyRequest extends RequestImpl {
459
460         public MyRequest(RequestParams params, String name, PdpMessage message) {
461             super(params, name, message);
462         }
463
464         @Override
465         public boolean reconfigure(PdpMessage newMessage) {
466             reconfigure2(newMessage);
467             return true;
468         }
469     }
470 }