82b9ed6ccb0c658cc49e6968dc618b430d0c18c7
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 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.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.powermock.reflect.Whitebox;
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     @BeforeClass
109     public static void setUpBeforeClass() {
110         ruleMaker = Whitebox.getInternalState(DroolsRuleTest.class, "ruleMaker");
111         httpClientMaker = Whitebox.getInternalState(DroolsRuleTest.class, "httpClientMaker");
112         simMaker = Whitebox.getInternalState(DroolsRuleTest.class, "simMaker");
113         topicMaker = Whitebox.getInternalState(DroolsRuleTest.class, "topicMaker");
114     }
115
116     /**
117      * Restores static values.
118      */
119     @AfterClass
120     public static void tearDownAfterClass() {
121         Whitebox.setInternalState(DroolsRuleTest.class, "ruleMaker", ruleMaker);
122         Whitebox.setInternalState(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
123         Whitebox.setInternalState(DroolsRuleTest.class, "simMaker", simMaker);
124         Whitebox.setInternalState(DroolsRuleTest.class, "topicMaker", topicMaker);
125     }
126
127     /**
128      * Sets up.
129      */
130     @Before
131     public void setUp() {
132         when(rules.getController()).thenReturn(controller);
133         when(rules.setupPolicyFromFile(any())).thenReturn(policy);
134
135         when(topics.createListener(DroolsRuleTest.POLICY_CL_MGT_TOPIC,
136              VirtualControlLoopNotification.class, controller)).thenReturn(policyClMgt);
137         when(topics.createListener(eq(DroolsRuleTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
138                         any(StandardCoder.class))).thenReturn(appcLcmRead);
139
140         Function<String, Rules> ruleMaker = this::makeRules;
141         Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
142         Supplier<Simulators> simMaker = this::makeSim;
143         Supplier<Topics> topicMaker = this::makeTopics;
144
145         Whitebox.setInternalState(DroolsRuleTest.class, "ruleMaker", ruleMaker);
146         Whitebox.setInternalState(DroolsRuleTest.class, "httpClientMaker", httpClientMaker);
147         Whitebox.setInternalState(DroolsRuleTest.class, "simMaker", simMaker);
148         Whitebox.setInternalState(DroolsRuleTest.class, "topicMaker", topicMaker);
149
150         clMgtQueue = new LinkedList<>();
151         appcLcmQueue = new LinkedList<>();
152
153         when(policyClMgt.await(any())).thenAnswer(args -> {
154             VirtualControlLoopNotification notif = clMgtQueue.remove();
155             Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
156             assertTrue(pred.test(notif));
157             return notif;
158         });
159
160         when(appcLcmRead.await(any())).thenAnswer(args -> {
161             AppcLcmDmaapWrapper req = appcLcmQueue.remove();
162             Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
163             assertTrue(pred.test(req));
164             return req;
165         });
166
167         permitCount = 0;
168         finalCount = 0;
169
170         base = new MyDroolsTest();
171         DroolsRuleTest.initStatics(CONTROLLER_NAME);
172         base.init();
173     }
174
175     @Test
176     public void testInitStatics() {
177         assertSame(rules, DroolsRuleTest.rules);
178         assertSame(httpClients, DroolsRuleTest.httpClients);
179         assertSame(simulators, DroolsRuleTest.simulators);
180     }
181
182     @Test
183     public void testFinishStatics() {
184         DroolsRuleTest.finishStatics();
185
186         verify(rules).destroy();
187         verify(httpClients).destroy();
188         verify(simulators).destroy();
189     }
190
191     @Test
192     public void testInit() {
193         assertSame(topics, BaseTest.getTopics());
194         assertSame(controller, base.controller);
195     }
196
197     @Test
198     public void testDroolsTestService123Compliant() {
199         enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
200         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
201         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
202         System.out.println("Drools TestTest Here");
203         base.testService123Compliant();
204
205         assertEquals(1, permitCount);
206         assertEquals(1, finalCount);
207
208         assertTrue(appcLcmQueue.isEmpty());
209         assertTrue(clMgtQueue.isEmpty());
210
211         // initial event
212         verify(topics).inject(eq(DroolsRuleTest.DCAE_TOPIC), any());
213
214         // replies to each APPC request
215         verify(topics, times(6)).inject(eq(DroolsRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
216     }
217
218     private void enqueueClMgt(ControlLoopNotificationType type) {
219         VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
220         notif.setNotification(type);
221         notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
222
223         clMgtQueue.add(notif);
224     }
225
226     private void enqueueAppcLcm(String... operationNames) {
227         for (String oper : operationNames) {
228             AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
229             req.setRpcName(oper);
230
231             AppcLcmBody body = new AppcLcmBody();
232             req.setBody(body);
233
234             AppcLcmInput input = new AppcLcmInput();
235             body.setInput(input);
236
237             AppcLcmCommonHeader header = new AppcLcmCommonHeader();
238             input.setCommonHeader(header);
239
240             header.setSubRequestId("my-subrequest-id");
241
242             appcLcmQueue.add(req);
243         }
244     }
245
246     private Rules makeRules(String controllerName) {
247         return rules;
248     }
249
250     private HttpClients makeHttpClients() {
251         return httpClients;
252     }
253
254     private Simulators makeSim() {
255         return simulators;
256     }
257
258     private Topics makeTopics() {
259         return topics;
260     }
261     /*
262      * We don't want junit trying to run this, so it's marked "Ignore".
263      */
264
265     @Ignore
266     private class MyDroolsTest extends DroolsRuleTest {
267
268         @Override
269         protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
270             permitCount++;
271         }
272
273         @Override
274         protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
275                         Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
276             finalCount++;
277             return policyClMgt.await(notif -> notif.getNotification() == finalType);
278         }
279     }
280 }