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;
34 import org.junit.Test;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
40 import org.onap.policy.apex.model.policymodel.concepts.AxPolicies;
41 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
42 import org.onap.policy.apex.model.policymodel.concepts.AxState;
43 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
44 import org.onap.policy.apex.model.policymodel.concepts.AxStateTree;
45 import org.onap.policy.apex.model.policymodel.handling.SupportApexPolicyModelCreator;
50 * @author Liam Fallon (liam.fallon@ericsson.com)
52 public class PoliciesTest {
55 public void testPolicies() {
56 final TreeMap<String, AxState> stateMap = new TreeMap<>();
57 final TreeMap<String, AxState> stateMapEmpty = new TreeMap<>();
59 assertNotNull(new AxPolicy());
60 assertNotNull(new AxPolicy(new AxArtifactKey()));
61 assertNotNull(new AxPolicy(new AxArtifactKey(), "PolicyTemplate", stateMapEmpty, "FirstState"));
63 AxPolicy policy = new AxPolicy();
65 final AxArtifactKey policyKey = new AxArtifactKey("PolicyName", "0.0.1");
67 final AxState firstState = new AxState(new AxReferenceKey(policy.getKey(), "FirstState"));
68 final AxState badState = new AxState(new AxReferenceKey(policy.getKey(), "BadState"));
69 final AxStateOutput badStateOutput = new AxStateOutput(badState.getKey(), AxArtifactKey.getNullKey(),
70 new AxReferenceKey(policyKey, "BadNextState"));
71 badState.getStateOutputs().put(badStateOutput.getKey().getLocalName(), badStateOutput);
72 stateMap.put(firstState.getKey().getLocalName(), firstState);
76 fail("test should throw an exception here");
77 } catch (final Exception e) {
78 assertEquals("key may not be null", e.getMessage());
81 policy.setKey(policyKey);
82 assertEquals("PolicyName:0.0.1", policy.getKey().getId());
83 assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId());
86 policy.setTemplate(null);
87 fail("test should throw an exception here");
88 } catch (final Exception e) {
89 assertEquals("template may not be null", e.getMessage());
92 policy.setTemplate("PolicyTemplate");
93 assertEquals("PolicyTemplate", policy.getTemplate());
96 policy.setStateMap(null);
97 fail("test should throw an exception here");
98 } catch (final Exception e) {
99 assertEquals("stateMap may not be null", e.getMessage());
102 policy.setStateMap(stateMap);
103 assertEquals(stateMap, policy.getStateMap());
106 policy.setFirstState(null);
107 fail("test should throw an exception here");
108 } catch (final Exception e) {
109 assertEquals("firstState may not be null", e.getMessage());
112 policy.setFirstState("FirstState");
113 assertEquals("FirstState", policy.getFirstState());
115 assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId());
117 policy = new SupportApexPolicyModelCreator().getModel().getPolicies().get("policy");
119 AxValidationResult result = new AxValidationResult();
120 result = policy.validate(result);
121 assertEquals(ValidationResult.VALID, result.getValidationResult());
123 final AxArtifactKey savedPolicyKey = policy.getKey();
124 policy.setKey(AxArtifactKey.getNullKey());
125 result = new AxValidationResult();
126 result = policy.validate(result);
127 assertEquals(ValidationResult.INVALID, result.getValidationResult());
129 policy.setKey(savedPolicyKey);
130 result = new AxValidationResult();
131 result = policy.validate(result);
132 assertEquals(ValidationResult.VALID, result.getValidationResult());
134 final String savedTemplate = policy.getTemplate();
135 policy.setTemplate("");
136 result = new AxValidationResult();
137 result = policy.validate(result);
138 assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());
140 policy.setTemplate(savedTemplate);
141 result = new AxValidationResult();
142 result = policy.validate(result);
143 assertEquals(ValidationResult.VALID, result.getValidationResult());
145 final Map<String, AxState> savedStateMap = policy.getStateMap();
147 policy.setStateMap(stateMapEmpty);
148 result = new AxValidationResult();
149 result = policy.validate(result);
150 assertEquals(ValidationResult.INVALID, result.getValidationResult());
152 policy.setStateMap(savedStateMap);
153 result = new AxValidationResult();
154 result = policy.validate(result);
155 assertEquals(ValidationResult.VALID, result.getValidationResult());
157 savedStateMap.put(AxKey.NULL_KEY_NAME, firstState);
158 result = new AxValidationResult();
159 result = policy.validate(result);
160 assertEquals(ValidationResult.INVALID, result.getValidationResult());
162 savedStateMap.remove(AxKey.NULL_KEY_NAME);
163 result = new AxValidationResult();
164 result = policy.validate(result);
165 assertEquals(ValidationResult.VALID, result.getValidationResult());
167 savedStateMap.put("NullState", null);
168 result = new AxValidationResult();
169 result = policy.validate(result);
170 assertEquals(ValidationResult.INVALID, result.getValidationResult());
172 savedStateMap.remove("NullState");
173 result = new AxValidationResult();
174 result = policy.validate(result);
175 assertEquals(ValidationResult.VALID, result.getValidationResult());
177 savedStateMap.put("BadStateKey", firstState);
178 result = new AxValidationResult();
179 result = policy.validate(result);
180 assertEquals(ValidationResult.INVALID, result.getValidationResult());
182 savedStateMap.remove("BadStateKey");
183 result = new AxValidationResult();
184 result = policy.validate(result);
185 assertEquals(ValidationResult.VALID, result.getValidationResult());
187 savedStateMap.put(badState.getKey().getLocalName(), badState);
188 result = new AxValidationResult();
189 result = policy.validate(result);
190 assertEquals(ValidationResult.INVALID, result.getValidationResult());
192 savedStateMap.remove(badState.getKey().getLocalName());
193 result = new AxValidationResult();
194 result = policy.validate(result);
195 assertEquals(ValidationResult.VALID, result.getValidationResult());
197 final String savedFirstState = policy.getFirstState();
199 policy.setFirstState("");
200 result = new AxValidationResult();
201 result = policy.validate(result);
202 assertEquals(ValidationResult.INVALID, result.getValidationResult());
204 policy.setFirstState(savedFirstState);
205 result = new AxValidationResult();
206 result = policy.validate(result);
207 assertEquals(ValidationResult.VALID, result.getValidationResult());
209 policy.setFirstState("NonExistantFirstState");
210 result = new AxValidationResult();
211 result = policy.validate(result);
212 assertEquals(ValidationResult.INVALID, result.getValidationResult());
214 policy.setFirstState(savedFirstState);
215 result = new AxValidationResult();
216 result = policy.validate(result);
217 assertEquals(ValidationResult.VALID, result.getValidationResult());
219 final AxState clonedState = new AxState(policy.getStateMap().get("state"));
220 clonedState.getKey().setLocalName("ClonedState");
221 clonedState.afterUnmarshal(null, null);
223 savedStateMap.put(clonedState.getKey().getLocalName(), clonedState);
224 result = new AxValidationResult();
225 result = policy.validate(result);
226 assertEquals(ValidationResult.WARNING, result.getValidationResult());
228 savedStateMap.remove(clonedState.getKey().getLocalName());
229 result = new AxValidationResult();
230 result = policy.validate(result);
231 assertEquals(ValidationResult.VALID, result.getValidationResult());
235 final AxPolicy clonedPolicy = new AxPolicy(policy);
236 assertEquals("AxPolicy:(key=AxArtifactKey:(name=policy,version=0.0.1),template=FREEFORM,sta",
237 clonedPolicy.toString().substring(0, 77));
239 assertFalse(policy.hashCode() == 0);
241 assertTrue(policy.equals(policy));
242 assertTrue(policy.equals(clonedPolicy));
243 assertFalse(policy.equals(null));
244 assertFalse(policy.equals((Object)"Hello"));
245 assertFalse(policy.equals(
246 new AxPolicy(AxArtifactKey.getNullKey(), savedTemplate, savedStateMap, savedFirstState)));
247 assertFalse(policy.equals(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState)));
248 assertFalse(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState)));
249 assertFalse(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState")));
250 assertTrue(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState)));
252 assertEquals(0, policy.compareTo(policy));
253 assertEquals(0, policy.compareTo(clonedPolicy));
254 assertNotEquals(0, policy.compareTo(new AxArtifactKey()));
255 assertNotEquals(0, policy.compareTo(null));
256 assertNotEquals(0, policy.compareTo(
257 new AxPolicy(AxArtifactKey.getNullKey(), savedTemplate, savedStateMap, savedFirstState)));
259 policy.compareTo(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState)));
261 policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState)));
263 policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState")));
264 assertEquals(0, policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState)));
266 assertNotNull(policy.getKeys());
268 final AxPolicies policies = new AxPolicies();
269 result = new AxValidationResult();
270 result = policies.validate(result);
271 assertEquals(ValidationResult.INVALID, result.getValidationResult());
273 // Invalid, no events in event map
274 policies.setKey(new AxArtifactKey("PoliciesKey", "0.0.1"));
275 assertEquals("PoliciesKey:0.0.1", policies.getKey().getId());
277 result = new AxValidationResult();
278 result = policies.validate(result);
279 assertEquals(ValidationResult.INVALID, result.getValidationResult());
281 policies.getPolicyMap().put(savedPolicyKey, policy);
282 result = new AxValidationResult();
283 result = policies.validate(result);
284 assertEquals(ValidationResult.VALID, result.getValidationResult());
286 policies.getPolicyMap().put(AxArtifactKey.getNullKey(), null);
287 result = new AxValidationResult();
288 result = policies.validate(result);
289 assertEquals(ValidationResult.INVALID, result.getValidationResult());
291 policies.getPolicyMap().remove(AxArtifactKey.getNullKey());
292 result = new AxValidationResult();
293 result = policies.validate(result);
294 assertEquals(ValidationResult.VALID, result.getValidationResult());
296 policies.getPolicyMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
297 result = new AxValidationResult();
298 result = policies.validate(result);
299 assertEquals(ValidationResult.INVALID, result.getValidationResult());
301 policies.getPolicyMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
302 result = new AxValidationResult();
303 result = policies.validate(result);
304 assertEquals(ValidationResult.VALID, result.getValidationResult());
306 policies.getPolicyMap().put(new AxArtifactKey("BadEventKey", "0.0.1"), policy);
307 result = new AxValidationResult();
308 result = policies.validate(result);
309 assertEquals(ValidationResult.INVALID, result.getValidationResult());
311 policies.getPolicyMap().remove(new AxArtifactKey("BadEventKey", "0.0.1"));
312 result = new AxValidationResult();
313 result = policies.validate(result);
314 assertEquals(ValidationResult.VALID, result.getValidationResult());
317 policies.afterUnmarshal(null, null);
319 final AxPolicies clonedPolicies = new AxPolicies(policies);
320 assertEquals("AxPolicies:(key=AxArtifactKey:(name=PoliciesKey,version=0.0.",
321 clonedPolicies.toString().substring(0, 60));
323 assertFalse(policies.hashCode() == 0);
325 assertTrue(policies.equals(policies));
326 assertTrue(policies.equals(clonedPolicies));
327 assertFalse(policies.equals(null));
328 assertFalse(policies.equals((Object)"Hello"));
329 assertFalse(policies.equals(new AxPolicies(new AxArtifactKey())));
331 assertEquals(0, policies.compareTo(policies));
332 assertEquals(0, policies.compareTo(clonedPolicies));
333 assertNotEquals(0, policies.compareTo(null));
334 assertNotEquals(0, policies.compareTo(new AxArtifactKey()));
335 assertNotEquals(0, policies.compareTo(new AxPolicies(new AxArtifactKey())));
337 clonedPolicies.get(savedPolicyKey).setTemplate("AnotherTemplate");
338 assertNotEquals(0, policies.compareTo(clonedPolicies));
340 assertEquals(policies.getKey(), policies.getKeys().get(0));
342 assertEquals("policy", policies.get("policy").getKey().getName());
343 assertEquals("policy", policies.get("policy", "0.0.1").getKey().getName());
344 assertEquals(1, policies.getAll("policy", "0.0.1").size());
345 assertEquals(0, policies.getAll("NonExistantPolicy").size());
347 AxStateTree stateTree = policy.getStateTree();
348 assertNotNull(stateTree);
349 assertNotNull(stateTree.getReferencedStateList());
350 assertNotNull(stateTree.getReferencedStateSet());
352 final AxState secondState = new AxState(policy.getStateMap().get("state"));
353 secondState.getKey().setLocalName("SecondState");
354 secondState.afterUnmarshal(null, null);
355 policy.getStateMap().put("SecondState", secondState);
356 policy.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey());
358 stateTree = policy.getStateTree();
359 assertNotNull(stateTree);
360 assertNotNull(stateTree.getReferencedStateList());
361 assertNotNull(stateTree.getReferencedStateSet());
362 assertNotNull(stateTree.getNextStates());
364 policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")
365 .setNextState(policy.getStateMap().get("state").getKey());
367 policy.getStateTree();
368 fail("test should throw an exception here");
369 } catch (final Exception e) {
370 assertEquals("loop detected in state tree for policy policy:0.0.1 state SecondState, "
371 + "next state state referenced more than once", e.getMessage());
374 policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")
375 .setNextState(AxReferenceKey.getNullKey());
377 final AxState thirdState = new AxState(policy.getStateMap().get("state"));
378 thirdState.getKey().setLocalName("ThirdState");
379 thirdState.afterUnmarshal(null, null);
380 policy.getStateMap().put("ThirdState", thirdState);
381 policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0").setNextState(thirdState.getKey());
382 policy.getStateMap().get("ThirdState").getStateOutputs().get("stateOutput0")
383 .setNextState(AxReferenceKey.getNullKey());
385 stateTree = policy.getStateTree();
387 final AxStateOutput ssS0Clone = new AxStateOutput(
388 policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0"));
389 ssS0Clone.getKey().setLocalName("ssS0Clone");
390 policy.getStateMap().get("SecondState").getStateOutputs().put("ssS0Clone", ssS0Clone);
393 policy.getStateTree();
394 fail("test should throw an exception here");
395 } catch (final Exception e) {
396 assertEquals("loop detected in state tree for policy policy:0.0.1 state SecondState, "
397 + "next state ThirdState referenced more than once", e.getMessage());
400 policy.getStateMap().get("SecondState").getStateOutputs().remove("ssS0Clone");
402 policy.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey());
403 secondState.getStateOutputs().get("stateOutput0").setNextState(thirdState.getKey());
404 thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey());
406 stateTree = policy.getStateTree();
407 assertNotNull(stateTree.getState());
409 thirdState.getStateOutputs().get("stateOutput0").setNextState(secondState.getKey());
412 policy.getStateTree();
413 fail("test should throw an exception here");
414 } catch (final Exception e) {
415 assertEquals("loop detected in state tree for policy policy:0.0.1 state ThirdState, "
416 + "next state SecondState referenced more than once", e.getMessage());
419 thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey());
421 stateTree = policy.getStateTree();
423 final AxStateTree otherStateTree = policy.getStateTree();
424 assertEquals(0, stateTree.compareTo(otherStateTree));
426 for (final AxStateTree childStateTree : stateTree.getNextStates()) {
427 assertNotEquals(0, stateTree.compareTo(childStateTree));
430 otherStateTree.getNextStates().clear();
431 assertNotEquals(0, stateTree.compareTo(otherStateTree));