2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.policymodel.concepts;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
29 import org.junit.Test;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
38 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
39 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
40 import org.onap.policy.apex.model.policymodel.handling.SupportApexPolicyModelCreator;
45 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public class PolicyModelTest {
50 public void testPolicyModel() {
51 assertNotNull(new AxPolicyModel());
52 assertNotNull(new AxPolicyModel(new AxArtifactKey()));
53 assertNotNull(new AxPolicyModel(new AxArtifactKey(), new AxContextSchemas(), new AxKeyInformation(),
54 new AxEvents(), new AxContextAlbums(), new AxTasks(), new AxPolicies()));
56 final AxArtifactKey modelKey = new AxArtifactKey("ModelKey", "0.0.1");
57 final AxArtifactKey schemasKey = new AxArtifactKey("SchemasKey", "0.0.1");
58 final AxArtifactKey eventsKey = new AxArtifactKey("EventsKey", "0.0.1");
59 final AxArtifactKey keyInfoKey = new AxArtifactKey("SchemasKey", "0.0.1");
60 final AxArtifactKey albumsKey = new AxArtifactKey("AlbumsKey", "0.0.1");
61 final AxArtifactKey tasksKey = new AxArtifactKey("TasksKey", "0.0.1");
62 final AxArtifactKey policiesKey = new AxArtifactKey("PoliciesKey", "0.0.1");
64 AxPolicyModel model = new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey),
65 new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey),
66 new AxTasks(tasksKey), new AxPolicies(policiesKey));
70 assertNotNull(model.getContextModel());
71 assertEquals("ModelKey:0.0.1", model.getKeys().get(0).getId());
75 assertEquals("AxPolicyModel:(AxPolicyModel:(key=AxArtifactKey:(n", model.toString().substring(0, 50));
77 final AxPolicyModel clonedModel = new AxPolicyModel(model);
79 assertNotEquals(0, model.hashCode());
81 // disabling sonar because this code tests the equals() method
82 assertEquals(model, model); // NOSONAR
83 assertEquals(model, clonedModel);
84 assertNotEquals(model, (Object) "Hello");
85 assertNotEquals(model, new AxPolicyModel(new AxArtifactKey()));
86 assertNotEquals(model, new AxPolicyModel(AxArtifactKey.getNullKey(), new AxContextSchemas(schemasKey),
87 new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey),
88 new AxTasks(tasksKey), new AxPolicies(policiesKey)));
89 assertNotEquals(model, new AxPolicyModel(modelKey, new AxContextSchemas(), new AxKeyInformation(keyInfoKey),
90 new AxEvents(eventsKey), new AxContextAlbums(albumsKey), new AxTasks(tasksKey),
91 new AxPolicies(policiesKey)));
92 assertNotEquals(model, new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), new AxKeyInformation(),
93 new AxEvents(eventsKey), new AxContextAlbums(albumsKey), new AxTasks(tasksKey),
94 new AxPolicies(policiesKey)));
95 assertNotEquals(model, new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey),
96 new AxKeyInformation(keyInfoKey), new AxEvents(), new AxContextAlbums(albumsKey),
97 new AxTasks(tasksKey), new AxPolicies(policiesKey)));
98 assertNotEquals(model, new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey),
99 new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(),
100 new AxTasks(tasksKey), new AxPolicies(policiesKey)));
101 assertNotEquals(model, new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey),
102 new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey),
103 new AxTasks(), new AxPolicies(policiesKey)));
104 assertNotEquals(model, new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey),
105 new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey),
106 new AxTasks(tasksKey), new AxPolicies()));
107 assertEquals(model, new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey),
108 new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey),
109 new AxTasks(tasksKey), new AxPolicies(policiesKey)));
111 assertEquals(0, model.compareTo(model));
112 assertEquals(0, model.compareTo(clonedModel));
113 assertNotEquals(0, model.compareTo(new AxArtifactKey()));
114 assertNotEquals(0, model.compareTo(new AxPolicyModel(AxArtifactKey.getNullKey(),
115 new AxContextSchemas(schemasKey), new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey),
116 new AxContextAlbums(albumsKey), new AxTasks(tasksKey), new AxPolicies(policiesKey))));
117 assertNotEquals(0, model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(),
118 new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey),
119 new AxTasks(tasksKey), new AxPolicies(policiesKey))));
121 model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey),
122 new AxKeyInformation(), new AxEvents(eventsKey), new AxContextAlbums(albumsKey),
123 new AxTasks(tasksKey), new AxPolicies(policiesKey))));
124 assertNotEquals(0, model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey),
125 new AxKeyInformation(keyInfoKey), new AxEvents(), new AxContextAlbums(albumsKey),
126 new AxTasks(tasksKey), new AxPolicies(policiesKey))));
128 model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey),
129 new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey),
130 new AxContextAlbums(), new AxTasks(tasksKey), new AxPolicies(policiesKey))));
132 model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey),
133 new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey),
134 new AxContextAlbums(albumsKey), new AxTasks(), new AxPolicies(policiesKey))));
136 model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey),
137 new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey),
138 new AxContextAlbums(albumsKey), new AxTasks(tasksKey), new AxPolicies())));
139 assertEquals(0, model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey),
140 new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey),
141 new AxTasks(tasksKey), new AxPolicies(policiesKey))));
143 model = new SupportApexPolicyModelCreator().getModel();
145 AxValidationResult result = new AxValidationResult();
146 result = model.validate(result);
147 assertEquals(ValidationResult.VALID, result.getValidationResult());
149 final AxArtifactKey savedPolicyKey = model.getKey();
150 model.setKey(AxArtifactKey.getNullKey());
151 result = new AxValidationResult();
152 result = model.validate(result);
153 assertEquals(ValidationResult.INVALID, result.getValidationResult());
155 model.setKey(savedPolicyKey);
156 result = new AxValidationResult();
157 result = model.validate(result);
158 assertEquals(ValidationResult.VALID, result.getValidationResult());
160 final AxField badField = new AxField(new AxReferenceKey(model.getEvents().get("inEvent").getKey(), "BadField"),
161 new AxArtifactKey("NonExistantSchema", "0.0.1"));
162 model.getEvents().get("inEvent").getParameterMap().put(badField.getKey().getLocalName(), badField);
163 result = new AxValidationResult();
164 result = model.validate(result);
165 assertEquals(ValidationResult.INVALID, result.getValidationResult());
167 model.getEvents().get("inEvent").getParameterMap().remove(badField.getKey().getLocalName());
168 result = new AxValidationResult();
169 result = model.validate(result);
170 assertEquals(ValidationResult.VALID, result.getValidationResult());
172 final AxContextAlbum badAlbum = new AxContextAlbum(new AxArtifactKey("BadAlbum", "0.0.1"), "SomeScope", true,
173 new AxArtifactKey("NonExistantSchema", "0.0.1"));
174 model.getAlbums().getAlbumsMap().put(badAlbum.getKey(), badAlbum);
175 result = new AxValidationResult();
176 result = model.validate(result);
177 assertEquals(ValidationResult.INVALID, result.getValidationResult());
179 model.getAlbums().getAlbumsMap().remove(badAlbum.getKey());
180 result = new AxValidationResult();
181 result = model.validate(result);
182 assertEquals(ValidationResult.VALID, result.getValidationResult());
184 model.getTasks().get("task").getContextAlbumReferences()
185 .add(new AxArtifactKey("NonExistantContextAlbum", "0.0.1"));
186 result = new AxValidationResult();
187 result = model.validate(result);
188 assertEquals(ValidationResult.INVALID, result.getValidationResult());
190 model.getTasks().get("task").getContextAlbumReferences()
191 .remove(new AxArtifactKey("NonExistantContextAlbum", "0.0.1"));
192 result = new AxValidationResult();
193 result = model.validate(result);
194 assertEquals(ValidationResult.VALID, result.getValidationResult());
196 model.getPolicies().get("policy").getStateMap().get("state").getContextAlbumReferences()
197 .add(new AxArtifactKey("NonExistantContextAlbum", "0.0.1"));
198 result = new AxValidationResult();
199 result = model.validate(result);
200 assertEquals(ValidationResult.INVALID, result.getValidationResult());
202 model.getPolicies().get("policy").getStateMap().get("state").getContextAlbumReferences()
203 .remove(new AxArtifactKey("NonExistantContextAlbum", "0.0.1"));
204 result = new AxValidationResult();
205 result = model.validate(result);
206 assertEquals(ValidationResult.VALID, result.getValidationResult());
208 final AxArtifactKey savedTrigger = model.getPolicies().get("policy").getStateMap().get("state").getTrigger();
209 model.getPolicies().get("policy").getStateMap().get("state")
210 .setTrigger(new AxArtifactKey("NonExistantEvent", "0.0.1"));
211 result = new AxValidationResult();
212 result = model.validate(result);
213 assertEquals(ValidationResult.INVALID, result.getValidationResult());
215 model.getPolicies().get("policy").getStateMap().get("state").setTrigger(savedTrigger);
216 result = new AxValidationResult();
217 result = model.validate(result);
218 assertEquals(ValidationResult.VALID, result.getValidationResult());
220 final AxArtifactKey savedDefaultTask = model.getPolicies().get("policy").getStateMap().get("state")
222 model.getPolicies().get("policy").getStateMap().get("state")
223 .setDefaultTask(new AxArtifactKey("NonExistantTask", "0.0.1"));
224 result = new AxValidationResult();
225 result = model.validate(result);
226 assertEquals(ValidationResult.INVALID, result.getValidationResult());
228 model.getPolicies().get("policy").getStateMap().get("state").setDefaultTask(savedDefaultTask);
229 result = new AxValidationResult();
230 result = model.validate(result);
231 assertEquals(ValidationResult.VALID, result.getValidationResult());
233 // It is OK not to have TSL
234 final AxTaskSelectionLogic savedTaskSelectionLogic = model.getPolicies().get("policy").getStateMap()
235 .get("state").getTaskSelectionLogic();
236 model.getPolicies().get("policy").getStateMap().get("state")
237 .setTaskSelectionLogic(new AxTaskSelectionLogic(AxReferenceKey.getNullKey()));
238 result = new AxValidationResult();
239 result = model.validate(result);
240 assertEquals(ValidationResult.VALID, result.getValidationResult());
242 model.getPolicies().get("policy").getStateMap().get("state").setTaskSelectionLogic(savedTaskSelectionLogic);
243 result = new AxValidationResult();
244 result = model.validate(result);
245 assertEquals(ValidationResult.VALID, result.getValidationResult());
247 final AxStateOutput badStateOutput = new AxStateOutput(
248 new AxReferenceKey(model.getPolicies().get("policy").getStateMap().get("state").getKey(),
250 new AxArtifactKey("NonExistantEvent", "0.0.1"), AxReferenceKey.getNullKey());
251 model.getPolicies().get("policy").getStateMap().get("state").getStateOutputs()
252 .put(badStateOutput.getKey().getLocalName(), badStateOutput);
253 result = new AxValidationResult();
254 result = model.validate(result);
255 assertEquals(ValidationResult.INVALID, result.getValidationResult());
257 model.getPolicies().get("policy").getStateMap().get("state").getStateOutputs()
258 .remove(badStateOutput.getKey().getLocalName());
259 result = new AxValidationResult();
260 result = model.validate(result);
261 assertEquals(ValidationResult.VALID, result.getValidationResult());
263 final AxStateTaskReference badTaskReference = new AxStateTaskReference(
264 new AxReferenceKey(model.getPolicies().get("policy").getStateMap().get("state").getKey(),
266 AxStateTaskOutputType.LOGIC, badStateOutput.getKey());
267 model.getPolicies().get("policy").getStateMap().get("state").getTaskReferences()
268 .put(new AxArtifactKey("NonExistantTask", "0.0.1"), badTaskReference);
269 result = new AxValidationResult();
270 result = model.validate(result);
271 assertEquals(ValidationResult.INVALID, result.getValidationResult());
273 badTaskReference.setStateTaskOutputType(AxStateTaskOutputType.DIRECT);
274 result = new AxValidationResult();
275 result = model.validate(result);
276 assertEquals(ValidationResult.INVALID, result.getValidationResult());
278 model.getPolicies().get("policy").getStateMap().get("state").getTaskReferences()
279 .remove(new AxArtifactKey("NonExistantTask", "0.0.1"));
280 result = new AxValidationResult();
281 result = model.validate(result);
282 assertEquals(ValidationResult.VALID, result.getValidationResult());
284 final AxStateTaskReference tr = model.getPolicies().get("policy").getStateMap().get("state").getTaskReferences()
285 .get(new AxArtifactKey("task", "0.0.1"));
287 final String savedStateOutputName = tr.getOutput().getLocalName();
288 tr.getOutput().setLocalName("NonExistantOutput");
289 result = new AxValidationResult();
290 result = model.validate(result);
291 assertEquals(ValidationResult.INVALID, result.getValidationResult());
293 tr.getOutput().setLocalName(savedStateOutputName);
294 result = new AxValidationResult();
295 result = model.validate(result);
296 assertEquals(ValidationResult.VALID, result.getValidationResult());
298 final AxStateOutput so = model.getPolicies().get("policy").getStateMap().get("state").getStateOutputs()
299 .get(savedStateOutputName);
301 final AxArtifactKey savedOutEvent = so.getOutgoingEvent();
302 so.setOutgoingEvent(new AxArtifactKey("NonExistantEvent", "0.0.1"));
303 result = new AxValidationResult();
304 result = model.validate(result);
305 assertEquals(ValidationResult.INVALID, result.getValidationResult());
307 so.setOutgoingEvent(savedOutEvent);
308 result = new AxValidationResult();
309 result = model.validate(result);
310 assertEquals(ValidationResult.VALID, result.getValidationResult());