Changes for checkstyle 8.32
[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 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;
51
52 /**
53  * The Class TestYamlEventProtocol.
54  */
55 public class YamlEventProtocolTest {
56
57     /**
58      * Register test events and schemas.
59      *
60      * @throws IOException Signals that an I/O exception has occurred.
61      */
62     @BeforeClass
63     public static void registerTestEventsAndSchemas() throws IOException {
64         SchemaParameters schemaParameters = new SchemaParameters();
65         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
66         ParameterService.register(schemaParameters, true);
67
68         AxContextSchemas schemas = new AxContextSchemas();
69
70         AxContextSchema simpleIntSchema =
71                 new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", "java.lang.Integer");
72         schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
73
74         AxContextSchema simpleDoubleSchema =
75                 new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), "JAVA", "java.lang.Double");
76         schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema);
77
78         AxContextSchema simpleStringSchema =
79                 new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), "JAVA", "java.lang.String");
80         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
81
82         AxContextSchema arrayListSchema =
83                 new AxContextSchema(new AxArtifactKey("ArrayListSchema", "0.0.1"), "JAVA", "java.util.ArrayList");
84         schemas.getSchemasMap().put(arrayListSchema.getKey(), arrayListSchema);
85
86         AxContextSchema linkedHashMapSchema = new AxContextSchema(new AxArtifactKey("LinkedHashMapSchema", "0.0.1"),
87                 "JAVA", "java.util.LinkedHashMap");
88         schemas.getSchemasMap().put(linkedHashMapSchema.getKey(), linkedHashMapSchema);
89
90         ModelService.registerModel(AxContextSchemas.class, schemas);
91
92         AxEvents events = new AxEvents();
93
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);
97
98         AxEvent testEvent1 = new AxEvent(new AxArtifactKey("TestEvent1", "0.0.1"));
99         testEvent1.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
100         AxField te1Field0 =
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);
104
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);
114
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);
122
123         AxEvent testEvent4 = new AxEvent(new AxArtifactKey("TestEvent4", "0.0.1"));
124         testEvent4.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
125         AxField te4Field0 =
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);
129
130         AxEvent testEvent5 = new AxEvent(new AxArtifactKey("TestEvent5", "0.0.1"));
131         testEvent5.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
132         AxField te5Field0 =
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);
136
137         AxEvent testEvent6 = new AxEvent(new AxArtifactKey("TestEvent6", "0.0.1"));
138         testEvent6.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
139         AxField te6Field0 =
140                 new AxField(new AxReferenceKey(testEvent6.getKey(), "MarkMcGwire"), linkedHashMapSchema.getKey());
141         testEvent6.getParameterMap().put("Mark McGwire", te6Field0);
142         AxField te6Field1 =
143                 new AxField(new AxReferenceKey(testEvent6.getKey(), "SammySosa"), linkedHashMapSchema.getKey());
144         testEvent6.getParameterMap().put("Sammy Sosa", te6Field1);
145         events.getEventMap().put(testEvent6.getKey(), testEvent6);
146
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);
156
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);
164
165         AxEvent testEvent9 = new AxEvent(new AxArtifactKey("TestEvent9", "0.0.1"));
166         testEvent9.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
167         AxField te9Field0 =
168                 new AxField(new AxReferenceKey(testEvent9.getKey(), "ChicagoCubs"), arrayListSchema.getKey());
169         testEvent9.getParameterMap().put("ChicagoCubs", te9Field0);
170         AxField te9Field1 =
171                 new AxField(new AxReferenceKey(testEvent9.getKey(), "AtlantaBraves"), arrayListSchema.getKey());
172         testEvent9.getParameterMap().put("AtlantaBraves", te9Field1);
173         events.getEventMap().put(testEvent9.getKey(), testEvent9);
174
175         AxEvent testEvent10 = new AxEvent(new AxArtifactKey("TestEvent10", "0.0.1"));
176         testEvent10.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
177         AxField te10Field0 =
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);
181
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);
187         AxField te11Field1 =
188                 new AxField(new AxReferenceKey(testEvent11.getKey(), "description"), simpleStringSchema.getKey(), true);
189         testEvent11.getParameterMap().put("description", te11Field1);
190         AxField te11Field2 =
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);
197
198         ModelService.registerModel(AxEvents.class, events);
199     }
200
201     /**
202      * Unregister test events and schemas.
203      */
204     @AfterClass
205     public static void unregisterTestEventsAndSchemas() {
206         ModelService.clear();
207         ParameterService.clear();
208     }
209
210     /**
211      * Test yaml processing.
212      *
213      * @throws ApexEventException the apex event exception
214      * @throws IOException Signals that an I/O exception has occurred.
215      */
216     @Test
217     public void testYamlProcessing() throws ApexEventException, IOException {
218         try {
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());
223         }
224
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");
239     }
240
241     /**
242      * Test yaml decode encode.
243      *
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.
250      */
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);
255
256         Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
257         converter.init(parameters);
258
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);
263
264         List<ApexEvent> eventList = new ArrayList<>();
265
266         TextBlock textBlock;
267         do {
268             textBlock = reader.readTextBlock();
269
270             eventList.addAll(converter.toApexEvent(eventName, textBlock.getText()));
271         } while (!textBlock.isEndOfText());
272
273         fileInputStream.close();
274
275         assertEquals(eventCount, eventList.size());
276
277         for (int eventNo = 0; eventNo < eventCount; eventNo++) {
278             assertEquals(parCount, eventList.get(0).size());
279
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*", ""));
284         }
285     }
286 }