ecff7e0bb8a78e641db1efa3768403d9f3fa6052
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.common.rules.test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertSame;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import java.util.LinkedList;
34 import java.util.Map;
35 import java.util.Queue;
36 import java.util.concurrent.atomic.AtomicLong;
37 import java.util.function.Predicate;
38 import java.util.function.Supplier;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Ignore;
43 import org.junit.Test;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.policy.appc.CommonHeader;
47 import org.onap.policy.appc.Request;
48 import org.onap.policy.appclcm.AppcLcmBody;
49 import org.onap.policy.appclcm.AppcLcmCommonHeader;
50 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
51 import org.onap.policy.appclcm.AppcLcmInput;
52 import org.onap.policy.common.utils.coder.StandardCoder;
53 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
54 import org.onap.policy.controlloop.ControlLoopNotificationType;
55 import org.onap.policy.controlloop.VirtualControlLoopNotification;
56 import org.onap.policy.drools.controller.DroolsController;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
59 import org.onap.policy.sdnr.PciBody;
60 import org.onap.policy.sdnr.PciCommonHeader;
61 import org.onap.policy.sdnr.PciMessage;
62 import org.onap.policy.sdnr.PciRequest;
63 import org.powermock.reflect.Whitebox;
64
65 public class BaseTestTest {
66     private static final String POLICY_NAME = "my-policy-name";
67
68     // saved values
69     private static Supplier<HttpClients> httpClientMaker;
70     private static Supplier<Simulators> simMaker;
71     private static Supplier<Topics> topicMaker;
72
73     private BaseTest base;
74     private LinkedList<VirtualControlLoopNotification> clMgtQueue;
75     private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
76     private Queue<Request> appcLegacyQueue;
77     private Queue<PciMessage> sdnrQueue;
78     private int permitCount;
79     private int finalCount;
80
81     @Mock
82     private HttpClients httpClients;
83     @Mock
84     private Simulators simulators;
85     @Mock
86     private Topics topics;
87     @Mock
88     private Listener<VirtualControlLoopNotification> policyClMgt;
89     @Mock
90     private Listener<Request> appcClSink;
91     @Mock
92     private Listener<AppcLcmDmaapWrapper> appcLcmRead;
93     @Mock
94     private Listener<PciMessage> sdnrClSink;
95     @Mock
96     private DroolsController drools;
97     @Mock
98     private ToscaPolicy policy;
99     @Mock
100     private ToscaPolicyIdentifier policyIdent;
101
102
103     /**
104      * Saves static values from the class.
105      */
106     @BeforeClass
107     public static void setUpBeforeClass() {
108         httpClientMaker = Whitebox.getInternalState(BaseTest.class, "httpClientMaker");
109         simMaker = Whitebox.getInternalState(BaseTest.class, "simMaker");
110         topicMaker = Whitebox.getInternalState(BaseTest.class, "topicMaker");
111     }
112
113     /**
114      * Restores static values.
115      */
116     @AfterClass
117     public static void tearDownAfterClass() {
118         Whitebox.setInternalState(BaseTest.class, "httpClientMaker", httpClientMaker);
119         Whitebox.setInternalState(BaseTest.class, "simMaker", simMaker);
120         Whitebox.setInternalState(BaseTest.class, "topicMaker", topicMaker);
121     }
122
123     /**
124      * Sets up.
125      */
126     @Before
127     public void setUp() {
128         MockitoAnnotations.initMocks(this);
129
130         when(policy.getIdentifier()).thenReturn(policyIdent);
131         when(policyIdent.getName()).thenReturn(POLICY_NAME);
132
133         when(topics.createListener(eq(BaseTest.POLICY_CL_MGT_TOPIC), eq(VirtualControlLoopNotification.class),
134                         any(StandardCoder.class))).thenReturn(policyClMgt);
135         when(topics.createListener(eq(BaseTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
136                         any(StandardCoder.class))).thenReturn(appcLcmRead);
137         when(topics.createListener(eq(BaseTest.APPC_CL_TOPIC), eq(Request.class),
138                         any(StandardCoderInstantAsMillis.class))).thenReturn(appcClSink);
139         when(topics.createListener(eq(BaseTest.SDNR_CL_TOPIC), eq(PciMessage.class),
140             any(StandardCoder.class))).thenReturn(sdnrClSink);
141
142         Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
143         Supplier<Simulators> simMaker = this::makeSim;
144         Supplier<Topics> topicMaker = this::makeTopics;
145
146         Whitebox.setInternalState(BaseTest.class, "httpClientMaker", httpClientMaker);
147         Whitebox.setInternalState(BaseTest.class, "simMaker", simMaker);
148         Whitebox.setInternalState(BaseTest.class, "topicMaker", topicMaker);
149
150         clMgtQueue = new LinkedList<>();
151         appcLcmQueue = new LinkedList<>();
152         appcLegacyQueue = new LinkedList<>();
153         sdnrQueue = new LinkedList<>();
154
155         when(policyClMgt.await(any())).thenAnswer(args -> {
156             VirtualControlLoopNotification notif = clMgtQueue.remove();
157             Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
158             assertTrue(pred.test(notif));
159             return notif;
160         });
161
162         when(appcLcmRead.await(any())).thenAnswer(args -> {
163             AppcLcmDmaapWrapper req = appcLcmQueue.remove();
164             Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
165             assertTrue(pred.test(req));
166             return req;
167         });
168
169         when(appcClSink.await(any())).thenAnswer(args -> {
170             Request req = appcLegacyQueue.remove();
171             Predicate<Request> pred = args.getArgument(0);
172             assertTrue(pred.test(req));
173             return req;
174         });
175
176         when(sdnrClSink.await(any())).thenAnswer(args -> {
177             PciMessage pcireq = sdnrQueue.remove();
178             Predicate<PciMessage> pred = args.getArgument(0);
179             assertTrue(pred.test(pcireq));
180             return pcireq;
181         });
182
183         permitCount = 0;
184         finalCount = 0;
185
186         base = new MyTest();
187
188         BaseTest.initStatics();
189         base.init();
190     }
191
192     @Test
193     public void testInitStatics() {
194         assertSame(httpClients, BaseTest.httpClients);
195         assertSame(simulators, BaseTest.simulators);
196     }
197
198     @Test
199     public void testFinishStatics() {
200         BaseTest.finishStatics();
201         verify(httpClients).destroy();
202         verify(simulators).destroy();
203     }
204
205     @Test
206     public void testInit() {
207         assertSame(topics, base.getTopics());
208     }
209
210     @Test
211     public void testFinish() {
212         base.finish();
213         verify(topics).destroy();
214     }
215
216     @Test
217     public void testTestService123Compliant() {
218         enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
219         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
220         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
221
222         base.testService123Compliant();
223
224         assertEquals(1, permitCount);
225         assertEquals(1, finalCount);
226
227         assertTrue(appcLcmQueue.isEmpty());
228         assertTrue(clMgtQueue.isEmpty());
229
230         // initial event
231         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
232
233         // replies to each APPC request
234         verify(topics, times(6)).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
235     }
236
237     @Test
238     public void testTestDuplicatesEvents() {
239         // the test expects the count to be incremented by 2 between calls
240         AtomicLong count = new AtomicLong(5);
241         base = spy(base);
242         when(base.getCreateCount()).thenAnswer(args -> count.getAndAdd(2));
243
244         enqueueAppcLcm("restart", "restart");
245         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
246         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
247
248         clMgtQueue.get(0).setAai(Map.of("generic-vnf.vnf-id", "duplicate-VNF"));
249         clMgtQueue.get(1).setAai(Map.of("generic-vnf.vnf-id", "vCPE_Infrastructure_vGMUX_demo_app"));
250
251         base.testDuplicatesEvents();
252
253         assertEquals(0, permitCount);
254         assertEquals(2, finalCount);
255
256         assertTrue(appcLcmQueue.isEmpty());
257         assertTrue(clMgtQueue.isEmpty());
258
259         // initial events
260         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
261         verify(topics, times(2)).inject(eq(BaseTest.DCAE_TOPIC), any(), any());
262
263         // two restarts
264         verify(topics, times(2)).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
265     }
266
267     @Test
268     public void testTestVcpeSunnyDayLegacy() {
269         checkAppcLcmPolicy("restart", base::testVcpeSunnyDayLegacy);
270     }
271
272     @Test
273     public void testTestVcpeSunnyDayCompliant() {
274         checkAppcLcmPolicy("restart", base::testVcpeSunnyDayCompliant);
275     }
276
277     @Test
278     public void testTestVcpeOnsetFloodPrevention() {
279         enqueueAppcLcm("restart");
280         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
281         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
282
283         base.testVcpeOnsetFloodPrevention();
284
285         assertEquals(1, permitCount);
286         assertEquals(1, finalCount);
287
288         assertTrue(appcLcmQueue.isEmpty());
289         assertTrue(clMgtQueue.isEmpty());
290
291         // initial events
292         verify(topics, times(3)).inject(eq(BaseTest.DCAE_TOPIC), any());
293
294         // one restart
295         verify(topics).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
296     }
297
298     @Test
299     public void testTestVdnsSunnyDayLegacy() {
300         checkHttpPolicy(base::testVdnsSunnyDayLegacy);
301     }
302
303     @Test
304     public void testTestVdnsSunnyDayCompliant() {
305         checkHttpPolicy(base::testVdnsSunnyDayCompliant);
306     }
307
308     @Test
309     public void testTestVdnsRainyDayCompliant() {
310         checkHttpPolicyCompliantFailure(base::testVdnsRainyDayCompliant);
311     }
312
313     @Test
314     public void testTestVfwSunnyDayLegacy() {
315         checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayLegacy);
316     }
317
318     @Test
319     public void testTestVfwSunnyDayCompliant() {
320         checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayCompliant);
321     }
322
323     @Test
324     public void testTestVfwRainyDayLegacyFailure() {
325         checkAppcLegacyPolicyOperationFailure("ModifyConfig", base::testVfwRainyDayLegacyFailure);
326     }
327
328     @Test
329     public void testTestVfwRainyDayOverallTimeout() {
330         checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayOverallTimeout);
331     }
332
333     @Test
334     public void testTestVfwRainyDayCompliantTimeout() {
335         checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayCompliantTimeout);
336     }
337
338     @Test
339     public void testTestVpciSunnyDayLegacy() {
340         checkSdnrPolicy("ModifyConfig", base::testVpciSunnyDayLegacy);
341     }
342
343     @Test
344     public void testTestVpciSunnyDayCompliant() {
345         checkSdnrPolicy("ModifyConfig", base::testVpciSunnyDayCompliant);
346     }
347
348     @Test
349     public void testTestVsonhSunnyDayLegacy() {
350         checkSdnrPolicy("ModifyConfigANR", base::testVsonhSunnyDayLegacy);
351     }
352
353     @Test
354     public void testTestVsonhSunnyDayCompliant() {
355         checkSdnrPolicy("ModifyConfigANR", base::testVsonhSunnyDayCompliant);
356     }
357
358     protected void checkAppcLcmPolicy(String operation, Runnable test) {
359         enqueueAppcLcm(operation);
360         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
361         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
362
363         test.run();
364
365         assertEquals(1, permitCount);
366         assertEquals(1, finalCount);
367
368         assertTrue(appcLcmQueue.isEmpty());
369         assertTrue(clMgtQueue.isEmpty());
370
371         // initial event
372         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
373
374         // reply to each APPC request
375         verify(topics).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
376     }
377
378     protected void checkAppcLegacyPolicy(String operation, Runnable test) {
379         enqueueAppcLegacy(operation);
380         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
381         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
382
383         test.run();
384
385         assertEquals(1, permitCount);
386         assertEquals(1, finalCount);
387
388         assertTrue(appcLcmQueue.isEmpty());
389         assertTrue(clMgtQueue.isEmpty());
390
391         // initial event
392         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
393
394         // reply to each APPC request
395         verify(topics).inject(eq(BaseTest.APPC_CL_TOPIC), any(), any());
396     }
397
398     protected void checkAppcLegacyPolicyOperationFailure(String operation, Runnable test) {
399         enqueueAppcLegacy(operation);
400         enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
401         enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
402
403         test.run();
404
405         assertEquals(1, permitCount);
406         assertEquals(1, finalCount);
407
408         assertTrue(appcLcmQueue.isEmpty());
409         assertTrue(clMgtQueue.isEmpty());
410
411         // initial event
412         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
413
414         // reply to each APPC request
415         verify(topics).inject(eq(BaseTest.APPC_CL_TOPIC), any(), any());
416     }
417
418     protected void checkAppcLegacyPolicyFinalFailure(String operation, Runnable test) {
419         enqueueAppcLegacy(operation);
420         enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
421
422         test.run();
423
424         assertEquals(1, permitCount);
425         assertEquals(1, finalCount);
426
427         assertTrue(appcLcmQueue.isEmpty());
428         assertTrue(clMgtQueue.isEmpty());
429
430         // initial event
431         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
432
433         // There were no requests sent
434     }
435
436     protected void checkSdnrPolicy(String operation, Runnable test) {
437         enqueueSdnr(operation);
438         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
439         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
440
441         test.run();
442
443         assertEquals(1, permitCount);
444         assertEquals(1, finalCount);
445
446         assertTrue(sdnrQueue.isEmpty());
447         assertTrue(clMgtQueue.isEmpty());
448
449         // initial event
450         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
451
452         // reply to each SDNR request
453         verify(topics).inject(eq(BaseTest.SDNR_CL_RSP_TOPIC), any(), any());
454     }
455
456     protected void checkHttpPolicy(Runnable test) {
457         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
458         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
459
460         test.run();
461
462         assertEquals(1, permitCount);
463         assertEquals(1, finalCount);
464
465         assertTrue(clMgtQueue.isEmpty());
466
467         // initial event
468         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
469     }
470
471     protected void checkHttpPolicyCompliantFailure(Runnable test) {
472         enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
473         enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
474
475         test.run();
476
477         assertEquals(1, permitCount);
478         assertEquals(1, finalCount);
479
480         assertTrue(clMgtQueue.isEmpty());
481
482         // initial event
483         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
484     }
485
486     private void enqueueClMgt(ControlLoopNotificationType type) {
487         VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
488         notif.setNotification(type);
489         notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
490
491         clMgtQueue.add(notif);
492     }
493
494     private void enqueueAppcLcm(String... operationNames) {
495         for (String oper : operationNames) {
496             AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
497             req.setRpcName(oper);
498
499             AppcLcmBody body = new AppcLcmBody();
500             req.setBody(body);
501
502             AppcLcmInput input = new AppcLcmInput();
503             body.setInput(input);
504
505             AppcLcmCommonHeader header = new AppcLcmCommonHeader();
506             input.setCommonHeader(header);
507
508             header.setSubRequestId("my-subrequest-id");
509
510             appcLcmQueue.add(req);
511         }
512     }
513
514     private void enqueueAppcLegacy(String... operationNames) {
515         for (String oper : operationNames) {
516             Request req = new Request();
517             req.setAction(oper);
518
519             CommonHeader header = new CommonHeader();
520             req.setCommonHeader(header);
521
522             header.setSubRequestId("my-subrequest-id");
523
524             appcLegacyQueue.add(req);
525         }
526     }
527
528     private void enqueueSdnr(String... operationNames) {
529         for (String oper : operationNames) {
530             PciMessage pcimessage = new PciMessage();
531             PciRequest req = new PciRequest();
532             PciBody body = new PciBody();
533             body.setInput(req);
534             pcimessage.setBody(body);
535             pcimessage.getBody().getInput().setAction(oper);
536             PciCommonHeader header = new PciCommonHeader();
537             pcimessage.getBody().getInput().setCommonHeader(header);
538
539             header.setSubRequestId("my-subrequest-id");
540
541             sdnrQueue.add(pcimessage);
542         }
543     }
544
545     private HttpClients makeHttpClients() {
546         return httpClients;
547     }
548
549     private Simulators makeSim() {
550         return simulators;
551     }
552
553     private Topics makeTopics() {
554         return topics;
555     }
556
557     /*
558      * We don't want junit trying to run this, so it's marked "Ignore".
559      */
560     @Ignore
561     private class MyTest extends BaseTest {
562
563         @Override
564         protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
565             permitCount++;
566         }
567
568         @Override
569         protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
570                         Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
571             finalCount++;
572             return policyClMgt.await(notif -> notif.getNotification() == finalType);
573         }
574     }
575 }