2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.controlloop.common.rules.test;
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.spy;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
34 import java.util.LinkedList;
36 import java.util.Queue;
37 import java.util.concurrent.atomic.AtomicLong;
38 import java.util.function.Predicate;
39 import java.util.function.Supplier;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Ignore;
44 import org.junit.Test;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.onap.policy.appc.CommonHeader;
48 import org.onap.policy.appc.Request;
49 import org.onap.policy.appclcm.AppcLcmBody;
50 import org.onap.policy.appclcm.AppcLcmCommonHeader;
51 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
52 import org.onap.policy.appclcm.AppcLcmInput;
53 import org.onap.policy.common.utils.coder.StandardCoder;
54 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
55 import org.onap.policy.controlloop.ControlLoopNotificationType;
56 import org.onap.policy.controlloop.VirtualControlLoopNotification;
57 import org.onap.policy.drools.controller.DroolsController;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
60 import org.onap.policy.sdnr.PciBody;
61 import org.onap.policy.sdnr.PciCommonHeader;
62 import org.onap.policy.sdnr.PciMessage;
63 import org.onap.policy.sdnr.PciRequest;
64 import org.powermock.reflect.Whitebox;
66 public class BaseTestTest {
67 private static final String POLICY_NAME = "my-policy-name";
70 private static Supplier<HttpClients> httpClientMaker;
71 private static Supplier<Simulators> simMaker;
72 private static Supplier<Topics> topicMaker;
74 private BaseTest base;
75 private LinkedList<VirtualControlLoopNotification> clMgtQueue;
76 private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
77 private Queue<Request> appcLegacyQueue;
78 private Queue<PciMessage> sdnrQueue;
79 private int permitCount;
80 private int finalCount;
83 private HttpClients httpClients;
85 private Simulators simulators;
87 private Topics topics;
89 private Listener<VirtualControlLoopNotification> policyClMgt;
91 private Listener<Request> appcClSink;
93 private Listener<AppcLcmDmaapWrapper> appcLcmRead;
95 private Listener<PciMessage> sdnrClSink;
97 private DroolsController drools;
99 private ToscaPolicy policy;
101 private ToscaConceptIdentifier policyIdent;
105 * Saves static values from the class.
108 public static void setUpBeforeClass() {
109 httpClientMaker = Whitebox.getInternalState(BaseTest.class, "httpClientMaker");
110 simMaker = Whitebox.getInternalState(BaseTest.class, "simMaker");
111 topicMaker = Whitebox.getInternalState(BaseTest.class, "topicMaker");
115 * Restores static values.
118 public static void tearDownAfterClass() {
119 Whitebox.setInternalState(BaseTest.class, "httpClientMaker", httpClientMaker);
120 Whitebox.setInternalState(BaseTest.class, "simMaker", simMaker);
121 Whitebox.setInternalState(BaseTest.class, "topicMaker", topicMaker);
128 public void setUp() {
129 MockitoAnnotations.initMocks(this);
131 when(policy.getIdentifier()).thenReturn(policyIdent);
132 when(policyIdent.getName()).thenReturn(POLICY_NAME);
134 when(topics.createListener(eq(BaseTest.POLICY_CL_MGT_TOPIC), eq(VirtualControlLoopNotification.class),
135 any(StandardCoder.class))).thenReturn(policyClMgt);
136 when(topics.createListener(eq(BaseTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
137 any(StandardCoder.class))).thenReturn(appcLcmRead);
138 when(topics.createListener(eq(BaseTest.APPC_CL_TOPIC), eq(Request.class),
139 any(StandardCoderInstantAsMillis.class))).thenReturn(appcClSink);
140 when(topics.createListener(eq(BaseTest.SDNR_CL_TOPIC), eq(PciMessage.class), any(StandardCoder.class)))
141 .thenReturn(sdnrClSink);
143 Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
144 Supplier<Simulators> simMaker = this::makeSim;
145 Supplier<Topics> topicMaker = this::makeTopics;
147 Whitebox.setInternalState(BaseTest.class, "httpClientMaker", httpClientMaker);
148 Whitebox.setInternalState(BaseTest.class, "simMaker", simMaker);
149 Whitebox.setInternalState(BaseTest.class, "topicMaker", topicMaker);
151 clMgtQueue = new LinkedList<>();
152 appcLcmQueue = new LinkedList<>();
153 appcLegacyQueue = new LinkedList<>();
154 sdnrQueue = new LinkedList<>();
156 when(policyClMgt.await(any())).thenAnswer(args -> {
157 VirtualControlLoopNotification notif = clMgtQueue.remove();
158 Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
159 assertTrue(pred.test(notif));
163 when(appcLcmRead.await(any())).thenAnswer(args -> {
164 AppcLcmDmaapWrapper req = appcLcmQueue.remove();
165 Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
166 assertTrue(pred.test(req));
170 when(appcClSink.await(any())).thenAnswer(args -> {
171 Request req = appcLegacyQueue.remove();
172 Predicate<Request> pred = args.getArgument(0);
173 assertTrue(pred.test(req));
177 when(sdnrClSink.await(any())).thenAnswer(args -> {
178 PciMessage pcireq = sdnrQueue.remove();
179 Predicate<PciMessage> pred = args.getArgument(0);
180 assertTrue(pred.test(pcireq));
189 BaseTest.initStatics();
194 public void testInitStatics() {
195 assertSame(httpClients, BaseTest.httpClients);
196 assertSame(simulators, BaseTest.simulators);
200 public void testFinishStatics() {
201 BaseTest.finishStatics();
202 verify(httpClients).destroy();
203 verify(simulators).destroy();
207 public void testInit() {
208 assertSame(topics, BaseTest.getTopics());
212 public void testFinish() {
214 verify(topics).destroy();
218 public void testTestService123Compliant() {
219 enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
220 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
221 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
223 base.testService123Compliant();
225 assertEquals(1, permitCount);
226 assertEquals(1, finalCount);
228 assertTrue(appcLcmQueue.isEmpty());
229 assertTrue(clMgtQueue.isEmpty());
232 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
234 // replies to each APPC request
235 verify(topics, times(6)).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
239 public void testTestDuplicatesEvents() {
240 // the test expects the count to be incremented by 2 between calls
241 AtomicLong count = new AtomicLong(5);
243 when(base.getCreateCount()).thenAnswer(args -> count.getAndAdd(2));
245 enqueueAppcLcm("restart", "restart");
246 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
247 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
249 clMgtQueue.get(0).setAai(Map.of("generic-vnf.vnf-id", "duplicate-VNF"));
250 clMgtQueue.get(1).setAai(Map.of("generic-vnf.vnf-id", "vCPE_Infrastructure_vGMUX_demo_app"));
252 base.testDuplicatesEvents();
254 assertEquals(0, permitCount);
255 assertEquals(2, finalCount);
257 assertTrue(appcLcmQueue.isEmpty());
258 assertTrue(clMgtQueue.isEmpty());
261 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
262 verify(topics, times(2)).inject(eq(BaseTest.DCAE_TOPIC), any(), any());
265 verify(topics, times(2)).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
269 public void testTestVcpeSunnyDayCompliant() {
270 checkAppcLcmPolicy("restart", base::testVcpeSunnyDayCompliant);
274 public void testTestVcpeOnsetFloodPrevention() {
275 enqueueAppcLcm("restart");
276 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
277 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
279 base.testVcpeOnsetFloodPrevention();
281 assertEquals(1, permitCount);
282 assertEquals(1, finalCount);
284 assertTrue(appcLcmQueue.isEmpty());
285 assertTrue(clMgtQueue.isEmpty());
288 verify(topics, times(3)).inject(eq(BaseTest.DCAE_TOPIC), any());
291 verify(topics).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
295 public void testTestVdnsSunnyDayCompliant() {
296 checkHttpPolicy(base::testVdnsSunnyDayCompliant);
300 public void testTestVdnsRainyDayCompliant() {
301 checkHttpPolicyCompliantFailure(base::testVdnsRainyDayCompliant);
305 public void testTestVfwSunnyDayCompliant() {
306 checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayCompliant);
310 public void testTestVfwRainyDayOverallTimeout() {
311 checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayOverallTimeout);
315 public void testTestVfwRainyDayCompliantTimeout() {
316 checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayCompliantTimeout);
320 public void testTestVpciSunnyDayCompliant() {
321 checkSdnrPolicy("ModifyConfig", base::testVpciSunnyDayCompliant);
325 public void testTestVsonhSunnyDayCompliant() {
326 checkSdnrPolicy("ModifyConfigANR", base::testVsonhSunnyDayCompliant);
329 protected void checkAppcLcmPolicy(String operation, Runnable test) {
330 enqueueAppcLcm(operation);
331 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
332 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
336 assertEquals(1, permitCount);
337 assertEquals(1, finalCount);
339 assertTrue(appcLcmQueue.isEmpty());
340 assertTrue(clMgtQueue.isEmpty());
343 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
345 // reply to each APPC request
346 verify(topics).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
349 protected void checkAppcLegacyPolicy(String operation, Runnable test) {
350 enqueueAppcLegacy(operation);
351 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
352 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
356 assertEquals(1, permitCount);
357 assertEquals(1, finalCount);
359 assertTrue(appcLcmQueue.isEmpty());
360 assertTrue(clMgtQueue.isEmpty());
363 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
365 // reply to each APPC request
366 verify(topics).inject(eq(BaseTest.APPC_CL_TOPIC), any(), any());
369 protected void checkAppcLegacyPolicyOperationFailure(String operation, Runnable test) {
370 enqueueAppcLegacy(operation);
371 enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
372 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
376 assertEquals(1, permitCount);
377 assertEquals(1, finalCount);
379 assertTrue(appcLcmQueue.isEmpty());
380 assertTrue(clMgtQueue.isEmpty());
383 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
385 // reply to each APPC request
386 verify(topics).inject(eq(BaseTest.APPC_CL_TOPIC), any(), any());
389 protected void checkAppcLegacyPolicyFinalFailure(String operation, Runnable test) {
390 enqueueAppcLegacy(operation);
391 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
395 assertEquals(1, permitCount);
396 assertEquals(1, finalCount);
398 assertTrue(appcLcmQueue.isEmpty());
399 assertTrue(clMgtQueue.isEmpty());
402 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
404 // There were no requests sent
407 protected void checkSdnrPolicy(String operation, Runnable test) {
408 enqueueSdnr(operation);
409 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
410 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
414 assertEquals(1, permitCount);
415 assertEquals(1, finalCount);
417 assertTrue(sdnrQueue.isEmpty());
418 assertTrue(clMgtQueue.isEmpty());
421 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
423 // reply to each SDNR request
424 verify(topics).inject(eq(BaseTest.SDNR_CL_RSP_TOPIC), any(), any());
427 protected void checkHttpPolicy(Runnable test) {
428 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
429 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
433 assertEquals(1, permitCount);
434 assertEquals(1, finalCount);
436 assertTrue(clMgtQueue.isEmpty());
439 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
442 protected void checkHttpPolicyCompliantFailure(Runnable test) {
443 enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
444 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
448 assertEquals(1, permitCount);
449 assertEquals(1, finalCount);
451 assertTrue(clMgtQueue.isEmpty());
454 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
457 private void enqueueClMgt(ControlLoopNotificationType type) {
458 VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
459 notif.setNotification(type);
460 notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
462 clMgtQueue.add(notif);
465 private void enqueueAppcLcm(String... operationNames) {
466 for (String oper : operationNames) {
467 AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
468 req.setRpcName(oper);
470 AppcLcmBody body = new AppcLcmBody();
473 AppcLcmInput input = new AppcLcmInput();
474 body.setInput(input);
476 AppcLcmCommonHeader header = new AppcLcmCommonHeader();
477 input.setCommonHeader(header);
479 header.setSubRequestId("my-subrequest-id");
481 appcLcmQueue.add(req);
485 private void enqueueAppcLegacy(String... operationNames) {
486 for (String oper : operationNames) {
487 Request req = new Request();
490 CommonHeader header = new CommonHeader();
491 req.setCommonHeader(header);
493 header.setSubRequestId("my-subrequest-id");
495 appcLegacyQueue.add(req);
499 private void enqueueSdnr(String... operationNames) {
500 for (String oper : operationNames) {
501 PciMessage pcimessage = new PciMessage();
502 PciRequest req = new PciRequest();
503 PciBody body = new PciBody();
505 pcimessage.setBody(body);
506 pcimessage.getBody().getInput().setAction(oper);
507 PciCommonHeader header = new PciCommonHeader();
508 pcimessage.getBody().getInput().setCommonHeader(header);
510 header.setSubRequestId("my-subrequest-id");
512 sdnrQueue.add(pcimessage);
516 private HttpClients makeHttpClients() {
520 private Simulators makeSim() {
524 private Topics makeTopics() {
529 * We don't want junit trying to run this, so it's marked "Ignore".
532 private class MyTest extends BaseTest {
535 protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
540 protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
541 Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
543 return policyClMgt.await(notif -> notif.getNotification() == finalType);