2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.event.protocol.yaml;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.util.ArrayList;
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.service.ModelService;
40 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
42 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
43 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
44 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
45 import org.onap.policy.apex.service.engine.event.ApexEvent;
46 import org.onap.policy.apex.service.engine.event.ApexEventException;
47 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.HeaderDelimitedTextBlockReader;
48 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock;
49 import org.onap.policy.common.parameters.ParameterService;
50 import org.onap.policy.common.utils.resources.TextFileUtils;
53 * The Class TestYamlEventProtocol.
55 public class YamlEventProtocolTest {
58 * Register test events and schemas.
60 * @throws IOException Signals that an I/O exception has occurred.
63 public static void registerTestEventsAndSchemas() throws IOException {
64 SchemaParameters schemaParameters = new SchemaParameters();
65 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
66 ParameterService.register(schemaParameters, true);
68 AxContextSchemas schemas = new AxContextSchemas();
70 AxContextSchema simpleIntSchema =
71 new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", "java.lang.Integer");
72 schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
74 AxContextSchema simpleDoubleSchema =
75 new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), "JAVA", "java.lang.Double");
76 schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema);
78 AxContextSchema simpleStringSchema =
79 new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), "JAVA", "java.lang.String");
80 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
82 AxContextSchema arrayListSchema =
83 new AxContextSchema(new AxArtifactKey("ArrayListSchema", "0.0.1"), "JAVA", "java.util.ArrayList");
84 schemas.getSchemasMap().put(arrayListSchema.getKey(), arrayListSchema);
86 AxContextSchema linkedHashMapSchema = new AxContextSchema(new AxArtifactKey("LinkedHashMapSchema", "0.0.1"),
87 "JAVA", "java.util.LinkedHashMap");
88 schemas.getSchemasMap().put(linkedHashMapSchema.getKey(), linkedHashMapSchema);
90 ModelService.registerModel(AxContextSchemas.class, schemas);
92 AxEvents events = new AxEvents();
94 AxEvent testEvent0 = new AxEvent(new AxArtifactKey("TestEvent0", "0.0.1"));
95 testEvent0.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
96 events.getEventMap().put(testEvent0.getKey(), testEvent0);
98 AxEvent testEvent1 = new AxEvent(new AxArtifactKey("TestEvent1", "0.0.1"));
99 testEvent1.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
101 new AxField(new AxReferenceKey(testEvent1.getKey(), "yaml_field"), arrayListSchema.getKey());
102 testEvent1.getParameterMap().put("yaml_field", te1Field0);
103 events.getEventMap().put(testEvent1.getKey(), testEvent1);
105 AxEvent testEvent2 = new AxEvent(new AxArtifactKey("TestEvent2", "0.0.1"));
106 testEvent2.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
107 AxField te2Field0 = new AxField(new AxReferenceKey(testEvent2.getKey(), "hr"), simpleIntSchema.getKey());
108 testEvent2.getParameterMap().put("hr", te2Field0);
109 AxField te2Field1 = new AxField(new AxReferenceKey(testEvent2.getKey(), "avg"), simpleDoubleSchema.getKey());
110 testEvent2.getParameterMap().put("avg", te2Field1);
111 AxField te2Field2 = new AxField(new AxReferenceKey(testEvent2.getKey(), "rbi"), simpleIntSchema.getKey());
112 testEvent2.getParameterMap().put("rbi", te2Field2);
113 events.getEventMap().put(testEvent2.getKey(), testEvent2);
115 AxEvent testEvent3 = new AxEvent(new AxArtifactKey("TestEvent3", "0.0.1"));
116 testEvent3.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
117 AxField te3Field0 = new AxField(new AxReferenceKey(testEvent3.getKey(), "american"), arrayListSchema.getKey());
118 testEvent3.getParameterMap().put("american", te3Field0);
119 AxField te3Field1 = new AxField(new AxReferenceKey(testEvent3.getKey(), "national"), arrayListSchema.getKey());
120 testEvent3.getParameterMap().put("national", te3Field1);
121 events.getEventMap().put(testEvent3.getKey(), testEvent3);
123 AxEvent testEvent4 = new AxEvent(new AxArtifactKey("TestEvent4", "0.0.1"));
124 testEvent4.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
126 new AxField(new AxReferenceKey(testEvent4.getKey(), "yaml_field"), arrayListSchema.getKey());
127 testEvent4.getParameterMap().put("yaml_field", te4Field0);
128 events.getEventMap().put(testEvent4.getKey(), testEvent4);
130 AxEvent testEvent5 = new AxEvent(new AxArtifactKey("TestEvent5", "0.0.1"));
131 testEvent5.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
133 new AxField(new AxReferenceKey(testEvent5.getKey(), "yaml_field"), arrayListSchema.getKey());
134 testEvent5.getParameterMap().put("yaml_field", te5Field0);
135 events.getEventMap().put(testEvent5.getKey(), testEvent5);
137 AxEvent testEvent6 = new AxEvent(new AxArtifactKey("TestEvent6", "0.0.1"));
138 testEvent6.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
140 new AxField(new AxReferenceKey(testEvent6.getKey(), "MarkMcGwire"), linkedHashMapSchema.getKey());
141 testEvent6.getParameterMap().put("Mark McGwire", te6Field0);
143 new AxField(new AxReferenceKey(testEvent6.getKey(), "SammySosa"), linkedHashMapSchema.getKey());
144 testEvent6.getParameterMap().put("Sammy Sosa", te6Field1);
145 events.getEventMap().put(testEvent6.getKey(), testEvent6);
147 AxEvent testEvent7 = new AxEvent(new AxArtifactKey("TestEvent7", "0.0.1"));
148 testEvent7.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
149 AxField te7Field0 = new AxField(new AxReferenceKey(testEvent7.getKey(), "time"), simpleIntSchema.getKey());
150 testEvent7.getParameterMap().put("time", te7Field0);
151 AxField te7Field1 = new AxField(new AxReferenceKey(testEvent7.getKey(), "player"), simpleStringSchema.getKey());
152 testEvent7.getParameterMap().put("player", te7Field1);
153 AxField te7Field2 = new AxField(new AxReferenceKey(testEvent7.getKey(), "action"), simpleStringSchema.getKey());
154 testEvent7.getParameterMap().put("action", te7Field2);
155 events.getEventMap().put(testEvent7.getKey(), testEvent7);
157 AxEvent testEvent8 = new AxEvent(new AxArtifactKey("TestEvent8", "0.0.1"));
158 testEvent8.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
159 AxField te8Field0 = new AxField(new AxReferenceKey(testEvent8.getKey(), "hr"), arrayListSchema.getKey());
160 testEvent8.getParameterMap().put("hr", te8Field0);
161 AxField te8Field1 = new AxField(new AxReferenceKey(testEvent8.getKey(), "rbi"), arrayListSchema.getKey());
162 testEvent8.getParameterMap().put("rbi", te8Field1);
163 events.getEventMap().put(testEvent8.getKey(), testEvent8);
165 AxEvent testEvent9 = new AxEvent(new AxArtifactKey("TestEvent9", "0.0.1"));
166 testEvent9.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
168 new AxField(new AxReferenceKey(testEvent9.getKey(), "ChicagoCubs"), arrayListSchema.getKey());
169 testEvent9.getParameterMap().put("ChicagoCubs", te9Field0);
171 new AxField(new AxReferenceKey(testEvent9.getKey(), "AtlantaBraves"), arrayListSchema.getKey());
172 testEvent9.getParameterMap().put("AtlantaBraves", te9Field1);
173 events.getEventMap().put(testEvent9.getKey(), testEvent9);
175 AxEvent testEvent10 = new AxEvent(new AxArtifactKey("TestEvent10", "0.0.1"));
176 testEvent10.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
178 new AxField(new AxReferenceKey(testEvent10.getKey(), "yaml_field"), arrayListSchema.getKey());
179 testEvent10.getParameterMap().put("yaml_field", te10Field0);
180 events.getEventMap().put(testEvent10.getKey(), testEvent10);
182 AxEvent testEvent11 = new AxEvent(new AxArtifactKey("TestEvent11", "0.0.1"));
183 testEvent11.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
184 AxField te11Field0 = new AxField(new AxReferenceKey(testEvent11.getKey(), "tosca_definitions_version"),
185 simpleStringSchema.getKey());
186 testEvent11.getParameterMap().put("tosca_definitions_version", te11Field0);
188 new AxField(new AxReferenceKey(testEvent11.getKey(), "description"), simpleStringSchema.getKey(), true);
189 testEvent11.getParameterMap().put("description", te11Field1);
191 new AxField(new AxReferenceKey(testEvent11.getKey(), "node_types"), linkedHashMapSchema.getKey(), true);
192 testEvent11.getParameterMap().put("node_types", te11Field2);
193 AxField te11Field3 = new AxField(new AxReferenceKey(testEvent11.getKey(), "topology_template"),
194 linkedHashMapSchema.getKey());
195 testEvent11.getParameterMap().put("topology_template", te11Field3);
196 events.getEventMap().put(testEvent11.getKey(), testEvent11);
198 ModelService.registerModel(AxEvents.class, events);
202 * Unregister test events and schemas.
205 public static void unregisterTestEventsAndSchemas() {
206 ModelService.clear();
207 ParameterService.clear();
211 * Test yaml processing.
213 * @throws ApexEventException the apex event exception
214 * @throws IOException Signals that an I/O exception has occurred.
217 public void testYamlProcessing() throws ApexEventException, IOException {
219 testYamlDecodeEncode("TestEvent0", 1, 0, "Empty0");
220 fail("test should fail here");
221 } catch (ApexEventException e) {
222 assertEquals("event processing failed, event is null", e.getMessage());
225 testYamlDecodeEncode("TestEvent0", 1, 0, "Empty1");
226 testYamlDecodeEncode("TestEvent1", 1, 1, "Collection0");
227 testYamlDecodeEncode("TestEvent2", 1, 3, "Collection1");
228 testYamlDecodeEncode("TestEvent3", 1, 2, "Collection2");
229 testYamlDecodeEncode("TestEvent4", 1, 1, "Collection3");
230 testYamlDecodeEncode("TestEvent5", 1, 1, "Collection4");
231 testYamlDecodeEncode("TestEvent6", 1, 2, "Collection5");
232 testYamlDecodeEncode("TestEvent1", 2, 1, "Structure0");
233 testYamlDecodeEncode("TestEvent7", 2, 3, "Structure1");
234 testYamlDecodeEncode("TestEvent8", 1, 2, "Structure2");
235 testYamlDecodeEncode("TestEvent8", 1, 2, "Structure3");
236 testYamlDecodeEncode("TestEvent9", 1, 2, "Structure4");
237 testYamlDecodeEncode("TestEvent10", 1, 1, "Structure5");
238 testYamlDecodeEncode("TestEvent11", 1, 4, "TOSCA0");
242 * Test yaml decode encode.
244 * @param eventName the event name
245 * @param eventCount the event count
246 * @param parCount the par count
247 * @param fileName the file name
248 * @throws ApexEventException the apex event exception
249 * @throws IOException Signals that an I/O exception has occurred.
251 private void testYamlDecodeEncode(final String eventName, final int eventCount, final int parCount,
252 final String fileName) throws ApexEventException, IOException {
253 YamlEventProtocolParameters parameters = new YamlEventProtocolParameters();
254 parameters.setDelimiterAtStart(false);
256 Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
257 converter.init(parameters);
259 String filePath = "src/test/resources/yaml_in/" + fileName + ".yaml";
260 FileInputStream fileInputStream = new FileInputStream(new File(filePath));
261 HeaderDelimitedTextBlockReader reader = new HeaderDelimitedTextBlockReader(parameters);
262 reader.init(fileInputStream);
264 List<ApexEvent> eventList = new ArrayList<>();
268 textBlock = reader.readTextBlock();
270 eventList.addAll(converter.toApexEvent(eventName, textBlock.getText()));
271 } while (!textBlock.isEndOfText());
273 fileInputStream.close();
275 assertEquals(eventCount, eventList.size());
277 for (int eventNo = 0; eventNo < eventCount; eventNo++) {
278 assertEquals(parCount, eventList.get(0).size());
280 String eventYaml = (String) converter.fromApexEvent(eventList.get(eventNo));
281 String expectedYaml = TextFileUtils
282 .getTextFileAsString("src/test/resources/yaml_out/" + fileName + '_' + eventNo + ".yaml");
283 assertEquals(expectedYaml.replaceAll("\\s*", ""), eventYaml.replaceAll("\\s*", ""));