2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.common.rules.test;
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.spy;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.util.LinkedList;
35 import java.util.Queue;
36 import java.util.concurrent.atomic.AtomicLong;
37 import java.util.function.Predicate;
38 import java.util.function.Supplier;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Ignore;
43 import org.junit.Test;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.policy.appc.CommonHeader;
47 import org.onap.policy.appc.Request;
48 import org.onap.policy.appclcm.AppcLcmBody;
49 import org.onap.policy.appclcm.AppcLcmCommonHeader;
50 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
51 import org.onap.policy.appclcm.AppcLcmInput;
52 import org.onap.policy.common.utils.coder.StandardCoder;
53 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
54 import org.onap.policy.controlloop.ControlLoopNotificationType;
55 import org.onap.policy.controlloop.VirtualControlLoopNotification;
56 import org.onap.policy.drools.controller.DroolsController;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
59 import org.onap.policy.sdnr.PciBody;
60 import org.onap.policy.sdnr.PciCommonHeader;
61 import org.onap.policy.sdnr.PciMessage;
62 import org.onap.policy.sdnr.PciRequest;
63 import org.powermock.reflect.Whitebox;
65 public class BaseTestTest {
66 private static final String POLICY_NAME = "my-policy-name";
69 private static Supplier<HttpClients> httpClientMaker;
70 private static Supplier<Simulators> simMaker;
71 private static Supplier<Topics> topicMaker;
73 private BaseTest base;
74 private LinkedList<VirtualControlLoopNotification> clMgtQueue;
75 private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
76 private Queue<Request> appcLegacyQueue;
77 private Queue<PciMessage> sdnrQueue;
78 private int permitCount;
79 private int finalCount;
82 private HttpClients httpClients;
84 private Simulators simulators;
86 private Topics topics;
88 private Listener<VirtualControlLoopNotification> policyClMgt;
90 private Listener<Request> appcClSink;
92 private Listener<AppcLcmDmaapWrapper> appcLcmRead;
94 private Listener<PciMessage> sdnrClSink;
96 private DroolsController drools;
98 private ToscaPolicy policy;
100 private ToscaPolicyIdentifier policyIdent;
104 * Saves static values from the class.
107 public static void setUpBeforeClass() {
108 httpClientMaker = Whitebox.getInternalState(BaseTest.class, "httpClientMaker");
109 simMaker = Whitebox.getInternalState(BaseTest.class, "simMaker");
110 topicMaker = Whitebox.getInternalState(BaseTest.class, "topicMaker");
114 * Restores static values.
117 public static void tearDownAfterClass() {
118 Whitebox.setInternalState(BaseTest.class, "httpClientMaker", httpClientMaker);
119 Whitebox.setInternalState(BaseTest.class, "simMaker", simMaker);
120 Whitebox.setInternalState(BaseTest.class, "topicMaker", topicMaker);
127 public void setUp() {
128 MockitoAnnotations.initMocks(this);
130 when(policy.getIdentifier()).thenReturn(policyIdent);
131 when(policyIdent.getName()).thenReturn(POLICY_NAME);
133 when(topics.createListener(eq(BaseTest.POLICY_CL_MGT_TOPIC), eq(VirtualControlLoopNotification.class),
134 any(StandardCoder.class))).thenReturn(policyClMgt);
135 when(topics.createListener(eq(BaseTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
136 any(StandardCoder.class))).thenReturn(appcLcmRead);
137 when(topics.createListener(eq(BaseTest.APPC_CL_TOPIC), eq(Request.class),
138 any(StandardCoderInstantAsMillis.class))).thenReturn(appcClSink);
139 when(topics.createListener(eq(BaseTest.SDNR_CL_TOPIC), eq(PciMessage.class),
140 any(StandardCoder.class))).thenReturn(sdnrClSink);
142 Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
143 Supplier<Simulators> simMaker = this::makeSim;
144 Supplier<Topics> topicMaker = this::makeTopics;
146 Whitebox.setInternalState(BaseTest.class, "httpClientMaker", httpClientMaker);
147 Whitebox.setInternalState(BaseTest.class, "simMaker", simMaker);
148 Whitebox.setInternalState(BaseTest.class, "topicMaker", topicMaker);
150 clMgtQueue = new LinkedList<>();
151 appcLcmQueue = new LinkedList<>();
152 appcLegacyQueue = new LinkedList<>();
153 sdnrQueue = new LinkedList<>();
155 when(policyClMgt.await(any())).thenAnswer(args -> {
156 VirtualControlLoopNotification notif = clMgtQueue.remove();
157 Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
158 assertTrue(pred.test(notif));
162 when(appcLcmRead.await(any())).thenAnswer(args -> {
163 AppcLcmDmaapWrapper req = appcLcmQueue.remove();
164 Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
165 assertTrue(pred.test(req));
169 when(appcClSink.await(any())).thenAnswer(args -> {
170 Request req = appcLegacyQueue.remove();
171 Predicate<Request> pred = args.getArgument(0);
172 assertTrue(pred.test(req));
176 when(sdnrClSink.await(any())).thenAnswer(args -> {
177 PciMessage pcireq = sdnrQueue.remove();
178 Predicate<PciMessage> pred = args.getArgument(0);
179 assertTrue(pred.test(pcireq));
188 BaseTest.initStatics();
193 public void testInitStatics() {
194 assertSame(httpClients, BaseTest.httpClients);
195 assertSame(simulators, BaseTest.simulators);
199 public void testFinishStatics() {
200 BaseTest.finishStatics();
201 verify(httpClients).destroy();
202 verify(simulators).destroy();
206 public void testInit() {
207 assertSame(topics, base.getTopics());
211 public void testFinish() {
213 verify(topics).destroy();
217 public void testTestService123Compliant() {
218 enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
219 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
220 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
222 base.testService123Compliant();
224 assertEquals(1, permitCount);
225 assertEquals(1, finalCount);
227 assertTrue(appcLcmQueue.isEmpty());
228 assertTrue(clMgtQueue.isEmpty());
231 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
233 // replies to each APPC request
234 verify(topics, times(6)).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
238 public void testTestDuplicatesEvents() {
239 // the test expects the count to be incremented by 2 between calls
240 AtomicLong count = new AtomicLong(5);
242 when(base.getCreateCount()).thenAnswer(args -> count.getAndAdd(2));
244 enqueueAppcLcm("restart", "restart");
245 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
246 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
248 clMgtQueue.get(0).setAai(Map.of("generic-vnf.vnf-id", "duplicate-VNF"));
249 clMgtQueue.get(1).setAai(Map.of("generic-vnf.vnf-id", "vCPE_Infrastructure_vGMUX_demo_app"));
251 base.testDuplicatesEvents();
253 assertEquals(0, permitCount);
254 assertEquals(2, finalCount);
256 assertTrue(appcLcmQueue.isEmpty());
257 assertTrue(clMgtQueue.isEmpty());
260 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
261 verify(topics, times(2)).inject(eq(BaseTest.DCAE_TOPIC), any(), any());
264 verify(topics, times(2)).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
268 public void testTestVcpeSunnyDayLegacy() {
269 checkAppcLcmPolicy("restart", base::testVcpeSunnyDayLegacy);
273 public void testTestVcpeSunnyDayCompliant() {
274 checkAppcLcmPolicy("restart", base::testVcpeSunnyDayCompliant);
278 public void testTestVcpeOnsetFloodPrevention() {
279 enqueueAppcLcm("restart");
280 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
281 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
283 base.testVcpeOnsetFloodPrevention();
285 assertEquals(1, permitCount);
286 assertEquals(1, finalCount);
288 assertTrue(appcLcmQueue.isEmpty());
289 assertTrue(clMgtQueue.isEmpty());
292 verify(topics, times(3)).inject(eq(BaseTest.DCAE_TOPIC), any());
295 verify(topics).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
299 public void testTestVdnsSunnyDayLegacy() {
300 checkHttpPolicy(base::testVdnsSunnyDayLegacy);
304 public void testTestVdnsSunnyDayCompliant() {
305 checkHttpPolicy(base::testVdnsSunnyDayCompliant);
309 public void testTestVdnsRainyDayCompliant() {
310 checkHttpPolicyCompliantFailure(base::testVdnsRainyDayCompliant);
314 public void testTestVfwSunnyDayLegacy() {
315 checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayLegacy);
319 public void testTestVfwSunnyDayCompliant() {
320 checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayCompliant);
324 public void testTestVfwRainyDayLegacyFailure() {
325 checkAppcLegacyPolicyOperationFailure("ModifyConfig", base::testVfwRainyDayLegacyFailure);
329 public void testTestVfwRainyDayOverallTimeout() {
330 checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayOverallTimeout);
334 public void testTestVfwRainyDayCompliantTimeout() {
335 checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayCompliantTimeout);
339 public void testTestVpciSunnyDayLegacy() {
340 checkSdnrPolicy("ModifyConfig", base::testVpciSunnyDayLegacy);
344 public void testTestVpciSunnyDayCompliant() {
345 checkSdnrPolicy("ModifyConfig", base::testVpciSunnyDayCompliant);
349 public void testTestVsonhSunnyDayLegacy() {
350 checkSdnrPolicy("ModifyConfigANR", base::testVsonhSunnyDayLegacy);
354 public void testTestVsonhSunnyDayCompliant() {
355 checkSdnrPolicy("ModifyConfigANR", base::testVsonhSunnyDayCompliant);
358 protected void checkAppcLcmPolicy(String operation, Runnable test) {
359 enqueueAppcLcm(operation);
360 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
361 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
365 assertEquals(1, permitCount);
366 assertEquals(1, finalCount);
368 assertTrue(appcLcmQueue.isEmpty());
369 assertTrue(clMgtQueue.isEmpty());
372 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
374 // reply to each APPC request
375 verify(topics).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
378 protected void checkAppcLegacyPolicy(String operation, Runnable test) {
379 enqueueAppcLegacy(operation);
380 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
381 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
385 assertEquals(1, permitCount);
386 assertEquals(1, finalCount);
388 assertTrue(appcLcmQueue.isEmpty());
389 assertTrue(clMgtQueue.isEmpty());
392 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
394 // reply to each APPC request
395 verify(topics).inject(eq(BaseTest.APPC_CL_TOPIC), any(), any());
398 protected void checkAppcLegacyPolicyOperationFailure(String operation, Runnable test) {
399 enqueueAppcLegacy(operation);
400 enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
401 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
405 assertEquals(1, permitCount);
406 assertEquals(1, finalCount);
408 assertTrue(appcLcmQueue.isEmpty());
409 assertTrue(clMgtQueue.isEmpty());
412 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
414 // reply to each APPC request
415 verify(topics).inject(eq(BaseTest.APPC_CL_TOPIC), any(), any());
418 protected void checkAppcLegacyPolicyFinalFailure(String operation, Runnable test) {
419 enqueueAppcLegacy(operation);
420 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
424 assertEquals(1, permitCount);
425 assertEquals(1, finalCount);
427 assertTrue(appcLcmQueue.isEmpty());
428 assertTrue(clMgtQueue.isEmpty());
431 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
433 // There were no requests sent
436 protected void checkSdnrPolicy(String operation, Runnable test) {
437 enqueueSdnr(operation);
438 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
439 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
443 assertEquals(1, permitCount);
444 assertEquals(1, finalCount);
446 assertTrue(sdnrQueue.isEmpty());
447 assertTrue(clMgtQueue.isEmpty());
450 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
452 // reply to each SDNR request
453 verify(topics).inject(eq(BaseTest.SDNR_CL_RSP_TOPIC), any(), any());
456 protected void checkHttpPolicy(Runnable test) {
457 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
458 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
462 assertEquals(1, permitCount);
463 assertEquals(1, finalCount);
465 assertTrue(clMgtQueue.isEmpty());
468 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
471 protected void checkHttpPolicyCompliantFailure(Runnable test) {
472 enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
473 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
477 assertEquals(1, permitCount);
478 assertEquals(1, finalCount);
480 assertTrue(clMgtQueue.isEmpty());
483 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
486 private void enqueueClMgt(ControlLoopNotificationType type) {
487 VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
488 notif.setNotification(type);
489 notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
491 clMgtQueue.add(notif);
494 private void enqueueAppcLcm(String... operationNames) {
495 for (String oper : operationNames) {
496 AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
497 req.setRpcName(oper);
499 AppcLcmBody body = new AppcLcmBody();
502 AppcLcmInput input = new AppcLcmInput();
503 body.setInput(input);
505 AppcLcmCommonHeader header = new AppcLcmCommonHeader();
506 input.setCommonHeader(header);
508 header.setSubRequestId("my-subrequest-id");
510 appcLcmQueue.add(req);
514 private void enqueueAppcLegacy(String... operationNames) {
515 for (String oper : operationNames) {
516 Request req = new Request();
519 CommonHeader header = new CommonHeader();
520 req.setCommonHeader(header);
522 header.setSubRequestId("my-subrequest-id");
524 appcLegacyQueue.add(req);
528 private void enqueueSdnr(String... operationNames) {
529 for (String oper : operationNames) {
530 PciMessage pcimessage = new PciMessage();
531 PciRequest req = new PciRequest();
532 PciBody body = new PciBody();
534 pcimessage.setBody(body);
535 pcimessage.getBody().getInput().setAction(oper);
536 PciCommonHeader header = new PciCommonHeader();
537 pcimessage.getBody().getInput().setCommonHeader(header);
539 header.setSubRequestId("my-subrequest-id");
541 sdnrQueue.add(pcimessage);
545 private HttpClients makeHttpClients() {
549 private Simulators makeSim() {
553 private Topics makeTopics() {
558 * We don't want junit trying to run this, so it's marked "Ignore".
561 private class MyTest extends BaseTest {
564 protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
569 protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
570 Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
572 return policyClMgt.await(notif -> notif.getNotification() == finalType);