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.powermock.reflect.Whitebox;
 
  61 public class BaseRuleTestTest {
 
  62     private static final String CONTROLLER_NAME = "my-controller-name";
 
  63     private static final String POLICY_NAME = "my-policy-name";
 
  66     private static Function<String, Rules> ruleMaker;
 
  67     private static Supplier<HttpClients> httpClientMaker;
 
  68     private static Supplier<Simulators> simMaker;
 
  69     private static Supplier<Topics> topicMaker;
 
  71     private BaseRuleTest base;
 
  72     private LinkedList<VirtualControlLoopNotification> clMgtQueue;
 
  73     private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
 
  74     private Queue<Request> appcLegacyQueue;
 
  75     private int permitCount;
 
  76     private int finalCount;
 
  79     private PolicyController controller;
 
  83     private HttpClients httpClients;
 
  85     private Simulators simulators;
 
  87     private Topics topics;
 
  89     private Listener<VirtualControlLoopNotification> policyClMgt;
 
  91     private Listener<Request> appcClSink;
 
  93     private Listener<AppcLcmDmaapWrapper> appcLcmRead;
 
  95     private DroolsController drools;
 
  97     private ToscaPolicy policy;
 
  99     private ToscaPolicyIdentifier policyIdent;
 
 103      * Saves static values from the class.
 
 106     public static void setUpBeforeClass() {
 
 107         ruleMaker = Whitebox.getInternalState(BaseRuleTest.class, "ruleMaker");
 
 108         httpClientMaker = Whitebox.getInternalState(BaseRuleTest.class, "httpClientMaker");
 
 109         simMaker = Whitebox.getInternalState(BaseRuleTest.class, "simMaker");
 
 110         topicMaker = Whitebox.getInternalState(BaseRuleTest.class, "topicMaker");
 
 114      * Restores static values.
 
 117     public static void tearDownAfterClass() {
 
 118         Whitebox.setInternalState(BaseRuleTest.class, "ruleMaker", ruleMaker);
 
 119         Whitebox.setInternalState(BaseRuleTest.class, "httpClientMaker", httpClientMaker);
 
 120         Whitebox.setInternalState(BaseRuleTest.class, "simMaker", simMaker);
 
 121         Whitebox.setInternalState(BaseRuleTest.class, "topicMaker", topicMaker);
 
 128     public void setUp() {
 
 129         MockitoAnnotations.initMocks(this);
 
 131         when(policy.getIdentifier()).thenReturn(policyIdent);
 
 132         when(policyIdent.getName()).thenReturn(POLICY_NAME);
 
 134         when(drools.factCount(CONTROLLER_NAME)).thenReturn(0L);
 
 135         when(controller.getDrools()).thenReturn(drools);
 
 137         when(rules.getControllerName()).thenReturn(CONTROLLER_NAME);
 
 138         when(rules.getController()).thenReturn(controller);
 
 139         when(rules.setupPolicyFromFile(any())).thenAnswer(args -> {
 
 140             when(drools.factCount(CONTROLLER_NAME)).thenReturn(2L);
 
 144         when(topics.createListener(BaseRuleTest.POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller))
 
 145                         .thenReturn(policyClMgt);
 
 146         when(topics.createListener(eq(BaseRuleTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
 
 147                         any(StandardCoder.class))).thenReturn(appcLcmRead);
 
 148         when(topics.createListener(eq(BaseRuleTest.APPC_CL_TOPIC), eq(Request.class),
 
 149                         any(StandardCoderInstantAsMillis.class))).thenReturn(appcClSink);
 
 151         Function<String, Rules> ruleMaker = this::makeRules;
 
 152         Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
 
 153         Supplier<Simulators> simMaker = this::makeSim;
 
 154         Supplier<Topics> topicMaker = this::makeTopics;
 
 156         Whitebox.setInternalState(BaseRuleTest.class, "ruleMaker", ruleMaker);
 
 157         Whitebox.setInternalState(BaseRuleTest.class, "httpClientMaker", httpClientMaker);
 
 158         Whitebox.setInternalState(BaseRuleTest.class, "simMaker", simMaker);
 
 159         Whitebox.setInternalState(BaseRuleTest.class, "topicMaker", topicMaker);
 
 161         clMgtQueue = new LinkedList<>();
 
 162         appcLcmQueue = new LinkedList<>();
 
 163         appcLegacyQueue = new LinkedList<>();
 
 165         when(policyClMgt.await(any())).thenAnswer(args -> {
 
 166             VirtualControlLoopNotification notif = clMgtQueue.remove();
 
 167             Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
 
 168             assertTrue(pred.test(notif));
 
 172         when(appcLcmRead.await(any())).thenAnswer(args -> {
 
 173             AppcLcmDmaapWrapper req = appcLcmQueue.remove();
 
 174             Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
 
 175             assertTrue(pred.test(req));
 
 179         when(appcClSink.await(any())).thenAnswer(args -> {
 
 180             Request req = appcLegacyQueue.remove();
 
 181             Predicate<Request> pred = args.getArgument(0);
 
 182             assertTrue(pred.test(req));
 
 191         BaseRuleTest.initStatics(CONTROLLER_NAME);
 
 196     public void testInitStatics() {
 
 197         assertSame(rules, BaseRuleTest.rules);
 
 198         assertSame(httpClients, BaseRuleTest.httpClients);
 
 199         assertSame(simulators, BaseRuleTest.simulators);
 
 203     public void testFinishStatics() {
 
 204         BaseRuleTest.finishStatics();
 
 206         verify(rules).destroy();
 
 207         verify(httpClients).destroy();
 
 208         verify(simulators).destroy();
 
 212     public void testInit() {
 
 213         assertSame(topics, base.getTopics());
 
 214         assertSame(controller, base.controller);
 
 218     public void testFinish() {
 
 221         verify(topics).destroy();
 
 222         verify(rules).resetFacts();
 
 226     public void testTestService123Compliant() {
 
 227         enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
 
 228         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
 
 229         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
 
 231         base.testService123Compliant();
 
 233         assertEquals(1, permitCount);
 
 234         assertEquals(1, finalCount);
 
 236         assertTrue(appcLcmQueue.isEmpty());
 
 237         assertTrue(clMgtQueue.isEmpty());
 
 240         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
 
 242         // replies to each APPC request
 
 243         verify(topics, times(6)).inject(eq(BaseRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
 
 247     public void testTestDuplicatesEvents() {
 
 248         enqueueAppcLcm("restart", "restart");
 
 249         enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
 
 250         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
 
 251         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
 
 253         clMgtQueue.get(1).setAai(Map.of("generic-vnf.vnf-id", "duplicate-VNF"));
 
 254         clMgtQueue.get(2).setAai(Map.of("generic-vnf.vnf-id", "vCPE_Infrastructure_vGMUX_demo_app"));
 
 256         base.testDuplicatesEvents();
 
 258         assertEquals(0, permitCount);
 
 259         assertEquals(3, finalCount);
 
 261         assertTrue(appcLcmQueue.isEmpty());
 
 262         assertTrue(clMgtQueue.isEmpty());
 
 265         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
 
 266         verify(topics, times(2)).inject(eq(BaseRuleTest.DCAE_TOPIC), any(), any());
 
 269         verify(topics, times(2)).inject(eq(BaseRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
 
 273     public void testTestVcpeSunnyDayLegacy() {
 
 274         checkAppcLcmPolicy("restart", base::testVcpeSunnyDayLegacy);
 
 278     public void testTestVcpeSunnyDayCompliant() {
 
 279         checkAppcLcmPolicy("restart", base::testVcpeSunnyDayCompliant);
 
 283     public void testTestVcpeOnsetFloodPrevention() {
 
 284         enqueueAppcLcm("restart");
 
 285         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
 
 286         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
 
 288         base.testVcpeOnsetFloodPrevention();
 
 290         assertEquals(1, permitCount);
 
 291         assertEquals(1, finalCount);
 
 293         assertTrue(appcLcmQueue.isEmpty());
 
 294         assertTrue(clMgtQueue.isEmpty());
 
 297         verify(topics, times(3)).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
 
 300         verify(topics).inject(eq(BaseRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
 
 304     public void testTestVdnsSunnyDayLegacy() {
 
 305         checkHttpPolicy(base::testVdnsSunnyDayLegacy);
 
 309     public void testTestVdnsSunnyDayCompliant() {
 
 310         checkHttpPolicy(base::testVdnsSunnyDayCompliant);
 
 314     public void testTestVfwSunnyDayLegacy() {
 
 315         checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayLegacy);
 
 319     public void testTestVfwSunnyDayCompliant() {
 
 320         checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayCompliant);
 
 324     public void testTestVfwRainyDayLegacyFailure() {
 
 325         checkAppcLegacyPolicyOperationFailure("ModifyConfig", base::testVfwRainyDayLegacyFailure);
 
 329     public void testTestVfwRainyDayOverallTimeout() {
 
 330         checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayOverallTimeout);
 
 334     public void testTestVfwRainyDayCompliantTimeout() {
 
 335         checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayCompliantTimeout);
 
 338     protected void checkAppcLcmPolicy(String operation, Runnable test) {
 
 339         enqueueAppcLcm(operation);
 
 340         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
 
 341         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
 
 345         assertEquals(1, permitCount);
 
 346         assertEquals(1, finalCount);
 
 348         assertTrue(appcLcmQueue.isEmpty());
 
 349         assertTrue(clMgtQueue.isEmpty());
 
 352         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
 
 354         // reply to each APPC request
 
 355         verify(topics).inject(eq(BaseRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
 
 358     protected void checkAppcLegacyPolicy(String operation, Runnable test) {
 
 359         enqueueAppcLegacy(operation);
 
 360         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
 
 361         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
 
 365         assertEquals(1, permitCount);
 
 366         assertEquals(1, finalCount);
 
 368         assertTrue(appcLcmQueue.isEmpty());
 
 369         assertTrue(clMgtQueue.isEmpty());
 
 372         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
 
 374         // reply to each APPC request
 
 375         verify(topics).inject(eq(BaseRuleTest.APPC_CL_TOPIC), any(), any());
 
 378     protected void checkAppcLegacyPolicyOperationFailure(String operation, Runnable test) {
 
 379         enqueueAppcLegacy(operation);
 
 380         enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
 
 381         enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
 
 385         assertEquals(1, permitCount);
 
 386         assertEquals(1, finalCount);
 
 388         assertTrue(appcLcmQueue.isEmpty());
 
 389         assertTrue(clMgtQueue.isEmpty());
 
 392         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
 
 394         // reply to each APPC request
 
 395         verify(topics).inject(eq(BaseRuleTest.APPC_CL_TOPIC), any(), any());
 
 398     protected void checkAppcLegacyPolicyFinalFailure(String operation, Runnable test) {
 
 399         enqueueAppcLegacy(operation);
 
 400         enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
 
 404         assertEquals(1, permitCount);
 
 405         assertEquals(1, finalCount);
 
 407         assertTrue(appcLcmQueue.isEmpty());
 
 408         assertTrue(clMgtQueue.isEmpty());
 
 411         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
 
 413         // There were no requests sent
 
 416     protected void checkHttpPolicy(Runnable test) {
 
 417         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
 
 418         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
 
 422         assertEquals(1, permitCount);
 
 423         assertEquals(1, finalCount);
 
 425         assertTrue(clMgtQueue.isEmpty());
 
 428         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
 
 431     private void enqueueClMgt(ControlLoopNotificationType type) {
 
 432         VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
 
 433         notif.setNotification(type);
 
 434         notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
 
 436         clMgtQueue.add(notif);
 
 439     private void enqueueAppcLcm(String... operationNames) {
 
 440         for (String oper : operationNames) {
 
 441             AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
 
 442             req.setRpcName(oper);
 
 444             AppcLcmBody body = new AppcLcmBody();
 
 447             AppcLcmInput input = new AppcLcmInput();
 
 448             body.setInput(input);
 
 450             AppcLcmCommonHeader header = new AppcLcmCommonHeader();
 
 451             input.setCommonHeader(header);
 
 453             header.setSubRequestId("my-subrequest-id");
 
 455             appcLcmQueue.add(req);
 
 459     private void enqueueAppcLegacy(String... operationNames) {
 
 460         for (String oper : operationNames) {
 
 461             Request req = new Request();
 
 464             CommonHeader header = new CommonHeader();
 
 465             req.setCommonHeader(header);
 
 467             header.setSubRequestId("my-subrequest-id");
 
 469             appcLegacyQueue.add(req);
 
 473     private Rules makeRules(String controllerName) {
 
 477     private HttpClients makeHttpClients() {
 
 481     private Simulators makeSim() {
 
 485     private Topics makeTopics() {
 
 490      * We don't want junit trying to run this, so it's marked "Ignore".
 
 493     private class MyTest extends BaseRuleTest {
 
 496         protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
 
 501         protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
 
 502                         Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
 
 504             return policyClMgt.await(notif -> notif.getNotification() == finalType);