40196eb8ac81800d793cbf7e8d453bd6c850ae0f
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.event.protocol.yaml;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
31
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.basicmodel.service.ParameterService;
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.model.utilities.TextFileUtils;
47 import org.onap.policy.apex.service.engine.event.ApexEvent;
48 import org.onap.policy.apex.service.engine.event.ApexEventException;
49 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.HeaderDelimitedTextBlockReader;
50 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock;
51
52 public class TestYamlEventProtocol {
53     @BeforeClass
54     public static void registerTestEventsAndSchemas() throws IOException {
55         SchemaParameters schemaParameters = new SchemaParameters();
56         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
57         ParameterService.registerParameters(SchemaParameters.class, schemaParameters);
58
59         AxContextSchemas schemas = new AxContextSchemas();
60
61         AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
62                         "java.lang.Integer");
63         schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
64
65         AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), "JAVA",
66                         "java.lang.Double");
67         schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema);
68
69         AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), "JAVA",
70                         "java.lang.String");
71         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
72
73         AxContextSchema arrayListSchema = new AxContextSchema(new AxArtifactKey("ArrayListSchema", "0.0.1"), "JAVA",
74                         "java.util.ArrayList");
75         schemas.getSchemasMap().put(arrayListSchema.getKey(), arrayListSchema);
76
77         AxContextSchema linkedHashMapSchema = new AxContextSchema(new AxArtifactKey("LinkedHashMapSchema", "0.0.1"), "JAVA",
78                         "java.util.LinkedHashMap");
79         schemas.getSchemasMap().put(linkedHashMapSchema.getKey(), linkedHashMapSchema);
80
81         ModelService.registerModel(AxContextSchemas.class, schemas);
82
83         AxEvents events = new AxEvents();
84
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);
88
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);
95
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);
105
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);
115
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);
122
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);
129
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);
139
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);
152
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);
162
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);
172
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);
179
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);
195
196         ModelService.registerModel(AxEvents.class, events);
197     }
198
199     @AfterClass
200     public static void unregisterTestEventsAndSchemas() {
201         ModelService.clear();
202     }
203
204     @Test
205     public void testYamlProcessing() throws ApexEventException, IOException {
206         try {
207             testYamlDecodeEncode("TestEvent0", 1, 0, "Empty0");
208             fail("test should fail here");
209         } catch (ApexEventException e) {
210             assertEquals("event processing failed, event is null", e.getMessage());
211         }
212
213         testYamlDecodeEncode("TestEvent0", 1, 0, "Empty1");
214         testYamlDecodeEncode("TestEvent1", 1, 1, "Collection0");
215         testYamlDecodeEncode("TestEvent2", 1, 3, "Collection1");
216         testYamlDecodeEncode("TestEvent3", 1, 2, "Collection2");
217         testYamlDecodeEncode("TestEvent4", 1, 1, "Collection3");
218         testYamlDecodeEncode("TestEvent5", 1, 1, "Collection4");
219         testYamlDecodeEncode("TestEvent6", 1, 2, "Collection5");
220         testYamlDecodeEncode("TestEvent1", 2, 1, "Structure0");
221         testYamlDecodeEncode("TestEvent7", 2, 3, "Structure1");
222         testYamlDecodeEncode("TestEvent8", 1, 2, "Structure2");
223         testYamlDecodeEncode("TestEvent8", 1, 2, "Structure3");
224         testYamlDecodeEncode("TestEvent9", 1, 2, "Structure4");
225         testYamlDecodeEncode("TestEvent10", 1, 1, "Structure5");
226         testYamlDecodeEncode("TestEvent11", 1, 4, "TOSCA0");
227     }
228
229     private void testYamlDecodeEncode(final String eventName, final int eventCount, final int parCount, final String fileName)
230                     throws ApexEventException, IOException {
231         YamlEventProtocolParameters parameters = new YamlEventProtocolParameters();
232         parameters.setDelimiterAtStart(false);
233
234         Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
235         converter.init(parameters);
236
237         String filePath = "src/test/resources/yaml_in/" + fileName + ".yaml";
238         FileInputStream fileInputStream = new FileInputStream(new File(filePath));
239         HeaderDelimitedTextBlockReader reader = new HeaderDelimitedTextBlockReader(parameters);
240         reader.init(fileInputStream);
241        
242         List<ApexEvent> eventList = new ArrayList<>();
243         
244         TextBlock textBlock;
245         do {
246              textBlock = reader.readTextBlock();
247              
248              eventList.addAll(converter.toApexEvent(eventName, textBlock.getText()));
249         } while (!textBlock.isEndOfText());
250         
251         fileInputStream.close();
252
253         assertEquals(eventCount, eventList.size());
254
255         for (int eventNo = 0; eventNo < eventCount; eventNo++) {
256             assertEquals(parCount, eventList.get(0).size());
257
258             String eventYaml = (String) converter.fromApexEvent(eventList.get(eventNo));
259             String expectedYaml = TextFileUtils.getTextFileAsString("src/test/resources/yaml_out/" + fileName + '_' + eventNo + ".yaml");
260             assertEquals(expectedYaml.replaceAll("\\s*", ""), eventYaml.replaceAll("\\s*", ""));
261         }
262     }
263 }