4b9f5774d0b9557519c8d040d1dffc8093d89cb1
[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 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.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;
32
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;
60
61 @RunWith(MockitoJUnitRunner.class)
62 public class DroolsRuleTestTest {
63
64     private static final String CONTROLLER_NAME = "my-controller-name";
65     private static final String POLICY_NAME = "my-policy-name";
66
67     // saved values
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;
72
73     private DroolsRuleTest base;
74     private LinkedList<VirtualControlLoopNotification> clMgtQueue;
75     private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
76     private int permitCount;
77     private int finalCount;
78
79     @Mock
80     private PolicyController controller;
81     @Mock
82     private Rules rules;
83     @Mock
84     private HttpClients httpClients;
85     @Mock
86     private Simulators simulators;
87     @Mock
88     private Topics topics;
89     @Mock
90     private Listener<VirtualControlLoopNotification> policyClMgt;
91     @Mock
92     private Listener<Request> appcClSink;
93     @Mock
94     private Listener<AppcLcmDmaapWrapper> appcLcmRead;
95     @Mock
96     private Listener<PciMessage> sdnrClSink;
97     @Mock
98     private DroolsController drools;
99     @Mock
100     private ToscaPolicy policy;
101     @Mock
102     private ToscaConceptIdentifier policyIdent;
103
104
105     /**
106      * Saves static values from the class.
107      */
108     @SuppressWarnings("unchecked")
109     @BeforeClass
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");
115     }
116
117     /**
118      * Restores static values.
119      */
120     @AfterClass
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);
126     }
127
128     /**
129      * Sets up.
130      */
131     @Before
132     public void setUp() {
133         when(rules.getController()).thenReturn(controller);
134         when(rules.setupPolicyFromFile(any())).thenReturn(policy);
135
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);
140
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;
145
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);
150
151         clMgtQueue = new LinkedList<>();
152         appcLcmQueue = new LinkedList<>();
153
154         when(policyClMgt.await(any())).thenAnswer(args -> {
155             VirtualControlLoopNotification notif = clMgtQueue.remove();
156             Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
157             assertTrue(pred.test(notif));
158             return notif;
159         });
160
161         when(appcLcmRead.await(any())).thenAnswer(args -> {
162             AppcLcmDmaapWrapper req = appcLcmQueue.remove();
163             Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
164             assertTrue(pred.test(req));
165             return req;
166         });
167
168         permitCount = 0;
169         finalCount = 0;
170
171         base = new MyDroolsTest();
172         DroolsRuleTest.initStatics(CONTROLLER_NAME);
173         base.init();
174     }
175
176     @Test
177     public void testInitStatics() {
178         assertSame(rules, DroolsRuleTest.rules);
179         assertSame(httpClients, DroolsRuleTest.httpClients);
180         assertSame(simulators, DroolsRuleTest.simulators);
181     }
182
183     @Test
184     public void testFinishStatics() {
185         DroolsRuleTest.finishStatics();
186
187         verify(rules).destroy();
188         verify(httpClients).destroy();
189         verify(simulators).destroy();
190     }
191
192     @Test
193     public void testInit() {
194         assertSame(topics, BaseTest.getTopics());
195         assertSame(controller, base.controller);
196     }
197
198     @Test
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();
205
206         assertEquals(1, permitCount);
207         assertEquals(1, finalCount);
208
209         assertTrue(appcLcmQueue.isEmpty());
210         assertTrue(clMgtQueue.isEmpty());
211
212         // initial event
213         verify(topics).inject(eq(DroolsRuleTest.DCAE_TOPIC), any());
214
215         // replies to each APPC request
216         verify(topics, times(6)).inject(eq(DroolsRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
217     }
218
219     private void enqueueClMgt(ControlLoopNotificationType type) {
220         VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
221         notif.setNotification(type);
222         notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
223
224         clMgtQueue.add(notif);
225     }
226
227     private void enqueueAppcLcm(String... operationNames) {
228         for (String oper : operationNames) {
229             AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
230             req.setRpcName(oper);
231
232             AppcLcmBody body = new AppcLcmBody();
233             req.setBody(body);
234
235             AppcLcmInput input = new AppcLcmInput();
236             body.setInput(input);
237
238             AppcLcmCommonHeader header = new AppcLcmCommonHeader();
239             input.setCommonHeader(header);
240
241             header.setSubRequestId("my-subrequest-id");
242
243             appcLcmQueue.add(req);
244         }
245     }
246
247     private Rules makeRules(String controllerName) {
248         return rules;
249     }
250
251     private HttpClients makeHttpClients() {
252         return httpClients;
253     }
254
255     private Simulators makeSim() {
256         return simulators;
257     }
258
259     private Topics makeTopics() {
260         return topics;
261     }
262     /*
263      * We don't want junit trying to run this, so it's marked "Ignore".
264      */
265
266     @Ignore
267     private class MyDroolsTest extends DroolsRuleTest {
268
269         @Override
270         protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
271             permitCount++;
272         }
273
274         @Override
275         protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
276                         Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
277             finalCount++;
278             return policyClMgt.await(notif -> notif.getNotification() == finalType);
279         }
280     }
281 }