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