a21fa0a93b82ff56478c377b84e265853d365492
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.common.rules.test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertSame;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.util.LinkedList;
33 import java.util.Queue;
34 import java.util.function.Function;
35 import java.util.function.Predicate;
36 import java.util.function.Supplier;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.policy.appc.Request;
45 import org.onap.policy.appclcm.AppcLcmBody;
46 import org.onap.policy.appclcm.AppcLcmCommonHeader;
47 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
48 import org.onap.policy.appclcm.AppcLcmInput;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
51 import org.onap.policy.controlloop.ControlLoopNotificationType;
52 import org.onap.policy.controlloop.VirtualControlLoopNotification;
53 import org.onap.policy.drools.controller.DroolsController;
54 import org.onap.policy.drools.system.PolicyController;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
57 import org.onap.policy.sdnr.PciMessage;
58 import org.powermock.reflect.Whitebox;
59
60 public class DroolsRuleTestTest {
61
62     private static final String CONTROLLER_NAME = "my-controller-name";
63     private static final String POLICY_NAME = "my-policy-name";
64
65     // saved values
66     private static Function<String, Rules> ruleMaker;
67     private static Supplier<HttpClients> httpClientMaker;
68     private static Supplier<Simulators> simMaker;
69     private static Supplier<Topics> topicMaker;
70
71     private DroolsRuleTest base;
72     private LinkedList<VirtualControlLoopNotification> clMgtQueue;
73     private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
74     private int permitCount;
75     private int finalCount;
76
77     @Mock
78     private PolicyController controller;
79     @Mock
80     private Rules rules;
81     @Mock
82     private HttpClients httpClients;
83     @Mock
84     private Simulators simulators;
85     @Mock
86     private Topics topics;
87     @Mock
88     private Listener<VirtualControlLoopNotification> policyClMgt;
89     @Mock
90     private Listener<Request> appcClSink;
91     @Mock
92     private Listener<AppcLcmDmaapWrapper> appcLcmRead;
93     @Mock
94     private Listener<PciMessage> sdnrClSink;
95     @Mock
96     private DroolsController drools;
97     @Mock
98     private ToscaPolicy policy;
99     @Mock
100     private ToscaPolicyIdentifier policyIdent;
101
102
103     /**
104      * Saves static values from the class.
105      */
106     @BeforeClass
107     public static void setUpBeforeClass() {
108         ruleMaker = Whitebox.getInternalState(DroolsRuleTest.class, "ruleMaker");
109         httpClientMaker = Whitebox.getInternalState(DroolsRuleTest.class, "httpClientMaker");
110         simMaker = Whitebox.getInternalState(DroolsRuleTest.class, "simMaker");
111         topicMaker = Whitebox.getInternalState(DroolsRuleTest.class, "topicMaker");
112     }
113
114     /**
115      * Restores static values.
116      */
117     @AfterClass
118     public static void tearDownAfterClass() {
119         Whitebox.setInternalState(DroolsRuleTest.class, "ruleMaker", ruleMaker);
120         Whitebox.setInternalState(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
121         Whitebox.setInternalState(DroolsRuleTest.class, "simMaker", simMaker);
122         Whitebox.setInternalState(DroolsRuleTest.class, "topicMaker", topicMaker);
123     }
124
125     /**
126      * Sets up.
127      */
128     @Before
129     public void setUp() {
130         MockitoAnnotations.initMocks(this);
131
132         when(policy.getIdentifier()).thenReturn(policyIdent);
133         when(policyIdent.getName()).thenReturn(POLICY_NAME);
134
135         when(drools.factCount(CONTROLLER_NAME)).thenReturn(0L);
136         when(controller.getDrools()).thenReturn(drools);
137
138         when(rules.getControllerName()).thenReturn(CONTROLLER_NAME);
139         when(rules.getController()).thenReturn(controller);
140         when(rules.setupPolicyFromFile(any())).thenAnswer(args -> {
141             when(drools.factCount(CONTROLLER_NAME)).thenReturn(2L);
142             return policy;
143         });
144
145         when(topics.createListener(DroolsRuleTest.POLICY_CL_MGT_TOPIC,
146              VirtualControlLoopNotification.class, controller)).thenReturn(policyClMgt);
147         when(topics.createListener(eq(DroolsRuleTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
148                         any(StandardCoder.class))).thenReturn(appcLcmRead);
149         when(topics.createListener(eq(DroolsRuleTest.APPC_CL_TOPIC), eq(Request.class),
150                         any(StandardCoderInstantAsMillis.class))).thenReturn(appcClSink);
151         when(topics.createListener(eq(DroolsRuleTest.SDNR_CL_TOPIC), eq(PciMessage.class),
152             any(StandardCoder.class))).thenReturn(sdnrClSink);
153
154         Function<String, Rules> ruleMaker = this::makeRules;
155         Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
156         Supplier<Simulators> simMaker = this::makeSim;
157         Supplier<Topics> topicMaker = this::makeTopics;
158
159         Whitebox.setInternalState(DroolsRuleTest.class, "ruleMaker", ruleMaker);
160         Whitebox.setInternalState(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
161         Whitebox.setInternalState(DroolsRuleTest.class, "simMaker", simMaker);
162         Whitebox.setInternalState(DroolsRuleTest.class, "topicMaker", topicMaker);
163
164         clMgtQueue = new LinkedList<>();
165         appcLcmQueue = new LinkedList<>();
166
167         when(policyClMgt.await(any())).thenAnswer(args -> {
168             VirtualControlLoopNotification notif = clMgtQueue.remove();
169             Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
170             assertTrue(pred.test(notif));
171             return notif;
172         });
173
174         when(appcLcmRead.await(any())).thenAnswer(args -> {
175             AppcLcmDmaapWrapper req = appcLcmQueue.remove();
176             Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
177             assertTrue(pred.test(req));
178             return req;
179         });
180
181         permitCount = 0;
182         finalCount = 0;
183
184         base = new MyDroolsTest();
185         DroolsRuleTest.initStatics(CONTROLLER_NAME);
186         base.init();
187     }
188
189     @Test
190     public void testInitStatics() {
191         assertSame(rules, DroolsRuleTest.rules);
192         assertSame(httpClients, DroolsRuleTest.httpClients);
193         assertSame(simulators, DroolsRuleTest.simulators);
194     }
195
196     @Test
197     public void testFinishStatics() {
198         DroolsRuleTest.finishStatics();
199
200         verify(rules).destroy();
201         verify(httpClients).destroy();
202         verify(simulators).destroy();
203     }
204
205     @Test
206     public void testInit() {
207         assertSame(topics, base.getTopics());
208         assertSame(controller, base.controller);
209     }
210
211     @Test
212     public void testDroolsTestService123Compliant() {
213         enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
214         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
215         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
216         System.out.println("Drools TestTest Here");
217         base.testService123Compliant();
218
219         assertEquals(1, permitCount);
220         assertEquals(1, finalCount);
221
222         assertTrue(appcLcmQueue.isEmpty());
223         assertTrue(clMgtQueue.isEmpty());
224
225         // initial event
226         verify(topics).inject(eq(DroolsRuleTest.DCAE_TOPIC), any());
227
228         // replies to each APPC request
229         verify(topics, times(6)).inject(eq(DroolsRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
230     }
231
232     private void enqueueClMgt(ControlLoopNotificationType type) {
233         VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
234         notif.setNotification(type);
235         notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
236
237         clMgtQueue.add(notif);
238     }
239
240     private void enqueueAppcLcm(String... operationNames) {
241         for (String oper : operationNames) {
242             AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
243             req.setRpcName(oper);
244
245             AppcLcmBody body = new AppcLcmBody();
246             req.setBody(body);
247
248             AppcLcmInput input = new AppcLcmInput();
249             body.setInput(input);
250
251             AppcLcmCommonHeader header = new AppcLcmCommonHeader();
252             input.setCommonHeader(header);
253
254             header.setSubRequestId("my-subrequest-id");
255
256             appcLcmQueue.add(req);
257         }
258     }
259
260     private Rules makeRules(String controllerName) {
261         return rules;
262     }
263
264     private HttpClients makeHttpClients() {
265         return httpClients;
266     }
267
268     private Simulators makeSim() {
269         return simulators;
270     }
271
272     private Topics makeTopics() {
273         return topics;
274     }
275     /*
276      * We don't want junit trying to run this, so it's marked "Ignore".
277      */
278
279     @Ignore
280     private class MyDroolsTest extends DroolsRuleTest {
281
282         @Override
283         protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
284             permitCount++;
285         }
286
287         @Override
288         protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
289                         Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
290             finalCount++;
291             return policyClMgt.await(notif -> notif.getNotification() == finalType);
292         }
293     }
294 }