Merge "Delete template.demo submodule, amsterdam controller"
[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 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 lombok.Getter;
24 import org.junit.After;
25 import org.junit.AfterClass;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.runner.RunWith;
29 import org.onap.policy.controlloop.common.rules.test.BaseRuleTest;
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.utils.PropertyUtil;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.simulators.Util;
36
37 /**
38  * Tests use case rules. Only a subset of available tests will work with these rules, thus
39  * a "FilterRunner" is used to filter out the other test cases.
40  *
41  * <p/>
42  * Note: this only runs tests whose names start with "testV" (e.g., testVcpe(),
43  * testVdns()).
44  */
45 @RunWith(NamedRunner.class)
46 @TestNames(prefixes = {"testV"})
47
48 public class UsecasesTest extends BaseRuleTest {
49
50     @Getter()
51     protected static final String CONTROLLER_NAME = "usecases";
52
53     /**
54      * Sets up statics.
55      */
56     @BeforeClass
57     public static void setUpBeforeClass() {
58         initStatics(CONTROLLER_NAME);
59
60         rules.configure("src/main/resources");
61         PropertyUtil.setSystemProperties(rules.getPdpdRepo().getSystemProperties("controlloop"));
62         rules.getPdpd().setEnvironment(rules.getPdpdRepo().getEnvironmentProperties("controlloop.properties"));
63
64         rules.start();
65         simulators.start(Util::buildAaiSim, Util::buildSoSim, Util::buildVfcSim, Util::buildGuardSim,
66                         Util::buildSdncSim);
67     }
68
69     /**
70      * Cleans up statics.
71      */
72     @AfterClass
73     public static void tearDownAfterClass() {
74         finishStatics();
75     }
76
77     /**
78      * Sets up.
79      */
80     @Before
81     public void setUp() {
82         init();
83     }
84
85     /**
86      * Tears down.
87      */
88     @After
89     public void tearDown() {
90         finish();
91     }
92
93     @Override
94     protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
95         String policyName = policy.getIdentifier().getName();
96
97         policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.ACTIVE
98                         && (policyName + ".EVENT").equals(notif.getPolicyName()));
99
100         policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION
101                         && (policyName + ".EVENT.MANAGER.OPERATION.LOCKED.GUARD_NOT_YET_QUERIED")
102                                         .equals(notif.getPolicyName()));
103
104         policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION
105                         && (policyName + ".GUARD.RESPONSE").equals(notif.getPolicyName())
106                         && notif.getMessage().startsWith("Guard result") && notif.getMessage().endsWith("Permit"));
107
108         policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION
109                         && (policyName + ".EVENT.MANAGER.OPERATION.LOCKED.GUARD_PERMITTED")
110                                         .equals(notif.getPolicyName()));
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                         && (policy.getIdentifier().getName() + ".EVENT.MANAGER").equals(notif.getPolicyName()));
119     }
120 }