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.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.Util.Pair;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
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.
49 public class ControlLoopParamsCleanupTest {
50 private static final Logger logger = LoggerFactory.getLogger(ControlLoopParamsCleanupTest.class);
52 private static final String YAML = "src/test/resources/yaml/policy_ControlLoop_ParamsCleanup-test.yaml";
55 * YAML to be used when the first rule set is updated.
57 private static final String YAML2 = "src/test/resources/yaml/policy_ControlLoop_ParamsCleanup-test2.yaml";
59 private static final String POLICY_VERSION = "v2.0";
61 private static final String POLICY_NAME = "CL_CleanupTest";
63 private static final String POLICY_SCOPE = "type=operational";
65 private static final String CONTROL_LOOP_NAME = "ControlLoop-Params-Cleanup-Test";
67 private static final String DROOLS_TEMPLATE = "../archetype-cl-amsterdam/src/main/resources/archetype-resources/"
68 + "src/main/resources/__closedLoopControlName__.drl";
70 // values specific to the second copy of the rules
72 private static final String YAML_B = "src/test/resources/yaml/policy_ControlLoop_ParamsCleanup-test-B.yaml";
73 private static final String POLICY_NAME_B = "CL_CleanupTest_B";
74 private static final String CONTROL_LOOP_NAME_B = "ControlLoop-Params-Cleanup-Test-B";
76 private static KieSession kieSession;
77 private static Util.RuleSpec[] specifications;
80 * Setup the simulator.
83 public static void setUpSimulator() {
84 LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO");
87 specifications = new Util.RuleSpec[2];
89 specifications[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME,
90 POLICY_VERSION, loadYaml(YAML));
92 specifications[1] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B,
93 POLICY_VERSION, loadYaml(YAML_B));
95 kieSession = Util.buildContainer(POLICY_VERSION, specifications);
97 } catch (IOException e) {
98 logger.error("Could not create kieSession", e);
99 fail("Could not create kieSession");
107 public static void tearDown() {
108 kieSession.dispose();
112 public void test() throws IOException {
115 * Let rules create Params objects. There should be one object for each set of
118 kieSession.fireAllRules();
119 List<Object> facts = getSessionObjects();
120 assertEquals(specifications.length, facts.size());
121 Iterator<Object> iter = facts.iterator();
123 final Object fact1 = iter.next();
124 assertTrue(fact1.toString().contains(loadYaml(YAML)));
126 final Object fact1b = iter.next();
127 assertTrue(fact1b.toString().contains(loadYaml(YAML_B)));
129 logger.info("UPDATING VERSION TO v3.0");
130 updatePolicy(YAML2, "v3.0");
133 * Let rules update Params objects. The Params for the first set of rules should
134 * now be deleted and replaced with a new one, while the Params for the second set
135 * should be unchanged.
137 kieSession.fireAllRules();
138 facts = getSessionObjects();
139 assertEquals(specifications.length, facts.size());
140 iter = facts.iterator();
142 final Object fact2 = iter.next();
143 assertTrue(fact2 != fact1);
144 assertTrue(fact2 != fact1b);
145 assertTrue(fact2.toString().contains(loadYaml(YAML2)));
147 assertTrue(iter.next() == fact1b);
149 logger.info("UPDATING VERSION TO v4.0");
150 updatePolicy(YAML, "v4.0");
153 * Let rules update Params objects. The Params for the first set of rules should
154 * now be deleted and replaced with a new one, while the Params for the second set
155 * should be unchanged.
157 kieSession.fireAllRules();
158 facts = getSessionObjects();
159 assertEquals(specifications.length, facts.size());
160 iter = facts.iterator();
162 final Object fact3 = iter.next();
163 assertTrue(fact3.toString().contains(loadYaml(YAML)));
164 assertTrue(fact3 != fact2);
165 assertTrue(fact3 != fact1b);
167 assertTrue(iter.next() == fact1b);
169 logger.info("UPDATING VERSION TO v4.0 (i.e., unchanged)");
170 updatePolicy(YAML, "v4.0");
173 * Let rules update Params objects. As the version (and YAML) are unchanged for
174 * either rule set, both Params objects should be unchanged.
176 kieSession.fireAllRules();
177 facts = getSessionObjects();
178 assertEquals(specifications.length, facts.size());
179 iter = facts.iterator();
180 assertTrue(iter.next() == fact3);
181 assertTrue(iter.next() == fact1b);
185 * Updates the policy, changing the YAML associated with the first rule set.
187 * @param yamlFile name of the YAML file
188 * @param policyVersion policy version
189 * @throws IOException if an error occurs
191 private static void updatePolicy(String yamlFile, String policyVersion) throws IOException {
193 specifications[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME,
194 policyVersion, loadYaml(yamlFile));
197 * Update the policy within the container.
199 Util.updateContainer(policyVersion, specifications);
203 * Loads a YAML file and URL-encodes it.
205 * @param yamlFile name of the YAML file
206 * @return the contents of the specified file, URL-encoded
207 * @throws UnsupportedEncodingException if an error occurs
209 private static String loadYaml(String yamlFile) throws UnsupportedEncodingException {
210 Pair<ControlLoopPolicy, String> pair = Util.loadYaml(yamlFile);
212 assertNotNull(pair.first);
213 assertNotNull(pair.first.getControlLoop());
214 assertNotNull(pair.first.getControlLoop().getControlLoopName());
215 assertTrue(pair.first.getControlLoop().getControlLoopName().length() > 0);
217 return URLEncoder.encode(pair.second, "UTF-8");
221 * Gets the session objects.
223 * @return the session objects
225 private static List<Object> getSessionObjects() {
226 // sort the objects so we know the order
227 LinkedList<Object> lst = new LinkedList<>(kieSession.getObjects());
228 lst.sort((left, right) -> left.toString().compareTo(right.toString()));