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), any(StandardCoder.class)))
140 .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, BaseTest.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 testTestVcpeSunnyDayCompliant() {
269 checkAppcLcmPolicy("restart", base::testVcpeSunnyDayCompliant);
273 public void testTestVcpeOnsetFloodPrevention() {
274 enqueueAppcLcm("restart");
275 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
276 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
278 base.testVcpeOnsetFloodPrevention();
280 assertEquals(1, permitCount);
281 assertEquals(1, finalCount);
283 assertTrue(appcLcmQueue.isEmpty());
284 assertTrue(clMgtQueue.isEmpty());
287 verify(topics, times(3)).inject(eq(BaseTest.DCAE_TOPIC), any());
290 verify(topics).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
294 public void testTestVdnsSunnyDayCompliant() {
295 checkHttpPolicy(base::testVdnsSunnyDayCompliant);
299 public void testTestVdnsRainyDayCompliant() {
300 checkHttpPolicyCompliantFailure(base::testVdnsRainyDayCompliant);
304 public void testTestVfwSunnyDayCompliant() {
305 checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayCompliant);
309 public void testTestVfwRainyDayOverallTimeout() {
310 checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayOverallTimeout);
314 public void testTestVfwRainyDayCompliantTimeout() {
315 checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayCompliantTimeout);
319 public void testTestVpciSunnyDayCompliant() {
320 checkSdnrPolicy("ModifyConfig", base::testVpciSunnyDayCompliant);
324 public void testTestVsonhSunnyDayCompliant() {
325 checkSdnrPolicy("ModifyConfigANR", base::testVsonhSunnyDayCompliant);
328 protected void checkAppcLcmPolicy(String operation, Runnable test) {
329 enqueueAppcLcm(operation);
330 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
331 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
335 assertEquals(1, permitCount);
336 assertEquals(1, finalCount);
338 assertTrue(appcLcmQueue.isEmpty());
339 assertTrue(clMgtQueue.isEmpty());
342 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
344 // reply to each APPC request
345 verify(topics).inject(eq(BaseTest.APPC_LCM_WRITE_TOPIC), any(), any());
348 protected void checkAppcLegacyPolicy(String operation, Runnable test) {
349 enqueueAppcLegacy(operation);
350 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
351 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
355 assertEquals(1, permitCount);
356 assertEquals(1, finalCount);
358 assertTrue(appcLcmQueue.isEmpty());
359 assertTrue(clMgtQueue.isEmpty());
362 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
364 // reply to each APPC request
365 verify(topics).inject(eq(BaseTest.APPC_CL_TOPIC), any(), any());
368 protected void checkAppcLegacyPolicyOperationFailure(String operation, Runnable test) {
369 enqueueAppcLegacy(operation);
370 enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
371 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
375 assertEquals(1, permitCount);
376 assertEquals(1, finalCount);
378 assertTrue(appcLcmQueue.isEmpty());
379 assertTrue(clMgtQueue.isEmpty());
382 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
384 // reply to each APPC request
385 verify(topics).inject(eq(BaseTest.APPC_CL_TOPIC), any(), any());
388 protected void checkAppcLegacyPolicyFinalFailure(String operation, Runnable test) {
389 enqueueAppcLegacy(operation);
390 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
394 assertEquals(1, permitCount);
395 assertEquals(1, finalCount);
397 assertTrue(appcLcmQueue.isEmpty());
398 assertTrue(clMgtQueue.isEmpty());
401 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
403 // There were no requests sent
406 protected void checkSdnrPolicy(String operation, Runnable test) {
407 enqueueSdnr(operation);
408 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
409 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
413 assertEquals(1, permitCount);
414 assertEquals(1, finalCount);
416 assertTrue(sdnrQueue.isEmpty());
417 assertTrue(clMgtQueue.isEmpty());
420 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
422 // reply to each SDNR request
423 verify(topics).inject(eq(BaseTest.SDNR_CL_RSP_TOPIC), any(), any());
426 protected void checkHttpPolicy(Runnable test) {
427 enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
428 enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
432 assertEquals(1, permitCount);
433 assertEquals(1, finalCount);
435 assertTrue(clMgtQueue.isEmpty());
438 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
441 protected void checkHttpPolicyCompliantFailure(Runnable test) {
442 enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
443 enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
447 assertEquals(1, permitCount);
448 assertEquals(1, finalCount);
450 assertTrue(clMgtQueue.isEmpty());
453 verify(topics).inject(eq(BaseTest.DCAE_TOPIC), any());
456 private void enqueueClMgt(ControlLoopNotificationType type) {
457 VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
458 notif.setNotification(type);
459 notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
461 clMgtQueue.add(notif);
464 private void enqueueAppcLcm(String... operationNames) {
465 for (String oper : operationNames) {
466 AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
467 req.setRpcName(oper);
469 AppcLcmBody body = new AppcLcmBody();
472 AppcLcmInput input = new AppcLcmInput();
473 body.setInput(input);
475 AppcLcmCommonHeader header = new AppcLcmCommonHeader();
476 input.setCommonHeader(header);
478 header.setSubRequestId("my-subrequest-id");
480 appcLcmQueue.add(req);
484 private void enqueueAppcLegacy(String... operationNames) {
485 for (String oper : operationNames) {
486 Request req = new Request();
489 CommonHeader header = new CommonHeader();
490 req.setCommonHeader(header);
492 header.setSubRequestId("my-subrequest-id");
494 appcLegacyQueue.add(req);
498 private void enqueueSdnr(String... operationNames) {
499 for (String oper : operationNames) {
500 PciMessage pcimessage = new PciMessage();
501 PciRequest req = new PciRequest();
502 PciBody body = new PciBody();
504 pcimessage.setBody(body);
505 pcimessage.getBody().getInput().setAction(oper);
506 PciCommonHeader header = new PciCommonHeader();
507 pcimessage.getBody().getInput().setCommonHeader(header);
509 header.setSubRequestId("my-subrequest-id");
511 sdnrQueue.add(pcimessage);
515 private HttpClients makeHttpClients() {
519 private Simulators makeSim() {
523 private Topics makeTopics() {
528 * We don't want junit trying to run this, so it's marked "Ignore".
531 private class MyTest extends BaseTest {
534 protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
539 protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
540 Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
542 return policyClMgt.await(notif -> notif.getNotification() == finalType);