2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.policymodel.concepts;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
32 import java.util.TreeMap;
33 import org.junit.Test;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
39 import org.onap.policy.apex.model.policymodel.handling.SupportApexPolicyModelCreator;
44 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class PoliciesTest {
49 public void testPolicies() {
50 final TreeMap<String, AxState> stateMap = new TreeMap<>();
51 final TreeMap<String, AxState> stateMapEmpty = new TreeMap<>();
53 assertNotNull(new AxPolicy());
54 assertNotNull(new AxPolicy(new AxArtifactKey()));
55 assertNotNull(new AxPolicy(new AxArtifactKey(), "PolicyTemplate", stateMapEmpty, "FirstState"));
57 AxPolicy policy = new AxPolicy();
59 final AxArtifactKey policyKey = new AxArtifactKey("PolicyName", "0.0.1");
61 final AxState firstState = new AxState(new AxReferenceKey(policy.getKey(), "FirstState"));
62 final AxState badState = new AxState(new AxReferenceKey(policy.getKey(), "BadState"));
63 final AxStateOutput badStateOutput = new AxStateOutput(badState.getKey(), AxArtifactKey.getNullKey(),
64 new AxReferenceKey(policyKey, "BadNextState"));
65 badState.getStateOutputs().put(badStateOutput.getKey().getLocalName(), badStateOutput);
66 stateMap.put(firstState.getKey().getLocalName(), firstState);
70 fail("test should throw an exception here");
71 } catch (final Exception e) {
72 assertEquals("key may not be null", e.getMessage());
75 policy.setKey(policyKey);
76 assertEquals("PolicyName:0.0.1", policy.getKey().getId());
77 assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId());
80 policy.setTemplate(null);
81 fail("test should throw an exception here");
82 } catch (final Exception e) {
83 assertEquals("template may not be null", e.getMessage());
86 policy.setTemplate("PolicyTemplate");
87 assertEquals("PolicyTemplate", policy.getTemplate());
90 policy.setStateMap(null);
91 fail("test should throw an exception here");
92 } catch (final Exception e) {
93 assertEquals("stateMap may not be null", e.getMessage());
96 policy.setStateMap(stateMap);
97 assertEquals(stateMap, policy.getStateMap());
100 policy.setFirstState(null);
101 fail("test should throw an exception here");
102 } catch (final Exception e) {
103 assertEquals("firstState may not be null", e.getMessage());
106 policy.setFirstState("FirstState");
107 assertEquals("FirstState", policy.getFirstState());
109 assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId());
111 policy = new SupportApexPolicyModelCreator().getModel().getPolicies().get("policy");
113 AxValidationResult result = new AxValidationResult();
114 result = policy.validate(result);
115 assertEquals(ValidationResult.VALID, result.getValidationResult());
117 final AxArtifactKey savedPolicyKey = policy.getKey();
118 policy.setKey(AxArtifactKey.getNullKey());
119 result = new AxValidationResult();
120 result = policy.validate(result);
121 assertEquals(ValidationResult.INVALID, result.getValidationResult());
123 policy.setKey(savedPolicyKey);
124 result = new AxValidationResult();
125 result = policy.validate(result);
126 assertEquals(ValidationResult.VALID, result.getValidationResult());
128 final String savedTemplate = policy.getTemplate();
129 policy.setTemplate("");
130 result = new AxValidationResult();
131 result = policy.validate(result);
132 assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());
134 policy.setTemplate(savedTemplate);
135 result = new AxValidationResult();
136 result = policy.validate(result);
137 assertEquals(ValidationResult.VALID, result.getValidationResult());
139 final Map<String, AxState> savedStateMap = policy.getStateMap();
141 policy.setStateMap(stateMapEmpty);
142 result = new AxValidationResult();
143 result = policy.validate(result);
144 assertEquals(ValidationResult.INVALID, result.getValidationResult());
146 policy.setStateMap(savedStateMap);
147 result = new AxValidationResult();
148 result = policy.validate(result);
149 assertEquals(ValidationResult.VALID, result.getValidationResult());
151 savedStateMap.put(AxKey.NULL_KEY_NAME, firstState);
152 result = new AxValidationResult();
153 result = policy.validate(result);
154 assertEquals(ValidationResult.INVALID, result.getValidationResult());
156 savedStateMap.remove(AxKey.NULL_KEY_NAME);
157 result = new AxValidationResult();
158 result = policy.validate(result);
159 assertEquals(ValidationResult.VALID, result.getValidationResult());
161 savedStateMap.put("NullState", null);
162 result = new AxValidationResult();
163 result = policy.validate(result);
164 assertEquals(ValidationResult.INVALID, result.getValidationResult());
166 savedStateMap.remove("NullState");
167 result = new AxValidationResult();
168 result = policy.validate(result);
169 assertEquals(ValidationResult.VALID, result.getValidationResult());
171 savedStateMap.put("BadStateKey", firstState);
172 result = new AxValidationResult();
173 result = policy.validate(result);
174 assertEquals(ValidationResult.INVALID, result.getValidationResult());
176 savedStateMap.remove("BadStateKey");
177 result = new AxValidationResult();
178 result = policy.validate(result);
179 assertEquals(ValidationResult.VALID, result.getValidationResult());
181 savedStateMap.put(badState.getKey().getLocalName(), badState);
182 result = new AxValidationResult();
183 result = policy.validate(result);
184 assertEquals(ValidationResult.INVALID, result.getValidationResult());
186 savedStateMap.remove(badState.getKey().getLocalName());
187 result = new AxValidationResult();
188 result = policy.validate(result);
189 assertEquals(ValidationResult.VALID, result.getValidationResult());
191 final String savedFirstState = policy.getFirstState();
193 policy.setFirstState("");
194 result = new AxValidationResult();
195 result = policy.validate(result);
196 assertEquals(ValidationResult.INVALID, result.getValidationResult());
198 policy.setFirstState(savedFirstState);
199 result = new AxValidationResult();
200 result = policy.validate(result);
201 assertEquals(ValidationResult.VALID, result.getValidationResult());
203 policy.setFirstState("NonExistantFirstState");
204 result = new AxValidationResult();
205 result = policy.validate(result);
206 assertEquals(ValidationResult.INVALID, result.getValidationResult());
208 policy.setFirstState(savedFirstState);
209 result = new AxValidationResult();
210 result = policy.validate(result);
211 assertEquals(ValidationResult.VALID, result.getValidationResult());
213 final AxState clonedState = new AxState(policy.getStateMap().get("state"));
214 clonedState.getKey().setLocalName("ClonedState");
215 clonedState.afterUnmarshal(null, null);
217 savedStateMap.put(clonedState.getKey().getLocalName(), clonedState);
218 result = new AxValidationResult();
219 result = policy.validate(result);
220 assertEquals(ValidationResult.WARNING, result.getValidationResult());
222 savedStateMap.remove(clonedState.getKey().getLocalName());
223 result = new AxValidationResult();
224 result = policy.validate(result);
225 assertEquals(ValidationResult.VALID, result.getValidationResult());
229 final AxPolicy clonedPolicy = new AxPolicy(policy);
230 assertEquals("AxPolicy:(key=AxArtifactKey:(name=policy,version=0.0.1),template=FREEFORM,sta",
231 clonedPolicy.toString().substring(0, 77));
233 assertFalse(policy.hashCode() == 0);
235 assertTrue(policy.equals(policy));
236 assertTrue(policy.equals(clonedPolicy));
237 assertFalse(policy.equals(null));
238 assertFalse(policy.equals((Object) "Hello"));
239 assertFalse(policy.equals(
240 new AxPolicy(AxArtifactKey.getNullKey(), savedTemplate, savedStateMap, savedFirstState)));
241 assertFalse(policy.equals(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState)));
242 assertFalse(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState)));
243 assertFalse(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState")));
244 assertTrue(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState)));
246 assertEquals(0, policy.compareTo(policy));
247 assertEquals(0, policy.compareTo(clonedPolicy));
248 assertNotEquals(0, policy.compareTo(new AxArtifactKey()));
249 assertNotEquals(0, policy.compareTo(null));
250 assertNotEquals(0, policy.compareTo(
251 new AxPolicy(AxArtifactKey.getNullKey(), savedTemplate, savedStateMap, savedFirstState)));
253 policy.compareTo(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState)));
255 policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState)));
257 policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState")));
258 assertEquals(0, policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState)));
260 assertNotNull(policy.getKeys());
262 final AxPolicies policies = new AxPolicies();
263 result = new AxValidationResult();
264 result = policies.validate(result);
265 assertEquals(ValidationResult.INVALID, result.getValidationResult());
267 // Invalid, no events in event map
268 policies.setKey(new AxArtifactKey("PoliciesKey", "0.0.1"));
269 assertEquals("PoliciesKey:0.0.1", policies.getKey().getId());
271 result = new AxValidationResult();
272 result = policies.validate(result);
273 assertEquals(ValidationResult.INVALID, result.getValidationResult());
275 policies.getPolicyMap().put(savedPolicyKey, policy);
276 result = new AxValidationResult();
277 result = policies.validate(result);
278 assertEquals(ValidationResult.VALID, result.getValidationResult());
280 policies.getPolicyMap().put(AxArtifactKey.getNullKey(), null);
281 result = new AxValidationResult();
282 result = policies.validate(result);
283 assertEquals(ValidationResult.INVALID, result.getValidationResult());
285 policies.getPolicyMap().remove(AxArtifactKey.getNullKey());
286 result = new AxValidationResult();
287 result = policies.validate(result);
288 assertEquals(ValidationResult.VALID, result.getValidationResult());
290 policies.getPolicyMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
291 result = new AxValidationResult();
292 result = policies.validate(result);
293 assertEquals(ValidationResult.INVALID, result.getValidationResult());
295 policies.getPolicyMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
296 result = new AxValidationResult();
297 result = policies.validate(result);
298 assertEquals(ValidationResult.VALID, result.getValidationResult());
300 policies.getPolicyMap().put(new AxArtifactKey("BadEventKey", "0.0.1"), policy);
301 result = new AxValidationResult();
302 result = policies.validate(result);
303 assertEquals(ValidationResult.INVALID, result.getValidationResult());
305 policies.getPolicyMap().remove(new AxArtifactKey("BadEventKey", "0.0.1"));
306 result = new AxValidationResult();
307 result = policies.validate(result);
308 assertEquals(ValidationResult.VALID, result.getValidationResult());
311 policies.afterUnmarshal(null, null);
313 final AxPolicies clonedPolicies = new AxPolicies(policies);
314 assertEquals("AxPolicies:(key=AxArtifactKey:(name=PoliciesKey,version=0.0.",
315 clonedPolicies.toString().substring(0, 60));
317 assertFalse(policies.hashCode() == 0);
319 assertTrue(policies.equals(policies));
320 assertTrue(policies.equals(clonedPolicies));
321 assertFalse(policies.equals(null));
322 assertFalse(policies.equals((Object) "Hello"));
323 assertFalse(policies.equals(new AxPolicies(new AxArtifactKey())));
325 assertEquals(0, policies.compareTo(policies));
326 assertEquals(0, policies.compareTo(clonedPolicies));
327 assertNotEquals(0, policies.compareTo(null));
328 assertNotEquals(0, policies.compareTo(new AxArtifactKey()));
329 assertNotEquals(0, policies.compareTo(new AxPolicies(new AxArtifactKey())));
331 clonedPolicies.get(savedPolicyKey).setTemplate("AnotherTemplate");
332 assertNotEquals(0, policies.compareTo(clonedPolicies));
334 assertEquals(policies.getKey(), policies.getKeys().get(0));
336 assertEquals("policy", policies.get("policy").getKey().getName());
337 assertEquals("policy", policies.get("policy", "0.0.1").getKey().getName());
338 assertEquals(1, policies.getAll("policy", "0.0.1").size());
339 assertEquals(0, policies.getAll("NonExistantPolicy").size());
341 AxStateTree stateTree = policy.getStateTree();
342 assertNotNull(stateTree);
343 assertNotNull(stateTree.getReferencedStateList());
344 assertNotNull(stateTree.getReferencedStateSet());
346 final AxState secondState = new AxState(policy.getStateMap().get("state"));
347 secondState.getKey().setLocalName("SecondState");
348 secondState.afterUnmarshal(null, null);
349 policy.getStateMap().put("SecondState", secondState);
350 policy.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey());
352 stateTree = policy.getStateTree();
353 assertNotNull(stateTree);
354 assertNotNull(stateTree.getReferencedStateList());
355 assertNotNull(stateTree.getReferencedStateSet());
356 assertNotNull(stateTree.getNextStates());
358 policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")
359 .setNextState(policy.getStateMap().get("state").getKey());
361 policy.getStateTree();
362 fail("test should throw an exception here");
363 } catch (final Exception e) {
364 assertEquals("loop detected in state tree for policy policy:0.0.1 state SecondState, "
365 + "next state state referenced more than once", e.getMessage());
368 policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")
369 .setNextState(AxReferenceKey.getNullKey());
371 final AxState thirdState = new AxState(policy.getStateMap().get("state"));
372 thirdState.getKey().setLocalName("ThirdState");
373 thirdState.afterUnmarshal(null, null);
374 policy.getStateMap().put("ThirdState", thirdState);
375 policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0").setNextState(thirdState.getKey());
376 policy.getStateMap().get("ThirdState").getStateOutputs().get("stateOutput0")
377 .setNextState(AxReferenceKey.getNullKey());
379 stateTree = policy.getStateTree();
381 final AxStateOutput ssS0Clone = new AxStateOutput(
382 policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0"));
383 ssS0Clone.getKey().setLocalName("ssS0Clone");
384 policy.getStateMap().get("SecondState").getStateOutputs().put("ssS0Clone", ssS0Clone);
387 policy.getStateTree();
388 fail("test should throw an exception here");
389 } catch (final Exception e) {
390 assertEquals("loop detected in state tree for policy policy:0.0.1 state SecondState, "
391 + "next state ThirdState referenced more than once", e.getMessage());
394 policy.getStateMap().get("SecondState").getStateOutputs().remove("ssS0Clone");
396 policy.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey());
397 secondState.getStateOutputs().get("stateOutput0").setNextState(thirdState.getKey());
398 thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey());
400 stateTree = policy.getStateTree();
401 assertNotNull(stateTree.getState());
403 thirdState.getStateOutputs().get("stateOutput0").setNextState(secondState.getKey());
406 policy.getStateTree();
407 fail("test should throw an exception here");
408 } catch (final Exception e) {
409 assertEquals("loop detected in state tree for policy policy:0.0.1 state ThirdState, "
410 + "next state SecondState referenced more than once", e.getMessage());
413 thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey());
415 stateTree = policy.getStateTree();
417 final AxStateTree otherStateTree = policy.getStateTree();
418 assertEquals(0, stateTree.compareTo(otherStateTree));
420 for (final AxStateTree childStateTree : stateTree.getNextStates()) {
421 assertNotEquals(0, stateTree.compareTo(childStateTree));
424 otherStateTree.getNextStates().clear();
425 assertNotEquals(0, stateTree.compareTo(otherStateTree));