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.eventmodel.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.AxKey;
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;
41 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class EventsTest {
46 public void testEvents() {
47 final TreeMap<String, AxField> parameterMap = new TreeMap<>();
48 final TreeMap<String, AxField> parameterMapEmpty = new TreeMap<>();
50 assertNotNull(new AxEvent());
51 assertNotNull(new AxEvent(new AxArtifactKey()));
52 assertNotNull(new AxEvent(new AxArtifactKey(), "namespace"));
53 assertNotNull(new AxEvent(new AxArtifactKey(), "namespace", "source", "target"));
54 assertNotNull(new AxEvent(new AxArtifactKey(), "namespace", "source", "target"));
55 assertNotNull(new AxEvent(new AxArtifactKey(), "namespace", "source", "target", parameterMap));
57 final AxEvent event = new AxEvent();
59 final AxArtifactKey eventKey = new AxArtifactKey("EventName", "0.0.1");
60 event.setKey(eventKey);
61 assertEquals("EventName:0.0.1", event.getKey().getId());
62 assertEquals("EventName:0.0.1", event.getKeys().get(0).getId());
64 event.setNameSpace("namespace");
65 assertEquals("namespace", event.getNameSpace());
67 event.setSource("source");
68 assertEquals("source", event.getSource());
70 event.setTarget("target");
71 assertEquals("target", event.getTarget());
73 event.setParameterMap(parameterMap);
74 assertEquals(0, event.getParameterMap().size());
76 final AxField eventField =
77 new AxField(new AxReferenceKey(eventKey, "Field0"), new AxArtifactKey("Field0Schema", "0.0.1"));
78 event.getParameterMap().put(eventField.getKey().getLocalName(), eventField);
79 assertEquals(1, event.getParameterMap().size());
81 final AxField eventFieldBadParent =
82 new AxField(new AxReferenceKey(new AxArtifactKey("OtherEvent", "0.0.01"), "Field0"),
83 new AxArtifactKey("Field0Schema", "0.0.1"));
85 final AxArtifactKey newEventKey = new AxArtifactKey("NewEventName", "0.0.1");
86 event.setKey(newEventKey);
87 assertEquals("NewEventName:0.0.1", event.getKey().getId());
88 assertEquals("NewEventName:0.0.1", event.getKeys().get(0).getId());
89 assertEquals("NewEventName:0.0.1",
90 event.getParameterMap().get("Field0").getKey().getParentArtifactKey().getId());
91 event.setKey(eventKey);
92 assertEquals("EventName:0.0.1", event.getKey().getId());
93 assertEquals("EventName:0.0.1", event.getKeys().get(0).getId());
95 assertTrue("Field0", event.getFields().contains(eventField));
96 assertTrue(event.hasFields(new TreeSet<AxField>(parameterMap.values())));
98 AxValidationResult result = new AxValidationResult();
99 result = event.validate(result);
100 assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
102 event.setKey(AxArtifactKey.getNullKey());
103 result = new AxValidationResult();
104 result = event.validate(result);
105 assertEquals(ValidationResult.INVALID, result.getValidationResult());
107 event.setKey(eventKey);
108 result = new AxValidationResult();
109 result = event.validate(result);
110 assertEquals(ValidationResult.VALID, result.getValidationResult());
112 event.setNameSpace("");
113 result = new AxValidationResult();
114 result = event.validate(result);
115 assertEquals(ValidationResult.WARNING, result.getValidationResult());
117 event.setNameSpace("namespace");
118 result = new AxValidationResult();
119 result = event.validate(result);
120 assertEquals(ValidationResult.VALID, result.getValidationResult());
123 result = new AxValidationResult();
124 result = event.validate(result);
125 assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());
127 event.setSource("source");
128 result = new AxValidationResult();
129 result = event.validate(result);
130 assertEquals(ValidationResult.VALID, result.getValidationResult());
133 result = new AxValidationResult();
134 result = event.validate(result);
135 assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());
137 event.setTarget("target");
138 result = new AxValidationResult();
139 result = event.validate(result);
140 assertEquals(ValidationResult.VALID, result.getValidationResult());
142 event.getParameterMap().put(AxKey.NULL_KEY_NAME, null);
143 result = new AxValidationResult();
144 result = event.validate(result);
145 assertEquals(ValidationResult.INVALID, result.getValidationResult());
147 event.getParameterMap().remove(AxKey.NULL_KEY_NAME);
148 result = new AxValidationResult();
149 result = event.validate(result);
150 assertEquals(ValidationResult.VALID, result.getValidationResult());
152 event.getParameterMap().put("NullField", null);
153 result = new AxValidationResult();
154 result = event.validate(result);
155 assertEquals(ValidationResult.INVALID, result.getValidationResult());
157 event.getParameterMap().remove("NullField");
158 result = new AxValidationResult();
159 result = event.validate(result);
160 assertEquals(ValidationResult.VALID, result.getValidationResult());
162 event.getParameterMap().put("NullField", eventField);
163 result = new AxValidationResult();
164 result = event.validate(result);
165 assertEquals(ValidationResult.INVALID, result.getValidationResult());
167 event.getParameterMap().remove("NullField");
168 result = new AxValidationResult();
169 result = event.validate(result);
170 assertEquals(ValidationResult.VALID, result.getValidationResult());
172 event.getParameterMap().put("BadParent", eventFieldBadParent);
173 result = new AxValidationResult();
174 result = event.validate(result);
175 assertEquals(ValidationResult.INVALID, result.getValidationResult());
177 event.getParameterMap().remove("BadParent");
178 result = new AxValidationResult();
179 result = event.validate(result);
180 assertEquals(ValidationResult.VALID, result.getValidationResult());
183 event.afterUnmarshal(null, null);
185 final AxEvent clonedEvent = new AxEvent(event);
186 assertEquals("AxEvent:(key=AxArtifactKey:(name=EventName,version=0.0.1),nameSpace=namespace",
187 clonedEvent.toString().substring(0, 77));
189 assertFalse(event.hashCode() == 0);
191 assertTrue(event.equals(event));
192 assertTrue(event.equals(clonedEvent));
193 assertFalse(event.equals(null));
194 assertFalse(event.equals((Object) "Hello"));
196 event.equals(new AxEvent(AxArtifactKey.getNullKey(), "namespace", "source", "target", parameterMap)));
197 assertFalse(event.equals(new AxEvent(eventKey, "namespace1", "source", "target", parameterMap)));
198 assertFalse(event.equals(new AxEvent(eventKey, "namespace", "source2", "target", parameterMap)));
199 assertFalse(event.equals(new AxEvent(eventKey, "namespace", "source", "target3", parameterMap)));
200 assertFalse(event.equals(new AxEvent(eventKey, "namespace", "source", "target", parameterMapEmpty)));
201 assertTrue(event.equals(new AxEvent(eventKey, "namespace", "source", "target", parameterMap)));
203 assertEquals(0, event.compareTo(event));
204 assertEquals(0, event.compareTo(clonedEvent));
205 assertNotEquals(0, event.compareTo(new AxArtifactKey()));
206 assertNotEquals(0, event.compareTo(null));
207 assertNotEquals(0, event
208 .compareTo(new AxEvent(AxArtifactKey.getNullKey(), "namespace", "source", "target", parameterMap)));
209 assertNotEquals(0, event.compareTo(new AxEvent(eventKey, "namespace1", "source", "target", parameterMap)));
210 assertNotEquals(0, event.compareTo(new AxEvent(eventKey, "namespace", "source2", "target", parameterMap)));
211 assertNotEquals(0, event.compareTo(new AxEvent(eventKey, "namespace", "source", "target3", parameterMap)));
212 assertNotEquals(0, event.compareTo(new AxEvent(eventKey, "namespace", "source", "target", parameterMapEmpty)));
213 assertEquals(0, event.compareTo(new AxEvent(eventKey, "namespace", "source", "target", parameterMap)));
215 assertNotNull(event.getKeys());
217 final AxEvents events = new AxEvents();
218 result = new AxValidationResult();
219 result = events.validate(result);
220 assertEquals(ValidationResult.INVALID, result.getValidationResult());
222 // Invalid, no events in event map
223 events.setKey(new AxArtifactKey("EventsKey", "0.0.1"));
224 assertEquals("EventsKey:0.0.1", events.getKey().getId());
226 result = new AxValidationResult();
227 result = events.validate(result);
228 assertEquals(ValidationResult.INVALID, result.getValidationResult());
230 events.getEventMap().put(eventKey, event);
231 result = new AxValidationResult();
232 result = events.validate(result);
233 assertEquals(ValidationResult.VALID, result.getValidationResult());
235 events.getEventMap().put(AxArtifactKey.getNullKey(), null);
236 result = new AxValidationResult();
237 result = events.validate(result);
238 assertEquals(ValidationResult.INVALID, result.getValidationResult());
240 events.getEventMap().remove(AxArtifactKey.getNullKey());
241 result = new AxValidationResult();
242 result = events.validate(result);
243 assertEquals(ValidationResult.VALID, result.getValidationResult());
245 events.getEventMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
246 result = new AxValidationResult();
247 result = events.validate(result);
248 assertEquals(ValidationResult.INVALID, result.getValidationResult());
250 events.getEventMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
251 result = new AxValidationResult();
252 result = events.validate(result);
253 assertEquals(ValidationResult.VALID, result.getValidationResult());
255 events.getEventMap().put(new AxArtifactKey("BadEventKey", "0.0.1"), event);
256 result = new AxValidationResult();
257 result = events.validate(result);
258 assertEquals(ValidationResult.INVALID, result.getValidationResult());
260 events.getEventMap().remove(new AxArtifactKey("BadEventKey", "0.0.1"));
261 result = new AxValidationResult();
262 result = events.validate(result);
263 assertEquals(ValidationResult.VALID, result.getValidationResult());
266 events.afterUnmarshal(null, null);
268 final AxEvents clonedEvents = new AxEvents(events);
269 assertEquals("AxEvents:(key=AxArtifactKey:(name=EventsKey,version=0.0.1),e",
270 clonedEvents.toString().substring(0, 60));
272 assertFalse(events.hashCode() == 0);
274 assertTrue(events.equals(events));
275 assertTrue(events.equals(clonedEvents));
276 assertFalse(events.equals(null));
277 assertFalse(events.equals((Object) "Hello"));
278 assertFalse(events.equals(new AxEvents(new AxArtifactKey())));
280 assertEquals(0, events.compareTo(events));
281 assertEquals(0, events.compareTo(clonedEvents));
282 assertNotEquals(0, events.compareTo(null));
283 assertNotEquals(0, events.compareTo(new AxArtifactKey()));
284 assertNotEquals(0, events.compareTo(new AxEvents(new AxArtifactKey())));
286 clonedEvents.get(eventKey).setSource("AnotherSource");
287 assertNotEquals(0, events.compareTo(clonedEvents));
289 assertEquals(events.getKey(), events.getKeys().get(0));
291 assertEquals("EventName", events.get("EventName").getKey().getName());
292 assertEquals("EventName", events.get("EventName", "0.0.1").getKey().getName());
293 assertEquals(1, events.getAll("EventName", "0.0.1").size());
294 assertEquals(0, events.getAll("NonExistantEventsName").size());