2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 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.plugins.event.protocol.yaml;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertThrows;
29 import static org.junit.Assert.assertTrue;
31 import java.io.IOException;
32 import java.util.List;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
37 import org.onap.policy.apex.context.parameters.SchemaParameters;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
40 import org.onap.policy.apex.model.basicmodel.service.ModelService;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
43 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
44 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
45 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
46 import org.onap.policy.apex.service.engine.event.ApexEvent;
47 import org.onap.policy.apex.service.engine.event.ApexEventException;
48 import org.onap.policy.common.parameters.ParameterService;
51 * The Class TestYamlPluginStability.
53 public class YamlPluginStabilityTest {
54 static AxEvent testEvent;
57 * Register test events and schemas.
59 * @throws IOException Signals that an I/O exception has occurred.
62 public static void registerTestEventsAndSchemas() throws IOException {
63 SchemaParameters schemaParameters = new SchemaParameters();
64 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
65 ParameterService.register(schemaParameters);
67 AxContextSchemas schemas = new AxContextSchemas();
69 AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
71 schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
73 AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"),
74 "JAVA", "java.lang.Double");
75 schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema);
77 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
78 "JAVA", "java.lang.String");
79 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
81 ModelService.registerModel(AxContextSchemas.class, schemas);
83 testEvent = new AxEvent(new AxArtifactKey("TestEvent", "0.0.1"));
84 testEvent.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
85 AxField teField0 = new AxField(new AxReferenceKey(testEvent.getKey(), "intValue"), simpleIntSchema.getKey());
86 testEvent.getParameterMap().put("intValue", teField0);
87 AxField teField1 = new AxField(new AxReferenceKey(testEvent.getKey(), "doubleValue"),
88 simpleDoubleSchema.getKey());
89 testEvent.getParameterMap().put("doubleValue", teField1);
90 AxField teField2 = new AxField(new AxReferenceKey(testEvent.getKey(), "stringValue"),
91 simpleStringSchema.getKey(), true);
92 testEvent.getParameterMap().put("stringValue", teField2);
94 AxEvents events = new AxEvents();
95 events.getEventMap().put(testEvent.getKey(), testEvent);
97 ModelService.registerModel(AxEvents.class, events);
101 * Unregister test events and schemas.
104 public static void unregisterTestEventsAndSchemas() {
105 ModelService.clear();
106 ParameterService.clear();
112 * @throws ApexEventException the apex event exception
115 public void testStability() throws ApexEventException {
116 Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
118 assertThatThrownBy(() -> converter.init(null))
119 .hasMessage("specified consumer properties are not applicable to the YAML event protocol");
120 YamlEventProtocolParameters pars = new YamlEventProtocolParameters();
121 converter.init(pars);
123 assertThatThrownBy(() -> converter.toApexEvent("NonExistantEvent", ""))
124 .hasMessageContaining("Failed to unmarshal YAML event")
125 .getCause().hasMessageStartingWith("an event definition for an event named \"NonExistantEvent\"");
126 assertThatThrownBy(() -> converter.toApexEvent("TestEvent", null))
127 .hasMessage("event processing failed, event is null");
128 assertThatThrownBy(() -> converter.toApexEvent("TestEvent", 1))
129 .hasMessage("error converting event \"1\" to a string");
130 assertThatThrownBy(() -> converter.toApexEvent("TestEvent", ""))
131 .getCause().hasMessageContaining("Field \"doubleValue\" is missing");
132 assertThatThrownBy(() -> converter.fromApexEvent(null))
133 .hasMessage("event processing failed, Apex event is null");
134 ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(),
135 testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget());
136 apexEvent.put("doubleValue", 123.45);
137 apexEvent.put("intValue", 123);
138 apexEvent.put("stringValue", "123.45");
140 apexEvent.setExceptionMessage("my wonderful exception message");
141 String yamlString = (String) converter.fromApexEvent(apexEvent);
142 assertTrue(yamlString.contains("my wonderful exception message"));
144 apexEvent.remove("intValue");
145 assertThatThrownBy(() -> converter.fromApexEvent(apexEvent))
146 .hasMessageContaining("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing");
147 assertThatThrownBy(() -> converter.toApexEvent(null, ""))
148 .hasMessageStartingWith("Failed to unmarshal YAML event")
149 .getCause().hasMessageStartingWith("event received without mandatory parameter \"name\"");
150 pars.setNameAlias("TheNameField");
151 assertThatThrownBy(() -> converter.toApexEvent(null, ""))
152 .hasMessageStartingWith("Failed to unmarshal YAML event")
153 .getCause().hasMessageStartingWith("event received without mandatory parameter \"name\"");
154 apexEvent.put("intValue", 123);
156 apexEvent.remove("stringValue");
157 yamlString = (String) converter.fromApexEvent(apexEvent);
158 apexEvent.put("stringValue", "123.45");
160 String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123";
162 List<ApexEvent> eventList = converter.toApexEvent("TestEvent", yamlInputString);
163 assertEquals(123.45, eventList.get(0).get("doubleValue"));
165 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null";
167 eventList = converter.toApexEvent("TestEvent", yamlInputString);
168 assertEquals(null, eventList.get(0).get("stringValue"));
170 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent";
171 pars.setNameAlias("stringValue");
172 eventList = converter.toApexEvent(null, yamlInputString);
173 assertEquals("TestEvent", eventList.get(0).get("stringValue"));
175 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent";
176 eventList = converter.toApexEvent("TestEvent", yamlInputString);
177 assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue"));
179 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1";
180 pars.setNameAlias(null);
181 pars.setVersionAlias("stringValue");
182 eventList = converter.toApexEvent("TestEvent", yamlInputString);
183 assertEquals("0.0.1", eventList.get(0).get("stringValue"));
185 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace";
186 pars.setVersionAlias(null);
187 pars.setNameSpaceAlias("stringValue");
188 final String yamlInputStringCopy = yamlInputString;
189 assertThatThrownBy(() -> converter.toApexEvent("TestEvent", yamlInputStringCopy))
190 .hasMessageStartingWith("Failed to unmarshal YAML event")
191 .getCause().hasMessageStartingWith("namespace \"org.some.other.namespace\" on event");
192 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n"
193 + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml";
194 eventList = converter.toApexEvent("TestEvent", yamlInputString);
195 assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace());
197 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource";
198 pars.setNameSpaceAlias(null);
199 pars.setSourceAlias("stringValue");
200 eventList = converter.toApexEvent("TestEvent", yamlInputString);
201 assertEquals("MySource", eventList.get(0).getSource());
203 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget";
204 pars.setSourceAlias(null);
205 pars.setTargetAlias("stringValue");
206 eventList = converter.toApexEvent("TestEvent", yamlInputString);
207 assertEquals("MyTarget", eventList.get(0).getTarget());
208 pars.setTargetAlias(null);
210 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString";
211 pars.setSourceAlias(null);
212 pars.setTargetAlias("intValue");
213 assertThatThrownBy(() -> converter.toApexEvent("TestEvent", yamlInputStringCopy))
214 .hasMessageStartingWith("Failed to unmarshal YAML event")
215 .getCause().hasMessageStartingWith("field \"target\" with type \"java.lang.Integer\"");
216 pars.setTargetAlias(null);
218 assertThatThrownBy(() -> converter.toApexEvent("TestEvent", "doubleValue: 123.45\n" + "intValue: ~\n"
219 + "stringValue: MyString")).getCause().hasMessageStartingWith("mandatory field \"intValue\" is missing");