65a6305f59bbe129b510a0f15bd3a3fd0726ff69
[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 / YamlEventProtocolTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.event.protocol.yaml;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
37 import org.onap.policy.apex.context.parameters.SchemaParameters;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
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.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;
51 import org.onap.policy.common.utils.resources.TextFileUtils;
52
53 /**
54  * The Class TestYamlEventProtocol.
55  */
56 public class YamlEventProtocolTest {
57
58     /**
59      * Register test events and schemas.
60      *
61      * @throws IOException Signals that an I/O exception has occurred.
62      */
63     @BeforeClass
64     public static void registerTestEventsAndSchemas() throws IOException {
65         SchemaParameters schemaParameters = new SchemaParameters();
66         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
67         ParameterService.register(schemaParameters, true);
68
69         AxContextSchemas schemas = new AxContextSchemas();
70
71         AxContextSchema simpleIntSchema =
72                 new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", "java.lang.Integer");
73         schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
74
75         AxContextSchema simpleDoubleSchema =
76                 new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), "JAVA", "java.lang.Double");
77         schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema);
78
79         AxContextSchema simpleStringSchema =
80                 new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), "JAVA", "java.lang.String");
81         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
82
83         AxContextSchema arrayListSchema =
84                 new AxContextSchema(new AxArtifactKey("ArrayListSchema", "0.0.1"), "JAVA", "java.util.ArrayList");
85         schemas.getSchemasMap().put(arrayListSchema.getKey(), arrayListSchema);
86
87         AxContextSchema linkedHashMapSchema = new AxContextSchema(new AxArtifactKey("LinkedHashMapSchema", "0.0.1"),
88                 "JAVA", "java.util.LinkedHashMap");
89         schemas.getSchemasMap().put(linkedHashMapSchema.getKey(), linkedHashMapSchema);
90
91         ModelService.registerModel(AxContextSchemas.class, schemas);
92
93         AxEvents events = new AxEvents();
94
95         AxEvent testEvent0 = new AxEvent(new AxArtifactKey("TestEvent0", "0.0.1"));
96         testEvent0.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
97         events.getEventMap().put(testEvent0.getKey(), testEvent0);
98
99         AxEvent testEvent1 = new AxEvent(new AxArtifactKey("TestEvent1", "0.0.1"));
100         testEvent1.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
101         AxField te1Field0 =
102                 new AxField(new AxReferenceKey(testEvent1.getKey(), "yaml_field"), arrayListSchema.getKey());
103         testEvent1.getParameterMap().put("yaml_field", te1Field0);
104         events.getEventMap().put(testEvent1.getKey(), testEvent1);
105
106         AxEvent testEvent2 = new AxEvent(new AxArtifactKey("TestEvent2", "0.0.1"));
107         testEvent2.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
108         AxField te2Field0 = new AxField(new AxReferenceKey(testEvent2.getKey(), "hr"), simpleIntSchema.getKey());
109         testEvent2.getParameterMap().put("hr", te2Field0);
110         AxField te2Field1 = new AxField(new AxReferenceKey(testEvent2.getKey(), "avg"), simpleDoubleSchema.getKey());
111         testEvent2.getParameterMap().put("avg", te2Field1);
112         AxField te2Field2 = new AxField(new AxReferenceKey(testEvent2.getKey(), "rbi"), simpleIntSchema.getKey());
113         testEvent2.getParameterMap().put("rbi", te2Field2);
114         events.getEventMap().put(testEvent2.getKey(), testEvent2);
115
116         AxEvent testEvent3 = new AxEvent(new AxArtifactKey("TestEvent3", "0.0.1"));
117         testEvent3.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
118         AxField te3Field0 = new AxField(new AxReferenceKey(testEvent3.getKey(), "american"), arrayListSchema.getKey());
119         testEvent3.getParameterMap().put("american", te3Field0);
120         AxField te3Field1 = new AxField(new AxReferenceKey(testEvent3.getKey(), "national"), arrayListSchema.getKey());
121         testEvent3.getParameterMap().put("national", te3Field1);
122         events.getEventMap().put(testEvent3.getKey(), testEvent3);
123
124         AxEvent testEvent4 = new AxEvent(new AxArtifactKey("TestEvent4", "0.0.1"));
125         testEvent4.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
126         AxField te4Field0 =
127                 new AxField(new AxReferenceKey(testEvent4.getKey(), "yaml_field"), arrayListSchema.getKey());
128         testEvent4.getParameterMap().put("yaml_field", te4Field0);
129         events.getEventMap().put(testEvent4.getKey(), testEvent4);
130
131         AxEvent testEvent5 = new AxEvent(new AxArtifactKey("TestEvent5", "0.0.1"));
132         testEvent5.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
133         AxField te5Field0 =
134                 new AxField(new AxReferenceKey(testEvent5.getKey(), "yaml_field"), arrayListSchema.getKey());
135         testEvent5.getParameterMap().put("yaml_field", te5Field0);
136         events.getEventMap().put(testEvent5.getKey(), testEvent5);
137
138         AxEvent testEvent6 = new AxEvent(new AxArtifactKey("TestEvent6", "0.0.1"));
139         testEvent6.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
140         AxField te6Field0 =
141                 new AxField(new AxReferenceKey(testEvent6.getKey(), "MarkMcGwire"), linkedHashMapSchema.getKey());
142         testEvent6.getParameterMap().put("Mark McGwire", te6Field0);
143         AxField te6Field1 =
144                 new AxField(new AxReferenceKey(testEvent6.getKey(), "SammySosa"), linkedHashMapSchema.getKey());
145         testEvent6.getParameterMap().put("Sammy Sosa", te6Field1);
146         events.getEventMap().put(testEvent6.getKey(), testEvent6);
147
148         AxEvent testEvent7 = new AxEvent(new AxArtifactKey("TestEvent7", "0.0.1"));
149         testEvent7.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
150         AxField te7Field0 = new AxField(new AxReferenceKey(testEvent7.getKey(), "time"), simpleIntSchema.getKey());
151         testEvent7.getParameterMap().put("time", te7Field0);
152         AxField te7Field1 = new AxField(new AxReferenceKey(testEvent7.getKey(), "player"), simpleStringSchema.getKey());
153         testEvent7.getParameterMap().put("player", te7Field1);
154         AxField te7Field2 = new AxField(new AxReferenceKey(testEvent7.getKey(), "action"), simpleStringSchema.getKey());
155         testEvent7.getParameterMap().put("action", te7Field2);
156         events.getEventMap().put(testEvent7.getKey(), testEvent7);
157
158         AxEvent testEvent8 = new AxEvent(new AxArtifactKey("TestEvent8", "0.0.1"));
159         testEvent8.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
160         AxField te8Field0 = new AxField(new AxReferenceKey(testEvent8.getKey(), "hr"), arrayListSchema.getKey());
161         testEvent8.getParameterMap().put("hr", te8Field0);
162         AxField te8Field1 = new AxField(new AxReferenceKey(testEvent8.getKey(), "rbi"), arrayListSchema.getKey());
163         testEvent8.getParameterMap().put("rbi", te8Field1);
164         events.getEventMap().put(testEvent8.getKey(), testEvent8);
165
166         AxEvent testEvent9 = new AxEvent(new AxArtifactKey("TestEvent9", "0.0.1"));
167         testEvent9.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
168         AxField te9Field0 =
169                 new AxField(new AxReferenceKey(testEvent9.getKey(), "ChicagoCubs"), arrayListSchema.getKey());
170         testEvent9.getParameterMap().put("ChicagoCubs", te9Field0);
171         AxField te9Field1 =
172                 new AxField(new AxReferenceKey(testEvent9.getKey(), "AtlantaBraves"), arrayListSchema.getKey());
173         testEvent9.getParameterMap().put("AtlantaBraves", te9Field1);
174         events.getEventMap().put(testEvent9.getKey(), testEvent9);
175
176         AxEvent testEvent10 = new AxEvent(new AxArtifactKey("TestEvent10", "0.0.1"));
177         testEvent10.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
178         AxField te10Field0 =
179                 new AxField(new AxReferenceKey(testEvent10.getKey(), "yaml_field"), arrayListSchema.getKey());
180         testEvent10.getParameterMap().put("yaml_field", te10Field0);
181         events.getEventMap().put(testEvent10.getKey(), testEvent10);
182
183         AxEvent testEvent11 = new AxEvent(new AxArtifactKey("TestEvent11", "0.0.1"));
184         testEvent11.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
185         AxField te11Field0 = new AxField(new AxReferenceKey(testEvent11.getKey(), "tosca_definitions_version"),
186                 simpleStringSchema.getKey());
187         testEvent11.getParameterMap().put("tosca_definitions_version", te11Field0);
188         AxField te11Field1 =
189                 new AxField(new AxReferenceKey(testEvent11.getKey(), "description"), simpleStringSchema.getKey(), true);
190         testEvent11.getParameterMap().put("description", te11Field1);
191         AxField te11Field2 =
192                 new AxField(new AxReferenceKey(testEvent11.getKey(), "node_types"), linkedHashMapSchema.getKey(), true);
193         testEvent11.getParameterMap().put("node_types", te11Field2);
194         AxField te11Field3 = new AxField(new AxReferenceKey(testEvent11.getKey(), "topology_template"),
195                 linkedHashMapSchema.getKey());
196         testEvent11.getParameterMap().put("topology_template", te11Field3);
197         events.getEventMap().put(testEvent11.getKey(), testEvent11);
198
199         ModelService.registerModel(AxEvents.class, events);
200     }
201
202     /**
203      * Unregister test events and schemas.
204      */
205     @AfterClass
206     public static void unregisterTestEventsAndSchemas() {
207         ModelService.clear();
208         ParameterService.clear();
209     }
210
211     /**
212      * Test yaml processing.
213      *
214      * @throws ApexEventException the apex event exception
215      * @throws IOException Signals that an I/O exception has occurred.
216      */
217     @Test
218     public void testYamlProcessing() throws ApexEventException, IOException {
219         try {
220             testYamlDecodeEncode("TestEvent0", 1, 0, "Empty0");
221             fail("test should fail here");
222         } catch (ApexEventException e) {
223             assertEquals("event processing failed, event is null", e.getMessage());
224         }
225
226         testYamlDecodeEncode("TestEvent0", 1, 0, "Empty1");
227         testYamlDecodeEncode("TestEvent1", 1, 1, "Collection0");
228         testYamlDecodeEncode("TestEvent2", 1, 3, "Collection1");
229         testYamlDecodeEncode("TestEvent3", 1, 2, "Collection2");
230         testYamlDecodeEncode("TestEvent4", 1, 1, "Collection3");
231         testYamlDecodeEncode("TestEvent5", 1, 1, "Collection4");
232         testYamlDecodeEncode("TestEvent6", 1, 2, "Collection5");
233         testYamlDecodeEncode("TestEvent1", 2, 1, "Structure0");
234         testYamlDecodeEncode("TestEvent7", 2, 3, "Structure1");
235         testYamlDecodeEncode("TestEvent8", 1, 2, "Structure2");
236         testYamlDecodeEncode("TestEvent8", 1, 2, "Structure3");
237         testYamlDecodeEncode("TestEvent9", 1, 2, "Structure4");
238         testYamlDecodeEncode("TestEvent10", 1, 1, "Structure5");
239         testYamlDecodeEncode("TestEvent11", 1, 4, "TOSCA0");
240     }
241
242     /**
243      * Test yaml decode encode.
244      *
245      * @param eventName the event name
246      * @param eventCount the event count
247      * @param parCount the par count
248      * @param fileName the file name
249      * @throws ApexEventException the apex event exception
250      * @throws IOException Signals that an I/O exception has occurred.
251      */
252     private void testYamlDecodeEncode(final String eventName, final int eventCount, final int parCount,
253             final String fileName) throws ApexEventException, IOException {
254         YamlEventProtocolParameters parameters = new YamlEventProtocolParameters();
255         parameters.setDelimiterAtStart(false);
256
257         Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
258         converter.init(parameters);
259
260         String filePath = "src/test/resources/yaml_in/" + fileName + ".yaml";
261         FileInputStream fileInputStream = new FileInputStream(new File(filePath));
262         HeaderDelimitedTextBlockReader reader = new HeaderDelimitedTextBlockReader(parameters);
263         reader.init(fileInputStream);
264
265         List<ApexEvent> eventList = new ArrayList<>();
266
267         TextBlock textBlock;
268         do {
269             textBlock = reader.readTextBlock();
270
271             eventList.addAll(converter.toApexEvent(eventName, textBlock.getText()));
272         }
273         while (!textBlock.isEndOfText());
274
275         fileInputStream.close();
276
277         assertEquals(eventCount, eventList.size());
278
279         for (int eventNo = 0; eventNo < eventCount; eventNo++) {
280             assertEquals(parCount, eventList.get(0).size());
281
282             String eventYaml = (String) converter.fromApexEvent(eventList.get(eventNo));
283             String expectedYaml = TextFileUtils
284                     .getTextFileAsString("src/test/resources/yaml_out/" + fileName + '_' + eventNo + ".yaml");
285             assertEquals(expectedYaml.replaceAll("\\s*", ""), eventYaml.replaceAll("\\s*", ""));
286         }
287     }
288 }