aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/RequestImplTest.java
blob: d57f667fd0e0ecc573a99819096a5c6532821f21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.main.comm.msgdata;

import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.models.pdp.concepts.PdpMessage;
import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.pap.main.comm.CommonRequestBase;
import org.onap.policy.pap.main.comm.QueueToken;
import org.onap.policy.pap.main.parameters.RequestParams;

public class RequestImplTest extends CommonRequestBase {

    private MyRequest req;
    private PdpStatus response;
    private PdpStateChange msg;

    /**
     * Sets up.
     * @throws Exception if an error occurs
     */
    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();

        response = new PdpStatus();
        msg = new PdpStateChange();

        response.setName(PDP1);
        response.setResponse(new PdpResponseDetails());
        response.getResponse().setResponseTo(msg.getRequestId());
        msg.setName(PDP1);

        req = new MyRequest(reqParams, MY_REQ_NAME, msg);
        req.setListener(listener);
    }

    @Test
    public void testRequest_InvalidArgs() {
        // null params
        assertThatThrownBy(() -> new MyRequest(null, MY_REQ_NAME, msg)).isInstanceOf(NullPointerException.class);

        // null name
        assertThatThrownBy(() -> new MyRequest(reqParams, null, msg)).isInstanceOf(NullPointerException.class);

        // null message
        assertThatThrownBy(() -> new MyRequest(reqParams, MY_REQ_NAME, null)).isInstanceOf(NullPointerException.class);

        // invalid params
        assertThatIllegalArgumentException().isThrownBy(() -> new MyRequest(new RequestParams(), MY_REQ_NAME, msg));
    }

    @Test
    public void testReconfigure2_WrongMsgClass() {
        assertThatIllegalArgumentException().isThrownBy(() -> req.reconfigure(new PdpUpdate()))
                        .withMessage("expecting PdpStateChange instead of PdpUpdate");
    }

    @Test
    public void testReconfigure2_NotPublishing() {

        // replace the message with a new message
        req.reconfigure(new PdpStateChange());

        // nothing should have been placed in the queue
        assertNull(queue.poll());
    }

    @Test
    public void testRequestImpl_testReconfigure2_Publishing() {
        req.startPublishing();

        // replace the message with a new message
        PdpStateChange msg2 = new PdpStateChange();
        req.reconfigure(msg2);

        // should have cancelled the first timer
        verify(timer).cancel();

        // should only be one token in the queue
        QueueToken<PdpMessage> token = queue.poll();
        assertNotNull(token);
        assertSame(msg2, token.get());

        verify(dispatcher).register(eq(msg.getRequestId()), any());
        verify(timers).register(eq(msg.getRequestId()), any());
        verify(publisher).enqueue(token);

        verify(dispatcher).unregister(eq(msg.getRequestId()));

        verify(dispatcher).register(eq(msg2.getRequestId()), any());
        verify(timers).register(eq(msg2.getRequestId()), any());
        verify(publisher).enqueue(any());
    }

    @Test
    public void testIsPublishing() {
        assertFalse(req.isPublishing());

        req.startPublishing();
        assertTrue(req.isPublishing());

        req.stopPublishing();
        assertFalse(req.isPublishing());
    }

    @Test
    public void testStartPublishingQueueToken_NoListener() {
        req.setListener(null);
        assertThatIllegalStateException().isThrownBy(() -> req.startPublishing())
                        .withMessage("listener has not been set");
    }

    @Test
    public void testStartPublishing() {
        req.startPublishing();

        assertTrue(req.isPublishing());

        verify(dispatcher).register(eq(msg.getRequestId()), any());
        verify(timers).register(eq(msg.getRequestId()), any());
        verify(publisher).enqueue(any());

        QueueToken<PdpMessage> token = queue.poll();
        assertNotNull(token);
        assertSame(msg, token.get());


        // invoking start() again has no effect - invocation counts remain the same
        req.startPublishing();
        verify(dispatcher, times(1)).register(any(), any());
        verify(timers, times(1)).register(any(), any());
        verify(publisher, times(1)).enqueue(any());
        assertNull(queue.poll());
    }

    @Test
    public void testStopPublishing() {
        // not publishing yet
        req.stopPublishing();
        assertFalse(req.isPublishing());

        // now we'll publish
        req.startPublishing();

        req.stopPublishing();
        assertFalse(req.isPublishing());

        // should only be one token in the queue - should be nulled out
        QueueToken<PdpMessage> token = queue.poll();
        assertNotNull(token);
        assertNull(token.get());

        verify(dispatcher).unregister(eq(msg.getRequestId()));
        verify(timer).cancel();
    }

    @Test
    public void testStopPublishingBoolean_NotPublishing() {
        // should not throw an exception
        Assertions.assertThatCode(() -> req.stopPublishing()).doesNotThrowAnyException();
    }

    @Test
    public void testStopPublishingBoolean_TruePublishing() {
        req.startPublishing();

        req.stopPublishing();

        // should be nulled out
        QueueToken<PdpMessage> token = queue.poll();
        assertNotNull(token);
        assertNull(token.get());

        verify(dispatcher).unregister(eq(msg.getRequestId()));
        verify(timer).cancel();

        // if start publishing again - should use a new token
        req.startPublishing();
        QueueToken<PdpMessage> token2 = queue.poll();
        assertNotNull(token2);
        assertNotSame(token, token2);
        assertSame(msg, token2.get());
    }

    @Test
    public void testEnqueue() {
        req.startPublishing();

        // replace the message with a new message
        PdpStateChange msg2 = new PdpStateChange();
        req.reconfigure(msg2);

        // should still only be one token in the queue
        QueueToken<PdpMessage> token = queue.poll();
        assertNull(queue.poll());
        assertNotNull(token);
        assertSame(msg2, token.get());

        // force the token to be nulled out
        req.stopPublishing();

        // enqueue a new message
        PdpStateChange msg3 = new PdpStateChange();
        req.reconfigure(msg3);
        req.startPublishing();

        // a new token should have been placed in the queue
        QueueToken<PdpMessage> token2 = queue.poll();
        assertNotSame(token, token2);
        assertNull(queue.poll());
        assertNotNull(token2);
        assertSame(msg3, token2.get());

        // zap the token, indicating it's been published
        token2.replaceItem(null);
        PdpStateChange msg4 = new PdpStateChange();
        req.reconfigure(msg4);

        // a new token should have been placed in the queue
        QueueToken<PdpMessage> token3 = queue.poll();
        assertNotSame(token2, token3);
        assertNull(queue.poll());
        assertNotNull(token3);
        assertSame(msg4, token3.get());
    }

    @Test
    public void testResetRetryCount_testBumpRetryCount() {
        req = new MyRequest(new RequestParams().setMaxRetryCount(2).setModifyLock(lock).setPdpPublisher(publisher)
                        .setResponseDispatcher(dispatcher).setTimers(timers), MY_REQ_NAME, msg);
        req.setListener(listener);

        assertEquals(0, req.getRetryCount());
        assertTrue(req.bumpRetryCount());
        assertTrue(req.bumpRetryCount());

        // limit should now be reached and it should go no further
        assertFalse(req.bumpRetryCount());
        assertFalse(req.bumpRetryCount());

        assertEquals(2, req.getRetryCount());

        req.resetRetryCount();
        assertEquals(0, req.getRetryCount());
    }

    @Test
    public void testProcessResponse() {
        req.startPublishing();

        invokeProcessResponse(response);

        verify(listener).success(PDP1);
        verify(listener, never()).failure(any(), any());
        verify(timer).cancel();
    }

    @Test
    public void testProcessResponse_NotPublishing() {
        // force registration with the dispatcher - needed by invokeProcessResponse(response)
        req.startPublishing();
        req.stopPublishing();

        invokeProcessResponse(response);

        verify(listener, never()).success(any());
        verify(listener, never()).failure(any(), any());
    }

    @Test
    public void testProcessResponse_WrongRequest() {
        req.startPublishing();

        response.getResponse().setResponseTo(DIFFERENT);

        invokeProcessResponse(response);

        verify(listener, never()).success(any());
        verify(listener, never()).failure(any(), any());
        verify(timer, never()).cancel();
    }

    @Test
    public void testProcessResponse_ResponseFailed() {
        req.startPublishing();

        response.setName(DIFFERENT);

        invokeProcessResponse(response);

        verify(listener, never()).success(any());
        verify(listener).failure(DIFFERENT, "PDP name does not match");
        verify(timer).cancel();
    }

    @Test
    public void testHandleTimeout() {
        req.startPublishing();

        // remove it from the queue
        queue.poll().replaceItem(null);

        invokeTimeoutHandler();

        // count should have been bumped
        assertEquals(1, req.getRetryCount());

        // should have invoked startPublishing() a second time
        verify(dispatcher, times(2)).register(eq(msg.getRequestId()), any());
    }

    @Test
    public void testHandleTimeout_NotPublishing() {
        req.startPublishing();

        req.stopPublishing();

        invokeTimeoutHandler();

        // should NOT have invoked startPublishing() a second time
        verify(dispatcher, times(1)).register(eq(msg.getRequestId()), any());
        verify(listener, never()).retryCountExhausted();
    }

    @Test
    public void testHandleTimeout_RetryExhausted() {
        req.startPublishing();

        // exhaust the count
        req.bumpRetryCount();
        req.bumpRetryCount();
        req.bumpRetryCount();

        // remove it from the queue
        queue.poll().replaceItem(null);

        invokeTimeoutHandler();

        // should NOT have invoked startPublishing() a second time
        verify(dispatcher, times(1)).register(eq(msg.getRequestId()), any());

        verify(listener).retryCountExhausted();
    }

    @Test
    public void testCheckResponse_Matched() {
        req.startPublishing();

        invokeProcessResponse(response);

        verify(listener).success(PDP1);
        verify(listener, never()).failure(any(), any());
    }

    @Test
    public void testCheckResponse_NullName() {
        req.startPublishing();

        response.setName(null);

        invokeProcessResponse(response);

        verify(listener, never()).success(any());
        verify(listener).failure(null, "null PDP name");
    }

    @Test
    public void testCheckResponse_MismatchedName() {
        req.startPublishing();

        response.setName(DIFFERENT);

        invokeProcessResponse(response);

        verify(listener, never()).success(any());
        verify(listener).failure(DIFFERENT, "PDP name does not match");
    }

    @Test
    public void testCheckResponse_MismatchedNameWithBroadcast() {
        msg.setName(null);
        req.startPublishing();

        response.setName(DIFFERENT);

        invokeProcessResponse(response);

        verify(listener).success(DIFFERENT);
        verify(listener, never()).failure(any(), any());
    }

    @Test
    public void testGetName() {
        assertEquals(MY_REQ_NAME, req.getName());
    }

    @Test
    public void testGetMessage() {
        assertSame(msg, req.getMessage());

        PdpStateChange msg2 = new PdpStateChange();
        req.reconfigure(msg2);
        assertSame(msg2, req.getMessage());
    }

    @Test
    public void testGetParams() {
        assertSame(reqParams, req.getParams());
    }

    @Test
    public void testGetUndeployPolicies() {
        assertTrue(req.getUndeployPolicies().isEmpty());
    }

    private class MyRequest extends RequestImpl {

        public MyRequest(RequestParams params, String name, PdpMessage message) {
            super(params, name, message);
        }

        @Override
        public boolean reconfigure(PdpMessage newMessage) {
            reconfigure2(newMessage);
            return true;
        }
    }
}