Remove deprecated DMAAP dependency
[policy/drools-applications.git] / controlloop / common / rules-test / src / test / java / org / onap / policy / controlloop / common / rules / test / DroolsRuleTestTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.common.rules.test;
23
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;
33
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;
55
56 class DroolsRuleTestTest {
57
58     private static final String CONTROLLER_NAME = "my-controller-name";
59     private static final String POLICY_NAME = "my-policy-name";
60
61     // saved values
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;
66
67     private DroolsRuleTest base;
68     private LinkedList<VirtualControlLoopNotification> clMgtQueue;
69     private Queue<AppcLcmMessageWrapper> appcLcmQueue;
70     private int permitCount;
71     private int finalCount;
72
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);
82
83     /**
84      * Saves static values from the class.
85      */
86     @SuppressWarnings("unchecked")
87     @BeforeAll
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");
93     }
94
95     /**
96      * Restores static values.
97      */
98     @AfterAll
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);
104     }
105
106     /**
107      * Sets up.
108      */
109     @BeforeEach
110     public void setUp() {
111         when(rules.getController()).thenReturn(controller);
112         when(rules.setupPolicyFromFile(any())).thenReturn(policy);
113
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);
118
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;
123
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);
128
129         clMgtQueue = new LinkedList<>();
130         appcLcmQueue = new LinkedList<>();
131
132         when(policyClMgt.await(any())).thenAnswer(args -> {
133             VirtualControlLoopNotification notif = clMgtQueue.remove();
134             Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
135             assertTrue(pred.test(notif));
136             return notif;
137         });
138
139         when(appcLcmRead.await(any())).thenAnswer(args -> {
140             AppcLcmMessageWrapper req = appcLcmQueue.remove();
141             Predicate<AppcLcmMessageWrapper> pred = args.getArgument(0);
142             assertTrue(pred.test(req));
143             return req;
144         });
145
146         permitCount = 0;
147         finalCount = 0;
148
149         base = new MyDroolsTest();
150         DroolsRuleTest.initStatics(CONTROLLER_NAME);
151         base.init();
152     }
153
154     @Test
155     void testInitStatics() {
156         assertSame(rules, DroolsRuleTest.rules);
157         assertSame(httpClients, DroolsRuleTest.httpClients);
158         assertSame(simulators, DroolsRuleTest.simulators);
159     }
160
161     @Test
162     void testFinishStatics() {
163         DroolsRuleTest.finishStatics();
164
165         verify(rules).destroy();
166         verify(httpClients).destroy();
167         verify(simulators).destroy();
168     }
169
170     @Test
171     void testInit() {
172         assertSame(topics, BaseTest.getTopics());
173         assertSame(controller, base.controller);
174     }
175
176     @Test
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();
183
184         assertEquals(1, permitCount);
185         assertEquals(1, finalCount);
186
187         assertTrue(appcLcmQueue.isEmpty());
188         assertTrue(clMgtQueue.isEmpty());
189
190         // initial event
191         verify(topics).inject(eq(DroolsRuleTest.DCAE_TOPIC), any());
192
193         // replies to each APPC request
194         verify(topics, times(6)).inject(eq(DroolsRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
195     }
196
197     private void enqueueClMgt(ControlLoopNotificationType type) {
198         VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
199         notif.setNotification(type);
200         notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
201
202         clMgtQueue.add(notif);
203     }
204
205     private void enqueueAppcLcm(String... operationNames) {
206         for (String oper : operationNames) {
207             AppcLcmMessageWrapper req = new AppcLcmMessageWrapper();
208             req.setRpcName(oper);
209
210             AppcLcmBody body = new AppcLcmBody();
211             req.setBody(body);
212
213             AppcLcmInput input = new AppcLcmInput();
214             body.setInput(input);
215
216             AppcLcmCommonHeader header = new AppcLcmCommonHeader();
217             input.setCommonHeader(header);
218
219             header.setSubRequestId("my-subrequest-id");
220
221             appcLcmQueue.add(req);
222         }
223     }
224
225     private Rules makeRules(String controllerName) {
226         return rules;
227     }
228
229     private HttpClients makeHttpClients() {
230         return httpClients;
231     }
232
233     private Simulators makeSim() {
234         return simulators;
235     }
236
237     private Topics makeTopics() {
238         return topics;
239     }
240     /*
241      * We don't want junit trying to run this, so it's marked "Ignore".
242      */
243
244     @Disabled
245     private class MyDroolsTest extends DroolsRuleTest {
246
247         @Override
248         protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
249             permitCount++;
250         }
251
252         @Override
253         protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
254                         Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
255             finalCount++;
256             return policyClMgt.await(notif -> notif.getNotification() == finalType);
257         }
258     }
259 }