7f62961be28f6076f388c1d6e8b798b72707daac
[policy/apex-pdp.git] / plugins / plugins-event / plugins-event-protocol / plugins-event-protocol-yaml / src / test / java / org / onap / policy / apex / plugins / event / protocol / yaml / YamlPluginStabilityTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.event.protocol.yaml;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.util.List;
29
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
34 import org.onap.policy.apex.context.parameters.SchemaParameters;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
37 import org.onap.policy.apex.model.basicmodel.service.ModelService;
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;
46 import org.onap.policy.common.parameters.ParameterService;
47
48 /**
49  * The Class TestYamlPluginStability.
50  */
51 public class YamlPluginStabilityTest {
52     static AxEvent testEvent;
53
54     /**
55      * Register test events and schemas.
56      *
57      * @throws IOException Signals that an I/O exception has occurred.
58      */
59     @BeforeClass
60     public static void registerTestEventsAndSchemas() throws IOException {
61         SchemaParameters schemaParameters = new SchemaParameters();
62         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
63         ParameterService.register(schemaParameters);
64
65         AxContextSchemas schemas = new AxContextSchemas();
66
67         AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
68                         "java.lang.Integer");
69         schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
70
71         AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"),
72                         "JAVA", "java.lang.Double");
73         schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema);
74
75         AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
76                         "JAVA", "java.lang.String");
77         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
78
79         ModelService.registerModel(AxContextSchemas.class, schemas);
80
81         testEvent = new AxEvent(new AxArtifactKey("TestEvent", "0.0.1"));
82         testEvent.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
83         AxField teField0 = new AxField(new AxReferenceKey(testEvent.getKey(), "intValue"), simpleIntSchema.getKey());
84         testEvent.getParameterMap().put("intValue", teField0);
85         AxField teField1 = new AxField(new AxReferenceKey(testEvent.getKey(), "doubleValue"),
86                         simpleDoubleSchema.getKey());
87         testEvent.getParameterMap().put("doubleValue", teField1);
88         AxField teField2 = new AxField(new AxReferenceKey(testEvent.getKey(), "stringValue"),
89                         simpleStringSchema.getKey(), true);
90         testEvent.getParameterMap().put("stringValue", teField2);
91
92         AxEvents events = new AxEvents();
93         events.getEventMap().put(testEvent.getKey(), testEvent);
94
95         ModelService.registerModel(AxEvents.class, events);
96     }
97
98     /**
99      * Unregister test events and schemas.
100      */
101     @AfterClass
102     public static void unregisterTestEventsAndSchemas() {
103         ModelService.clear();
104         ParameterService.clear();
105     }
106
107     /**
108      * Test stability.
109      *
110      * @throws ApexEventException the apex event exception
111      */
112     @Test
113     public void testStability() throws ApexEventException {
114         Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
115
116         try {
117             converter.init(null);
118             fail("this test should throw an exception");
119         } catch (ApexEventRuntimeException e) {
120             assertEquals("specified consumer properties are not applicable to the YAML event protocol", e.getMessage());
121         }
122
123         YamlEventProtocolParameters pars = new YamlEventProtocolParameters();
124         converter.init(pars);
125
126         try {
127             converter.toApexEvent("NonExistantEvent", "");
128             fail("this test should throw an exception");
129         } catch (ApexEventException e) {
130             assertEquals("Failed to unmarshal YAML event: an event definition for an event named \"NonExistantEvent\"",
131                             e.getMessage().substring(0, 89));
132         }
133
134         try {
135             converter.toApexEvent("TestEvent", null);
136             fail("this test should throw an exception");
137         } catch (ApexEventException e) {
138             assertEquals("event processing failed, event is null", e.getMessage());
139         }
140
141         try {
142             converter.toApexEvent("TestEvent", 1);
143             fail("this test should throw an exception");
144         } catch (ApexEventException e) {
145             assertEquals("error converting event \"1\" to a string", e.getMessage());
146         }
147
148         try {
149             converter.toApexEvent("TestEvent", "");
150             fail("this test should throw an exception");
151         } catch (ApexEventException e) {
152             assertTrue(e.getMessage().contains("Field \"doubleValue\" is missing"));
153         }
154
155         try {
156             converter.fromApexEvent(null);
157             fail("this test should throw an exception");
158         } catch (ApexEventException e) {
159             assertEquals("event processing failed, Apex event is null", e.getMessage());
160         }
161
162         ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(),
163                         testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget());
164         apexEvent.put("doubleValue", 123.45);
165         apexEvent.put("intValue", 123);
166         apexEvent.put("stringValue", "123.45");
167
168         apexEvent.setExceptionMessage("my wonderful exception message");
169         String yamlString = (String) converter.fromApexEvent(apexEvent);
170         assertTrue(yamlString.contains("my wonderful exception message"));
171
172         apexEvent.remove("intValue");
173         try {
174             yamlString = (String) converter.fromApexEvent(apexEvent);
175             fail("this test should throw an exception");
176         } catch (ApexEventRuntimeException e) {
177             assertEquals("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing",
178                             e.getMessage().substring(0, 72));
179         }
180
181         try {
182             converter.toApexEvent(null, "");
183             fail("this test should throw an exception");
184         } catch (ApexEventException e) {
185             assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"",
186                             e.getMessage().substring(0, 81));
187         }
188
189         pars.setNameAlias("TheNameField");
190         try {
191             converter.toApexEvent(null, "");
192             fail("this test should throw an exception");
193         } catch (ApexEventException e) {
194             assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"",
195                             e.getMessage().substring(0, 81));
196         }
197
198         apexEvent.put("intValue", 123);
199
200         apexEvent.remove("stringValue");
201         yamlString = (String) converter.fromApexEvent(apexEvent);
202         apexEvent.put("stringValue", "123.45");
203
204         String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123";
205
206         List<ApexEvent> eventList = converter.toApexEvent("TestEvent", yamlInputString);
207         assertEquals(123.45, eventList.get(0).get("doubleValue"));
208
209         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null";
210
211         eventList = converter.toApexEvent("TestEvent", yamlInputString);
212         assertEquals(null, eventList.get(0).get("stringValue"));
213
214         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent";
215         pars.setNameAlias("stringValue");
216         eventList = converter.toApexEvent(null, yamlInputString);
217         assertEquals("TestEvent", eventList.get(0).get("stringValue"));
218
219         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent";
220         eventList = converter.toApexEvent("TestEvent", yamlInputString);
221         assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue"));
222
223         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1";
224         pars.setNameAlias(null);
225         pars.setVersionAlias("stringValue");
226         eventList = converter.toApexEvent("TestEvent", yamlInputString);
227         assertEquals("0.0.1", eventList.get(0).get("stringValue"));
228
229         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace";
230         pars.setVersionAlias(null);
231         pars.setNameSpaceAlias("stringValue");
232         try {
233             converter.toApexEvent("TestEvent", yamlInputString);
234             fail("this test should throw an exception");
235         } catch (ApexEventException e) {
236             assertEquals("Failed to unmarshal YAML event: namespace \"org.some.other.namespace\" on event",
237                             e.getMessage().substring(0, 77));
238         }
239
240         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n"
241                         + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml";
242         eventList = converter.toApexEvent("TestEvent", yamlInputString);
243         assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace());
244
245         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource";
246         pars.setNameSpaceAlias(null);
247         pars.setSourceAlias("stringValue");
248         eventList = converter.toApexEvent("TestEvent", yamlInputString);
249         assertEquals("MySource", eventList.get(0).getSource());
250
251         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget";
252         pars.setSourceAlias(null);
253         pars.setTargetAlias("stringValue");
254         eventList = converter.toApexEvent("TestEvent", yamlInputString);
255         assertEquals("MyTarget", eventList.get(0).getTarget());
256         pars.setTargetAlias(null);
257
258         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString";
259         pars.setSourceAlias(null);
260         pars.setTargetAlias("intValue");
261         try {
262             converter.toApexEvent("TestEvent", yamlInputString);
263             fail("this test should throw an exception");
264         } catch (ApexEventException e) {
265             assertEquals("Failed to unmarshal YAML event: field \"target\" with type \"java.lang.Integer\"",
266                             e.getMessage().substring(0, 76));
267         }
268         pars.setTargetAlias(null);
269
270         yamlInputString = "doubleValue: 123.45\n" + "intValue: ~\n" + "stringValue: MyString";
271         try {
272             converter.toApexEvent("TestEvent", yamlInputString);
273             fail("this test should throw an exception");
274         } catch (ApexEventException e) {
275             assertTrue(e.getMessage().contains("mandatory field \"intValue\" is missing"));
276         }
277     }
278 }