2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.policy.appc.Request;
46 import org.onap.policy.appclcm.AppcLcmBody;
47 import org.onap.policy.appclcm.AppcLcmCommonHeader;
48 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
49 import org.onap.policy.appclcm.AppcLcmInput;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
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 public class DroolsRuleTestTest {
63 private static final String CONTROLLER_NAME = "my-controller-name";
64 private static final String POLICY_NAME = "my-policy-name";
67 private static Function<String, Rules> ruleMaker;
68 private static Supplier<HttpClients> httpClientMaker;
69 private static Supplier<Simulators> simMaker;
70 private static Supplier<Topics> topicMaker;
72 private DroolsRuleTest base;
73 private LinkedList<VirtualControlLoopNotification> clMgtQueue;
74 private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
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 Listener<PciMessage> sdnrClSink;
97 private DroolsController drools;
99 private ToscaPolicy policy;
101 private ToscaConceptIdentifier policyIdent;
105 * Saves static values from the class.
108 public static void setUpBeforeClass() {
109 ruleMaker = Whitebox.getInternalState(DroolsRuleTest.class, "ruleMaker");
110 httpClientMaker = Whitebox.getInternalState(DroolsRuleTest.class, "httpClientMaker");
111 simMaker = Whitebox.getInternalState(DroolsRuleTest.class, "simMaker");
112 topicMaker = Whitebox.getInternalState(DroolsRuleTest.class, "topicMaker");
116 * Restores static values.
119 public static void tearDownAfterClass() {
120 Whitebox.setInternalState(DroolsRuleTest.class, "ruleMaker", ruleMaker);
121 Whitebox.setInternalState(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
122 Whitebox.setInternalState(DroolsRuleTest.class, "simMaker", simMaker);
123 Whitebox.setInternalState(DroolsRuleTest.class, "topicMaker", topicMaker);
130 public void setUp() {
131 MockitoAnnotations.initMocks(this);
133 when(policy.getIdentifier()).thenReturn(policyIdent);
134 when(policyIdent.getName()).thenReturn(POLICY_NAME);
136 when(drools.factCount(CONTROLLER_NAME)).thenReturn(0L);
137 when(controller.getDrools()).thenReturn(drools);
139 when(rules.getControllerName()).thenReturn(CONTROLLER_NAME);
140 when(rules.getController()).thenReturn(controller);
141 when(rules.setupPolicyFromFile(any())).thenAnswer(args -> {
142 when(drools.factCount(CONTROLLER_NAME)).thenReturn(2L);
146 when(topics.createListener(DroolsRuleTest.POLICY_CL_MGT_TOPIC,
147 VirtualControlLoopNotification.class, controller)).thenReturn(policyClMgt);
148 when(topics.createListener(eq(DroolsRuleTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
149 any(StandardCoder.class))).thenReturn(appcLcmRead);
150 when(topics.createListener(eq(DroolsRuleTest.APPC_CL_TOPIC), eq(Request.class),
151 any(StandardCoderInstantAsMillis.class))).thenReturn(appcClSink);
152 when(topics.createListener(eq(DroolsRuleTest.SDNR_CL_TOPIC), eq(PciMessage.class),
153 any(StandardCoder.class))).thenReturn(sdnrClSink);
155 Function<String, Rules> ruleMaker = this::makeRules;
156 Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
157 Supplier<Simulators> simMaker = this::makeSim;
158 Supplier<Topics> topicMaker = this::makeTopics;
160 Whitebox.setInternalState(DroolsRuleTest.class, "ruleMaker", ruleMaker);
161 Whitebox.setInternalState(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
162 Whitebox.setInternalState(DroolsRuleTest.class, "simMaker", simMaker);
163 Whitebox.setInternalState(DroolsRuleTest.class, "topicMaker", topicMaker);
165 clMgtQueue = new LinkedList<>();
166 appcLcmQueue = new LinkedList<>();
168 when(policyClMgt.await(any())).thenAnswer(args -> {
169 VirtualControlLoopNotification notif = clMgtQueue.remove();
170 Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
171 assertTrue(pred.test(notif));
175 when(appcLcmRead.await(any())).thenAnswer(args -> {
176 AppcLcmDmaapWrapper req = appcLcmQueue.remove();
177 Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
178 assertTrue(pred.test(req));
185 base = new MyDroolsTest();
186 DroolsRuleTest.initStatics(CONTROLLER_NAME);
191 public void testInitStatics() {
192 assertSame(rules, DroolsRuleTest.rules);
193 assertSame(httpClients, DroolsRuleTest.httpClients);
194 assertSame(simulators, DroolsRuleTest.simulators);
198 public void testFinishStatics() {
199 DroolsRuleTest.finishStatics();
201 verify(rules).destroy();
202 verify(httpClients).destroy();
203 verify(simulators).destroy();
207 public void testInit() {
208 assertSame(topics, BaseTest.getTopics());
209 assertSame(controller, base.controller);
213 public void testDroolsTestService123Compliant() {
214 enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
215 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
216 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
217 System.out.println("Drools TestTest Here");
218 base.testService123Compliant();
220 assertEquals(1, permitCount);
221 assertEquals(1, finalCount);
223 assertTrue(appcLcmQueue.isEmpty());
224 assertTrue(clMgtQueue.isEmpty());
227 verify(topics).inject(eq(DroolsRuleTest.DCAE_TOPIC), any());
229 // replies to each APPC request
230 verify(topics, times(6)).inject(eq(DroolsRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
233 private void enqueueClMgt(ControlLoopNotificationType type) {
234 VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
235 notif.setNotification(type);
236 notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
238 clMgtQueue.add(notif);
241 private void enqueueAppcLcm(String... operationNames) {
242 for (String oper : operationNames) {
243 AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
244 req.setRpcName(oper);
246 AppcLcmBody body = new AppcLcmBody();
249 AppcLcmInput input = new AppcLcmInput();
250 body.setInput(input);
252 AppcLcmCommonHeader header = new AppcLcmCommonHeader();
253 input.setCommonHeader(header);
255 header.setSubRequestId("my-subrequest-id");
257 appcLcmQueue.add(req);
261 private Rules makeRules(String controllerName) {
265 private HttpClients makeHttpClients() {
269 private Simulators makeSim() {
273 private Topics makeTopics() {
277 * We don't want junit trying to run this, so it's marked "Ignore".
281 private class MyDroolsTest extends DroolsRuleTest {
284 protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
289 protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
290 Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
292 return policyClMgt.await(notif -> notif.getNotification() == finalType);