2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.protocol.yaml;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
27 import java.io.IOException;
28 import java.util.List;
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
33 import org.onap.policy.apex.context.parameters.SchemaParameters;
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.service.ModelService;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
39 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
40 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
41 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
42 import org.onap.policy.apex.service.engine.event.ApexEvent;
43 import org.onap.policy.apex.service.engine.event.ApexEventException;
44 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
45 import org.onap.policy.common.parameters.ParameterService;
48 * The Class TestYamlPluginStability.
50 public class YamlPluginStabilityTest {
51 static AxEvent testEvent;
54 * Register test events and schemas.
56 * @throws IOException Signals that an I/O exception has occurred.
59 public static void registerTestEventsAndSchemas() throws IOException {
60 SchemaParameters schemaParameters = new SchemaParameters();
61 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
62 ParameterService.register(schemaParameters);
64 AxContextSchemas schemas = new AxContextSchemas();
66 AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
68 schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
70 AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"),
71 "JAVA", "java.lang.Double");
72 schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema);
74 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
75 "JAVA", "java.lang.String");
76 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
78 ModelService.registerModel(AxContextSchemas.class, schemas);
80 testEvent = new AxEvent(new AxArtifactKey("TestEvent", "0.0.1"));
81 testEvent.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
82 AxField teField0 = new AxField(new AxReferenceKey(testEvent.getKey(), "intValue"), simpleIntSchema.getKey());
83 testEvent.getParameterMap().put("intValue", teField0);
84 AxField teField1 = new AxField(new AxReferenceKey(testEvent.getKey(), "doubleValue"),
85 simpleDoubleSchema.getKey());
86 testEvent.getParameterMap().put("doubleValue", teField1);
87 AxField teField2 = new AxField(new AxReferenceKey(testEvent.getKey(), "stringValue"),
88 simpleStringSchema.getKey(), true);
89 testEvent.getParameterMap().put("stringValue", teField2);
91 AxEvents events = new AxEvents();
92 events.getEventMap().put(testEvent.getKey(), testEvent);
94 ModelService.registerModel(AxEvents.class, events);
98 * Unregister test events and schemas.
101 public static void unregisterTestEventsAndSchemas() {
102 ModelService.clear();
103 ParameterService.clear();
109 * @throws ApexEventException the apex event exception
112 public void testStability() throws ApexEventException {
113 Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
116 converter.init(null);
117 fail("this test should throw an exception");
118 } catch (ApexEventRuntimeException e) {
119 assertEquals("specified consumer properties are not applicable to the YAML event protocol", e.getMessage());
122 YamlEventProtocolParameters pars = new YamlEventProtocolParameters();
123 converter.init(pars);
126 converter.toApexEvent("NonExistantEvent", "");
127 fail("this test should throw an exception");
128 } catch (ApexEventException e) {
129 assertEquals("Failed to unmarshal YAML event: an event definition for an event named \"NonExistantEvent\"",
130 e.getMessage().substring(0, 89));
134 converter.toApexEvent("TestEvent", null);
135 fail("this test should throw an exception");
136 } catch (ApexEventException e) {
137 assertEquals("event processing failed, event is null", e.getMessage());
141 converter.toApexEvent("TestEvent", 1);
142 fail("this test should throw an exception");
143 } catch (ApexEventException e) {
144 assertEquals("error converting event \"1\" to a string", e.getMessage());
148 converter.toApexEvent("TestEvent", "");
149 fail("this test should throw an exception");
150 } catch (ApexEventException e) {
151 assertTrue(e.getMessage().contains("Field \"doubleValue\" is missing"));
155 converter.fromApexEvent(null);
156 fail("this test should throw an exception");
157 } catch (ApexEventException e) {
158 assertEquals("event processing failed, Apex event is null", e.getMessage());
161 ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(),
162 testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget());
163 apexEvent.put("doubleValue", 123.45);
164 apexEvent.put("intValue", 123);
165 apexEvent.put("stringValue", "123.45");
167 apexEvent.setExceptionMessage("my wonderful exception message");
168 String yamlString = (String) converter.fromApexEvent(apexEvent);
169 assertTrue(yamlString.contains("my wonderful exception message"));
171 apexEvent.remove("intValue");
173 yamlString = (String) converter.fromApexEvent(apexEvent);
174 fail("this test should throw an exception");
175 } catch (ApexEventRuntimeException e) {
176 assertEquals("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing",
177 e.getMessage().substring(0, 72));
181 converter.toApexEvent(null, "");
182 fail("this test should throw an exception");
183 } catch (ApexEventException e) {
184 assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"",
185 e.getMessage().substring(0, 81));
188 pars.setNameAlias("TheNameField");
190 converter.toApexEvent(null, "");
191 fail("this test should throw an exception");
192 } catch (ApexEventException e) {
193 assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"",
194 e.getMessage().substring(0, 81));
197 apexEvent.put("intValue", 123);
199 apexEvent.remove("stringValue");
200 yamlString = (String) converter.fromApexEvent(apexEvent);
201 apexEvent.put("stringValue", "123.45");
203 String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123";
205 List<ApexEvent> eventList = converter.toApexEvent("TestEvent", yamlInputString);
206 assertEquals(123.45, eventList.get(0).get("doubleValue"));
208 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null";
210 eventList = converter.toApexEvent("TestEvent", yamlInputString);
211 assertEquals(null, eventList.get(0).get("stringValue"));
213 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent";
214 pars.setNameAlias("stringValue");
215 eventList = converter.toApexEvent(null, yamlInputString);
216 assertEquals("TestEvent", eventList.get(0).get("stringValue"));
218 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent";
219 eventList = converter.toApexEvent("TestEvent", yamlInputString);
220 assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue"));
222 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1";
223 pars.setNameAlias(null);
224 pars.setVersionAlias("stringValue");
225 eventList = converter.toApexEvent("TestEvent", yamlInputString);
226 assertEquals("0.0.1", eventList.get(0).get("stringValue"));
228 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace";
229 pars.setVersionAlias(null);
230 pars.setNameSpaceAlias("stringValue");
232 converter.toApexEvent("TestEvent", yamlInputString);
233 fail("this test should throw an exception");
234 } catch (ApexEventException e) {
235 assertEquals("Failed to unmarshal YAML event: namespace \"org.some.other.namespace\" on event",
236 e.getMessage().substring(0, 77));
239 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n"
240 + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml";
241 eventList = converter.toApexEvent("TestEvent", yamlInputString);
242 assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace());
244 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource";
245 pars.setNameSpaceAlias(null);
246 pars.setSourceAlias("stringValue");
247 eventList = converter.toApexEvent("TestEvent", yamlInputString);
248 assertEquals("MySource", eventList.get(0).getSource());
250 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget";
251 pars.setSourceAlias(null);
252 pars.setTargetAlias("stringValue");
253 eventList = converter.toApexEvent("TestEvent", yamlInputString);
254 assertEquals("MyTarget", eventList.get(0).getTarget());
255 pars.setTargetAlias(null);
257 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString";
258 pars.setSourceAlias(null);
259 pars.setTargetAlias("intValue");
261 converter.toApexEvent("TestEvent", yamlInputString);
262 fail("this test should throw an exception");
263 } catch (ApexEventException e) {
264 assertEquals("Failed to unmarshal YAML event: field \"target\" with type \"java.lang.Integer\"",
265 e.getMessage().substring(0, 76));
267 pars.setTargetAlias(null);
269 yamlInputString = "doubleValue: 123.45\n" + "intValue: ~\n" + "stringValue: MyString";
271 converter.toApexEvent("TestEvent", yamlInputString);
272 fail("this test should throw an exception");
273 } catch (ApexEventException e) {
274 assertTrue(e.getMessage().contains("mandatory field \"intValue\" is missing"));