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.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;
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.register(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() {
92 ParameterService.clear();
96 public void testStability() throws ApexEventException {
97 Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
100 converter.init(null);
101 fail("this test should throw an exception");
102 } catch (ApexEventRuntimeException e) {
103 assertEquals("specified consumer properties are not applicable to the YAML event protocol", e.getMessage());
106 YamlEventProtocolParameters pars = new YamlEventProtocolParameters();
107 converter.init(pars);
110 converter.toApexEvent("NonExistantEvent", "");
111 fail("this test should throw an exception");
112 } catch (ApexEventException e) {
113 assertEquals("Failed to unmarshal YAML event: an event definition for an event named \"NonExistantEvent\"",
114 e.getMessage().substring(0, 89));
118 converter.toApexEvent("TestEvent", null);
119 fail("this test should throw an exception");
120 } catch (ApexEventException e) {
121 assertEquals("event processing failed, event is null", e.getMessage());
125 converter.toApexEvent("TestEvent", 1);
126 fail("this test should throw an exception");
127 } catch (ApexEventException e) {
128 assertEquals("error converting event \"1\" to a string", e.getMessage());
132 converter.toApexEvent("TestEvent", "");
133 fail("this test should throw an exception");
134 } catch (ApexEventException e) {
135 assertTrue(e.getMessage().contains("Field \"doubleValue\" is missing"));
139 converter.fromApexEvent(null);
140 fail("this test should throw an exception");
141 } catch (ApexEventException e) {
142 assertEquals("event processing failed, Apex event is null", e.getMessage());
145 ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(),
146 testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget());
147 apexEvent.put("doubleValue", 123.45);
148 apexEvent.put("intValue", 123);
149 apexEvent.put("stringValue", "123.45");
151 apexEvent.setExceptionMessage("my wonderful exception message");
152 String yamlString = (String) converter.fromApexEvent(apexEvent);
153 assertTrue(yamlString.contains("my wonderful exception message"));
155 apexEvent.remove("intValue");
157 yamlString = (String) converter.fromApexEvent(apexEvent);
158 fail("this test should throw an exception");
159 } catch (ApexEventRuntimeException e) {
160 assertEquals("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing",
161 e.getMessage().substring(0, 72));
165 converter.toApexEvent(null, "");
166 fail("this test should throw an exception");
167 } catch (ApexEventException e) {
168 assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"",
169 e.getMessage().substring(0, 81));
172 pars.setNameAlias("TheNameField");
174 converter.toApexEvent(null, "");
175 fail("this test should throw an exception");
176 } catch (ApexEventException e) {
177 assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"",
178 e.getMessage().substring(0, 81));
181 apexEvent.put("intValue", 123);
183 apexEvent.remove("stringValue");
184 yamlString = (String) converter.fromApexEvent(apexEvent);
185 apexEvent.put("stringValue", "123.45");
187 String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123";
189 List<ApexEvent> eventList = converter.toApexEvent("TestEvent", yamlInputString);
190 assertEquals(123.45, eventList.get(0).get("doubleValue"));
192 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null";
194 eventList = converter.toApexEvent("TestEvent", yamlInputString);
195 assertEquals(null, eventList.get(0).get("stringValue"));
197 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent";
198 pars.setNameAlias("stringValue");
199 eventList = converter.toApexEvent(null, yamlInputString);
200 assertEquals("TestEvent", eventList.get(0).get("stringValue"));
202 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent";
203 eventList = converter.toApexEvent("TestEvent", yamlInputString);
204 assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue"));
206 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1";
207 pars.setNameAlias(null);
208 pars.setVersionAlias("stringValue");
209 eventList = converter.toApexEvent("TestEvent", yamlInputString);
210 assertEquals("0.0.1", eventList.get(0).get("stringValue"));
212 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace";
213 pars.setVersionAlias(null);
214 pars.setNameSpaceAlias("stringValue");
216 converter.toApexEvent("TestEvent", yamlInputString);
217 fail("this test should throw an exception");
218 } catch (ApexEventException e) {
219 assertEquals("Failed to unmarshal YAML event: namespace \"org.some.other.namespace\" on event",
220 e.getMessage().substring(0, 77));
223 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml";
224 eventList = converter.toApexEvent("TestEvent", yamlInputString);
225 assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace());
227 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource";
228 pars.setNameSpaceAlias(null);
229 pars.setSourceAlias("stringValue");
230 eventList = converter.toApexEvent("TestEvent", yamlInputString);
231 assertEquals("MySource", eventList.get(0).getSource());
233 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget";
234 pars.setSourceAlias(null);
235 pars.setTargetAlias("stringValue");
236 eventList = converter.toApexEvent("TestEvent", yamlInputString);
237 assertEquals("MyTarget", eventList.get(0).getTarget());
238 pars.setTargetAlias(null);
240 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString";
241 pars.setSourceAlias(null);
242 pars.setTargetAlias("intValue");
244 converter.toApexEvent("TestEvent", yamlInputString);
245 fail("this test should throw an exception");
246 } catch (ApexEventException e) {
247 assertEquals("Failed to unmarshal YAML event: field \"target\" with type \"java.lang.Integer\"",
248 e.getMessage().substring(0, 76));
250 pars.setTargetAlias(null);
252 yamlInputString = "doubleValue: 123.45\n" + "intValue: ~\n"+ "stringValue: MyString";
254 converter.toApexEvent("TestEvent", yamlInputString);
255 fail("this test should throw an exception");
256 } catch (ApexEventException e) {
257 assertTrue(e.getMessage().contains("mandatory field \"intValue\" is missing"));