2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021,2023 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.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.junit.MockitoJUnitRunner;
48 import org.onap.policy.appc.CommonHeader;
49 import org.onap.policy.appc.Request;
50 import org.onap.policy.appclcm.AppcLcmBody;
51 import org.onap.policy.appclcm.AppcLcmCommonHeader;
52 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
53 import org.onap.policy.appclcm.AppcLcmInput;
54 import org.onap.policy.common.utils.coder.StandardCoder;
55 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
56 import org.onap.policy.controlloop.ControlLoopNotificationType;
57 import org.onap.policy.controlloop.VirtualControlLoopNotification;
58 import org.onap.policy.drools.controller.DroolsController;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
60 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
61 import org.onap.policy.sdnr.PciBody;
62 import org.onap.policy.sdnr.PciCommonHeader;
63 import org.onap.policy.sdnr.PciMessage;
64 import org.onap.policy.sdnr.PciRequest;
65 import org.springframework.test.util.ReflectionTestUtils;
67 @RunWith(MockitoJUnitRunner.class)
68 public class BaseTestTest {
69 private static final String POLICY_NAME = "my-policy-name";
72 private static Supplier<HttpClients> httpClientMaker;
73 private static Supplier<Simulators> simMaker;
74 private static Supplier<Topics> topicMaker;
76 private BaseTest base;
77 private LinkedList<VirtualControlLoopNotification> clMgtQueue;
78 private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
79 private Queue<Request> appcLegacyQueue;
80 private Queue<PciMessage> sdnrQueue;
81 private int permitCount;
82 private int finalCount;
85 private HttpClients httpClients;
87 private Simulators simulators;
89 private Topics topics;
91 private Listener<VirtualControlLoopNotification> policyClMgt;
93 private Listener<Request> appcClSink;
95 private Listener<AppcLcmDmaapWrapper> appcLcmRead;
97 private Listener<PciMessage> sdnrClSink;
99 private DroolsController drools;
101 private ToscaPolicy policy;
103 private ToscaConceptIdentifier policyIdent;
107 * Saves static values from the class.
109 @SuppressWarnings("unchecked")
111 public static void setUpBeforeClass() {
112 httpClientMaker = (Supplier<HttpClients>) ReflectionTestUtils.getField(BaseTest.class, "httpClientMaker");
113 simMaker = (Supplier<Simulators>) ReflectionTestUtils.getField(BaseTest.class, "simMaker");
114 topicMaker = (Supplier<Topics>) ReflectionTestUtils.getField(BaseTest.class, "topicMaker");
118 * Restores static values.
121 public static void tearDownAfterClass() {
122 ReflectionTestUtils.setField(BaseTest.class, "httpClientMaker", httpClientMaker);
123 ReflectionTestUtils.setField(BaseTest.class, "simMaker", simMaker);
124 ReflectionTestUtils.setField(BaseTest.class, "topicMaker", topicMaker);
131 public void setUp() {
132 when(topics.createListener(eq(BaseTest.POLICY_CL_MGT_TOPIC), eq(VirtualControlLoopNotification.class),
133 any(StandardCoder.class))).thenReturn(policyClMgt);
134 when(topics.createListener(eq(BaseTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
135 any(StandardCoder.class))).thenReturn(appcLcmRead);
136 when(topics.createListener(eq(BaseTest.APPC_CL_TOPIC), eq(Request.class),
137 any(StandardCoderInstantAsMillis.class))).thenReturn(appcClSink);
138 when(topics.createListener(eq(BaseTest.SDNR_CL_TOPIC), eq(PciMessage.class), any(StandardCoder.class)))
139 .thenReturn(sdnrClSink);
141 Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
142 Supplier<Simulators> simMaker = this::makeSim;
143 Supplier<Topics> topicMaker = this::makeTopics;
145 ReflectionTestUtils.setField(BaseTest.class, "httpClientMaker", httpClientMaker);
146 ReflectionTestUtils.setField(BaseTest.class, "simMaker", simMaker);
147 ReflectionTestUtils.setField(BaseTest.class, "topicMaker", topicMaker);
149 clMgtQueue = new LinkedList<>();
150 appcLcmQueue = new LinkedList<>();
151 appcLegacyQueue = new LinkedList<>();
152 sdnrQueue = new LinkedList<>();
154 when(policyClMgt.await(any())).thenAnswer(args -> {
155 VirtualControlLoopNotification notif = clMgtQueue.remove();
156 Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
157 assertTrue(pred.test(notif));
161 when(appcLcmRead.await(any())).thenAnswer(args -> {
162 AppcLcmDmaapWrapper req = appcLcmQueue.remove();
163 Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
164 assertTrue(pred.test(req));
168 when(appcClSink.await(any())).thenAnswer(args -> {
169 Request req = appcLegacyQueue.remove();
170 Predicate<Request> pred = args.getArgument(0);
171 assertTrue(pred.test(req));
175 when(sdnrClSink.await(any())).thenAnswer(args -> {
176 PciMessage pcireq = sdnrQueue.remove();
177 Predicate<PciMessage> pred = args.getArgument(0);
178 assertTrue(pred.test(pcireq));
187 BaseTest.initStatics();
192 public void testInitStatics() {
193 assertSame(httpClients, BaseTest.httpClients);
194 assertSame(simulators, BaseTest.simulators);
198 public void testFinishStatics() {
199 BaseTest.finishStatics();
200 verify(httpClients).destroy();
201 verify(simulators).destroy();
205 public void testInit() {
206 assertSame(topics, BaseTest.getTopics());
210 public void testFinish() {
212 verify(topics).destroy();
216 public void testTestService123Compliant() {
217 enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
218 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
219 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
221 base.testService123Compliant();
223 assertEquals(1, permitCount);
224 assertEquals(1, finalCount);
226 assertTrue(appcLcmQueue.isEmpty());
227 assertTrue(clMgtQueue.isEmpty());
230 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
232 // replies to each APPC request
233 verify(topics, times(6)).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
237 public void testTestDuplicatesEvents() {
238 // the test expects the count to be incremented by 2 between calls
239 AtomicLong count = new AtomicLong(5);
241 when(base.getCreateCount()).thenAnswer(args -> count.getAndAdd(2));
243 enqueueAppcLcm("restart", "restart");
244 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
245 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
247 clMgtQueue.get(0).setAai(Map.of("generic-vnf.vnf-id", "duplicate-VNF"));
248 clMgtQueue.get(1).setAai(Map.of("generic-vnf.vnf-id", "vCPE_Infrastructure_vGMUX_demo_app"));
250 base.testDuplicatesEvents();
252 assertEquals(0, permitCount);
253 assertEquals(2, finalCount);
255 assertTrue(appcLcmQueue.isEmpty());
256 assertTrue(clMgtQueue.isEmpty());
259 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
260 verify(topics, times(2)).inject(eq(BaseTest.DCAE_TOPIC), any(), any());
263 verify(topics, times(2)).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
267 public void testTestVcpeSunnyDayCompliant() {
268 checkAppcLcmPolicy("restart", base::testVcpeSunnyDayCompliant);
272 public void testTestVcpeOnsetFloodPrevention() {
273 enqueueAppcLcm("restart");
274 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
275 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
277 base.testVcpeOnsetFloodPrevention();
279 assertEquals(1, permitCount);
280 assertEquals(1, finalCount);
282 assertTrue(appcLcmQueue.isEmpty());
283 assertTrue(clMgtQueue.isEmpty());
286 verify(topics, times(3)).inject(eq(BaseTest.DCAE_TOPIC), any());
289 verify(topics).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
293 public void testTestVdnsSunnyDayCompliant() {
294 checkHttpPolicy(base::testVdnsSunnyDayCompliant);
298 public void testTestVdnsRainyDayCompliant() {
299 checkHttpPolicyCompliantFailure(base::testVdnsRainyDayCompliant);
303 public void testTestVfwSunnyDayCompliant() {
304 checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayCompliant);
308 public void testTestVfwRainyDayOverallTimeout() {
309 checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayOverallTimeout);
313 public void testTestVfwRainyDayCompliantTimeout() {
314 checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayCompliantTimeout);
318 public void testTestVpciSunnyDayCompliant() {
319 checkSdnrPolicy("ModifyConfig", base::testVpciSunnyDayCompliant);
323 public void testTestVsonhSunnyDayCompliant() {
324 checkSdnrPolicy("ModifyConfigANR", base::testVsonhSunnyDayCompliant);
327 protected void checkAppcLcmPolicy(String operation, Runnable test) {
328 enqueueAppcLcm(operation);
329 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
330 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
334 assertEquals(1, permitCount);
335 assertEquals(1, finalCount);
337 assertTrue(appcLcmQueue.isEmpty());
338 assertTrue(clMgtQueue.isEmpty());
341 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
343 // reply to each APPC request
344 verify(topics).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
347 protected void checkAppcLegacyPolicy(String operation, Runnable test) {
348 enqueueAppcLegacy(operation);
349 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
350 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
354 assertEquals(1, permitCount);
355 assertEquals(1, finalCount);
357 assertTrue(appcLcmQueue.isEmpty());
358 assertTrue(clMgtQueue.isEmpty());
361 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
363 // reply to each APPC request
364 verify(topics).inject(eq(BaseTest.APPC_CL_TOPIC), any(), any());
367 protected void checkAppcLegacyPolicyOperationFailure(String operation, Runnable test) {
368 enqueueAppcLegacy(operation);
369 enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
370 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
374 assertEquals(1, permitCount);
375 assertEquals(1, finalCount);
377 assertTrue(appcLcmQueue.isEmpty());
378 assertTrue(clMgtQueue.isEmpty());
381 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
383 // reply to each APPC request
384 verify(topics).inject(eq(BaseTest.APPC_CL_TOPIC), any(), any());
387 protected void checkAppcLegacyPolicyFinalFailure(String operation, Runnable test) {
388 enqueueAppcLegacy(operation);
389 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
393 assertEquals(1, permitCount);
394 assertEquals(1, finalCount);
396 assertTrue(appcLcmQueue.isEmpty());
397 assertTrue(clMgtQueue.isEmpty());
400 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
402 // There were no requests sent
405 protected void checkSdnrPolicy(String operation, Runnable test) {
406 enqueueSdnr(operation);
407 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
408 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
412 assertEquals(1, permitCount);
413 assertEquals(1, finalCount);
415 assertTrue(sdnrQueue.isEmpty());
416 assertTrue(clMgtQueue.isEmpty());
419 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
421 // reply to each SDNR request
422 verify(topics).inject(eq(BaseTest.SDNR_CL_RSP_TOPIC), any(), any());
425 protected void checkHttpPolicy(Runnable test) {
426 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
427 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
431 assertEquals(1, permitCount);
432 assertEquals(1, finalCount);
434 assertTrue(clMgtQueue.isEmpty());
437 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
440 protected void checkHttpPolicyCompliantFailure(Runnable test) {
441 enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
442 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
446 assertEquals(1, permitCount);
447 assertEquals(1, finalCount);
449 assertTrue(clMgtQueue.isEmpty());
452 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
455 private void enqueueClMgt(ControlLoopNotificationType type) {
456 VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
457 notif.setNotification(type);
458 notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
460 clMgtQueue.add(notif);
463 private void enqueueAppcLcm(String... operationNames) {
464 for (String oper : operationNames) {
465 AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
466 req.setRpcName(oper);
468 AppcLcmBody body = new AppcLcmBody();
471 AppcLcmInput input = new AppcLcmInput();
472 body.setInput(input);
474 AppcLcmCommonHeader header = new AppcLcmCommonHeader();
475 input.setCommonHeader(header);
477 header.setSubRequestId("my-subrequest-id");
479 appcLcmQueue.add(req);
483 private void enqueueAppcLegacy(String... operationNames) {
484 for (String oper : operationNames) {
485 Request req = new Request();
488 CommonHeader header = new CommonHeader();
489 req.setCommonHeader(header);
491 header.setSubRequestId("my-subrequest-id");
493 appcLegacyQueue.add(req);
497 private void enqueueSdnr(String... operationNames) {
498 for (String oper : operationNames) {
499 PciMessage pcimessage = new PciMessage();
500 PciRequest req = new PciRequest();
501 PciBody body = new PciBody();
503 pcimessage.setBody(body);
504 pcimessage.getBody().getInput().setAction(oper);
505 PciCommonHeader header = new PciCommonHeader();
506 pcimessage.getBody().getInput().setCommonHeader(header);
508 header.setSubRequestId("my-subrequest-id");
510 sdnrQueue.add(pcimessage);
514 private HttpClients makeHttpClients() {
518 private Simulators makeSim() {
522 private Topics makeTopics() {
527 * We don't want junit trying to run this, so it's marked "Ignore".
530 private class MyTest extends BaseTest {
533 protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
538 protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
539 Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
541 return policyClMgt.await(notif -> notif.getNotification() == finalType);