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