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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.protocol.yaml;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 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.model.utilities.TextFileUtils;
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.apex.service.engine.event.impl.filecarrierplugin.consumer.HeaderDelimitedTextBlockReader;
49 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock;
50 import org.onap.policy.common.parameters.ParameterService;
52 public class TestYamlEventProtocol {
54 public static void registerTestEventsAndSchemas() throws IOException {
55 SchemaParameters schemaParameters = new SchemaParameters();
56 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
57 ParameterService.register(schemaParameters, true);
59 AxContextSchemas schemas = new AxContextSchemas();
61 AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
63 schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
65 AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), "JAVA",
67 schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema);
69 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), "JAVA",
71 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
73 AxContextSchema arrayListSchema = new AxContextSchema(new AxArtifactKey("ArrayListSchema", "0.0.1"), "JAVA",
74 "java.util.ArrayList");
75 schemas.getSchemasMap().put(arrayListSchema.getKey(), arrayListSchema);
77 AxContextSchema linkedHashMapSchema = new AxContextSchema(new AxArtifactKey("LinkedHashMapSchema", "0.0.1"), "JAVA",
78 "java.util.LinkedHashMap");
79 schemas.getSchemasMap().put(linkedHashMapSchema.getKey(), linkedHashMapSchema);
81 ModelService.registerModel(AxContextSchemas.class, schemas);
83 AxEvents events = new AxEvents();
85 AxEvent testEvent0 = new AxEvent(new AxArtifactKey("TestEvent0", "0.0.1"));
86 testEvent0.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
87 events.getEventMap().put(testEvent0.getKey(), testEvent0);
89 AxEvent testEvent1 = new AxEvent(new AxArtifactKey("TestEvent1", "0.0.1"));
90 testEvent1.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
91 AxField te1Field0 = new AxField(new AxReferenceKey(testEvent1.getKey(), "yaml_field"),
92 arrayListSchema.getKey());
93 testEvent1.getParameterMap().put("yaml_field", te1Field0);
94 events.getEventMap().put(testEvent1.getKey(), testEvent1);
96 AxEvent testEvent2 = new AxEvent(new AxArtifactKey("TestEvent2", "0.0.1"));
97 testEvent2.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
98 AxField te2Field0 = new AxField(new AxReferenceKey(testEvent2.getKey(), "hr"), simpleIntSchema.getKey());
99 testEvent2.getParameterMap().put("hr", te2Field0);
100 AxField te2Field1 = new AxField(new AxReferenceKey(testEvent2.getKey(), "avg"), simpleDoubleSchema.getKey());
101 testEvent2.getParameterMap().put("avg", te2Field1);
102 AxField te2Field2 = new AxField(new AxReferenceKey(testEvent2.getKey(), "rbi"), simpleIntSchema.getKey());
103 testEvent2.getParameterMap().put("rbi", te2Field2);
104 events.getEventMap().put(testEvent2.getKey(), testEvent2);
106 AxEvent testEvent3 = new AxEvent(new AxArtifactKey("TestEvent3", "0.0.1"));
107 testEvent3.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
108 AxField te3Field0 = new AxField(new AxReferenceKey(testEvent3.getKey(), "american"),
109 arrayListSchema.getKey());
110 testEvent3.getParameterMap().put("american", te3Field0);
111 AxField te3Field1 = new AxField(new AxReferenceKey(testEvent3.getKey(), "national"),
112 arrayListSchema.getKey());
113 testEvent3.getParameterMap().put("national", te3Field1);
114 events.getEventMap().put(testEvent3.getKey(), testEvent3);
116 AxEvent testEvent4 = new AxEvent(new AxArtifactKey("TestEvent4", "0.0.1"));
117 testEvent4.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
118 AxField te4Field0 = new AxField(new AxReferenceKey(testEvent4.getKey(), "yaml_field"),
119 arrayListSchema.getKey());
120 testEvent4.getParameterMap().put("yaml_field", te4Field0);
121 events.getEventMap().put(testEvent4.getKey(), testEvent4);
123 AxEvent testEvent5 = new AxEvent(new AxArtifactKey("TestEvent5", "0.0.1"));
124 testEvent5.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
125 AxField te5Field0 = new AxField(new AxReferenceKey(testEvent5.getKey(), "yaml_field"),
126 arrayListSchema.getKey());
127 testEvent5.getParameterMap().put("yaml_field", te5Field0);
128 events.getEventMap().put(testEvent5.getKey(), testEvent5);
130 AxEvent testEvent6 = new AxEvent(new AxArtifactKey("TestEvent6", "0.0.1"));
131 testEvent6.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
132 AxField te6Field0 = new AxField(new AxReferenceKey(testEvent6.getKey(), "MarkMcGwire"),
133 linkedHashMapSchema.getKey());
134 testEvent6.getParameterMap().put("Mark McGwire", te6Field0);
135 AxField te6Field1 = new AxField(new AxReferenceKey(testEvent6.getKey(), "SammySosa"),
136 linkedHashMapSchema.getKey());
137 testEvent6.getParameterMap().put("Sammy Sosa", te6Field1);
138 events.getEventMap().put(testEvent6.getKey(), testEvent6);
140 AxEvent testEvent7 = new AxEvent(new AxArtifactKey("TestEvent7", "0.0.1"));
141 testEvent7.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
142 AxField te7Field0 = new AxField(new AxReferenceKey(testEvent7.getKey(), "time"),
143 simpleIntSchema.getKey());
144 testEvent7.getParameterMap().put("time", te7Field0);
145 AxField te7Field1 = new AxField(new AxReferenceKey(testEvent7.getKey(), "player"),
146 simpleStringSchema.getKey());
147 testEvent7.getParameterMap().put("player", te7Field1);
148 AxField te7Field2 = new AxField(new AxReferenceKey(testEvent7.getKey(), "action"),
149 simpleStringSchema.getKey());
150 testEvent7.getParameterMap().put("action", te7Field2);
151 events.getEventMap().put(testEvent7.getKey(), testEvent7);
153 AxEvent testEvent8 = new AxEvent(new AxArtifactKey("TestEvent8", "0.0.1"));
154 testEvent8.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
155 AxField te8Field0 = new AxField(new AxReferenceKey(testEvent8.getKey(), "hr"),
156 arrayListSchema.getKey());
157 testEvent8.getParameterMap().put("hr", te8Field0);
158 AxField te8Field1 = new AxField(new AxReferenceKey(testEvent8.getKey(), "rbi"),
159 arrayListSchema.getKey());
160 testEvent8.getParameterMap().put("rbi", te8Field1);
161 events.getEventMap().put(testEvent8.getKey(), testEvent8);
163 AxEvent testEvent9 = new AxEvent(new AxArtifactKey("TestEvent9", "0.0.1"));
164 testEvent9.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
165 AxField te9Field0 = new AxField(new AxReferenceKey(testEvent9.getKey(), "ChicagoCubs"),
166 arrayListSchema.getKey());
167 testEvent9.getParameterMap().put("ChicagoCubs", te9Field0);
168 AxField te9Field1 = new AxField(new AxReferenceKey(testEvent9.getKey(), "AtlantaBraves"),
169 arrayListSchema.getKey());
170 testEvent9.getParameterMap().put("AtlantaBraves", te9Field1);
171 events.getEventMap().put(testEvent9.getKey(), testEvent9);
173 AxEvent testEvent10 = new AxEvent(new AxArtifactKey("TestEvent10", "0.0.1"));
174 testEvent10.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
175 AxField te10Field0 = new AxField(new AxReferenceKey(testEvent10.getKey(), "yaml_field"),
176 arrayListSchema.getKey());
177 testEvent10.getParameterMap().put("yaml_field", te10Field0);
178 events.getEventMap().put(testEvent10.getKey(), testEvent10);
180 AxEvent testEvent11 = new AxEvent(new AxArtifactKey("TestEvent11", "0.0.1"));
181 testEvent11.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
182 AxField te11Field0 = new AxField(new AxReferenceKey(testEvent11.getKey(), "tosca_definitions_version"),
183 simpleStringSchema.getKey());
184 testEvent11.getParameterMap().put("tosca_definitions_version", te11Field0);
185 AxField te11Field1 = new AxField(new AxReferenceKey(testEvent11.getKey(), "description"),
186 simpleStringSchema.getKey(), true);
187 testEvent11.getParameterMap().put("description", te11Field1);
188 AxField te11Field2 = new AxField(new AxReferenceKey(testEvent11.getKey(), "node_types"),
189 linkedHashMapSchema.getKey(), true);
190 testEvent11.getParameterMap().put("node_types", te11Field2);
191 AxField te11Field3 = new AxField(new AxReferenceKey(testEvent11.getKey(), "topology_template"),
192 linkedHashMapSchema.getKey());
193 testEvent11.getParameterMap().put("topology_template", te11Field3);
194 events.getEventMap().put(testEvent11.getKey(), testEvent11);
196 ModelService.registerModel(AxEvents.class, events);
200 public static void unregisterTestEventsAndSchemas() {
201 ModelService.clear();
202 ParameterService.clear();
206 public void testYamlProcessing() throws ApexEventException, IOException {
208 testYamlDecodeEncode("TestEvent0", 1, 0, "Empty0");
209 fail("test should fail here");
210 } catch (ApexEventException e) {
211 assertEquals("event processing failed, event is null", e.getMessage());
214 testYamlDecodeEncode("TestEvent0", 1, 0, "Empty1");
215 testYamlDecodeEncode("TestEvent1", 1, 1, "Collection0");
216 testYamlDecodeEncode("TestEvent2", 1, 3, "Collection1");
217 testYamlDecodeEncode("TestEvent3", 1, 2, "Collection2");
218 testYamlDecodeEncode("TestEvent4", 1, 1, "Collection3");
219 testYamlDecodeEncode("TestEvent5", 1, 1, "Collection4");
220 testYamlDecodeEncode("TestEvent6", 1, 2, "Collection5");
221 testYamlDecodeEncode("TestEvent1", 2, 1, "Structure0");
222 testYamlDecodeEncode("TestEvent7", 2, 3, "Structure1");
223 testYamlDecodeEncode("TestEvent8", 1, 2, "Structure2");
224 testYamlDecodeEncode("TestEvent8", 1, 2, "Structure3");
225 testYamlDecodeEncode("TestEvent9", 1, 2, "Structure4");
226 testYamlDecodeEncode("TestEvent10", 1, 1, "Structure5");
227 testYamlDecodeEncode("TestEvent11", 1, 4, "TOSCA0");
230 private void testYamlDecodeEncode(final String eventName, final int eventCount, final int parCount, final String fileName)
231 throws ApexEventException, IOException {
232 YamlEventProtocolParameters parameters = new YamlEventProtocolParameters();
233 parameters.setDelimiterAtStart(false);
235 Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
236 converter.init(parameters);
238 String filePath = "src/test/resources/yaml_in/" + fileName + ".yaml";
239 FileInputStream fileInputStream = new FileInputStream(new File(filePath));
240 HeaderDelimitedTextBlockReader reader = new HeaderDelimitedTextBlockReader(parameters);
241 reader.init(fileInputStream);
243 List<ApexEvent> eventList = new ArrayList<>();
247 textBlock = reader.readTextBlock();
249 eventList.addAll(converter.toApexEvent(eventName, textBlock.getText()));
250 } while (!textBlock.isEndOfText());
252 fileInputStream.close();
254 assertEquals(eventCount, eventList.size());
256 for (int eventNo = 0; eventNo < eventCount; eventNo++) {
257 assertEquals(parCount, eventList.get(0).size());
259 String eventYaml = (String) converter.fromApexEvent(eventList.get(eventNo));
260 String expectedYaml = TextFileUtils.getTextFileAsString("src/test/resources/yaml_out/" + fileName + '_' + eventNo + ".yaml");
261 assertEquals(expectedYaml.replaceAll("\\s*", ""), eventYaml.replaceAll("\\s*", ""));