2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021, 2023-2024 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.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertSame;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
34 import java.util.LinkedList;
35 import java.util.Queue;
36 import java.util.function.Function;
37 import java.util.function.Predicate;
38 import java.util.function.Supplier;
39 import org.junit.jupiter.api.AfterAll;
40 import org.junit.jupiter.api.BeforeAll;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Disabled;
43 import org.junit.jupiter.api.Test;
44 import org.onap.policy.appclcm.AppcLcmBody;
45 import org.onap.policy.appclcm.AppcLcmCommonHeader;
46 import org.onap.policy.appclcm.AppcLcmInput;
47 import org.onap.policy.appclcm.AppcLcmMessageWrapper;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.controlloop.ControlLoopNotificationType;
50 import org.onap.policy.controlloop.VirtualControlLoopNotification;
51 import org.onap.policy.drools.controller.DroolsController;
52 import org.onap.policy.drools.system.PolicyController;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
54 import org.springframework.test.util.ReflectionTestUtils;
56 class DroolsRuleTestTest {
58 private static final String CONTROLLER_NAME = "my-controller-name";
59 private static final String POLICY_NAME = "my-policy-name";
62 private static Function<String, Rules> ruleMaker;
63 private static Supplier<HttpClients> httpClientMaker;
64 private static Supplier<Simulators> simMaker;
65 private static Supplier<Topics> topicMaker;
67 private DroolsRuleTest base;
68 private LinkedList<VirtualControlLoopNotification> clMgtQueue;
69 private Queue<AppcLcmMessageWrapper> appcLcmQueue;
70 private int permitCount;
71 private int finalCount;
73 private final PolicyController controller = mock(PolicyController.class);
74 private final Rules rules = mock(Rules.class);
75 private final HttpClients httpClients = mock(HttpClients.class);
76 private final Simulators simulators = mock(Simulators.class);
77 private final Topics topics = mock(Topics.class);
78 private final Listener<VirtualControlLoopNotification> policyClMgt = mock();
79 private final Listener<AppcLcmMessageWrapper> appcLcmRead = mock();
80 private final DroolsController drools = mock(DroolsController.class);
81 private final ToscaPolicy policy = mock(ToscaPolicy.class);
84 * Saves static values from the class.
86 @SuppressWarnings("unchecked")
88 public static void setUpBeforeClass() {
89 ruleMaker = (Function<String, Rules>) ReflectionTestUtils.getField(DroolsRuleTest.class, "ruleMaker");
90 httpClientMaker = (Supplier<HttpClients>) ReflectionTestUtils.getField(DroolsRuleTest.class, "httpClientMaker");
91 simMaker = (Supplier<Simulators>) ReflectionTestUtils.getField(DroolsRuleTest.class, "simMaker");
92 topicMaker = (Supplier<Topics>) ReflectionTestUtils.getField(DroolsRuleTest.class, "topicMaker");
96 * Restores static values.
99 public static void tearDownAfterClass() {
100 ReflectionTestUtils.setField(DroolsRuleTest.class, "ruleMaker", ruleMaker);
101 ReflectionTestUtils.setField(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
102 ReflectionTestUtils.setField(DroolsRuleTest.class, "simMaker", simMaker);
103 ReflectionTestUtils.setField(DroolsRuleTest.class, "topicMaker", topicMaker);
110 public void setUp() {
111 when(rules.getController()).thenReturn(controller);
112 when(rules.setupPolicyFromFile(any())).thenReturn(policy);
114 when(topics.createListener(DroolsRuleTest.POLICY_CL_MGT_TOPIC,
115 VirtualControlLoopNotification.class, controller)).thenReturn(policyClMgt);
116 when(topics.createListener(eq(DroolsRuleTest.APPC_LCM_READ_TOPIC), eq(AppcLcmMessageWrapper.class),
117 any(StandardCoder.class))).thenReturn(appcLcmRead);
119 Function<String, Rules> ruleMaker = this::makeRules;
120 Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
121 Supplier<Simulators> simMaker = this::makeSim;
122 Supplier<Topics> topicMaker = this::makeTopics;
124 ReflectionTestUtils.setField(DroolsRuleTest.class, "ruleMaker", ruleMaker);
125 ReflectionTestUtils.setField(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
126 ReflectionTestUtils.setField(DroolsRuleTest.class, "simMaker", simMaker);
127 ReflectionTestUtils.setField(DroolsRuleTest.class, "topicMaker", topicMaker);
129 clMgtQueue = new LinkedList<>();
130 appcLcmQueue = new LinkedList<>();
132 when(policyClMgt.await(any())).thenAnswer(args -> {
133 VirtualControlLoopNotification notif = clMgtQueue.remove();
134 Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
135 assertTrue(pred.test(notif));
139 when(appcLcmRead.await(any())).thenAnswer(args -> {
140 AppcLcmMessageWrapper req = appcLcmQueue.remove();
141 Predicate<AppcLcmMessageWrapper> pred = args.getArgument(0);
142 assertTrue(pred.test(req));
149 base = new MyDroolsTest();
150 DroolsRuleTest.initStatics(CONTROLLER_NAME);
155 void testInitStatics() {
156 assertSame(rules, DroolsRuleTest.rules);
157 assertSame(httpClients, DroolsRuleTest.httpClients);
158 assertSame(simulators, DroolsRuleTest.simulators);
162 void testFinishStatics() {
163 DroolsRuleTest.finishStatics();
165 verify(rules).destroy();
166 verify(httpClients).destroy();
167 verify(simulators).destroy();
172 assertSame(topics, BaseTest.getTopics());
173 assertSame(controller, base.controller);
177 void testDroolsTestService123Compliant() {
178 enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
179 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
180 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
181 System.out.println("Drools TestTest Here");
182 base.testService123Compliant();
184 assertEquals(1, permitCount);
185 assertEquals(1, finalCount);
187 assertTrue(appcLcmQueue.isEmpty());
188 assertTrue(clMgtQueue.isEmpty());
191 verify(topics).inject(eq(DroolsRuleTest.DCAE_TOPIC), any());
193 // replies to each APPC request
194 verify(topics, times(6)).inject(eq(DroolsRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
197 private void enqueueClMgt(ControlLoopNotificationType type) {
198 VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
199 notif.setNotification(type);
200 notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
202 clMgtQueue.add(notif);
205 private void enqueueAppcLcm(String... operationNames) {
206 for (String oper : operationNames) {
207 AppcLcmMessageWrapper req = new AppcLcmMessageWrapper();
208 req.setRpcName(oper);
210 AppcLcmBody body = new AppcLcmBody();
213 AppcLcmInput input = new AppcLcmInput();
214 body.setInput(input);
216 AppcLcmCommonHeader header = new AppcLcmCommonHeader();
217 input.setCommonHeader(header);
219 header.setSubRequestId("my-subrequest-id");
221 appcLcmQueue.add(req);
225 private Rules makeRules(String controllerName) {
229 private HttpClients makeHttpClients() {
233 private Simulators makeSim() {
237 private Topics makeTopics() {
241 * We don't want junit trying to run this, so it's marked "Ignore".
245 private class MyDroolsTest extends DroolsRuleTest {
248 protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
253 protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
254 Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
256 return policyClMgt.await(notif -> notif.getNotification() == finalType);