Changes for checkstyle 8.32
[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 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;
40
41 /**
42  * Test apex policies.
43  * 
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  */
46 public class PoliciesTest {
47
48     @Test
49     public void testPolicies() {
50         final TreeMap<String, AxState> stateMap = new TreeMap<>();
51         final TreeMap<String, AxState> stateMapEmpty = new TreeMap<>();
52
53         assertNotNull(new AxPolicy());
54         assertNotNull(new AxPolicy(new AxArtifactKey()));
55         assertNotNull(new AxPolicy(new AxArtifactKey(), "PolicyTemplate", stateMapEmpty, "FirstState"));
56
57         AxPolicy policy = new AxPolicy();
58
59         final AxArtifactKey policyKey = new AxArtifactKey("PolicyName", "0.0.1");
60
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);
67
68         try {
69             policy.setKey(null);
70             fail("test should throw an exception here");
71         } catch (final Exception e) {
72             assertEquals("key may not be null", e.getMessage());
73         }
74
75         policy.setKey(policyKey);
76         assertEquals("PolicyName:0.0.1", policy.getKey().getId());
77         assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId());
78
79         try {
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());
84         }
85
86         policy.setTemplate("PolicyTemplate");
87         assertEquals("PolicyTemplate", policy.getTemplate());
88
89         try {
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());
94         }
95
96         policy.setStateMap(stateMap);
97         assertEquals(stateMap, policy.getStateMap());
98
99         try {
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());
104         }
105
106         policy.setFirstState("FirstState");
107         assertEquals("FirstState", policy.getFirstState());
108
109         assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId());
110
111         policy = new SupportApexPolicyModelCreator().getModel().getPolicies().get("policy");
112
113         AxValidationResult result = new AxValidationResult();
114         result = policy.validate(result);
115         assertEquals(ValidationResult.VALID, result.getValidationResult());
116
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());
122
123         policy.setKey(savedPolicyKey);
124         result = new AxValidationResult();
125         result = policy.validate(result);
126         assertEquals(ValidationResult.VALID, result.getValidationResult());
127
128         final String savedTemplate = policy.getTemplate();
129         policy.setTemplate("");
130         result = new AxValidationResult();
131         result = policy.validate(result);
132         assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());
133
134         policy.setTemplate(savedTemplate);
135         result = new AxValidationResult();
136         result = policy.validate(result);
137         assertEquals(ValidationResult.VALID, result.getValidationResult());
138
139         final Map<String, AxState> savedStateMap = policy.getStateMap();
140
141         policy.setStateMap(stateMapEmpty);
142         result = new AxValidationResult();
143         result = policy.validate(result);
144         assertEquals(ValidationResult.INVALID, result.getValidationResult());
145
146         policy.setStateMap(savedStateMap);
147         result = new AxValidationResult();
148         result = policy.validate(result);
149         assertEquals(ValidationResult.VALID, result.getValidationResult());
150
151         savedStateMap.put(AxKey.NULL_KEY_NAME, firstState);
152         result = new AxValidationResult();
153         result = policy.validate(result);
154         assertEquals(ValidationResult.INVALID, result.getValidationResult());
155
156         savedStateMap.remove(AxKey.NULL_KEY_NAME);
157         result = new AxValidationResult();
158         result = policy.validate(result);
159         assertEquals(ValidationResult.VALID, result.getValidationResult());
160
161         savedStateMap.put("NullState", null);
162         result = new AxValidationResult();
163         result = policy.validate(result);
164         assertEquals(ValidationResult.INVALID, result.getValidationResult());
165
166         savedStateMap.remove("NullState");
167         result = new AxValidationResult();
168         result = policy.validate(result);
169         assertEquals(ValidationResult.VALID, result.getValidationResult());
170
171         savedStateMap.put("BadStateKey", firstState);
172         result = new AxValidationResult();
173         result = policy.validate(result);
174         assertEquals(ValidationResult.INVALID, result.getValidationResult());
175
176         savedStateMap.remove("BadStateKey");
177         result = new AxValidationResult();
178         result = policy.validate(result);
179         assertEquals(ValidationResult.VALID, result.getValidationResult());
180
181         savedStateMap.put(badState.getKey().getLocalName(), badState);
182         result = new AxValidationResult();
183         result = policy.validate(result);
184         assertEquals(ValidationResult.INVALID, result.getValidationResult());
185
186         savedStateMap.remove(badState.getKey().getLocalName());
187         result = new AxValidationResult();
188         result = policy.validate(result);
189         assertEquals(ValidationResult.VALID, result.getValidationResult());
190
191         final String savedFirstState = policy.getFirstState();
192
193         policy.setFirstState("");
194         result = new AxValidationResult();
195         result = policy.validate(result);
196         assertEquals(ValidationResult.INVALID, result.getValidationResult());
197
198         policy.setFirstState(savedFirstState);
199         result = new AxValidationResult();
200         result = policy.validate(result);
201         assertEquals(ValidationResult.VALID, result.getValidationResult());
202
203         policy.setFirstState("NonExistantFirstState");
204         result = new AxValidationResult();
205         result = policy.validate(result);
206         assertEquals(ValidationResult.INVALID, result.getValidationResult());
207
208         policy.setFirstState(savedFirstState);
209         result = new AxValidationResult();
210         result = policy.validate(result);
211         assertEquals(ValidationResult.VALID, result.getValidationResult());
212
213         final AxState clonedState = new AxState(policy.getStateMap().get("state"));
214         clonedState.getKey().setLocalName("ClonedState");
215         clonedState.afterUnmarshal(null, null);
216
217         savedStateMap.put(clonedState.getKey().getLocalName(), clonedState);
218         result = new AxValidationResult();
219         result = policy.validate(result);
220         assertEquals(ValidationResult.WARNING, result.getValidationResult());
221
222         savedStateMap.remove(clonedState.getKey().getLocalName());
223         result = new AxValidationResult();
224         result = policy.validate(result);
225         assertEquals(ValidationResult.VALID, result.getValidationResult());
226
227         policy.clean();
228
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));
232
233         assertFalse(policy.hashCode() == 0);
234
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)));
245
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)));
252         assertNotEquals(0,
253                         policy.compareTo(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState)));
254         assertNotEquals(0,
255                         policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState)));
256         assertNotEquals(0,
257                         policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState")));
258         assertEquals(0, policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState)));
259
260         assertNotNull(policy.getKeys());
261
262         final AxPolicies policies = new AxPolicies();
263         result = new AxValidationResult();
264         result = policies.validate(result);
265         assertEquals(ValidationResult.INVALID, result.getValidationResult());
266
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());
270
271         result = new AxValidationResult();
272         result = policies.validate(result);
273         assertEquals(ValidationResult.INVALID, result.getValidationResult());
274
275         policies.getPolicyMap().put(savedPolicyKey, policy);
276         result = new AxValidationResult();
277         result = policies.validate(result);
278         assertEquals(ValidationResult.VALID, result.getValidationResult());
279
280         policies.getPolicyMap().put(AxArtifactKey.getNullKey(), null);
281         result = new AxValidationResult();
282         result = policies.validate(result);
283         assertEquals(ValidationResult.INVALID, result.getValidationResult());
284
285         policies.getPolicyMap().remove(AxArtifactKey.getNullKey());
286         result = new AxValidationResult();
287         result = policies.validate(result);
288         assertEquals(ValidationResult.VALID, result.getValidationResult());
289
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());
294
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());
299
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());
304
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());
309
310         policies.clean();
311         policies.afterUnmarshal(null, null);
312
313         final AxPolicies clonedPolicies = new AxPolicies(policies);
314         assertEquals("AxPolicies:(key=AxArtifactKey:(name=PoliciesKey,version=0.0.",
315                         clonedPolicies.toString().substring(0, 60));
316
317         assertFalse(policies.hashCode() == 0);
318
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())));
324
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())));
330
331         clonedPolicies.get(savedPolicyKey).setTemplate("AnotherTemplate");
332         assertNotEquals(0, policies.compareTo(clonedPolicies));
333
334         assertEquals(policies.getKey(), policies.getKeys().get(0));
335
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());
340
341         AxStateTree stateTree = policy.getStateTree();
342         assertNotNull(stateTree);
343         assertNotNull(stateTree.getReferencedStateList());
344         assertNotNull(stateTree.getReferencedStateSet());
345
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());
351
352         stateTree = policy.getStateTree();
353         assertNotNull(stateTree);
354         assertNotNull(stateTree.getReferencedStateList());
355         assertNotNull(stateTree.getReferencedStateSet());
356         assertNotNull(stateTree.getNextStates());
357
358         policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")
359                         .setNextState(policy.getStateMap().get("state").getKey());
360         try {
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());
366         }
367
368         policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")
369                         .setNextState(AxReferenceKey.getNullKey());
370
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());
378
379         stateTree = policy.getStateTree();
380
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);
385
386         try {
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());
392         }
393
394         policy.getStateMap().get("SecondState").getStateOutputs().remove("ssS0Clone");
395
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());
399
400         stateTree = policy.getStateTree();
401         assertNotNull(stateTree.getState());
402
403         thirdState.getStateOutputs().get("stateOutput0").setNextState(secondState.getKey());
404
405         try {
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());
411         }
412
413         thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey());
414
415         stateTree = policy.getStateTree();
416
417         final AxStateTree otherStateTree = policy.getStateTree();
418         assertEquals(0, stateTree.compareTo(otherStateTree));
419
420         for (final AxStateTree childStateTree : stateTree.getNextStates()) {
421             assertNotEquals(0, stateTree.compareTo(childStateTree));
422         }
423
424         otherStateTree.getNextStates().clear();
425         assertNotEquals(0, stateTree.compareTo(otherStateTree));
426     }
427 }