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