fdab1d8c1bf0142640f9a8e0a30be792e741fdbf
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020,2023 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.apex.plugins.event.protocol.yaml;
25
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
29
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;
49
50 /**
51  * The Class TestYamlPluginStability.
52  */
53 public class YamlPluginStabilityTest {
54     static AxEvent testEvent;
55
56     /**
57      * Register test events and schemas.
58      *
59      * @throws IOException Signals that an I/O exception has occurred.
60      */
61     @BeforeClass
62     public static void registerTestEventsAndSchemas() throws IOException {
63         SchemaParameters schemaParameters = new SchemaParameters();
64         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
65         ParameterService.register(schemaParameters);
66
67         AxContextSchemas schemas = new AxContextSchemas();
68
69         AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
70                         "java.lang.Integer");
71         schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
72
73         AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"),
74                         "JAVA", "java.lang.Double");
75         schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema);
76
77         AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
78                         "JAVA", "java.lang.String");
79         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
80
81         ModelService.registerModel(AxContextSchemas.class, schemas);
82
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);
94
95         AxEvents events = new AxEvents();
96         events.getEventMap().put(testEvent.getKey(), testEvent);
97
98         ModelService.registerModel(AxEvents.class, events);
99     }
100
101     /**
102      * Unregister test events and schemas.
103      */
104     @AfterClass
105     public static void unregisterTestEventsAndSchemas() {
106         ModelService.clear();
107         ParameterService.clear();
108     }
109
110     /**
111      * Test stability.
112      *
113      * @throws ApexEventException the apex event exception
114      */
115     @SuppressWarnings("deprecation")
116     @Test
117     public void testStability() throws ApexEventException {
118         Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
119
120         assertThatThrownBy(() -> converter.init(null))
121             .hasMessage("specified consumer properties are not applicable to the YAML event protocol");
122         YamlEventProtocolParameters pars = new YamlEventProtocolParameters();
123         converter.init(pars);
124
125         assertThatThrownBy(() -> converter.toApexEvent("NonExistantEvent", ""))
126             .hasMessageContaining("Failed to unmarshal YAML event")
127             .getCause().hasMessageStartingWith("an event definition for an event named \"NonExistantEvent\"");
128         assertThatThrownBy(() -> converter.toApexEvent("TestEvent", null))
129             .hasMessage("event processing failed, event is null");
130         assertThatThrownBy(() -> converter.toApexEvent("TestEvent", 1))
131             .hasMessage("error converting event \"1\" to a string");
132         assertThatThrownBy(() -> converter.toApexEvent("TestEvent", ""))
133             .getCause().hasMessageContaining("Field \"doubleValue\" is missing");
134         assertThatThrownBy(() -> converter.fromApexEvent(null))
135             .hasMessage("event processing failed, Apex event is null");
136         ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(),
137                 testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget(),
138                 testEvent.getToscaPolicyState());
139         apexEvent.put("doubleValue", 123.45);
140         apexEvent.put("intValue", 123);
141         apexEvent.put("stringValue", "123.45");
142
143         apexEvent.setExceptionMessage("my wonderful exception message");
144         String yamlString = (String) converter.fromApexEvent(apexEvent);
145         assertTrue(yamlString.contains("my wonderful exception message"));
146
147         apexEvent.remove("intValue");
148         assertThatThrownBy(() -> converter.fromApexEvent(apexEvent))
149             .hasMessageContaining("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing");
150         assertThatThrownBy(() -> converter.toApexEvent(null, ""))
151             .hasMessageStartingWith("Failed to unmarshal YAML event")
152             .getCause().hasMessageStartingWith("event received without mandatory parameter \"name\"");
153         pars.setNameAlias("TheNameField");
154         assertThatThrownBy(() -> converter.toApexEvent(null, ""))
155             .hasMessageStartingWith("Failed to unmarshal YAML event")
156             .getCause().hasMessageStartingWith("event received without mandatory parameter \"name\"");
157         apexEvent.put("intValue", 123);
158
159         apexEvent.remove("stringValue");
160         yamlString = (String) converter.fromApexEvent(apexEvent);
161         apexEvent.put("stringValue", "123.45");
162
163         String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123";
164
165         List<ApexEvent> eventList = converter.toApexEvent("TestEvent", yamlInputString);
166         assertEquals(123.45, eventList.get(0).get("doubleValue"));
167
168         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null";
169
170         eventList = converter.toApexEvent("TestEvent", yamlInputString);
171         assertEquals(null, eventList.get(0).get("stringValue"));
172
173         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent";
174         pars.setNameAlias("stringValue");
175         eventList = converter.toApexEvent(null, yamlInputString);
176         assertEquals("TestEvent", eventList.get(0).get("stringValue"));
177
178         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent";
179         eventList = converter.toApexEvent("TestEvent", yamlInputString);
180         assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue"));
181
182         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1";
183         pars.setNameAlias(null);
184         pars.setVersionAlias("stringValue");
185         eventList = converter.toApexEvent("TestEvent", yamlInputString);
186         assertEquals("0.0.1", eventList.get(0).get("stringValue"));
187
188         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace";
189         pars.setVersionAlias(null);
190         pars.setNameSpaceAlias("stringValue");
191         final String yamlInputStringCopy = yamlInputString;
192         assertThatThrownBy(() -> converter.toApexEvent("TestEvent", yamlInputStringCopy))
193             .hasMessageStartingWith("Failed to unmarshal YAML event")
194             .getCause().hasMessageStartingWith("namespace \"org.some.other.namespace\" on event");
195         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n"
196                         + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml";
197         eventList = converter.toApexEvent("TestEvent", yamlInputString);
198         assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace());
199
200         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource";
201         pars.setNameSpaceAlias(null);
202         pars.setSourceAlias("stringValue");
203         eventList = converter.toApexEvent("TestEvent", yamlInputString);
204         assertEquals("MySource", eventList.get(0).getSource());
205
206         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget";
207         pars.setSourceAlias(null);
208         pars.setTargetAlias("stringValue");
209         eventList = converter.toApexEvent("TestEvent", yamlInputString);
210         assertEquals("MyTarget", eventList.get(0).getTarget());
211         pars.setTargetAlias(null);
212
213         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString";
214         pars.setSourceAlias(null);
215         pars.setTargetAlias("intValue");
216         assertThatThrownBy(() -> converter.toApexEvent("TestEvent", yamlInputStringCopy))
217             .hasMessageStartingWith("Failed to unmarshal YAML event")
218             .getCause().hasMessageStartingWith("field \"target\" with type \"java.lang.Integer\"");
219         pars.setTargetAlias(null);
220
221         assertThatThrownBy(() -> converter.toApexEvent("TestEvent", "doubleValue: 123.45\n" + "intValue: ~\n"
222             + "stringValue: MyString")).getCause().hasMessageStartingWith("mandatory field \"intValue\" is missing");
223     }
224 }