c3214d3047fe11577c0730fc5477379df22e7cb6
[policy/apex-pdp.git] / tools / model-generator / src / test / java / org / onap / policy / apex / tools / model / generator / SchemaUtilsTest.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.tools.model.generator;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.apache.avro.Schema;
34 import org.apache.avro.Schema.Field;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
38 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
39 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
40 import org.onap.policy.apex.context.parameters.SchemaParameters;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
44 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
45 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
46 import org.onap.policy.apex.model.basicmodel.service.ModelService;
47 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
48 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
49 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
50 import org.onap.policy.apex.model.eventmodel.concepts.AxInputField;
51 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
52 import org.onap.policy.apex.plugins.context.schema.avro.AvroSchemaHelper;
53 import org.onap.policy.apex.plugins.context.schema.avro.AvroSchemaHelperParameters;
54 import org.onap.policy.apex.service.engine.event.ApexEventException;
55 import org.onap.policy.common.parameters.ParameterService;
56 import org.onap.policy.common.utils.resources.TextFileUtils;
57
58 /**
59  * Test the Key Info Getter.
60  */
61 public class SchemaUtilsTest {
62     private static AxPolicyModel avroModel;
63
64     /**
65      * Read the models into strings.
66      *
67      * @throws IOException on model reading errors
68      * @throws ApexModelException on model reading exceptions
69      */
70     @BeforeClass
71     public static void readSimpleModel() throws IOException, ApexModelException {
72         String avroModelString = TextFileUtils.getTextFileAsString("target/examples/models/pcvs/vpnsla/vpnsla.json");
73
74         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
75         avroModel = modelReader.read(new ByteArrayInputStream(avroModelString.getBytes()));
76     }
77
78     @Test
79     public void testSchemaUtilsErrors() throws ApexEventException {
80         AxEvent event = avroModel.getEvents().get("CustomerContextEventIn");
81         AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl");
82
83         AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1");
84         try {
85             SchemaUtils.getEventSchema(event);
86             fail("test should throw an exception");
87         } catch (Exception apEx) {
88             assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas"
89                     + " not found in model service", apEx.getMessage());
90         }
91
92         try {
93             Map<String, Schema> preexistingParamSchemas = new LinkedHashMap<>();
94             SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas);
95             fail("test should throw an exception");
96         } catch (Exception apEx) {
97             assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas"
98                     + " not found in model service", apEx.getMessage());
99         }
100
101         List<Field> skeletonFields = SchemaUtils.getSkeletonEventSchemaFields();
102         assertEquals(5, skeletonFields.size());
103
104         try {
105             AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory()
106                     .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey());
107
108             Map<String, Schema> schemaMap = new LinkedHashMap<>();
109             SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap);
110             fail("test should throw an exception");
111         } catch (Exception apEx) {
112             assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas"
113                     + " not found in model service", apEx.getMessage());
114         }
115     }
116
117     @Test
118     public void testSchemaUtils() throws ApexEventException {
119         ParameterService.clear();
120         final SchemaParameters schemaParameters = new SchemaParameters();
121         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
122         ParameterService.register(schemaParameters);
123
124         ModelService.registerModel(AxModel.class, avroModel);
125         ModelService.registerModel(AxContextSchemas.class, avroModel.getSchemas());
126
127         AxEvent event = avroModel.getEvents().get("CustomerContextEventIn");
128         AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl");
129         AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1");
130
131         Schema eventSchema = SchemaUtils.getEventSchema(event);
132         assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"",
133                 eventSchema.toString().substring(0, 48));
134
135         Map<String, Schema> preexistingParamSchemas = new LinkedHashMap<>();
136         Schema epSchema =
137                 SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas);
138         assertEquals("\"string\"", epSchema.toString());
139
140         List<Field> skeletonFields = SchemaUtils.getSkeletonEventSchemaFields();
141         assertEquals(5, skeletonFields.size());
142
143         try {
144             AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory()
145                     .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey());
146
147             Map<String, Schema> schemaMap = new LinkedHashMap<>();
148             SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap);
149             fail("test should throw an exception");
150         } catch (Exception apEx) {
151             assertEquals("context schema helper parameters not found for context schema  \"Avro\"", apEx.getMessage());
152         }
153
154         schemaParameters.getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
155
156         AvroSchemaHelper schemaHelper =
157                 (AvroSchemaHelper) new SchemaHelperFactory().createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey());
158
159         Map<String, Schema> schemaMap = new LinkedHashMap<>();
160         try {
161             SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap);
162         } catch (Exception exc) {
163             fail("test should not throw an exception");
164         }
165
166         eventSchema = SchemaUtils.getEventSchema(event);
167         assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"",
168                 eventSchema.toString().substring(0, 48));
169
170         epSchema = SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas);
171         assertEquals("\"string\"", epSchema.toString());
172
173         AxInputField inField =
174                 new AxInputField(new AxReferenceKey("FieldParent", "0.0.1", "Field"), avroCtxtSchema.getKey(), false);
175
176         Schema ep2Schema = SchemaUtils.getEventParameterSchema(inField, preexistingParamSchemas);
177         assertEquals("{\"type\":\"record\",\"name\":\"TopologyNodes\"", ep2Schema.toString().substring(0, 39));
178
179         skeletonFields = SchemaUtils.getSkeletonEventSchemaFields();
180         assertEquals(5, skeletonFields.size());
181
182         schemaParameters.getSchemaHelperParameterMap().put("Avro", new JavaSchemaHelperParameters());
183         try {
184             ep2Schema = SchemaUtils.getEventParameterSchema(inField, preexistingParamSchemas);
185             fail("test should throw an exception");
186         } catch (Exception apEx) {
187             assertEquals("FieldParent:0.0.1:NULL:Field: class/type", apEx.getMessage().substring(0, 40));
188         }
189
190         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
191         ModelService.clear();
192     }
193 }