625a1a0f1e0b41218990ea51beb205b32428a2fb
[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  * Modifications Copyright (C) 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop;
23
24 import org.junit.jupiter.api.AfterAll;
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeAll;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.extension.ExtendWith;
29 import org.onap.policy.controlloop.common.rules.test.DroolsRuleTest;
30 import org.onap.policy.controlloop.common.rules.test.Listener;
31 import org.onap.policy.controlloop.common.rules.test.NamedRunner;
32 import org.onap.policy.controlloop.common.rules.test.TestNames;
33 import org.onap.policy.drools.apps.controller.usecases.UsecasesEventManager;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.simulators.Util;
36
37 /**
38  * Tests use cases using Usecases rules.
39  *
40  * <p/>
41  * Note: this runs ALL tests (i.e., any whose names start with "test").
42  */
43 @ExtendWith(NamedRunner.class)
44 @TestNames(prefixes = {"test"})
45 class UsecasesTest extends DroolsRuleTest {
46     protected static final String CONTROLLER_NAME = "usecases";
47
48
49     /**
50      * Sets up statics.
51      */
52     @BeforeAll
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     @AfterAll
68     public static void tearDownAfterClass() {
69         finishStatics();
70     }
71
72     /**
73      * Sets up.
74      */
75     @BeforeEach
76     public void setUp() {
77         init();
78     }
79
80     /**
81      * Tears down.
82      */
83     @AfterEach
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 }