1974413ff50b07c36b21f2c7a8787a25e02bf93e
[policy/apex-pdp.git] / model / policy-model / src / test / java / org / onap / policy / apex / model / policymodel / concepts / PoliciesTest.java
1 /*-
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
9  * 
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  * 
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.
17  * 
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.policymodel.concepts;
23
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;
30
31 import java.util.Map;
32 import java.util.TreeMap;
33
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;
46
47 /**
48  * Test apex policies.
49  * 
50  * @author Liam Fallon (liam.fallon@ericsson.com)
51  */
52 public class PoliciesTest {
53
54     @Test
55     public void testPolicies() {
56         final TreeMap<String, AxState> stateMap = new TreeMap<>();
57         final TreeMap<String, AxState> stateMapEmpty = new TreeMap<>();
58
59         assertNotNull(new AxPolicy());
60         assertNotNull(new AxPolicy(new AxArtifactKey()));
61         assertNotNull(new AxPolicy(new AxArtifactKey(), "PolicyTemplate", stateMapEmpty, "FirstState"));
62
63         AxPolicy policy = new AxPolicy();
64
65         final AxArtifactKey policyKey = new AxArtifactKey("PolicyName", "0.0.1");
66
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);
73
74         try {
75             policy.setKey(null);
76             fail("test should throw an exception here");
77         } catch (final Exception e) {
78             assertEquals("key may not be null", e.getMessage());
79         }
80
81         policy.setKey(policyKey);
82         assertEquals("PolicyName:0.0.1", policy.getKey().getId());
83         assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId());
84
85         try {
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());
90         }
91
92         policy.setTemplate("PolicyTemplate");
93         assertEquals("PolicyTemplate", policy.getTemplate());
94
95         try {
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());
100         }
101
102         policy.setStateMap(stateMap);
103         assertEquals(stateMap, policy.getStateMap());
104
105         try {
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());
110         }
111
112         policy.setFirstState("FirstState");
113         assertEquals("FirstState", policy.getFirstState());
114
115         assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId());
116
117         policy = new SupportApexPolicyModelCreator().getModel().getPolicies().get("policy");
118
119         AxValidationResult result = new AxValidationResult();
120         result = policy.validate(result);
121         assertEquals(ValidationResult.VALID, result.getValidationResult());
122
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());
128
129         policy.setKey(savedPolicyKey);
130         result = new AxValidationResult();
131         result = policy.validate(result);
132         assertEquals(ValidationResult.VALID, result.getValidationResult());
133
134         final String savedTemplate = policy.getTemplate();
135         policy.setTemplate("");
136         result = new AxValidationResult();
137         result = policy.validate(result);
138         assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());
139
140         policy.setTemplate(savedTemplate);
141         result = new AxValidationResult();
142         result = policy.validate(result);
143         assertEquals(ValidationResult.VALID, result.getValidationResult());
144
145         final Map<String, AxState> savedStateMap = policy.getStateMap();
146
147         policy.setStateMap(stateMapEmpty);
148         result = new AxValidationResult();
149         result = policy.validate(result);
150         assertEquals(ValidationResult.INVALID, result.getValidationResult());
151
152         policy.setStateMap(savedStateMap);
153         result = new AxValidationResult();
154         result = policy.validate(result);
155         assertEquals(ValidationResult.VALID, result.getValidationResult());
156
157         savedStateMap.put(AxKey.NULL_KEY_NAME, firstState);
158         result = new AxValidationResult();
159         result = policy.validate(result);
160         assertEquals(ValidationResult.INVALID, result.getValidationResult());
161
162         savedStateMap.remove(AxKey.NULL_KEY_NAME);
163         result = new AxValidationResult();
164         result = policy.validate(result);
165         assertEquals(ValidationResult.VALID, result.getValidationResult());
166
167         savedStateMap.put("NullState", null);
168         result = new AxValidationResult();
169         result = policy.validate(result);
170         assertEquals(ValidationResult.INVALID, result.getValidationResult());
171
172         savedStateMap.remove("NullState");
173         result = new AxValidationResult();
174         result = policy.validate(result);
175         assertEquals(ValidationResult.VALID, result.getValidationResult());
176
177         savedStateMap.put("BadStateKey", firstState);
178         result = new AxValidationResult();
179         result = policy.validate(result);
180         assertEquals(ValidationResult.INVALID, result.getValidationResult());
181
182         savedStateMap.remove("BadStateKey");
183         result = new AxValidationResult();
184         result = policy.validate(result);
185         assertEquals(ValidationResult.VALID, result.getValidationResult());
186
187         savedStateMap.put(badState.getKey().getLocalName(), badState);
188         result = new AxValidationResult();
189         result = policy.validate(result);
190         assertEquals(ValidationResult.INVALID, result.getValidationResult());
191
192         savedStateMap.remove(badState.getKey().getLocalName());
193         result = new AxValidationResult();
194         result = policy.validate(result);
195         assertEquals(ValidationResult.VALID, result.getValidationResult());
196
197         final String savedFirstState = policy.getFirstState();
198
199         policy.setFirstState("");
200         result = new AxValidationResult();
201         result = policy.validate(result);
202         assertEquals(ValidationResult.INVALID, result.getValidationResult());
203
204         policy.setFirstState(savedFirstState);
205         result = new AxValidationResult();
206         result = policy.validate(result);
207         assertEquals(ValidationResult.VALID, result.getValidationResult());
208
209         policy.setFirstState("NonExistantFirstState");
210         result = new AxValidationResult();
211         result = policy.validate(result);
212         assertEquals(ValidationResult.INVALID, result.getValidationResult());
213
214         policy.setFirstState(savedFirstState);
215         result = new AxValidationResult();
216         result = policy.validate(result);
217         assertEquals(ValidationResult.VALID, result.getValidationResult());
218
219         final AxState clonedState = new AxState(policy.getStateMap().get("state"));
220         clonedState.getKey().setLocalName("ClonedState");
221         clonedState.afterUnmarshal(null, null);
222
223         savedStateMap.put(clonedState.getKey().getLocalName(), clonedState);
224         result = new AxValidationResult();
225         result = policy.validate(result);
226         assertEquals(ValidationResult.WARNING, result.getValidationResult());
227
228         savedStateMap.remove(clonedState.getKey().getLocalName());
229         result = new AxValidationResult();
230         result = policy.validate(result);
231         assertEquals(ValidationResult.VALID, result.getValidationResult());
232
233         policy.clean();
234
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));
238
239         assertFalse(policy.hashCode() == 0);
240
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)));
251
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)));
258         assertNotEquals(0,
259                         policy.compareTo(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState)));
260         assertNotEquals(0,
261                         policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState)));
262         assertNotEquals(0,
263                         policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState")));
264         assertEquals(0, policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState)));
265
266         assertNotNull(policy.getKeys());
267
268         final AxPolicies policies = new AxPolicies();
269         result = new AxValidationResult();
270         result = policies.validate(result);
271         assertEquals(ValidationResult.INVALID, result.getValidationResult());
272
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());
276
277         result = new AxValidationResult();
278         result = policies.validate(result);
279         assertEquals(ValidationResult.INVALID, result.getValidationResult());
280
281         policies.getPolicyMap().put(savedPolicyKey, policy);
282         result = new AxValidationResult();
283         result = policies.validate(result);
284         assertEquals(ValidationResult.VALID, result.getValidationResult());
285
286         policies.getPolicyMap().put(AxArtifactKey.getNullKey(), null);
287         result = new AxValidationResult();
288         result = policies.validate(result);
289         assertEquals(ValidationResult.INVALID, result.getValidationResult());
290
291         policies.getPolicyMap().remove(AxArtifactKey.getNullKey());
292         result = new AxValidationResult();
293         result = policies.validate(result);
294         assertEquals(ValidationResult.VALID, result.getValidationResult());
295
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());
300
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());
305
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());
310
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());
315
316         policies.clean();
317         policies.afterUnmarshal(null, null);
318
319         final AxPolicies clonedPolicies = new AxPolicies(policies);
320         assertEquals("AxPolicies:(key=AxArtifactKey:(name=PoliciesKey,version=0.0.",
321                         clonedPolicies.toString().substring(0, 60));
322
323         assertFalse(policies.hashCode() == 0);
324
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())));
330
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())));
336
337         clonedPolicies.get(savedPolicyKey).setTemplate("AnotherTemplate");
338         assertNotEquals(0, policies.compareTo(clonedPolicies));
339
340         assertEquals(policies.getKey(), policies.getKeys().get(0));
341
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());
346
347         AxStateTree stateTree = policy.getStateTree();
348         assertNotNull(stateTree);
349         assertNotNull(stateTree.getReferencedStateList());
350         assertNotNull(stateTree.getReferencedStateSet());
351
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());
357
358         stateTree = policy.getStateTree();
359         assertNotNull(stateTree);
360         assertNotNull(stateTree.getReferencedStateList());
361         assertNotNull(stateTree.getReferencedStateSet());
362         assertNotNull(stateTree.getNextStates());
363
364         policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")
365                         .setNextState(policy.getStateMap().get("state").getKey());
366         try {
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());
372         }
373
374         policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")
375                         .setNextState(AxReferenceKey.getNullKey());
376
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());
384
385         stateTree = policy.getStateTree();
386
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);
391
392         try {
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());
398         }
399
400         policy.getStateMap().get("SecondState").getStateOutputs().remove("ssS0Clone");
401
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());
405
406         stateTree = policy.getStateTree();
407         assertNotNull(stateTree.getState());
408
409         thirdState.getStateOutputs().get("stateOutput0").setNextState(secondState.getKey());
410
411         try {
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());
417         }
418
419         thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey());
420
421         stateTree = policy.getStateTree();
422
423         final AxStateTree otherStateTree = policy.getStateTree();
424         assertEquals(0, stateTree.compareTo(otherStateTree));
425
426         for (final AxStateTree childStateTree : stateTree.getNextStates()) {
427             assertNotEquals(0, stateTree.compareTo(childStateTree));
428         }
429
430         otherStateTree.getNextStates().clear();
431         assertNotEquals(0, stateTree.compareTo(otherStateTree));
432     }
433 }