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;
33 import java.util.Queue;
34 import java.util.function.Function;
35 import java.util.function.Predicate;
36 import java.util.function.Supplier;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.policy.appc.Request;
45 import org.onap.policy.appclcm.AppcLcmBody;
46 import org.onap.policy.appclcm.AppcLcmCommonHeader;
47 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
48 import org.onap.policy.appclcm.AppcLcmInput;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
51 import org.onap.policy.controlloop.ControlLoopNotificationType;
52 import org.onap.policy.controlloop.VirtualControlLoopNotification;
53 import org.onap.policy.drools.controller.DroolsController;
54 import org.onap.policy.drools.system.PolicyController;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
57 import org.onap.policy.sdnr.PciMessage;
58 import org.powermock.reflect.Whitebox;
60 public class DroolsRuleTestTest {
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 DroolsRuleTest base;
72 private LinkedList<VirtualControlLoopNotification> clMgtQueue;
73 private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
74 private int permitCount;
75 private int finalCount;
78 private PolicyController controller;
82 private HttpClients httpClients;
84 private Simulators simulators;
86 private Topics topics;
88 private Listener<VirtualControlLoopNotification> policyClMgt;
90 private Listener<Request> appcClSink;
92 private Listener<AppcLcmDmaapWrapper> appcLcmRead;
94 private Listener<PciMessage> sdnrClSink;
96 private DroolsController drools;
98 private ToscaPolicy policy;
100 private ToscaPolicyIdentifier policyIdent;
104 * Saves static values from the class.
107 public static void setUpBeforeClass() {
108 ruleMaker = Whitebox.getInternalState(DroolsRuleTest.class, "ruleMaker");
109 httpClientMaker = Whitebox.getInternalState(DroolsRuleTest.class, "httpClientMaker");
110 simMaker = Whitebox.getInternalState(DroolsRuleTest.class, "simMaker");
111 topicMaker = Whitebox.getInternalState(DroolsRuleTest.class, "topicMaker");
115 * Restores static values.
118 public static void tearDownAfterClass() {
119 Whitebox.setInternalState(DroolsRuleTest.class, "ruleMaker", ruleMaker);
120 Whitebox.setInternalState(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
121 Whitebox.setInternalState(DroolsRuleTest.class, "simMaker", simMaker);
122 Whitebox.setInternalState(DroolsRuleTest.class, "topicMaker", topicMaker);
129 public void setUp() {
130 MockitoAnnotations.initMocks(this);
132 when(policy.getIdentifier()).thenReturn(policyIdent);
133 when(policyIdent.getName()).thenReturn(POLICY_NAME);
135 when(drools.factCount(CONTROLLER_NAME)).thenReturn(0L);
136 when(controller.getDrools()).thenReturn(drools);
138 when(rules.getControllerName()).thenReturn(CONTROLLER_NAME);
139 when(rules.getController()).thenReturn(controller);
140 when(rules.setupPolicyFromFile(any())).thenAnswer(args -> {
141 when(drools.factCount(CONTROLLER_NAME)).thenReturn(2L);
145 when(topics.createListener(DroolsRuleTest.POLICY_CL_MGT_TOPIC,
146 VirtualControlLoopNotification.class, controller)).thenReturn(policyClMgt);
147 when(topics.createListener(eq(DroolsRuleTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
148 any(StandardCoder.class))).thenReturn(appcLcmRead);
149 when(topics.createListener(eq(DroolsRuleTest.APPC_CL_TOPIC), eq(Request.class),
150 any(StandardCoderInstantAsMillis.class))).thenReturn(appcClSink);
151 when(topics.createListener(eq(DroolsRuleTest.SDNR_CL_TOPIC), eq(PciMessage.class),
152 any(StandardCoder.class))).thenReturn(sdnrClSink);
154 Function<String, Rules> ruleMaker = this::makeRules;
155 Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
156 Supplier<Simulators> simMaker = this::makeSim;
157 Supplier<Topics> topicMaker = this::makeTopics;
159 Whitebox.setInternalState(DroolsRuleTest.class, "ruleMaker", ruleMaker);
160 Whitebox.setInternalState(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
161 Whitebox.setInternalState(DroolsRuleTest.class, "simMaker", simMaker);
162 Whitebox.setInternalState(DroolsRuleTest.class, "topicMaker", topicMaker);
164 clMgtQueue = new LinkedList<>();
165 appcLcmQueue = new LinkedList<>();
167 when(policyClMgt.await(any())).thenAnswer(args -> {
168 VirtualControlLoopNotification notif = clMgtQueue.remove();
169 Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
170 assertTrue(pred.test(notif));
174 when(appcLcmRead.await(any())).thenAnswer(args -> {
175 AppcLcmDmaapWrapper req = appcLcmQueue.remove();
176 Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
177 assertTrue(pred.test(req));
184 base = new MyDroolsTest();
185 DroolsRuleTest.initStatics(CONTROLLER_NAME);
190 public void testInitStatics() {
191 assertSame(rules, DroolsRuleTest.rules);
192 assertSame(httpClients, DroolsRuleTest.httpClients);
193 assertSame(simulators, DroolsRuleTest.simulators);
197 public void testFinishStatics() {
198 DroolsRuleTest.finishStatics();
200 verify(rules).destroy();
201 verify(httpClients).destroy();
202 verify(simulators).destroy();
206 public void testInit() {
207 assertSame(topics, base.getTopics());
208 assertSame(controller, base.controller);
212 public void testDroolsTestService123Compliant() {
213 enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
214 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
215 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
216 System.out.println("Drools TestTest Here");
217 base.testService123Compliant();
219 assertEquals(1, permitCount);
220 assertEquals(1, finalCount);
222 assertTrue(appcLcmQueue.isEmpty());
223 assertTrue(clMgtQueue.isEmpty());
226 verify(topics).inject(eq(DroolsRuleTest.DCAE_TOPIC), any());
228 // replies to each APPC request
229 verify(topics, times(6)).inject(eq(DroolsRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
232 private void enqueueClMgt(ControlLoopNotificationType type) {
233 VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
234 notif.setNotification(type);
235 notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
237 clMgtQueue.add(notif);
240 private void enqueueAppcLcm(String... operationNames) {
241 for (String oper : operationNames) {
242 AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
243 req.setRpcName(oper);
245 AppcLcmBody body = new AppcLcmBody();
248 AppcLcmInput input = new AppcLcmInput();
249 body.setInput(input);
251 AppcLcmCommonHeader header = new AppcLcmCommonHeader();
252 input.setCommonHeader(header);
254 header.setSubRequestId("my-subrequest-id");
256 appcLcmQueue.add(req);
260 private Rules makeRules(String controllerName) {
264 private HttpClients makeHttpClients() {
268 private Simulators makeSim() {
272 private Topics makeTopics() {
276 * We don't want junit trying to run this, so it's marked "Ignore".
280 private class MyDroolsTest extends DroolsRuleTest {
283 protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
288 protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
289 Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
291 return policyClMgt.await(notif -> notif.getNotification() == finalType);