e0696abff296a7b392aeb6b10d7f21d50e6fb97d
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * demo
4  * ================================================================================
5  * Copyright (C) 2018 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.template.demo.clc;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.IOException;
29 import java.io.UnsupportedEncodingException;
30 import java.net.URLEncoder;
31 import java.util.Iterator;
32 import java.util.LinkedList;
33 import java.util.List;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.kie.api.runtime.KieSession;
38 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
39 import org.onap.policy.drools.utils.logging.LoggerUtil;
40 import org.onap.policy.template.demo.clc.SupportUtil.Pair;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Verifies that Params objects are cleaned up when rules are updated. This loads
46  * <b>two</b> copies of the rule set into a single policy to ensure that the two copies
47  * interact appropriately with each other's Params objects.
48  */
49 public class ControlLoopParamsCleanupTest {
50     private static final Logger logger = LoggerFactory.getLogger(ControlLoopParamsCleanupTest.class);
51
52     private static final String YAML = "src/test/resources/yaml/policy_ControlLoop_ParamsCleanup-test.yaml";
53
54     /**
55      * YAML to be used when the first rule set is updated.
56      */
57     private static final String YAML2 = "src/test/resources/yaml/policy_ControlLoop_ParamsCleanup-test2.yaml";
58
59     private static final String POLICY_VERSION = "v2.0";
60
61     private static final String POLICY_NAME = "CL_CleanupTest";
62
63     private static final String POLICY_SCOPE = "type=operational";
64
65     private static final String CONTROL_LOOP_NAME = "ControlLoop-Params-Cleanup-Test";
66
67     private static final String DROOLS_TEMPLATE = "src/main/resources/__closedLoopControlName__.drl";
68
69     // values specific to the second copy of the rules
70
71     private static final String YAML_B = "src/test/resources/yaml/policy_ControlLoop_ParamsCleanup-test-B.yaml";
72     private static final String POLICY_NAME_B = "CL_CleanupTest_B";
73     private static final String CONTROL_LOOP_NAME_B = "ControlLoop-Params-Cleanup-Test-B";
74
75     private static KieSession kieSession;
76     private static SupportUtil.RuleSpec[] specifications;
77
78     /**
79      * Setup the simulator.
80      */
81     @BeforeClass
82     public static void setUpSimulator() {
83         LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO");
84
85         try {
86             specifications = new SupportUtil.RuleSpec[2];
87
88             specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME,
89                             POLICY_VERSION, loadYaml(YAML));
90
91             specifications[1] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE,
92                             POLICY_NAME_B, POLICY_VERSION, loadYaml(YAML_B));
93
94             kieSession = SupportUtil.buildContainer(POLICY_VERSION, specifications);
95
96         } catch (IOException e) {
97             logger.error("Could not create kieSession", e);
98             fail("Could not create kieSession");
99         }
100     }
101
102     /**
103      * Tear down.
104      */
105     @AfterClass
106     public static void tearDown() {
107         kieSession.dispose();
108     }
109
110     @Test
111     public void test() throws IOException {
112
113         /*
114          * Let rules create Params objects. There should be one object for each set of
115          * rules.
116          */
117         kieSession.fireAllRules();
118         List<Object> facts = getSessionObjects();
119         assertEquals(specifications.length, facts.size());
120         Iterator<Object> iter = facts.iterator();
121
122         final Object fact1 = iter.next();
123         assertTrue(fact1.toString().contains(loadYaml(YAML)));
124
125         final Object fact1b = iter.next();
126         assertTrue(fact1b.toString().contains(loadYaml(YAML_B)));
127
128         logger.info("UPDATING VERSION TO v3.0");
129         updatePolicy(YAML2, "v3.0");
130
131         /*
132          * Let rules update Params objects. The Params for the first set of rules should
133          * now be deleted and replaced with a new one, while the Params for the second set
134          * should be unchanged.
135          */
136         kieSession.fireAllRules();
137         facts = getSessionObjects();
138         assertEquals(specifications.length, facts.size());
139         iter = facts.iterator();
140
141         final Object fact2 = iter.next();
142         assertTrue(fact2 != fact1);
143         assertTrue(fact2 != fact1b);
144         assertTrue(fact2.toString().contains(loadYaml(YAML2)));
145
146         assertTrue(iter.next() == fact1b);
147
148         logger.info("UPDATING VERSION TO v4.0");
149         updatePolicy(YAML, "v4.0");
150
151         /*
152          * Let rules update Params objects. The Params for the first set of rules should
153          * now be deleted and replaced with a new one, while the Params for the second set
154          * should be unchanged.
155          */
156         kieSession.fireAllRules();
157         facts = getSessionObjects();
158         assertEquals(specifications.length, facts.size());
159         iter = facts.iterator();
160
161         final Object fact3 = iter.next();
162         assertTrue(fact3.toString().contains(loadYaml(YAML)));
163         assertTrue(fact3 != fact2);
164         assertTrue(fact3 != fact1b);
165
166         assertTrue(iter.next() == fact1b);
167
168         logger.info("UPDATING VERSION TO v4.0 (i.e., unchanged)");
169         updatePolicy(YAML, "v4.0");
170
171         /*
172          * Let rules update Params objects. As the version (and YAML) are unchanged for
173          * either rule set, both Params objects should be unchanged.
174          */
175         kieSession.fireAllRules();
176         facts = getSessionObjects();
177         assertEquals(specifications.length, facts.size());
178         iter = facts.iterator();
179         assertTrue(iter.next() == fact3);
180         assertTrue(iter.next() == fact1b);
181     }
182
183     /**
184      * Updates the policy, changing the YAML associated with the first rule set.
185      *
186      * @param yamlFile name of the YAML file
187      * @param policyVersion policy version
188      * @throws IOException if an error occurs
189      */
190     private static void updatePolicy(String yamlFile, String policyVersion) throws IOException {
191
192         specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME,
193                         policyVersion, loadYaml(yamlFile));
194
195         /*
196          * Update the policy within the container.
197          */
198         SupportUtil.updateContainer(policyVersion, specifications);
199     }
200
201     /**
202      * Loads a YAML file and URL-encodes it.
203      *
204      * @param yamlFile name of the YAML file
205      * @return the contents of the specified file, URL-encoded
206      * @throws UnsupportedEncodingException if an error occurs
207      */
208     private static String loadYaml(String yamlFile) throws UnsupportedEncodingException {
209         Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml(yamlFile);
210         assertNotNull(pair);
211         assertNotNull(pair.first);
212         assertNotNull(pair.first.getControlLoop());
213         assertNotNull(pair.first.getControlLoop().getControlLoopName());
214         assertTrue(pair.first.getControlLoop().getControlLoopName().length() > 0);
215
216         return URLEncoder.encode(pair.second, "UTF-8");
217     }
218
219     /**
220      * Gets the session objects.
221      *
222      * @return the session objects
223      */
224     private static List<Object> getSessionObjects() {
225         // sort the objects so we know the order
226         LinkedList<Object> lst = new LinkedList<>(kieSession.getObjects());
227         lst.sort((left, right) -> left.toString().compareTo(right.toString()));
228
229         return lst;
230     }
231 }