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