Replace try/catch with assertj
[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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
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
81         assertThatThrownBy(() -> SchemaUtils.getEventSchema(event))
82             .hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas"
83                 + " not found in model service");
84
85         assertThatThrownBy(() -> {
86             Map<String, Schema> preexistingParamSchemas = new LinkedHashMap<>();
87             SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas);
88         }).hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas"
89                 + " not found in model service");
90
91         List<Field> skeletonFields = SchemaUtils.getSkeletonEventSchemaFields();
92         assertEquals(5, skeletonFields.size());
93
94         AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl");
95         AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1");
96         assertThatThrownBy(() -> {
97             AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory()
98                     .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey());
99
100             Map<String, Schema> schemaMap = new LinkedHashMap<>();
101             SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap);
102         }).hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas"
103                 + " not found in model service");
104     }
105
106     @Test
107     public void testSchemaUtils() throws ApexEventException {
108         ParameterService.clear();
109         final SchemaParameters schemaParameters = new SchemaParameters();
110         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
111         ParameterService.register(schemaParameters);
112
113         ModelService.registerModel(AxModel.class, avroModel);
114         ModelService.registerModel(AxContextSchemas.class, avroModel.getSchemas());
115
116         AxEvent event = avroModel.getEvents().get("CustomerContextEventIn");
117
118         Schema eventSchema = SchemaUtils.getEventSchema(event);
119         assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"",
120                 eventSchema.toString().substring(0, 48));
121
122         Map<String, Schema> preexistingParamSchemas = new LinkedHashMap<>();
123         Schema epSchema =
124                 SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas);
125         assertEquals("\"string\"", epSchema.toString());
126
127         AxContextSchema avroCtxtSchema = avroModel.getSchemas().get("ctxtTopologyNodesDecl");
128         AxArtifactKey topoNodesKey = new AxArtifactKey("albumTopoNodes", "0.0.1");
129         List<Field> skeletonFields = SchemaUtils.getSkeletonEventSchemaFields();
130         assertEquals(5, skeletonFields.size());
131
132         assertThatThrownBy(() -> {
133             AvroSchemaHelper schemaHelper = (AvroSchemaHelper) new SchemaHelperFactory()
134                     .createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey());
135
136             Map<String, Schema> schemaMap = new LinkedHashMap<>();
137             SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap);
138         }).hasMessage("context schema helper parameters not found for context schema  \"Avro\"");
139
140         schemaParameters.getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
141
142         AvroSchemaHelper schemaHelper =
143                 (AvroSchemaHelper) new SchemaHelperFactory().createSchemaHelper(topoNodesKey, avroCtxtSchema.getKey());
144
145         Map<String, Schema> schemaMap = new LinkedHashMap<>();
146         SchemaUtils.processSubSchemas(schemaHelper.getAvroSchema(), schemaMap);
147
148         eventSchema = SchemaUtils.getEventSchema(event);
149         assertEquals("{\"type\":\"record\",\"name\":\"CustomerContextEventIn\"",
150                 eventSchema.toString().substring(0, 48));
151
152         epSchema = SchemaUtils.getEventParameterSchema(event.getParameterMap().get("links"), preexistingParamSchemas);
153         assertEquals("\"string\"", epSchema.toString());
154
155         AxInputField inField =
156                 new AxInputField(new AxReferenceKey("FieldParent", "0.0.1", "Field"), avroCtxtSchema.getKey(), false);
157
158         Schema ep2Schema = SchemaUtils.getEventParameterSchema(inField, preexistingParamSchemas);
159         assertEquals("{\"type\":\"record\",\"name\":\"TopologyNodes\"", ep2Schema.toString().substring(0, 39));
160
161         skeletonFields = SchemaUtils.getSkeletonEventSchemaFields();
162         assertEquals(5, skeletonFields.size());
163
164         schemaParameters.getSchemaHelperParameterMap().put("Avro", new JavaSchemaHelperParameters());
165         assertThatThrownBy(() -> SchemaUtils.getEventParameterSchema(inField, preexistingParamSchemas))
166             .hasMessageContaining("FieldParent:0.0.1:NULL:Field: class/type");
167
168         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
169         ModelService.clear();
170     }
171 }