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