ea90a6ba681684997ae55eaae78e3e1de92b99c2
[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), any(StandardCoder.class)))
140                         .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, BaseTest.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 testTestVcpeSunnyDayCompliant() {
269         checkAppcLcmPolicy("restart", base::testVcpeSunnyDayCompliant);
270     }
271
272     @Test
273     public void testTestVcpeOnsetFloodPrevention() {
274         enqueueAppcLcm("restart");
275         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
276         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
277
278         base.testVcpeOnsetFloodPrevention();
279
280         assertEquals(1, permitCount);
281         assertEquals(1, finalCount);
282
283         assertTrue(appcLcmQueue.isEmpty());
284         assertTrue(clMgtQueue.isEmpty());
285
286         // initial events
287         verify(topics, times(3)).inject(eq(BaseTest.DCAE_TOPIC), any());
288
289         // one restart
290         verify(topics).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
291     }
292
293     @Test
294     public void testTestVdnsSunnyDayCompliant() {
295         checkHttpPolicy(base::testVdnsSunnyDayCompliant);
296     }
297
298     @Test
299     public void testTestVdnsRainyDayCompliant() {
300         checkHttpPolicyCompliantFailure(base::testVdnsRainyDayCompliant);
301     }
302
303     @Test
304     public void testTestVfwSunnyDayCompliant() {
305         checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayCompliant);
306     }
307
308     @Test
309     public void testTestVfwRainyDayOverallTimeout() {
310         checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayOverallTimeout);
311     }
312
313     @Test
314     public void testTestVfwRainyDayCompliantTimeout() {
315         checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayCompliantTimeout);
316     }
317
318     @Test
319     public void testTestVpciSunnyDayCompliant() {
320         checkSdnrPolicy("ModifyConfig", base::testVpciSunnyDayCompliant);
321     }
322
323     @Test
324     public void testTestVsonhSunnyDayCompliant() {
325         checkSdnrPolicy("ModifyConfigANR", base::testVsonhSunnyDayCompliant);
326     }
327
328     protected void checkAppcLcmPolicy(String operation, Runnable test) {
329         enqueueAppcLcm(operation);
330         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
331         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
332
333         test.run();
334
335         assertEquals(1, permitCount);
336         assertEquals(1, finalCount);
337
338         assertTrue(appcLcmQueue.isEmpty());
339         assertTrue(clMgtQueue.isEmpty());
340
341         // initial event
342         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
343
344         // reply to each APPC request
345         verify(topics).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
346     }
347
348     protected void checkAppcLegacyPolicy(String operation, Runnable test) {
349         enqueueAppcLegacy(operation);
350         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
351         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
352
353         test.run();
354
355         assertEquals(1, permitCount);
356         assertEquals(1, finalCount);
357
358         assertTrue(appcLcmQueue.isEmpty());
359         assertTrue(clMgtQueue.isEmpty());
360
361         // initial event
362         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
363
364         // reply to each APPC request
365         verify(topics).inject(eq(BaseTest.APPC_CL_TOPIC), any(), any());
366     }
367
368     protected void checkAppcLegacyPolicyOperationFailure(String operation, Runnable test) {
369         enqueueAppcLegacy(operation);
370         enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
371         enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
372
373         test.run();
374
375         assertEquals(1, permitCount);
376         assertEquals(1, finalCount);
377
378         assertTrue(appcLcmQueue.isEmpty());
379         assertTrue(clMgtQueue.isEmpty());
380
381         // initial event
382         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
383
384         // reply to each APPC request
385         verify(topics).inject(eq(BaseTest.APPC_CL_TOPIC), any(), any());
386     }
387
388     protected void checkAppcLegacyPolicyFinalFailure(String operation, Runnable test) {
389         enqueueAppcLegacy(operation);
390         enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
391
392         test.run();
393
394         assertEquals(1, permitCount);
395         assertEquals(1, finalCount);
396
397         assertTrue(appcLcmQueue.isEmpty());
398         assertTrue(clMgtQueue.isEmpty());
399
400         // initial event
401         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
402
403         // There were no requests sent
404     }
405
406     protected void checkSdnrPolicy(String operation, Runnable test) {
407         enqueueSdnr(operation);
408         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
409         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
410
411         test.run();
412
413         assertEquals(1, permitCount);
414         assertEquals(1, finalCount);
415
416         assertTrue(sdnrQueue.isEmpty());
417         assertTrue(clMgtQueue.isEmpty());
418
419         // initial event
420         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
421
422         // reply to each SDNR request
423         verify(topics).inject(eq(BaseTest.SDNR_CL_RSP_TOPIC), any(), any());
424     }
425
426     protected void checkHttpPolicy(Runnable test) {
427         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
428         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
429
430         test.run();
431
432         assertEquals(1, permitCount);
433         assertEquals(1, finalCount);
434
435         assertTrue(clMgtQueue.isEmpty());
436
437         // initial event
438         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
439     }
440
441     protected void checkHttpPolicyCompliantFailure(Runnable test) {
442         enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
443         enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
444
445         test.run();
446
447         assertEquals(1, permitCount);
448         assertEquals(1, finalCount);
449
450         assertTrue(clMgtQueue.isEmpty());
451
452         // initial event
453         verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
454     }
455
456     private void enqueueClMgt(ControlLoopNotificationType type) {
457         VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
458         notif.setNotification(type);
459         notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
460
461         clMgtQueue.add(notif);
462     }
463
464     private void enqueueAppcLcm(String... operationNames) {
465         for (String oper : operationNames) {
466             AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
467             req.setRpcName(oper);
468
469             AppcLcmBody body = new AppcLcmBody();
470             req.setBody(body);
471
472             AppcLcmInput input = new AppcLcmInput();
473             body.setInput(input);
474
475             AppcLcmCommonHeader header = new AppcLcmCommonHeader();
476             input.setCommonHeader(header);
477
478             header.setSubRequestId("my-subrequest-id");
479
480             appcLcmQueue.add(req);
481         }
482     }
483
484     private void enqueueAppcLegacy(String... operationNames) {
485         for (String oper : operationNames) {
486             Request req = new Request();
487             req.setAction(oper);
488
489             CommonHeader header = new CommonHeader();
490             req.setCommonHeader(header);
491
492             header.setSubRequestId("my-subrequest-id");
493
494             appcLegacyQueue.add(req);
495         }
496     }
497
498     private void enqueueSdnr(String... operationNames) {
499         for (String oper : operationNames) {
500             PciMessage pcimessage = new PciMessage();
501             PciRequest req = new PciRequest();
502             PciBody body = new PciBody();
503             body.setInput(req);
504             pcimessage.setBody(body);
505             pcimessage.getBody().getInput().setAction(oper);
506             PciCommonHeader header = new PciCommonHeader();
507             pcimessage.getBody().getInput().setCommonHeader(header);
508
509             header.setSubRequestId("my-subrequest-id");
510
511             sdnrQueue.add(pcimessage);
512         }
513     }
514
515     private HttpClients makeHttpClients() {
516         return httpClients;
517     }
518
519     private Simulators makeSim() {
520         return simulators;
521     }
522
523     private Topics makeTopics() {
524         return topics;
525     }
526
527     /*
528      * We don't want junit trying to run this, so it's marked "Ignore".
529      */
530     @Ignore
531     private class MyTest extends BaseTest {
532
533         @Override
534         protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
535             permitCount++;
536         }
537
538         @Override
539         protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
540                         Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
541             finalCount++;
542             return policyClMgt.await(notif -> notif.getNotification() == finalType);
543         }
544     }
545 }