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;
30 import java.util.TreeMap;
31 import java.util.TreeSet;
33 import org.junit.Test;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
38 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
39 import org.onap.policy.apex.model.eventmodel.concepts.AxInputField;
40 import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField;
41 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
42 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
43 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
44 import org.onap.policy.apex.model.policymodel.concepts.AxTasks;
49 * @author Liam Fallon (liam.fallon@ericsson.com)
51 public class TasksTest {
54 public void testTasks() {
55 final TreeMap<String, AxInputField> ifEmptyMap = new TreeMap<>();
56 final TreeMap<String, AxOutputField> ofEmptyMap = new TreeMap<>();
57 final TreeMap<String, AxTaskParameter> tpEmptyMap = new TreeMap<>();
58 final TreeSet<AxArtifactKey> ctxtEmptySet = new TreeSet<>();
60 final TreeMap<String, AxInputField> ifMap = new TreeMap<>();
61 final TreeMap<String, AxOutputField> ofMap = new TreeMap<>();
62 final TreeMap<String, AxTaskParameter> tpMap = new TreeMap<>();
63 final TreeSet<AxArtifactKey> ctxtSet = new TreeSet<>();
65 assertNotNull(new AxTask());
66 assertNotNull(new AxTask(new AxArtifactKey()));
67 assertNotNull(new AxTask(new AxArtifactKey(), ifMap, ofMap, tpMap, ctxtSet, new AxTaskLogic()));
69 final AxTask task = new AxTask();
71 final AxArtifactKey taskKey = new AxArtifactKey("TaskName", "0.0.1");
73 assertEquals("TaskName:0.0.1", task.getKey().getId());
74 assertEquals("TaskName:0.0.1", task.getKeys().get(0).getId());
76 final AxArtifactKey f0SchemaKey = new AxArtifactKey("FS0", "0.0.1");
78 final AxInputField if0 = new AxInputField(new AxReferenceKey(taskKey, "IF0"), f0SchemaKey, false);
79 final AxInputField if1 = new AxInputField(new AxReferenceKey(taskKey, "IF1"), f0SchemaKey, false);
80 final AxOutputField of0 = new AxOutputField(new AxReferenceKey(taskKey, "OF0"), f0SchemaKey, false);
81 final AxOutputField of1 = new AxOutputField(new AxReferenceKey(taskKey, "OF1"), f0SchemaKey, false);
82 final AxTaskParameter tp0 = new AxTaskParameter(new AxReferenceKey(taskKey, "TP0"), "DefaultValue");
83 final AxArtifactKey cr0 = new AxArtifactKey("ContextReference", "0.0.1");
84 final AxTaskLogic tl = new AxTaskLogic(taskKey, "LogicName", "LogicFlavour", "Logic");
86 ifMap.put(if0.getKey().getLocalName(), if0);
87 ofMap.put(of0.getKey().getLocalName(), of0);
88 tpMap.put(tp0.getKey().getLocalName(), tp0);
91 task.setInputFields(ifMap);
92 assertEquals(ifMap, task.getInputFields());
93 assertTrue(task.getInputFieldSet().contains(if0));
94 assertTrue(task.getRawInputFields().keySet().contains(if0.getKey().getLocalName()));
96 task.setOutputFields(ofMap);
97 assertEquals(ofMap, task.getOutputFields());
98 assertTrue(task.getOutputFieldSet().contains(of0));
99 assertTrue(task.getRawOutputFields().keySet().contains(of0.getKey().getLocalName()));
101 final TreeMap<String, AxField> ifDupMap = new TreeMap<>();
102 final TreeMap<String, AxField> ofDupMap = new TreeMap<>();
103 ifDupMap.put(if1.getKey().getLocalName(), if1);
104 ofDupMap.put(of1.getKey().getLocalName(), of1);
105 task.duplicateInputFields(ifDupMap);
106 task.duplicateOutputFields(ofDupMap);
107 assertTrue(ifMap.containsKey("IF1"));
108 assertTrue(ofMap.containsKey("OF1"));
110 task.setTaskParameters(tpMap);
111 assertEquals(tpMap, task.getTaskParameters());
113 task.setContextAlbumReferences(ctxtSet);
114 assertEquals(ctxtSet, task.getContextAlbumReferences());
116 task.setTaskLogic(tl);
117 assertEquals(tl, task.getTaskLogic());
119 task.setKey(taskKey);
120 assertEquals("TaskName:0.0.1", task.getKey().getId());
121 assertEquals("TaskName:0.0.1", task.getKeys().get(0).getId());
123 task.afterUnmarshal(null, null);
124 assertEquals(1, task.getTaskParameters().size());
126 AxValidationResult result = new AxValidationResult();
127 result = task.validate(result);
128 assertEquals(ValidationResult.VALID, result.getValidationResult());
130 task.setKey(AxArtifactKey.getNullKey());
131 result = new AxValidationResult();
132 result = task.validate(result);
133 assertEquals(ValidationResult.INVALID, result.getValidationResult());
135 task.setKey(taskKey);
136 result = new AxValidationResult();
137 result = task.validate(result);
138 assertEquals(ValidationResult.VALID, result.getValidationResult());
140 task.setInputFields(ifEmptyMap);
141 result = new AxValidationResult();
142 result = task.validate(result);
143 assertEquals(ValidationResult.INVALID, result.getValidationResult());
145 task.setInputFields(ifMap);
146 result = new AxValidationResult();
147 result = task.validate(result);
148 assertEquals(ValidationResult.VALID, result.getValidationResult());
150 ifMap.put("NullField", null);
151 result = new AxValidationResult();
152 result = task.validate(result);
153 assertEquals(ValidationResult.INVALID, result.getValidationResult());
155 ifMap.remove("NullField");
156 result = new AxValidationResult();
157 result = task.validate(result);
158 assertEquals(ValidationResult.VALID, result.getValidationResult());
160 task.setOutputFields(ofEmptyMap);
161 result = new AxValidationResult();
162 result = task.validate(result);
163 assertEquals(ValidationResult.INVALID, result.getValidationResult());
165 task.setOutputFields(ofMap);
166 result = new AxValidationResult();
167 result = task.validate(result);
168 assertEquals(ValidationResult.VALID, result.getValidationResult());
170 ofMap.put("NullField", null);
171 result = new AxValidationResult();
172 result = task.validate(result);
173 assertEquals(ValidationResult.INVALID, result.getValidationResult());
175 ofMap.remove("NullField");
176 result = new AxValidationResult();
177 result = task.validate(result);
178 assertEquals(ValidationResult.VALID, result.getValidationResult());
180 // Empty task parameter map is OK
181 task.setTaskParameters(tpEmptyMap);
182 result = new AxValidationResult();
183 result = task.validate(result);
184 assertEquals(ValidationResult.VALID, result.getValidationResult());
186 task.setTaskParameters(tpMap);
187 result = new AxValidationResult();
188 result = task.validate(result);
189 assertEquals(ValidationResult.VALID, result.getValidationResult());
191 tpMap.put("NullField", null);
192 result = new AxValidationResult();
193 result = task.validate(result);
194 assertEquals(ValidationResult.INVALID, result.getValidationResult());
196 tpMap.remove("NullField");
197 result = new AxValidationResult();
198 result = task.validate(result);
199 assertEquals(ValidationResult.VALID, result.getValidationResult());
201 // Empty context reference set is OK
202 task.setContextAlbumReferences(ctxtEmptySet);
203 result = new AxValidationResult();
204 result = task.validate(result);
205 assertEquals(ValidationResult.VALID, result.getValidationResult());
207 task.setContextAlbumReferences(ctxtSet);
208 result = new AxValidationResult();
209 result = task.validate(result);
210 assertEquals(ValidationResult.VALID, result.getValidationResult());
212 ctxtSet.add(AxArtifactKey.getNullKey());
213 result = new AxValidationResult();
214 result = task.validate(result);
215 assertEquals(ValidationResult.INVALID, result.getValidationResult());
217 ctxtSet.remove(AxArtifactKey.getNullKey());
218 result = new AxValidationResult();
219 result = task.validate(result);
220 assertEquals(ValidationResult.VALID, result.getValidationResult());
224 final AxTask clonedTask = new AxTask(task);
225 assertEquals("AxTask:(key=AxArtifactKey:(name=TaskName", clonedTask.toString().substring(0, 40));
227 assertFalse(task.hashCode() == 0);
229 assertTrue(task.equals(task));
230 assertTrue(task.equals(clonedTask));
231 assertFalse(task.equals(null));
232 assertFalse(task.equals((Object)"Hello"));
233 assertFalse(task.equals(new AxTask(new AxArtifactKey(), ifMap, ofMap, tpMap, ctxtSet, tl)));
234 assertFalse(task.equals(new AxTask(taskKey, ifEmptyMap, ofMap, tpMap, ctxtSet, tl)));
235 assertFalse(task.equals(new AxTask(taskKey, ifMap, ofEmptyMap, tpMap, ctxtSet, tl)));
236 assertFalse(task.equals(new AxTask(taskKey, ifMap, ofMap, tpEmptyMap, ctxtSet, tl)));
237 assertFalse(task.equals(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtEmptySet, tl)));
238 assertFalse(task.equals(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtSet, new AxTaskLogic())));
239 assertTrue(task.equals(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtSet, tl)));
241 assertEquals(0, task.compareTo(task));
242 assertEquals(0, task.compareTo(clonedTask));
243 assertNotEquals(0, task.compareTo(new AxArtifactKey()));
244 assertNotEquals(0, task.compareTo(null));
245 assertNotEquals(0, task.compareTo(new AxTask(new AxArtifactKey(), ifMap, ofMap, tpMap, ctxtSet, tl)));
246 assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifEmptyMap, ofMap, tpMap, ctxtSet, tl)));
247 assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofEmptyMap, tpMap, ctxtSet, tl)));
248 assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofMap, tpEmptyMap, ctxtSet, tl)));
249 assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtEmptySet, tl)));
250 assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtSet, new AxTaskLogic())));
251 assertEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtSet, tl)));
253 assertNotNull(task.getKeys());
255 final AxTasks tasks = new AxTasks();
256 result = new AxValidationResult();
257 result = tasks.validate(result);
258 assertEquals(ValidationResult.INVALID, result.getValidationResult());
260 // Invalid, no tasks in task map
261 tasks.setKey(new AxArtifactKey("TasksKey", "0.0.1"));
262 assertEquals("TasksKey:0.0.1", tasks.getKey().getId());
264 result = new AxValidationResult();
265 result = tasks.validate(result);
266 assertEquals(ValidationResult.INVALID, result.getValidationResult());
268 tasks.getTaskMap().put(taskKey, task);
269 result = new AxValidationResult();
270 result = tasks.validate(result);
271 assertEquals(ValidationResult.VALID, result.getValidationResult());
273 tasks.getTaskMap().put(AxArtifactKey.getNullKey(), null);
274 result = new AxValidationResult();
275 result = tasks.validate(result);
276 assertEquals(ValidationResult.INVALID, result.getValidationResult());
278 tasks.getTaskMap().remove(AxArtifactKey.getNullKey());
279 result = new AxValidationResult();
280 result = tasks.validate(result);
281 assertEquals(ValidationResult.VALID, result.getValidationResult());
283 tasks.getTaskMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
284 result = new AxValidationResult();
285 result = tasks.validate(result);
286 assertEquals(ValidationResult.INVALID, result.getValidationResult());
288 tasks.getTaskMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
289 result = new AxValidationResult();
290 result = tasks.validate(result);
291 assertEquals(ValidationResult.VALID, result.getValidationResult());
293 tasks.getTaskMap().put(new AxArtifactKey("BadTaskKey", "0.0.1"), task);
294 result = new AxValidationResult();
295 result = tasks.validate(result);
296 assertEquals(ValidationResult.INVALID, result.getValidationResult());
298 tasks.getTaskMap().remove(new AxArtifactKey("BadTaskKey", "0.0.1"));
299 result = new AxValidationResult();
300 result = tasks.validate(result);
301 assertEquals(ValidationResult.VALID, result.getValidationResult());
304 tasks.afterUnmarshal(null, null);
306 final AxTasks clonedTasks = new AxTasks(tasks);
307 assertEquals("AxTasks:(key=AxArtifactKey:(name=TasksKey,version=0.0.1),tas",
308 clonedTasks.toString().substring(0, 60));
310 assertFalse(tasks.hashCode() == 0);
312 assertTrue(tasks.equals(tasks));
313 assertTrue(tasks.equals(clonedTasks));
314 assertFalse(tasks.equals(null));
315 assertFalse(tasks.equals((Object)"Hello"));
316 assertFalse(tasks.equals(new AxTasks(new AxArtifactKey())));
318 assertEquals(0, tasks.compareTo(tasks));
319 assertEquals(0, tasks.compareTo(clonedTasks));
320 assertNotEquals(0, tasks.compareTo(null));
321 assertNotEquals(0, tasks.compareTo(new AxArtifactKey()));
322 assertNotEquals(0, tasks.compareTo(new AxTasks(new AxArtifactKey())));
324 clonedTasks.get(taskKey).getTaskLogic().setLogic("SomeChangedLogic");
325 assertNotEquals(0, tasks.compareTo(clonedTasks));
327 assertEquals(tasks.getKey(), tasks.getKeys().get(0));
329 assertEquals("TaskName", tasks.get("TaskName").getKey().getName());
330 assertEquals("TaskName", tasks.get("TaskName", "0.0.1").getKey().getName());
331 assertEquals(1, tasks.getAll("TaskName", "0.0.1").size());
332 assertEquals(0, tasks.getAll("NonExistantTaskName").size());