2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
 
   6  * Modifications Copyright (C) 2021 Nordix Foundation.
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.onap.policy.controlloop.common.rules.test;
 
  24 import static org.junit.Assert.assertEquals;
 
  25 import static org.junit.Assert.assertSame;
 
  26 import static org.junit.Assert.assertTrue;
 
  27 import static org.mockito.ArgumentMatchers.any;
 
  28 import static org.mockito.ArgumentMatchers.eq;
 
  29 import static org.mockito.Mockito.times;
 
  30 import static org.mockito.Mockito.verify;
 
  31 import static org.mockito.Mockito.when;
 
  33 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.junit.runner.RunWith;
 
  44 import org.mockito.Mock;
 
  45 import org.mockito.junit.MockitoJUnitRunner;
 
  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.controlloop.ControlLoopNotificationType;
 
  53 import org.onap.policy.controlloop.VirtualControlLoopNotification;
 
  54 import org.onap.policy.drools.controller.DroolsController;
 
  55 import org.onap.policy.drools.system.PolicyController;
 
  56 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 
  57 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 
  58 import org.onap.policy.sdnr.PciMessage;
 
  59 import org.powermock.reflect.Whitebox;
 
  61 @RunWith(MockitoJUnitRunner.class)
 
  62 public class DroolsRuleTestTest {
 
  64     private static final String CONTROLLER_NAME = "my-controller-name";
 
  65     private static final String POLICY_NAME = "my-policy-name";
 
  68     private static Function<String, Rules> ruleMaker;
 
  69     private static Supplier<HttpClients> httpClientMaker;
 
  70     private static Supplier<Simulators> simMaker;
 
  71     private static Supplier<Topics> topicMaker;
 
  73     private DroolsRuleTest base;
 
  74     private LinkedList<VirtualControlLoopNotification> clMgtQueue;
 
  75     private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
 
  76     private int permitCount;
 
  77     private int finalCount;
 
  80     private PolicyController controller;
 
  84     private HttpClients httpClients;
 
  86     private Simulators simulators;
 
  88     private Topics topics;
 
  90     private Listener<VirtualControlLoopNotification> policyClMgt;
 
  92     private Listener<Request> appcClSink;
 
  94     private Listener<AppcLcmDmaapWrapper> appcLcmRead;
 
  96     private Listener<PciMessage> sdnrClSink;
 
  98     private DroolsController drools;
 
 100     private ToscaPolicy policy;
 
 102     private ToscaConceptIdentifier policyIdent;
 
 106      * Saves static values from the class.
 
 109     public static void setUpBeforeClass() {
 
 110         ruleMaker = Whitebox.getInternalState(DroolsRuleTest.class, "ruleMaker");
 
 111         httpClientMaker = Whitebox.getInternalState(DroolsRuleTest.class, "httpClientMaker");
 
 112         simMaker = Whitebox.getInternalState(DroolsRuleTest.class, "simMaker");
 
 113         topicMaker = Whitebox.getInternalState(DroolsRuleTest.class, "topicMaker");
 
 117      * Restores static values.
 
 120     public static void tearDownAfterClass() {
 
 121         Whitebox.setInternalState(DroolsRuleTest.class, "ruleMaker", ruleMaker);
 
 122         Whitebox.setInternalState(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
 
 123         Whitebox.setInternalState(DroolsRuleTest.class, "simMaker", simMaker);
 
 124         Whitebox.setInternalState(DroolsRuleTest.class, "topicMaker", topicMaker);
 
 131     public void setUp() {
 
 132         when(rules.getController()).thenReturn(controller);
 
 133         when(rules.setupPolicyFromFile(any())).thenReturn(policy);
 
 135         when(topics.createListener(DroolsRuleTest.POLICY_CL_MGT_TOPIC,
 
 136              VirtualControlLoopNotification.class, controller)).thenReturn(policyClMgt);
 
 137         when(topics.createListener(eq(DroolsRuleTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
 
 138                         any(StandardCoder.class))).thenReturn(appcLcmRead);
 
 140         Function<String, Rules> ruleMaker = this::makeRules;
 
 141         Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
 
 142         Supplier<Simulators> simMaker = this::makeSim;
 
 143         Supplier<Topics> topicMaker = this::makeTopics;
 
 145         Whitebox.setInternalState(DroolsRuleTest.class, "ruleMaker", ruleMaker);
 
 146         Whitebox.setInternalState(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
 
 147         Whitebox.setInternalState(DroolsRuleTest.class, "simMaker", simMaker);
 
 148         Whitebox.setInternalState(DroolsRuleTest.class, "topicMaker", topicMaker);
 
 150         clMgtQueue = new LinkedList<>();
 
 151         appcLcmQueue = new LinkedList<>();
 
 153         when(policyClMgt.await(any())).thenAnswer(args -> {
 
 154             VirtualControlLoopNotification notif = clMgtQueue.remove();
 
 155             Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
 
 156             assertTrue(pred.test(notif));
 
 160         when(appcLcmRead.await(any())).thenAnswer(args -> {
 
 161             AppcLcmDmaapWrapper req = appcLcmQueue.remove();
 
 162             Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
 
 163             assertTrue(pred.test(req));
 
 170         base = new MyDroolsTest();
 
 171         DroolsRuleTest.initStatics(CONTROLLER_NAME);
 
 176     public void testInitStatics() {
 
 177         assertSame(rules, DroolsRuleTest.rules);
 
 178         assertSame(httpClients, DroolsRuleTest.httpClients);
 
 179         assertSame(simulators, DroolsRuleTest.simulators);
 
 183     public void testFinishStatics() {
 
 184         DroolsRuleTest.finishStatics();
 
 186         verify(rules).destroy();
 
 187         verify(httpClients).destroy();
 
 188         verify(simulators).destroy();
 
 192     public void testInit() {
 
 193         assertSame(topics, BaseTest.getTopics());
 
 194         assertSame(controller, base.controller);
 
 198     public void testDroolsTestService123Compliant() {
 
 199         enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
 
 200         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
 
 201         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
 
 202         System.out.println("Drools TestTest Here");
 
 203         base.testService123Compliant();
 
 205         assertEquals(1, permitCount);
 
 206         assertEquals(1, finalCount);
 
 208         assertTrue(appcLcmQueue.isEmpty());
 
 209         assertTrue(clMgtQueue.isEmpty());
 
 212         verify(topics).inject(eq(DroolsRuleTest.DCAE_TOPIC), any());
 
 214         // replies to each APPC request
 
 215         verify(topics, times(6)).inject(eq(DroolsRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
 
 218     private void enqueueClMgt(ControlLoopNotificationType type) {
 
 219         VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
 
 220         notif.setNotification(type);
 
 221         notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
 
 223         clMgtQueue.add(notif);
 
 226     private void enqueueAppcLcm(String... operationNames) {
 
 227         for (String oper : operationNames) {
 
 228             AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
 
 229             req.setRpcName(oper);
 
 231             AppcLcmBody body = new AppcLcmBody();
 
 234             AppcLcmInput input = new AppcLcmInput();
 
 235             body.setInput(input);
 
 237             AppcLcmCommonHeader header = new AppcLcmCommonHeader();
 
 238             input.setCommonHeader(header);
 
 240             header.setSubRequestId("my-subrequest-id");
 
 242             appcLcmQueue.add(req);
 
 246     private Rules makeRules(String controllerName) {
 
 250     private HttpClients makeHttpClients() {
 
 254     private Simulators makeSim() {
 
 258     private Topics makeTopics() {
 
 262      * We don't want junit trying to run this, so it's marked "Ignore".
 
 266     private class MyDroolsTest extends DroolsRuleTest {
 
 269         protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
 
 274         protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
 
 275                         Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
 
 277             return policyClMgt.await(notif -> notif.getNotification() == finalType);