5G SON usecase A1 O1 policies support
[policy/drools-applications.git] / controlloop / common / rules-test / src / main / java / org / onap / policy / controlloop / common / rules / test / DroolsRuleTest.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.common.rules.test;
22
23 import java.util.function.Function;
24 import org.onap.policy.controlloop.VirtualControlLoopNotification;
25 import org.onap.policy.drools.system.PolicyController;
26 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
27
28 /**
29  * Superclass used for rule tests.
30  */
31 public abstract class DroolsRuleTest extends BaseTest {
32
33     // these may be overridden by junit tests
34     private static final Function<String, Rules> ruleMaker = Rules::new;
35
36     protected static Rules rules;
37
38     protected PolicyController controller;
39
40     /**
41      * Initializes {@link #rules}, {@link #httpClients}, and {@link #simulators}.
42      *
43      * @param controllerName the rule controller name
44      */
45     public static void initStatics(String controllerName) {
46         rules = ruleMaker.apply(controllerName);
47         BaseTest.initStatics();
48     }
49
50     /**
51      * Destroys {@link #httpClients}, {@link #simulators}, and {@link #rules}.
52      */
53     public static void finishStatics() {
54         BaseTest.finishStatics();
55         rules.destroy();
56     }
57
58     /**
59      * Initializes {@link #topics} and {@link #controller}.
60      */
61     @Override
62     public void init() {
63         super.init();
64         controller = rules.getController();
65     }
66
67     /**
68      * Destroys {@link #topics} and resets the rule facts.
69      */
70     @Override
71     public void finish() {
72         super.finish();
73         rules.resetFacts();
74     }
75
76     /**
77      * Returns ToscaPolicy from File.
78      *
79      * @param fileName a path name
80      * @return ToscaPolicy
81      */
82     @Override
83     protected ToscaPolicy checkPolicy(String fileName)  {
84         return rules.setupPolicyFromFile(fileName);
85     }
86
87     /**
88      * Returns Listener from createListener based on Coder.
89      * @return the Listener
90      */
91     @Override
92     protected Listener<VirtualControlLoopNotification> createNoficationTopicListener() {
93         return topics.createListener(POLICY_CL_MGT_TOPIC,
94             VirtualControlLoopNotification.class, controller);
95     }
96 }