0aca1b2b8ae71990b379daccb8e04add01c871bd
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 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.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.policy.appc.Request;
46 import org.onap.policy.appclcm.AppcLcmBody;
47 import org.onap.policy.appclcm.AppcLcmCommonHeader;
48 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
49 import org.onap.policy.appclcm.AppcLcmInput;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
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.powermock.reflect.Whitebox;
60
61 public class DroolsRuleTestTest {
62
63     private static final String CONTROLLER_NAME = "my-controller-name";
64     private static final String POLICY_NAME = "my-policy-name";
65
66     // saved values
67     private static Function<String, Rules> ruleMaker;
68     private static Supplier<HttpClients> httpClientMaker;
69     private static Supplier<Simulators> simMaker;
70     private static Supplier<Topics> topicMaker;
71
72     private DroolsRuleTest base;
73     private LinkedList<VirtualControlLoopNotification> clMgtQueue;
74     private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
75     private int permitCount;
76     private int finalCount;
77
78     @Mock
79     private PolicyController controller;
80     @Mock
81     private Rules rules;
82     @Mock
83     private HttpClients httpClients;
84     @Mock
85     private Simulators simulators;
86     @Mock
87     private Topics topics;
88     @Mock
89     private Listener<VirtualControlLoopNotification> policyClMgt;
90     @Mock
91     private Listener<Request> appcClSink;
92     @Mock
93     private Listener<AppcLcmDmaapWrapper> appcLcmRead;
94     @Mock
95     private Listener<PciMessage> sdnrClSink;
96     @Mock
97     private DroolsController drools;
98     @Mock
99     private ToscaPolicy policy;
100     @Mock
101     private ToscaConceptIdentifier policyIdent;
102
103
104     /**
105      * Saves static values from the class.
106      */
107     @BeforeClass
108     public static void setUpBeforeClass() {
109         ruleMaker = Whitebox.getInternalState(DroolsRuleTest.class, "ruleMaker");
110         httpClientMaker = Whitebox.getInternalState(DroolsRuleTest.class, "httpClientMaker");
111         simMaker = Whitebox.getInternalState(DroolsRuleTest.class, "simMaker");
112         topicMaker = Whitebox.getInternalState(DroolsRuleTest.class, "topicMaker");
113     }
114
115     /**
116      * Restores static values.
117      */
118     @AfterClass
119     public static void tearDownAfterClass() {
120         Whitebox.setInternalState(DroolsRuleTest.class, "ruleMaker", ruleMaker);
121         Whitebox.setInternalState(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
122         Whitebox.setInternalState(DroolsRuleTest.class, "simMaker", simMaker);
123         Whitebox.setInternalState(DroolsRuleTest.class, "topicMaker", topicMaker);
124     }
125
126     /**
127      * Sets up.
128      */
129     @Before
130     public void setUp() {
131         MockitoAnnotations.initMocks(this);
132
133         when(policy.getIdentifier()).thenReturn(policyIdent);
134         when(policyIdent.getName()).thenReturn(POLICY_NAME);
135
136         when(drools.factCount(CONTROLLER_NAME)).thenReturn(0L);
137         when(controller.getDrools()).thenReturn(drools);
138
139         when(rules.getControllerName()).thenReturn(CONTROLLER_NAME);
140         when(rules.getController()).thenReturn(controller);
141         when(rules.setupPolicyFromFile(any())).thenAnswer(args -> {
142             when(drools.factCount(CONTROLLER_NAME)).thenReturn(2L);
143             return policy;
144         });
145
146         when(topics.createListener(DroolsRuleTest.POLICY_CL_MGT_TOPIC,
147              VirtualControlLoopNotification.class, controller)).thenReturn(policyClMgt);
148         when(topics.createListener(eq(DroolsRuleTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
149                         any(StandardCoder.class))).thenReturn(appcLcmRead);
150         when(topics.createListener(eq(DroolsRuleTest.APPC_CL_TOPIC), eq(Request.class),
151                         any(StandardCoderInstantAsMillis.class))).thenReturn(appcClSink);
152         when(topics.createListener(eq(DroolsRuleTest.SDNR_CL_TOPIC), eq(PciMessage.class),
153             any(StandardCoder.class))).thenReturn(sdnrClSink);
154
155         Function<String, Rules> ruleMaker = this::makeRules;
156         Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
157         Supplier<Simulators> simMaker = this::makeSim;
158         Supplier<Topics> topicMaker = this::makeTopics;
159
160         Whitebox.setInternalState(DroolsRuleTest.class, "ruleMaker", ruleMaker);
161         Whitebox.setInternalState(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
162         Whitebox.setInternalState(DroolsRuleTest.class, "simMaker", simMaker);
163         Whitebox.setInternalState(DroolsRuleTest.class, "topicMaker", topicMaker);
164
165         clMgtQueue = new LinkedList<>();
166         appcLcmQueue = new LinkedList<>();
167
168         when(policyClMgt.await(any())).thenAnswer(args -> {
169             VirtualControlLoopNotification notif = clMgtQueue.remove();
170             Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
171             assertTrue(pred.test(notif));
172             return notif;
173         });
174
175         when(appcLcmRead.await(any())).thenAnswer(args -> {
176             AppcLcmDmaapWrapper req = appcLcmQueue.remove();
177             Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
178             assertTrue(pred.test(req));
179             return req;
180         });
181
182         permitCount = 0;
183         finalCount = 0;
184
185         base = new MyDroolsTest();
186         DroolsRuleTest.initStatics(CONTROLLER_NAME);
187         base.init();
188     }
189
190     @Test
191     public void testInitStatics() {
192         assertSame(rules, DroolsRuleTest.rules);
193         assertSame(httpClients, DroolsRuleTest.httpClients);
194         assertSame(simulators, DroolsRuleTest.simulators);
195     }
196
197     @Test
198     public void testFinishStatics() {
199         DroolsRuleTest.finishStatics();
200
201         verify(rules).destroy();
202         verify(httpClients).destroy();
203         verify(simulators).destroy();
204     }
205
206     @Test
207     public void testInit() {
208         assertSame(topics, BaseTest.getTopics());
209         assertSame(controller, base.controller);
210     }
211
212     @Test
213     public void testDroolsTestService123Compliant() {
214         enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
215         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
216         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
217         System.out.println("Drools TestTest Here");
218         base.testService123Compliant();
219
220         assertEquals(1, permitCount);
221         assertEquals(1, finalCount);
222
223         assertTrue(appcLcmQueue.isEmpty());
224         assertTrue(clMgtQueue.isEmpty());
225
226         // initial event
227         verify(topics).inject(eq(DroolsRuleTest.DCAE_TOPIC), any());
228
229         // replies to each APPC request
230         verify(topics, times(6)).inject(eq(DroolsRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
231     }
232
233     private void enqueueClMgt(ControlLoopNotificationType type) {
234         VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
235         notif.setNotification(type);
236         notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
237
238         clMgtQueue.add(notif);
239     }
240
241     private void enqueueAppcLcm(String... operationNames) {
242         for (String oper : operationNames) {
243             AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
244             req.setRpcName(oper);
245
246             AppcLcmBody body = new AppcLcmBody();
247             req.setBody(body);
248
249             AppcLcmInput input = new AppcLcmInput();
250             body.setInput(input);
251
252             AppcLcmCommonHeader header = new AppcLcmCommonHeader();
253             input.setCommonHeader(header);
254
255             header.setSubRequestId("my-subrequest-id");
256
257             appcLcmQueue.add(req);
258         }
259     }
260
261     private Rules makeRules(String controllerName) {
262         return rules;
263     }
264
265     private HttpClients makeHttpClients() {
266         return httpClients;
267     }
268
269     private Simulators makeSim() {
270         return simulators;
271     }
272
273     private Topics makeTopics() {
274         return topics;
275     }
276     /*
277      * We don't want junit trying to run this, so it's marked "Ignore".
278      */
279
280     @Ignore
281     private class MyDroolsTest extends DroolsRuleTest {
282
283         @Override
284         protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
285             permitCount++;
286         }
287
288         @Override
289         protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
290                         Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
291             finalCount++;
292             return policyClMgt.await(notif -> notif.getNotification() == finalType);
293         }
294     }
295 }