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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.tools.model.generator;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.util.LinkedHashMap;
30 import java.util.List;
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;
58 * Test the Key Info Getter.
60 public class SchemaUtilsTest {
61 private static AxPolicyModel avroModel;
64 * Read the models into strings.
66 * @throws IOException on model reading errors
67 * @throws ApexModelException on model reading exceptions
70 public static void readSimpleModel() throws IOException, ApexModelException {
71 String avroModelString = TextFileUtils.getTextFileAsString("target/examples/models/pcvs/vpnsla/vpnsla.json");
73 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
74 avroModel = modelReader.read(new ByteArrayInputStream(avroModelString.getBytes()));
78 public void testSchemaUtilsErrors() throws ApexEventException {
79 AxEvent event = avroModel.getEvents().get("CustomerContextEventIn");
80 AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl");
82 AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1");
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());
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());
100 List<Field> skeletonFields = SchemaUtils.getSkeletonEventSchemaFields();
101 assertEquals(5, skeletonFields.size());
104 AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory()
105 .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey());
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());
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);
123 ModelService.registerModel(AxModel.class, avroModel);
124 ModelService.registerModel(AxContextSchemas.class, avroModel.getSchemas());
126 AxEvent event = avroModel.getEvents().get("CustomerContextEventIn");
127 AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl");
128 AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1");
130 Schema eventSchema = SchemaUtils.getEventSchema(event);
131 assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"",
132 eventSchema.toString().substring(0, 48));
134 Map<String, Schema> preexistingParamSchemas = new LinkedHashMap<>();
136 SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas);
137 assertEquals("\"string\"", epSchema.toString());
139 List<Field> skeletonFields = SchemaUtils.getSkeletonEventSchemaFields();
140 assertEquals(5, skeletonFields.size());
143 AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory()
144 .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey());
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());
153 schemaParameters.getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
155 AvroSchemaHelper schemaHelper =
156 (AvroSchemaHelper) new SchemaHelperFactory().createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey());
158 Map<String, Schema> schemaMap = new LinkedHashMap<>();
160 SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap);
161 } catch (Exception exc) {
162 fail("test should not throw an exception");
165 eventSchema = SchemaUtils.getEventSchema(event);
166 assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"",
167 eventSchema.toString().substring(0, 48));
169 epSchema = SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas);
170 assertEquals("\"string\"", epSchema.toString());
172 AxInputField inField =
173 new AxInputField(new AxReferenceKey("FieldParent", "0.0.1", "Field"), avroCtxtSchema.getKey(), false);
175 Schema ep2Schema = SchemaUtils.getEventParameterSchema(inField, preexistingParamSchemas);
176 assertEquals("{\"type\":\"record\",\"name\":\"TopologyNodes\"", ep2Schema.toString().substring(0, 39));
178 skeletonFields = SchemaUtils.getSkeletonEventSchemaFields();
179 assertEquals(5, skeletonFields.size());
181 schemaParameters.getSchemaHelperParameterMap().put("Avro", new JavaSchemaHelperParameters());
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));
189 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
190 ModelService.clear();