transaction records cleanup
[policy/drools-applications.git] / controlloop / common / controller-usecases / src / test / java / org / onap / policy / controlloop / UsecasesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2022 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop;
22
23 import org.junit.After;
24 import org.junit.AfterClass;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.runner.RunWith;
28 import org.onap.policy.controlloop.common.rules.test.DroolsRuleTest;
29 import org.onap.policy.controlloop.common.rules.test.Listener;
30 import org.onap.policy.controlloop.common.rules.test.NamedRunner;
31 import org.onap.policy.controlloop.common.rules.test.TestNames;
32 import org.onap.policy.drools.apps.controller.usecases.UsecasesEventManager;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
34 import org.onap.policy.simulators.Util;
35
36 /**
37  * Tests use cases using Usecases rules.
38  *
39  * <p/>
40  * Note: this runs ALL tests (i.e., any whose names start with "test").
41  */
42 @RunWith(NamedRunner.class)
43 @TestNames(prefixes = {"test"})
44
45 public class UsecasesTest extends DroolsRuleTest {
46     protected static final String CONTROLLER_NAME = "usecases";
47
48
49     /**
50      * Sets up statics.
51      */
52     @BeforeClass
53     public static void setUpBeforeClass() {
54         initStatics(CONTROLLER_NAME);
55
56         rules.configure("src/main/resources");
57         httpClients.addClients("usecases");
58         simulators.start(Util::buildAaiSim, Util::buildSoSim, Util::buildVfcSim, Util::buildXacmlSim,
59                         Util::buildSdncSim);
60
61         rules.start();
62     }
63
64     /**
65      * Cleans up statics.
66      */
67     @AfterClass
68     public static void tearDownAfterClass() {
69         finishStatics();
70     }
71
72     /**
73      * Sets up.
74      */
75     @Before
76     public void setUp() {
77         init();
78     }
79
80     /**
81      * Tears down.
82      */
83     @After
84     public void tearDown() {
85         finish();
86     }
87
88     @Override
89     protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
90         String policyName = policy.getIdentifier().getName();
91
92         policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.ACTIVE
93                         && "EVENT.MANAGER.ACCEPT".equals(notif.getPolicyScope())
94                         && policyName.equals(notif.getPolicyName()));
95
96         policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION
97                         && "EVENT.MANAGER.PROCESS.GUARD.OUTCOME".equals(notif.getPolicyScope())
98                         && policyName.equals(notif.getPolicyName())
99                         && notif.getMessage().startsWith("Sending guard query"));
100
101         policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION
102                         && "EVENT.MANAGER.PROCESS.GUARD.OUTCOME".equals(notif.getPolicyScope())
103                         && policyName.equals(notif.getPolicyName())
104                         && notif.getMessage().startsWith("Guard result")
105                         && notif.getMessage().endsWith("Permit"));
106
107         policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION
108                         && "EVENT.MANAGER.PROCESS.POLICY.STARTED".equals(notif.getPolicyScope())
109                         && policyName.equals(notif.getPolicyName())
110                         && notif.getMessage().startsWith("actor="));
111     }
112
113     @Override
114     protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
115                     Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
116
117         return policyClMgt.await(notif -> notif.getNotification() == finalType
118                         && "EVENT.MANAGER.FINAL".equals(notif.getPolicyScope())
119                         && (policy.getIdentifier().getName()).equals(notif.getPolicyName()));
120     }
121
122     @Override
123     protected long getCreateCount() {
124         return UsecasesEventManager.getCreateCount();
125     }
126 }