2 * ============LICENSE_START=======================================================
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
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.controlloop.common.rules.test;
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.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.util.LinkedList;
34 import java.util.Queue;
35 import java.util.function.Function;
36 import java.util.function.Predicate;
37 import java.util.function.Supplier;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Ignore;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.policy.appc.CommonHeader;
46 import org.onap.policy.appc.Request;
47 import org.onap.policy.appclcm.AppcLcmBody;
48 import org.onap.policy.appclcm.AppcLcmCommonHeader;
49 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
50 import org.onap.policy.appclcm.AppcLcmInput;
51 import org.onap.policy.common.utils.coder.StandardCoder;
52 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
53 import org.onap.policy.controlloop.ControlLoopNotificationType;
54 import org.onap.policy.controlloop.VirtualControlLoopNotification;
55 import org.onap.policy.drools.controller.DroolsController;
56 import org.onap.policy.drools.system.PolicyController;
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;
65 public class BaseRuleTestTest {
66 private static final String CONTROLLER_NAME = "my-controller-name";
67 private static final String POLICY_NAME = "my-policy-name";
70 private static Function<String, Rules> ruleMaker;
71 private static Supplier<HttpClients> httpClientMaker;
72 private static Supplier<Simulators> simMaker;
73 private static Supplier<Topics> topicMaker;
75 private BaseRuleTest base;
76 private LinkedList<VirtualControlLoopNotification> clMgtQueue;
77 private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
78 private Queue<Request> appcLegacyQueue;
79 private Queue<PciMessage> sdnrQueue;
80 private int permitCount;
81 private int finalCount;
84 private PolicyController controller;
88 private HttpClients httpClients;
90 private Simulators simulators;
92 private Topics topics;
94 private Listener<VirtualControlLoopNotification> policyClMgt;
96 private Listener<Request> appcClSink;
98 private Listener<AppcLcmDmaapWrapper> appcLcmRead;
100 private Listener<PciMessage> sdnrClSink;
102 private DroolsController drools;
104 private ToscaPolicy policy;
106 private ToscaPolicyIdentifier policyIdent;
110 * Saves static values from the class.
113 public static void setUpBeforeClass() {
114 ruleMaker = Whitebox.getInternalState(BaseRuleTest.class, "ruleMaker");
115 httpClientMaker = Whitebox.getInternalState(BaseRuleTest.class, "httpClientMaker");
116 simMaker = Whitebox.getInternalState(BaseRuleTest.class, "simMaker");
117 topicMaker = Whitebox.getInternalState(BaseRuleTest.class, "topicMaker");
121 * Restores static values.
124 public static void tearDownAfterClass() {
125 Whitebox.setInternalState(BaseRuleTest.class, "ruleMaker", ruleMaker);
126 Whitebox.setInternalState(BaseRuleTest.class, "httpClientMaker", httpClientMaker);
127 Whitebox.setInternalState(BaseRuleTest.class, "simMaker", simMaker);
128 Whitebox.setInternalState(BaseRuleTest.class, "topicMaker", topicMaker);
135 public void setUp() {
136 MockitoAnnotations.initMocks(this);
138 when(policy.getIdentifier()).thenReturn(policyIdent);
139 when(policyIdent.getName()).thenReturn(POLICY_NAME);
141 when(drools.factCount(CONTROLLER_NAME)).thenReturn(0L);
142 when(controller.getDrools()).thenReturn(drools);
144 when(rules.getControllerName()).thenReturn(CONTROLLER_NAME);
145 when(rules.getController()).thenReturn(controller);
146 when(rules.setupPolicyFromFile(any())).thenAnswer(args -> {
147 when(drools.factCount(CONTROLLER_NAME)).thenReturn(2L);
151 when(topics.createListener(BaseRuleTest.POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller))
152 .thenReturn(policyClMgt);
153 when(topics.createListener(eq(BaseRuleTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
154 any(StandardCoder.class))).thenReturn(appcLcmRead);
155 when(topics.createListener(eq(BaseRuleTest.APPC_CL_TOPIC), eq(Request.class),
156 any(StandardCoderInstantAsMillis.class))).thenReturn(appcClSink);
157 when(topics.createListener(eq(BaseRuleTest.SDNR_CL_TOPIC), eq(PciMessage.class),
158 any(StandardCoder.class))).thenReturn(sdnrClSink);
160 Function<String, Rules> ruleMaker = this::makeRules;
161 Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
162 Supplier<Simulators> simMaker = this::makeSim;
163 Supplier<Topics> topicMaker = this::makeTopics;
165 Whitebox.setInternalState(BaseRuleTest.class, "ruleMaker", ruleMaker);
166 Whitebox.setInternalState(BaseRuleTest.class, "httpClientMaker", httpClientMaker);
167 Whitebox.setInternalState(BaseRuleTest.class, "simMaker", simMaker);
168 Whitebox.setInternalState(BaseRuleTest.class, "topicMaker", topicMaker);
170 clMgtQueue = new LinkedList<>();
171 appcLcmQueue = new LinkedList<>();
172 appcLegacyQueue = new LinkedList<>();
173 sdnrQueue = new LinkedList<>();
175 when(policyClMgt.await(any())).thenAnswer(args -> {
176 VirtualControlLoopNotification notif = clMgtQueue.remove();
177 Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
178 assertTrue(pred.test(notif));
182 when(appcLcmRead.await(any())).thenAnswer(args -> {
183 AppcLcmDmaapWrapper req = appcLcmQueue.remove();
184 Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
185 assertTrue(pred.test(req));
189 when(appcClSink.await(any())).thenAnswer(args -> {
190 Request req = appcLegacyQueue.remove();
191 Predicate<Request> pred = args.getArgument(0);
192 assertTrue(pred.test(req));
196 when(sdnrClSink.await(any())).thenAnswer(args -> {
197 PciMessage pcireq = sdnrQueue.remove();
198 Predicate<PciMessage> pred = args.getArgument(0);
199 assertTrue(pred.test(pcireq));
208 BaseRuleTest.initStatics(CONTROLLER_NAME);
213 public void testInitStatics() {
214 assertSame(rules, BaseRuleTest.rules);
215 assertSame(httpClients, BaseRuleTest.httpClients);
216 assertSame(simulators, BaseRuleTest.simulators);
220 public void testFinishStatics() {
221 BaseRuleTest.finishStatics();
223 verify(rules).destroy();
224 verify(httpClients).destroy();
225 verify(simulators).destroy();
229 public void testInit() {
230 assertSame(topics, base.getTopics());
231 assertSame(controller, base.controller);
235 public void testFinish() {
238 verify(topics).destroy();
239 verify(rules).resetFacts();
243 public void testTestService123Compliant() {
244 enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
245 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
246 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
248 base.testService123Compliant();
250 assertEquals(1, permitCount);
251 assertEquals(1, finalCount);
253 assertTrue(appcLcmQueue.isEmpty());
254 assertTrue(clMgtQueue.isEmpty());
257 verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
259 // replies to each APPC request
260 verify(topics, times(6)).inject(eq(BaseRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
264 public void testTestDuplicatesEvents() {
265 enqueueAppcLcm("restart", "restart");
266 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
267 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
268 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
270 clMgtQueue.get(1).setAai(Map.of("generic-vnf.vnf-id", "duplicate-VNF"));
271 clMgtQueue.get(2).setAai(Map.of("generic-vnf.vnf-id", "vCPE_Infrastructure_vGMUX_demo_app"));
273 base.testDuplicatesEvents();
275 assertEquals(0, permitCount);
276 assertEquals(3, finalCount);
278 assertTrue(appcLcmQueue.isEmpty());
279 assertTrue(clMgtQueue.isEmpty());
282 verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
283 verify(topics, times(2)).inject(eq(BaseRuleTest.DCAE_TOPIC), any(), any());
286 verify(topics, times(2)).inject(eq(BaseRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
290 public void testTestVcpeSunnyDayLegacy() {
291 checkAppcLcmPolicy("restart", base::testVcpeSunnyDayLegacy);
295 public void testTestVcpeSunnyDayCompliant() {
296 checkAppcLcmPolicy("restart", base::testVcpeSunnyDayCompliant);
300 public void testTestVcpeOnsetFloodPrevention() {
301 enqueueAppcLcm("restart");
302 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
303 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
305 base.testVcpeOnsetFloodPrevention();
307 assertEquals(1, permitCount);
308 assertEquals(1, finalCount);
310 assertTrue(appcLcmQueue.isEmpty());
311 assertTrue(clMgtQueue.isEmpty());
314 verify(topics, times(3)).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
317 verify(topics).inject(eq(BaseRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
321 public void testTestVdnsSunnyDayLegacy() {
322 checkHttpPolicy(base::testVdnsSunnyDayLegacy);
326 public void testTestVdnsSunnyDayCompliant() {
327 checkHttpPolicy(base::testVdnsSunnyDayCompliant);
331 public void testTestVfwSunnyDayLegacy() {
332 checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayLegacy);
336 public void testTestVfwSunnyDayCompliant() {
337 checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayCompliant);
341 public void testTestVfwRainyDayLegacyFailure() {
342 checkAppcLegacyPolicyOperationFailure("ModifyConfig", base::testVfwRainyDayLegacyFailure);
346 public void testTestVfwRainyDayOverallTimeout() {
347 checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayOverallTimeout);
351 public void testTestVfwRainyDayCompliantTimeout() {
352 checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayCompliantTimeout);
356 public void testTestVpciSunnyDayLegacy() {
357 checkSdnrPolicy("ModifyConfig", base::testVpciSunnyDayLegacy);
361 public void testTestVpciSunnyDayCompliant() {
362 checkSdnrPolicy("ModifyConfig", base::testVpciSunnyDayCompliant);
366 public void testTestVsonhSunnyDayLegacy() {
367 checkSdnrPolicy("ModifyConfigANR", base::testVsonhSunnyDayLegacy);
371 public void testTestVsonhSunnyDayCompliant() {
372 checkSdnrPolicy("ModifyConfigANR", base::testVsonhSunnyDayCompliant);
375 protected void checkAppcLcmPolicy(String operation, Runnable test) {
376 enqueueAppcLcm(operation);
377 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
378 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
382 assertEquals(1, permitCount);
383 assertEquals(1, finalCount);
385 assertTrue(appcLcmQueue.isEmpty());
386 assertTrue(clMgtQueue.isEmpty());
389 verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
391 // reply to each APPC request
392 verify(topics).inject(eq(BaseRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
395 protected void checkAppcLegacyPolicy(String operation, Runnable test) {
396 enqueueAppcLegacy(operation);
397 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
398 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
402 assertEquals(1, permitCount);
403 assertEquals(1, finalCount);
405 assertTrue(appcLcmQueue.isEmpty());
406 assertTrue(clMgtQueue.isEmpty());
409 verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
411 // reply to each APPC request
412 verify(topics).inject(eq(BaseRuleTest.APPC_CL_TOPIC), any(), any());
415 protected void checkAppcLegacyPolicyOperationFailure(String operation, Runnable test) {
416 enqueueAppcLegacy(operation);
417 enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
418 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
422 assertEquals(1, permitCount);
423 assertEquals(1, finalCount);
425 assertTrue(appcLcmQueue.isEmpty());
426 assertTrue(clMgtQueue.isEmpty());
429 verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
431 // reply to each APPC request
432 verify(topics).inject(eq(BaseRuleTest.APPC_CL_TOPIC), any(), any());
435 protected void checkAppcLegacyPolicyFinalFailure(String operation, Runnable test) {
436 enqueueAppcLegacy(operation);
437 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
441 assertEquals(1, permitCount);
442 assertEquals(1, finalCount);
444 assertTrue(appcLcmQueue.isEmpty());
445 assertTrue(clMgtQueue.isEmpty());
448 verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
450 // There were no requests sent
453 protected void checkSdnrPolicy(String operation, Runnable test) {
454 enqueueSdnr(operation);
455 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
456 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
460 assertEquals(1, permitCount);
461 assertEquals(1, finalCount);
463 assertTrue(sdnrQueue.isEmpty());
464 assertTrue(clMgtQueue.isEmpty());
467 verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
469 // reply to each SDNR request
470 verify(topics).inject(eq(BaseRuleTest.SDNR_CL_RSP_TOPIC), any(), any());
473 protected void checkHttpPolicy(Runnable test) {
474 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
475 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
479 assertEquals(1, permitCount);
480 assertEquals(1, finalCount);
482 assertTrue(clMgtQueue.isEmpty());
485 verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
488 private void enqueueClMgt(ControlLoopNotificationType type) {
489 VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
490 notif.setNotification(type);
491 notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
493 clMgtQueue.add(notif);
496 private void enqueueAppcLcm(String... operationNames) {
497 for (String oper : operationNames) {
498 AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
499 req.setRpcName(oper);
501 AppcLcmBody body = new AppcLcmBody();
504 AppcLcmInput input = new AppcLcmInput();
505 body.setInput(input);
507 AppcLcmCommonHeader header = new AppcLcmCommonHeader();
508 input.setCommonHeader(header);
510 header.setSubRequestId("my-subrequest-id");
512 appcLcmQueue.add(req);
516 private void enqueueAppcLegacy(String... operationNames) {
517 for (String oper : operationNames) {
518 Request req = new Request();
521 CommonHeader header = new CommonHeader();
522 req.setCommonHeader(header);
524 header.setSubRequestId("my-subrequest-id");
526 appcLegacyQueue.add(req);
530 private void enqueueSdnr(String... operationNames) {
531 for (String oper : operationNames) {
532 PciMessage pcimessage = new PciMessage();
533 PciRequest req = new PciRequest();
534 PciBody body = new PciBody();
536 pcimessage.setBody(body);
537 pcimessage.getBody().getInput().setAction(oper);
538 PciCommonHeader header = new PciCommonHeader();
539 pcimessage.getBody().getInput().setCommonHeader(header);
541 header.setSubRequestId("my-subrequest-id");
543 sdnrQueue.add(pcimessage);
547 private Rules makeRules(String controllerName) {
551 private HttpClients makeHttpClients() {
555 private Simulators makeSim() {
559 private Topics makeTopics() {
564 * We don't want junit trying to run this, so it's marked "Ignore".
567 private class MyTest extends BaseRuleTest {
570 protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
575 protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
576 Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
578 return policyClMgt.await(notif -> notif.getNotification() == finalType);