2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.template.demo;
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;
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.Ignore;
37 import org.junit.Test;
38 import org.kie.api.runtime.KieSession;
39 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
40 import org.onap.policy.drools.utils.logging.LoggerUtil;
41 import org.onap.policy.template.demo.SupportUtil.Pair;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Verifies that Params objects are cleaned up when rules are updated. This loads
47 * <b>two</b> copies of the rule set into a single policy to ensure that the two copies
48 * interact appropriately with each other's Params objects.
50 public class ControlLoopParamsCleanupTest {
51 private static final Logger logger = LoggerFactory.getLogger(ControlLoopParamsCleanupTest.class);
53 private static final String YAML = "src/test/resources/yaml/policy_ControlLoop_ParamsCleanup-test.yaml";
56 * YAML to be used when the first rule set is updated.
58 private static final String YAML2 = "src/test/resources/yaml/policy_ControlLoop_ParamsCleanup-test2.yaml";
60 private static final String POLICY_VERSION = "v2.0";
62 private static final String POLICY_NAME = "CL_CleanupTest";
64 private static final String POLICY_SCOPE = "type=operational";
66 private static final String CONTROL_LOOP_NAME = "ControlLoop-Params-Cleanup-Test";
68 private static final String DROOLS_TEMPLATE = "../archetype-cl-amsterdam/src/main/resources/archetype-resources/"
69 + "src/main/resources/__closedLoopControlName__.drl";
71 // values specific to the second copy of the rules
73 private static final String YAML_B = "src/test/resources/yaml/policy_ControlLoop_ParamsCleanup-test-B.yaml";
74 private static final String POLICY_NAME_B = "CL_CleanupTest_B";
75 private static final String CONTROL_LOOP_NAME_B = "ControlLoop-Params-Cleanup-Test-B";
77 private static KieSession kieSession;
78 private static SupportUtil.RuleSpec[] specifications;
81 * Setup the simulator.
84 public static void setUpSimulator() {
85 LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO");
88 specifications = new SupportUtil.RuleSpec[2];
90 specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME,
91 POLICY_VERSION, loadYaml(YAML));
93 specifications[1] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE,
94 POLICY_NAME_B, POLICY_VERSION, loadYaml(YAML_B));
96 kieSession = SupportUtil.buildContainer(POLICY_VERSION, specifications);
98 } catch (IOException e) {
99 logger.error("Could not create kieSession", e);
100 fail("Could not create kieSession");
108 public static void tearDown() {
109 kieSession.dispose();
114 public void test() throws IOException {
117 * Let rules create Params objects. There should be one object for each set of
120 kieSession.fireAllRules();
121 List<Object> facts = getSessionObjects();
122 assertEquals(specifications.length, facts.size());
123 Iterator<Object> iter = facts.iterator();
125 final Object fact1 = iter.next();
126 assertTrue(fact1.toString().contains(loadYaml(YAML)));
128 final Object fact1b = iter.next();
129 assertTrue(fact1b.toString().contains(loadYaml(YAML_B)));
131 logger.info("UPDATING VERSION TO v3.0");
132 updatePolicy(YAML2, "v3.0");
135 * Let rules update Params objects. The Params for the first set of rules should
136 * now be deleted and replaced with a new one, while the Params for the second set
137 * should be unchanged.
139 kieSession.fireAllRules();
140 facts = getSessionObjects();
141 assertEquals(specifications.length, facts.size());
142 iter = facts.iterator();
144 final Object fact2 = iter.next();
145 assertTrue(fact2 != fact1);
146 assertTrue(fact2 != fact1b);
147 assertTrue(fact2.toString().contains(loadYaml(YAML2)));
149 assertTrue(iter.next() == fact1b);
151 logger.info("UPDATING VERSION TO v4.0");
152 updatePolicy(YAML, "v4.0");
155 * Let rules update Params objects. The Params for the first set of rules should
156 * now be deleted and replaced with a new one, while the Params for the second set
157 * should be unchanged.
159 kieSession.fireAllRules();
160 facts = getSessionObjects();
161 assertEquals(specifications.length, facts.size());
162 iter = facts.iterator();
164 final Object fact3 = iter.next();
165 assertTrue(fact3.toString().contains(loadYaml(YAML)));
166 assertTrue(fact3 != fact2);
167 assertTrue(fact3 != fact1b);
169 assertTrue(iter.next() == fact1b);
171 logger.info("UPDATING VERSION TO v4.0 (i.e., unchanged)");
172 updatePolicy(YAML, "v4.0");
175 * Let rules update Params objects. As the version (and YAML) are unchanged for
176 * either rule set, both Params objects should be unchanged.
178 kieSession.fireAllRules();
179 facts = getSessionObjects();
180 assertEquals(specifications.length, facts.size());
181 iter = facts.iterator();
182 assertTrue(iter.next() == fact3);
183 assertTrue(iter.next() == fact1b);
186 * Now we'll delete the first rule set. That won't actually have any immediate
187 * effect, so then we'll update the second rule set, which should trigger a
190 SupportUtil.RuleSpec[] specs = new SupportUtil.RuleSpec[1];
191 specs[0] = specifications[1];
193 logger.info("UPDATING VERSION TO v5.0 - DELETED RULE SET");
194 SupportUtil.updateContainer("v5.0", specs);
196 specs[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B,
197 POLICY_VERSION, loadYaml(YAML));
199 logger.info("UPDATING VERSION TO v6.0 - UPDATED SECOND RULE SET");
200 SupportUtil.updateContainer("v6.0", specs);
202 kieSession.fireAllRules();
203 facts = getSessionObjects();
204 assertEquals(specs.length, facts.size());
205 iter = facts.iterator();
206 assertTrue(iter.next().toString().contains(CONTROL_LOOP_NAME_B));
210 * Updates the policy, changing the YAML associated with the first rule set.
212 * @param yamlFile name of the YAML file
213 * @param policyVersion policy version
214 * @throws IOException if an error occurs
216 private static void updatePolicy(String yamlFile, String policyVersion) throws IOException {
218 specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME,
219 policyVersion, loadYaml(yamlFile));
222 * Update the policy within the container.
224 SupportUtil.updateContainer(policyVersion, specifications);
228 * Loads a YAML file and URL-encodes it.
230 * @param yamlFile name of the YAML file
231 * @return the contents of the specified file, URL-encoded
232 * @throws UnsupportedEncodingException if an error occurs
234 private static String loadYaml(String yamlFile) throws UnsupportedEncodingException {
235 Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml(yamlFile);
237 assertNotNull(pair.first);
238 assertNotNull(pair.first.getControlLoop());
239 assertNotNull(pair.first.getControlLoop().getControlLoopName());
240 assertTrue(pair.first.getControlLoop().getControlLoopName().length() > 0);
242 return URLEncoder.encode(pair.second, "UTF-8");
246 * Gets the session objects.
248 * @return the session objects
250 private static List<Object> getSessionObjects() {
251 // sort the objects so we know the order
252 LinkedList<Object> lst = new LinkedList<>(kieSession.getObjects());
253 lst.sort((left, right) -> left.toString().compareTo(right.toString()));