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=========================================================
20 package org.onap.policy.apex.plugins.event.protocol.yaml;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
26 import java.io.IOException;
27 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.basicmodel.service.ParameterService;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
40 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
41 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
42 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
43 import org.onap.policy.apex.service.engine.event.ApexEvent;
44 import org.onap.policy.apex.service.engine.event.ApexEventException;
45 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
47 public class TestYamlPluginStability {
48 static AxEvent testEvent;
51 public static void registerTestEventsAndSchemas() throws IOException {
52 SchemaParameters schemaParameters = new SchemaParameters();
53 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
54 ParameterService.registerParameters(SchemaParameters.class, schemaParameters);
56 AxContextSchemas schemas = new AxContextSchemas();
58 AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
60 schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
62 AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"),
63 "JAVA", "java.lang.Double");
64 schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema);
66 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
67 "JAVA", "java.lang.String");
68 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
70 ModelService.registerModel(AxContextSchemas.class, schemas);
72 AxEvents events = new AxEvents();
74 testEvent = new AxEvent(new AxArtifactKey("TestEvent", "0.0.1"));
75 testEvent.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
76 AxField teField0 = new AxField(new AxReferenceKey(testEvent.getKey(), "intValue"), simpleIntSchema.getKey());
77 testEvent.getParameterMap().put("intValue", teField0);
78 AxField teField1 = new AxField(new AxReferenceKey(testEvent.getKey(), "doubleValue"),
79 simpleDoubleSchema.getKey());
80 testEvent.getParameterMap().put("doubleValue", teField1);
81 AxField teField2 = new AxField(new AxReferenceKey(testEvent.getKey(), "stringValue"),
82 simpleStringSchema.getKey(), true);
83 testEvent.getParameterMap().put("stringValue", teField2);
84 events.getEventMap().put(testEvent.getKey(), testEvent);
86 ModelService.registerModel(AxEvents.class, events);
90 public static void unregisterTestEventsAndSchemas() {
95 public void testStability() throws ApexEventException {
96 Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
100 fail("this test should throw an exception");
101 } catch (ApexEventRuntimeException e) {
102 assertEquals("specified consumer properties are not applicable to the YAML event protocol", e.getMessage());
105 YamlEventProtocolParameters pars = new YamlEventProtocolParameters();
106 converter.init(pars);
109 converter.toApexEvent("NonExistantEvent", "");
110 fail("this test should throw an exception");
111 } catch (ApexEventException e) {
112 assertEquals("Failed to unmarshal YAML event: an event definition for an event named \"NonExistantEvent\"",
113 e.getMessage().substring(0, 89));
117 converter.toApexEvent("TestEvent", null);
118 fail("this test should throw an exception");
119 } catch (ApexEventException e) {
120 assertEquals("event processing failed, event is null", e.getMessage());
124 converter.toApexEvent("TestEvent", 1);
125 fail("this test should throw an exception");
126 } catch (ApexEventException e) {
127 assertEquals("error converting event \"1\" to a string", e.getMessage());
131 converter.toApexEvent("TestEvent", "");
132 fail("this test should throw an exception");
133 } catch (ApexEventException e) {
134 assertTrue(e.getMessage().contains("Field \"doubleValue\" is missing"));
138 converter.fromApexEvent(null);
139 fail("this test should throw an exception");
140 } catch (ApexEventException e) {
141 assertEquals("event processing failed, Apex event is null", e.getMessage());
144 ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(),
145 testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget());
146 apexEvent.put("doubleValue", 123.45);
147 apexEvent.put("intValue", 123);
148 apexEvent.put("stringValue", "123.45");
150 apexEvent.setExceptionMessage("my wonderful exception message");
151 String yamlString = (String) converter.fromApexEvent(apexEvent);
152 assertTrue(yamlString.contains("my wonderful exception message"));
154 apexEvent.remove("intValue");
156 yamlString = (String) converter.fromApexEvent(apexEvent);
157 fail("this test should throw an exception");
158 } catch (ApexEventRuntimeException e) {
159 assertEquals("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing",
160 e.getMessage().substring(0, 72));
164 converter.toApexEvent(null, "");
165 fail("this test should throw an exception");
166 } catch (ApexEventException e) {
167 assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"",
168 e.getMessage().substring(0, 81));
171 pars.setNameAlias("TheNameField");
173 converter.toApexEvent(null, "");
174 fail("this test should throw an exception");
175 } catch (ApexEventException e) {
176 assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"",
177 e.getMessage().substring(0, 81));
180 apexEvent.put("intValue", 123);
182 apexEvent.remove("stringValue");
183 yamlString = (String) converter.fromApexEvent(apexEvent);
184 apexEvent.put("stringValue", "123.45");
186 String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123";
188 List<ApexEvent> eventList = converter.toApexEvent("TestEvent", yamlInputString);
189 assertEquals(123.45, eventList.get(0).get("doubleValue"));
191 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null";
193 eventList = converter.toApexEvent("TestEvent", yamlInputString);
194 assertEquals(null, eventList.get(0).get("stringValue"));
196 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent";
197 pars.setNameAlias("stringValue");
198 eventList = converter.toApexEvent(null, yamlInputString);
199 assertEquals("TestEvent", eventList.get(0).get("stringValue"));
201 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent";
202 eventList = converter.toApexEvent("TestEvent", yamlInputString);
203 assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue"));
205 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1";
206 pars.setNameAlias(null);
207 pars.setVersionAlias("stringValue");
208 eventList = converter.toApexEvent("TestEvent", yamlInputString);
209 assertEquals("0.0.1", eventList.get(0).get("stringValue"));
211 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace";
212 pars.setVersionAlias(null);
213 pars.setNameSpaceAlias("stringValue");
215 converter.toApexEvent("TestEvent", yamlInputString);
216 fail("this test should throw an exception");
217 } catch (ApexEventException e) {
218 assertEquals("Failed to unmarshal YAML event: namespace \"org.some.other.namespace\" on event",
219 e.getMessage().substring(0, 77));
222 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml";
223 eventList = converter.toApexEvent("TestEvent", yamlInputString);
224 assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace());
226 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource";
227 pars.setNameSpaceAlias(null);
228 pars.setSourceAlias("stringValue");
229 eventList = converter.toApexEvent("TestEvent", yamlInputString);
230 assertEquals("MySource", eventList.get(0).getSource());
232 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget";
233 pars.setSourceAlias(null);
234 pars.setTargetAlias("stringValue");
235 eventList = converter.toApexEvent("TestEvent", yamlInputString);
236 assertEquals("MyTarget", eventList.get(0).getTarget());
237 pars.setTargetAlias(null);
239 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString";
240 pars.setSourceAlias(null);
241 pars.setTargetAlias("intValue");
243 converter.toApexEvent("TestEvent", yamlInputString);
244 fail("this test should throw an exception");
245 } catch (ApexEventException e) {
246 assertEquals("Failed to unmarshal YAML event: field \"target\" with type \"java.lang.Integer\"",
247 e.getMessage().substring(0, 76));
249 pars.setTargetAlias(null);
251 yamlInputString = "doubleValue: 123.45\n" + "intValue: ~\n"+ "stringValue: MyString";
253 converter.toApexEvent("TestEvent", yamlInputString);
254 fail("this test should throw an exception");
255 } catch (ApexEventException e) {
256 assertTrue(e.getMessage().contains("mandatory field \"intValue\" is missing"));