2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.apex.plugins.event.protocol.yaml;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
30 import java.io.IOException;
31 import java.util.List;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
36 import org.onap.policy.apex.context.parameters.SchemaParameters;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxToscaPolicyProcessingStatus;
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 testEvent.setToscaPolicyState(AxToscaPolicyProcessingStatus.ENTRY.name());
86 AxField teField0 = new AxField(new AxReferenceKey(testEvent.getKey(), "intValue"), simpleIntSchema.getKey());
87 testEvent.getParameterMap().put("intValue", teField0);
88 AxField teField1 = new AxField(new AxReferenceKey(testEvent.getKey(), "doubleValue"),
89 simpleDoubleSchema.getKey());
90 testEvent.getParameterMap().put("doubleValue", teField1);
91 AxField teField2 = new AxField(new AxReferenceKey(testEvent.getKey(), "stringValue"),
92 simpleStringSchema.getKey(), true);
93 testEvent.getParameterMap().put("stringValue", teField2);
95 AxEvents events = new AxEvents();
96 events.getEventMap().put(testEvent.getKey(), testEvent);
98 ModelService.registerModel(AxEvents.class, events);
102 * Unregister test events and schemas.
105 public static void unregisterTestEventsAndSchemas() {
106 ModelService.clear();
107 ParameterService.clear();
113 * @throws ApexEventException the apex event exception
116 public void testStability() throws ApexEventException {
117 Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
119 assertThatThrownBy(() -> converter.init(null))
120 .hasMessage("specified consumer properties are not applicable to the YAML event protocol");
121 YamlEventProtocolParameters pars = new YamlEventProtocolParameters();
122 converter.init(pars);
124 assertThatThrownBy(() -> converter.toApexEvent("NonExistantEvent", ""))
125 .hasMessageContaining("Failed to unmarshal YAML event")
126 .getCause().hasMessageStartingWith("an event definition for an event named \"NonExistantEvent\"");
127 assertThatThrownBy(() -> converter.toApexEvent("TestEvent", null))
128 .hasMessage("event processing failed, event is null");
129 assertThatThrownBy(() -> converter.toApexEvent("TestEvent", 1))
130 .hasMessage("error converting event \"1\" to a string");
131 assertThatThrownBy(() -> converter.toApexEvent("TestEvent", ""))
132 .getCause().hasMessageContaining("Field \"doubleValue\" is missing");
133 assertThatThrownBy(() -> converter.fromApexEvent(null))
134 .hasMessage("event processing failed, Apex event is null");
135 ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(),
136 testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget(),
137 testEvent.getToscaPolicyState());
138 apexEvent.put("doubleValue", 123.45);
139 apexEvent.put("intValue", 123);
140 apexEvent.put("stringValue", "123.45");
142 apexEvent.setExceptionMessage("my wonderful exception message");
143 String yamlString = (String) converter.fromApexEvent(apexEvent);
144 assertTrue(yamlString.contains("my wonderful exception message"));
146 apexEvent.remove("intValue");
147 assertThatThrownBy(() -> converter.fromApexEvent(apexEvent))
148 .hasMessageContaining("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing");
149 assertThatThrownBy(() -> converter.toApexEvent(null, ""))
150 .hasMessageStartingWith("Failed to unmarshal YAML event")
151 .getCause().hasMessageStartingWith("event received without mandatory parameter \"name\"");
152 pars.setNameAlias("TheNameField");
153 assertThatThrownBy(() -> converter.toApexEvent(null, ""))
154 .hasMessageStartingWith("Failed to unmarshal YAML event")
155 .getCause().hasMessageStartingWith("event received without mandatory parameter \"name\"");
156 apexEvent.put("intValue", 123);
158 apexEvent.remove("stringValue");
159 yamlString = (String) converter.fromApexEvent(apexEvent);
160 apexEvent.put("stringValue", "123.45");
162 String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123";
164 List<ApexEvent> eventList = converter.toApexEvent("TestEvent", yamlInputString);
165 assertEquals(123.45, eventList.get(0).get("doubleValue"));
167 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null";
169 eventList = converter.toApexEvent("TestEvent", yamlInputString);
170 assertEquals(null, eventList.get(0).get("stringValue"));
172 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent";
173 pars.setNameAlias("stringValue");
174 eventList = converter.toApexEvent(null, yamlInputString);
175 assertEquals("TestEvent", eventList.get(0).get("stringValue"));
177 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent";
178 eventList = converter.toApexEvent("TestEvent", yamlInputString);
179 assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue"));
181 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1";
182 pars.setNameAlias(null);
183 pars.setVersionAlias("stringValue");
184 eventList = converter.toApexEvent("TestEvent", yamlInputString);
185 assertEquals("0.0.1", eventList.get(0).get("stringValue"));
187 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace";
188 pars.setVersionAlias(null);
189 pars.setNameSpaceAlias("stringValue");
190 final String yamlInputStringCopy = yamlInputString;
191 assertThatThrownBy(() -> converter.toApexEvent("TestEvent", yamlInputStringCopy))
192 .hasMessageStartingWith("Failed to unmarshal YAML event")
193 .getCause().hasMessageStartingWith("namespace \"org.some.other.namespace\" on event");
194 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n"
195 + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml";
196 eventList = converter.toApexEvent("TestEvent", yamlInputString);
197 assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace());
199 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource";
200 pars.setNameSpaceAlias(null);
201 pars.setSourceAlias("stringValue");
202 eventList = converter.toApexEvent("TestEvent", yamlInputString);
203 assertEquals("MySource", eventList.get(0).getSource());
205 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget";
206 pars.setSourceAlias(null);
207 pars.setTargetAlias("stringValue");
208 eventList = converter.toApexEvent("TestEvent", yamlInputString);
209 assertEquals("MyTarget", eventList.get(0).getTarget());
210 pars.setTargetAlias(null);
212 yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString";
213 pars.setSourceAlias(null);
214 pars.setTargetAlias("intValue");
215 assertThatThrownBy(() -> converter.toApexEvent("TestEvent", yamlInputStringCopy))
216 .hasMessageStartingWith("Failed to unmarshal YAML event")
217 .getCause().hasMessageStartingWith("field \"target\" with type \"java.lang.Integer\"");
218 pars.setTargetAlias(null);
220 assertThatThrownBy(() -> converter.toApexEvent("TestEvent", "doubleValue: 123.45\n" + "intValue: ~\n"
221 + "stringValue: MyString")).getCause().hasMessageStartingWith("mandatory field \"intValue\" is missing");