1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* Modifications Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.apex.model.policymodel.concepts;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import java.util.TreeMap;
import org.junit.Test;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
import org.onap.policy.apex.model.policymodel.handling.SupportApexPolicyModelCreator;
/**
* Test apex policies.
*
* @author Liam Fallon (liam.fallon@ericsson.com)
*/
public class PoliciesTest {
@Test
public void testPolicies() {
final TreeMap<String, AxState> stateMap = new TreeMap<>();
final TreeMap<String, AxState> stateMapEmpty = new TreeMap<>();
assertNotNull(new AxPolicy());
assertNotNull(new AxPolicy(new AxArtifactKey()));
assertNotNull(new AxPolicy(new AxArtifactKey(), "PolicyTemplate", stateMapEmpty, "FirstState"));
final AxPolicy policy = new AxPolicy();
final AxArtifactKey policyKey = new AxArtifactKey("PolicyName", "0.0.1");
final AxState firstState = new AxState(new AxReferenceKey(policy.getKey(), "FirstState"));
final AxState badState = new AxState(new AxReferenceKey(policy.getKey(), "BadState"));
final AxStateOutput badStateOutput = new AxStateOutput(badState.getKey(), AxArtifactKey.getNullKey(),
new AxReferenceKey(policyKey, "BadNextState"));
badState.getStateOutputs().put(badStateOutput.getKey().getLocalName(), badStateOutput);
stateMap.put(firstState.getKey().getLocalName(), firstState);
assertThatThrownBy(() -> policy.setKey(null))
.hasMessage("key may not be null");
policy.setKey(policyKey);
assertEquals("PolicyName:0.0.1", policy.getKey().getId());
assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId());
assertThatThrownBy(() -> policy.setTemplate(null))
.hasMessage("template may not be null");
policy.setTemplate("PolicyTemplate");
assertEquals("PolicyTemplate", policy.getTemplate());
assertThatThrownBy(() -> policy.setStateMap(null))
.hasMessage("stateMap may not be null");
policy.setStateMap(stateMap);
assertEquals(stateMap, policy.getStateMap());
assertThatThrownBy(() -> policy.setFirstState(null))
.hasMessage("firstState may not be null");
policy.setFirstState("FirstState");
assertEquals("FirstState", policy.getFirstState());
assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId());
final AxPolicy policyPN = new SupportApexPolicyModelCreator().getModel().getPolicies().get("policy");
AxValidationResult result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
final AxArtifactKey savedPolicyKey = policyPN.getKey();
policyPN.setKey(AxArtifactKey.getNullKey());
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
policyPN.setKey(savedPolicyKey);
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
final String savedTemplate = policyPN.getTemplate();
policyPN.setTemplate("");
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());
policyPN.setTemplate(savedTemplate);
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
final Map<String, AxState> savedStateMap = policyPN.getStateMap();
policyPN.setStateMap(stateMapEmpty);
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
policyPN.setStateMap(savedStateMap);
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
savedStateMap.put(AxKey.NULL_KEY_NAME, firstState);
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
savedStateMap.remove(AxKey.NULL_KEY_NAME);
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
savedStateMap.put("NullState", null);
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
savedStateMap.remove("NullState");
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
savedStateMap.put("BadStateKey", firstState);
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
savedStateMap.remove("BadStateKey");
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
savedStateMap.put(badState.getKey().getLocalName(), badState);
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
savedStateMap.remove(badState.getKey().getLocalName());
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
final String savedFirstState = policyPN.getFirstState();
policyPN.setFirstState("");
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
policyPN.setFirstState(savedFirstState);
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
policyPN.setFirstState("NonExistantFirstState");
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
policyPN.setFirstState(savedFirstState);
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
final AxState clonedState = new AxState(policyPN.getStateMap().get("state"));
clonedState.getKey().setLocalName("ClonedState");
clonedState.afterUnmarshal(null, null);
savedStateMap.put(clonedState.getKey().getLocalName(), clonedState);
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.WARNING, result.getValidationResult());
savedStateMap.remove(clonedState.getKey().getLocalName());
result = new AxValidationResult();
result = policyPN.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
policyPN.clean();
final AxPolicy clonedPolicy = new AxPolicy(policyPN);
assertEquals("AxPolicy:(key=AxArtifactKey:(name=policy,version=0.0.1),template=FREEFORM,sta",
clonedPolicy.toString().substring(0, 77));
assertFalse(policyPN.hashCode() == 0);
assertTrue(policyPN.equals(policyPN));
assertTrue(policyPN.equals(clonedPolicy));
assertFalse(policyPN.equals(null));
assertFalse(policyPN.equals((Object) "Hello"));
assertFalse(policyPN.equals(
new AxPolicy(AxArtifactKey.getNullKey(), savedTemplate, savedStateMap, savedFirstState)));
assertFalse(policyPN.equals(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState)));
assertFalse(policyPN.equals(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState)));
assertFalse(policyPN.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState")));
assertTrue(policyPN.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState)));
assertEquals(0, policyPN.compareTo(policyPN));
assertEquals(0, policyPN.compareTo(clonedPolicy));
assertNotEquals(0, policyPN.compareTo(new AxArtifactKey()));
assertNotEquals(0, policyPN.compareTo(null));
assertNotEquals(0, policyPN.compareTo(
new AxPolicy(AxArtifactKey.getNullKey(), savedTemplate, savedStateMap, savedFirstState)));
assertNotEquals(0, policyPN.compareTo(new AxPolicy(savedPolicyKey,
"SomeTemplate", savedStateMap, savedFirstState)));
assertNotEquals(0, policyPN.compareTo(new AxPolicy(savedPolicyKey, savedTemplate,
stateMapEmpty, savedFirstState)));
assertNotEquals(0, policyPN.compareTo(new AxPolicy(savedPolicyKey, savedTemplate,
savedStateMap, "SomeFirstState")));
assertEquals(0, policyPN.compareTo(new AxPolicy(savedPolicyKey, savedTemplate,
savedStateMap, savedFirstState)));
assertNotNull(policyPN.getKeys());
final AxPolicies policies = new AxPolicies();
result = new AxValidationResult();
result = policies.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
// Invalid, no events in event map
policies.setKey(new AxArtifactKey("PoliciesKey", "0.0.1"));
assertEquals("PoliciesKey:0.0.1", policies.getKey().getId());
result = new AxValidationResult();
result = policies.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
policies.getPolicyMap().put(savedPolicyKey, policyPN);
result = new AxValidationResult();
result = policies.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
policies.getPolicyMap().put(AxArtifactKey.getNullKey(), null);
result = new AxValidationResult();
result = policies.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
policies.getPolicyMap().remove(AxArtifactKey.getNullKey());
result = new AxValidationResult();
result = policies.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
policies.getPolicyMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
result = new AxValidationResult();
result = policies.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
policies.getPolicyMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
result = new AxValidationResult();
result = policies.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
policies.getPolicyMap().put(new AxArtifactKey("BadEventKey", "0.0.1"), policyPN);
result = new AxValidationResult();
result = policies.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
policies.getPolicyMap().remove(new AxArtifactKey("BadEventKey", "0.0.1"));
result = new AxValidationResult();
result = policies.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
policies.clean();
policies.afterUnmarshal(null, null);
final AxPolicies clonedPolicies = new AxPolicies(policies);
assertEquals("AxPolicies:(key=AxArtifactKey:(name=PoliciesKey,version=0.0.",
clonedPolicies.toString().substring(0, 60));
assertFalse(policies.hashCode() == 0);
assertTrue(policies.equals(policies));
assertTrue(policies.equals(clonedPolicies));
assertFalse(policies.equals(null));
assertFalse(policies.equals((Object) "Hello"));
assertFalse(policies.equals(new AxPolicies(new AxArtifactKey())));
assertEquals(0, policies.compareTo(policies));
assertEquals(0, policies.compareTo(clonedPolicies));
assertNotEquals(0, policies.compareTo(null));
assertNotEquals(0, policies.compareTo(new AxArtifactKey()));
assertNotEquals(0, policies.compareTo(new AxPolicies(new AxArtifactKey())));
clonedPolicies.get(savedPolicyKey).setTemplate("AnotherTemplate");
assertNotEquals(0, policies.compareTo(clonedPolicies));
assertEquals(policies.getKey(), policies.getKeys().get(0));
assertEquals("policy", policies.get("policy").getKey().getName());
assertEquals("policy", policies.get("policy", "0.0.1").getKey().getName());
assertEquals(1, policies.getAll("policy", "0.0.1").size());
assertEquals(0, policies.getAll("NonExistantPolicy").size());
AxStateTree stateTree = policy.getStateTree();
assertNotNull(stateTree);
assertNotNull(stateTree.getReferencedStateList());
assertNotNull(stateTree.getReferencedStateSet());
final AxState secondState = new AxState(policyPN.getStateMap().get("state"));
secondState.getKey().setLocalName("SecondState");
secondState.afterUnmarshal(null, null);
policyPN.getStateMap().put("SecondState", secondState);
policyPN.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey());
stateTree = policyPN.getStateTree();
assertNotNull(stateTree);
assertNotNull(stateTree.getReferencedStateList());
assertNotNull(stateTree.getReferencedStateSet());
assertNotNull(stateTree.getNextStates());
policyPN.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")
.setNextState(policyPN.getStateMap().get("state").getKey());
assertThatThrownBy(() -> policyPN.getStateTree())
.hasMessageContaining("loop detected in state tree for policy policy:0.0.1 state SecondState, "
+ "next state state referenced more than once");
policyPN.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")
.setNextState(AxReferenceKey.getNullKey());
final AxState thirdState = new AxState(policyPN.getStateMap().get("state"));
thirdState.getKey().setLocalName("ThirdState");
thirdState.afterUnmarshal(null, null);
policyPN.getStateMap().put("ThirdState", thirdState);
policyPN.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")
.setNextState(thirdState.getKey());
policyPN.getStateMap().get("ThirdState").getStateOutputs().get("stateOutput0")
.setNextState(AxReferenceKey.getNullKey());
stateTree = policyPN.getStateTree();
final AxStateOutput ssS0Clone = new AxStateOutput(
policyPN.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0"));
ssS0Clone.getKey().setLocalName("ssS0Clone");
policyPN.getStateMap().get("SecondState").getStateOutputs().put("ssS0Clone", ssS0Clone);
assertThatThrownBy(() -> policyPN.getStateTree())
.hasMessageContaining("loop detected in state tree for policy policy:0.0.1 state SecondState, "
+ "next state ThirdState referenced more than once");
policyPN.getStateMap().get("SecondState").getStateOutputs().remove("ssS0Clone");
policyPN.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey());
secondState.getStateOutputs().get("stateOutput0").setNextState(thirdState.getKey());
thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey());
stateTree = policyPN.getStateTree();
assertNotNull(stateTree.getState());
thirdState.getStateOutputs().get("stateOutput0").setNextState(secondState.getKey());
assertThatThrownBy(() -> policyPN.getStateTree())
.hasMessageContaining("loop detected in state tree for policy policy:0.0.1 state ThirdState, "
+ "next state SecondState referenced more than once");
thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey());
stateTree = policyPN.getStateTree();
final AxStateTree otherStateTree = policyPN.getStateTree();
assertEquals(0, stateTree.compareTo(otherStateTree));
for (final AxStateTree childStateTree : stateTree.getNextStates()) {
assertNotEquals(0, stateTree.compareTo(childStateTree));
}
otherStateTree.getNextStates().clear();
assertNotEquals(0, stateTree.compareTo(otherStateTree));
}
}
|