947e50c00bbf9333f2e2b77da0cc084b1aae8ba6
[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;
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.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;
44
45 /**
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.
49  */
50 public class ControlLoopParamsCleanupTest {
51     private static final Logger logger = LoggerFactory.getLogger(ControlLoopParamsCleanupTest.class);
52
53     private static final String YAML = "src/test/resources/yaml/policy_ControlLoop_ParamsCleanup-test.yaml";
54
55     /**
56      * YAML to be used when the first rule set is updated.
57      */
58     private static final String YAML2 = "src/test/resources/yaml/policy_ControlLoop_ParamsCleanup-test2.yaml";
59
60     private static final String POLICY_VERSION = "v2.0";
61
62     private static final String POLICY_NAME = "CL_CleanupTest";
63
64     private static final String POLICY_SCOPE = "type=operational";
65
66     private static final String CONTROL_LOOP_NAME = "ControlLoop-Params-Cleanup-Test";
67
68     private static final String DROOLS_TEMPLATE = "../archetype-cl-amsterdam/src/main/resources/archetype-resources/"
69                     + "src/main/resources/__closedLoopControlName__.drl";
70
71     // values specific to the second copy of the rules
72
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";
76
77     private static KieSession kieSession;
78     private static SupportUtil.RuleSpec[] specifications;
79
80     /**
81      * Setup the simulator.
82      */
83     @BeforeClass
84     public static void setUpSimulator() {
85         LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO");
86
87         try {
88             specifications = new SupportUtil.RuleSpec[2];
89
90             specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME,
91                             POLICY_VERSION, loadYaml(YAML));
92
93             specifications[1] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE,
94                             POLICY_NAME_B, POLICY_VERSION, loadYaml(YAML_B));
95
96             kieSession = SupportUtil.buildContainer(POLICY_VERSION, specifications);
97
98         } catch (IOException e) {
99             logger.error("Could not create kieSession", e);
100             fail("Could not create kieSession");
101         }
102     }
103
104     /**
105      * Tear down.
106      */
107     @AfterClass
108     public static void tearDown() {
109         kieSession.dispose();
110     }
111
112     @Ignore
113     @Test
114     public void test() throws IOException {
115
116         /*
117          * Let rules create Params objects. There should be one object for each set of
118          * rules.
119          */
120         kieSession.fireAllRules();
121         List<Object> facts = getSessionObjects();
122         assertEquals(specifications.length, facts.size());
123         Iterator<Object> iter = facts.iterator();
124
125         final Object fact1 = iter.next();
126         assertTrue(fact1.toString().contains(loadYaml(YAML)));
127
128         final Object fact1b = iter.next();
129         assertTrue(fact1b.toString().contains(loadYaml(YAML_B)));
130
131         logger.info("UPDATING VERSION TO v3.0");
132         updatePolicy(YAML2, "v3.0");
133
134         /*
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.
138          */
139         kieSession.fireAllRules();
140         facts = getSessionObjects();
141         assertEquals(specifications.length, facts.size());
142         iter = facts.iterator();
143
144         final Object fact2 = iter.next();
145         assertTrue(fact2 != fact1);
146         assertTrue(fact2 != fact1b);
147         assertTrue(fact2.toString().contains(loadYaml(YAML2)));
148
149         assertTrue(iter.next() == fact1b);
150
151         logger.info("UPDATING VERSION TO v4.0");
152         updatePolicy(YAML, "v4.0");
153
154         /*
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.
158          */
159         kieSession.fireAllRules();
160         facts = getSessionObjects();
161         assertEquals(specifications.length, facts.size());
162         iter = facts.iterator();
163
164         final Object fact3 = iter.next();
165         assertTrue(fact3.toString().contains(loadYaml(YAML)));
166         assertTrue(fact3 != fact2);
167         assertTrue(fact3 != fact1b);
168
169         assertTrue(iter.next() == fact1b);
170
171         logger.info("UPDATING VERSION TO v4.0 (i.e., unchanged)");
172         updatePolicy(YAML, "v4.0");
173
174         /*
175          * Let rules update Params objects. As the version (and YAML) are unchanged for
176          * either rule set, both Params objects should be unchanged.
177          */
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);
184         
185         /*
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
188          * clean-up of both.
189          */
190         SupportUtil.RuleSpec[] specs = new SupportUtil.RuleSpec[1];
191         specs[0] = specifications[1];
192
193         logger.info("UPDATING VERSION TO v5.0 - DELETED RULE SET");
194         SupportUtil.updateContainer("v5.0", specs);
195
196         specs[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B,
197                         POLICY_VERSION, loadYaml(YAML));
198         
199         logger.info("UPDATING VERSION TO v6.0 - UPDATED SECOND RULE SET");
200         SupportUtil.updateContainer("v6.0", specs);
201         
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));
207     }
208
209     /**
210      * Updates the policy, changing the YAML associated with the first rule set.
211      *
212      * @param yamlFile name of the YAML file
213      * @param policyVersion policy version
214      * @throws IOException if an error occurs
215      */
216     private static void updatePolicy(String yamlFile, String policyVersion) throws IOException {
217
218         specifications[0] = new SupportUtil.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME,
219                         policyVersion, loadYaml(yamlFile));
220
221         /*
222          * Update the policy within the container.
223          */
224         SupportUtil.updateContainer(policyVersion, specifications);
225     }
226
227     /**
228      * Loads a YAML file and URL-encodes it.
229      *
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
233      */
234     private static String loadYaml(String yamlFile) throws UnsupportedEncodingException {
235         Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml(yamlFile);
236         assertNotNull(pair);
237         assertNotNull(pair.first);
238         assertNotNull(pair.first.getControlLoop());
239         assertNotNull(pair.first.getControlLoop().getControlLoopName());
240         assertTrue(pair.first.getControlLoop().getControlLoopName().length() > 0);
241
242         return URLEncoder.encode(pair.second, "UTF-8");
243     }
244
245     /**
246      * Gets the session objects.
247      *
248      * @return the session objects
249      */
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()));
254
255         return lst;
256     }
257 }