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;
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;
59 * Test the Key Info Getter.
61 public class SchemaUtilsTest {
62 private static AxPolicyModel avroModel;
65 * Read the models into strings.
67 * @throws IOException on model reading errors
68 * @throws ApexModelException on model reading exceptions
71 public static void readSimpleModel() throws IOException, ApexModelException {
72 String avroModelString = TextFileUtils.getTextFileAsString("target/examples/models/pcvs/vpnsla/vpnsla.json");
74 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
75 avroModel = modelReader.read(new ByteArrayInputStream(avroModelString.getBytes()));
79 public void testSchemaUtilsErrors() throws ApexEventException {
80 AxEvent event = avroModel.getEvents().get("CustomerContextEventIn");
81 AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl");
83 AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1");
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());
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());
101 List<Field> skeletonFields = SchemaUtils.getSkeletonEventSchemaFields();
102 assertEquals(5, skeletonFields.size());
105 AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory()
106 .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey());
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());
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);
124 ModelService.registerModel(AxModel.class, avroModel);
125 ModelService.registerModel(AxContextSchemas.class, avroModel.getSchemas());
127 AxEvent event = avroModel.getEvents().get("CustomerContextEventIn");
128 AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl");
129 AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1");
131 Schema eventSchema = SchemaUtils.getEventSchema(event);
132 assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"",
133 eventSchema.toString().substring(0, 48));
135 Map<String, Schema> preexistingParamSchemas = new LinkedHashMap<>();
137 SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas);
138 assertEquals("\"string\"", epSchema.toString());
140 List<Field> skeletonFields = SchemaUtils.getSkeletonEventSchemaFields();
141 assertEquals(5, skeletonFields.size());
144 AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory()
145 .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey());
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());
154 schemaParameters.getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
156 AvroSchemaHelper schemaHelper =
157 (AvroSchemaHelper) new SchemaHelperFactory().createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey());
159 Map<String, Schema> schemaMap = new LinkedHashMap<>();
161 SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap);
162 } catch (Exception exc) {
163 fail("test should not throw an exception");
166 eventSchema = SchemaUtils.getEventSchema(event);
167 assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"",
168 eventSchema.toString().substring(0, 48));
170 epSchema = SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas);
171 assertEquals("\"string\"", epSchema.toString());
173 AxInputField inField =
174 new AxInputField(new AxReferenceKey("FieldParent", "0.0.1", "Field"), avroCtxtSchema.getKey(), false);
176 Schema ep2Schema = SchemaUtils.getEventParameterSchema(inField, preexistingParamSchemas);
177 assertEquals("{\"type\":\"record\",\"name\":\"TopologyNodes\"", ep2Schema.toString().substring(0, 39));
179 skeletonFields = SchemaUtils.getSkeletonEventSchemaFields();
180 assertEquals(5, skeletonFields.size());
182 schemaParameters.getSchemaHelperParameterMap().put("Avro", new JavaSchemaHelperParameters());
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));
190 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
191 ModelService.clear();