2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021,2023 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.springframework.test.util.ReflectionTestUtils;
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.
108 @SuppressWarnings("unchecked")
110 public static void setUpBeforeClass() {
111 ruleMaker = (Function<String, Rules>) ReflectionTestUtils.getField(DroolsRuleTest.class, "ruleMaker");
112 httpClientMaker = (Supplier<HttpClients>) ReflectionTestUtils.getField(DroolsRuleTest.class, "httpClientMaker");
113 simMaker = (Supplier<Simulators>) ReflectionTestUtils.getField(DroolsRuleTest.class, "simMaker");
114 topicMaker = (Supplier<Topics>) ReflectionTestUtils.getField(DroolsRuleTest.class, "topicMaker");
118 * Restores static values.
121 public static void tearDownAfterClass() {
122 ReflectionTestUtils.setField(DroolsRuleTest.class, "ruleMaker", ruleMaker);
123 ReflectionTestUtils.setField(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
124 ReflectionTestUtils.setField(DroolsRuleTest.class, "simMaker", simMaker);
125 ReflectionTestUtils.setField(DroolsRuleTest.class, "topicMaker", topicMaker);
132 public void setUp() {
133 when(rules.getController()).thenReturn(controller);
134 when(rules.setupPolicyFromFile(any())).thenReturn(policy);
136 when(topics.createListener(DroolsRuleTest.POLICY_CL_MGT_TOPIC,
137 VirtualControlLoopNotification.class, controller)).thenReturn(policyClMgt);
138 when(topics.createListener(eq(DroolsRuleTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
139 any(StandardCoder.class))).thenReturn(appcLcmRead);
141 Function<String, Rules> ruleMaker = this::makeRules;
142 Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
143 Supplier<Simulators> simMaker = this::makeSim;
144 Supplier<Topics> topicMaker = this::makeTopics;
146 ReflectionTestUtils.setField(DroolsRuleTest.class, "ruleMaker", ruleMaker);
147 ReflectionTestUtils.setField(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
148 ReflectionTestUtils.setField(DroolsRuleTest.class, "simMaker", simMaker);
149 ReflectionTestUtils.setField(DroolsRuleTest.class, "topicMaker", topicMaker);
151 clMgtQueue = new LinkedList<>();
152 appcLcmQueue = new LinkedList<>();
154 when(policyClMgt.await(any())).thenAnswer(args -> {
155 VirtualControlLoopNotification notif = clMgtQueue.remove();
156 Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
157 assertTrue(pred.test(notif));
161 when(appcLcmRead.await(any())).thenAnswer(args -> {
162 AppcLcmDmaapWrapper req = appcLcmQueue.remove();
163 Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
164 assertTrue(pred.test(req));
171 base = new MyDroolsTest();
172 DroolsRuleTest.initStatics(CONTROLLER_NAME);
177 public void testInitStatics() {
178 assertSame(rules, DroolsRuleTest.rules);
179 assertSame(httpClients, DroolsRuleTest.httpClients);
180 assertSame(simulators, DroolsRuleTest.simulators);
184 public void testFinishStatics() {
185 DroolsRuleTest.finishStatics();
187 verify(rules).destroy();
188 verify(httpClients).destroy();
189 verify(simulators).destroy();
193 public void testInit() {
194 assertSame(topics, BaseTest.getTopics());
195 assertSame(controller, base.controller);
199 public void testDroolsTestService123Compliant() {
200 enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
201 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
202 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
203 System.out.println("Drools TestTest Here");
204 base.testService123Compliant();
206 assertEquals(1, permitCount);
207 assertEquals(1, finalCount);
209 assertTrue(appcLcmQueue.isEmpty());
210 assertTrue(clMgtQueue.isEmpty());
213 verify(topics).inject(eq(DroolsRuleTest.DCAE_TOPIC), any());
215 // replies to each APPC request
216 verify(topics, times(6)).inject(eq(DroolsRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
219 private void enqueueClMgt(ControlLoopNotificationType type) {
220 VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
221 notif.setNotification(type);
222 notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
224 clMgtQueue.add(notif);
227 private void enqueueAppcLcm(String... operationNames) {
228 for (String oper : operationNames) {
229 AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
230 req.setRpcName(oper);
232 AppcLcmBody body = new AppcLcmBody();
235 AppcLcmInput input = new AppcLcmInput();
236 body.setInput(input);
238 AppcLcmCommonHeader header = new AppcLcmCommonHeader();
239 input.setCommonHeader(header);
241 header.setSubRequestId("my-subrequest-id");
243 appcLcmQueue.add(req);
247 private Rules makeRules(String controllerName) {
251 private HttpClients makeHttpClients() {
255 private Simulators makeSim() {
259 private Topics makeTopics() {
263 * We don't want junit trying to run this, so it's marked "Ignore".
267 private class MyDroolsTest extends DroolsRuleTest {
270 protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
275 protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
276 Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
278 return policyClMgt.await(notif -> notif.getNotification() == finalType);