2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.tools.model.generator.model2event;
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
26 import java.util.Arrays;
27 import org.apache.avro.Schema;
28 import org.apache.avro.Schema.Field;
29 import org.apache.avro.Schema.Type;
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
34 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
35 import org.onap.policy.apex.context.parameters.SchemaParameters;
36 import org.onap.policy.apex.model.basicmodel.service.ModelService;
37 import org.onap.policy.common.parameters.ParameterService;
38 import org.stringtemplate.v4.STGroupFile;
41 * Test the Model2EventSchema.
43 public class Model2EventSchemaTest {
44 String modelFile = "src/test/resources/blankSchema.json";
45 String type = "stimuli";
46 /** The name of the application. */
47 public static final String APP_NAME = "gen-model2eventSchema";
50 * Set ups parameterService for the test.
53 public static void prepareForTest() {
54 final SchemaParameters schemaParameters = new SchemaParameters();
55 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
56 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
57 if (!ParameterService.contains(ContextParameterConstants.SCHEMA_GROUP_NAME)) {
58 ParameterService.register(schemaParameters);
63 public void testEventSchemaBadModelFile() {
64 Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
65 assertThatCode(() -> {
66 int ret = app.runApp();
67 assertEquals(-1, ret);
68 }).doesNotThrowAnyException();
73 public void testEventSchemaBadType() {
74 modelFile = "src/test/resources/SmallModel.json";
76 Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
77 assertThatCode(() -> {
78 int ret = app.runApp();
79 assertEquals(-1, ret);
80 }).doesNotThrowAnyException();
84 public void testEventSchemaStimuli() {
85 modelFile = "src/test/resources/SmallModel.json";
87 Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
88 assertThatCode(() -> {
89 int ret = app.runApp();
91 }).doesNotThrowAnyException();
95 public void testEventSchemaResponse() {
96 modelFile = "src/test/resources/SmallModel.json";
98 Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
99 assertThatCode(() -> {
100 int ret = app.runApp();
101 assertEquals(0, ret);
102 }).doesNotThrowAnyException();
106 public void testEventSchemaInternal() {
107 modelFile = "src/test/resources/SmallModel.json";
109 Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
110 assertThatCode(() -> {
111 int ret = app.runApp();
112 assertEquals(0, ret);
113 }).doesNotThrowAnyException();
117 public void testEventSchemaNotSimpleType() {
118 modelFile = "src/test/resources/ExecutionPropertiesRestTestPolicyModel.json";
120 Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
121 final STGroupFile stg = new STGroupFile("org/onap/policy/apex/tools/model/generator/event-json.stg");
123 Field stringField = new Field("string", Schema.create(Type.STRING), null, null);
125 new Field("enum", Schema.createEnum("my_enum", "doc", null, Arrays.asList("a", "b", "c")), null, null);
126 Schema schema = Schema.createRecord("my_record", "doc", "mytest", false);
127 schema.setFields(Arrays.asList(stringField, enumField));
128 Schema arrayOut = Schema.createArray(schema);
129 Schema mapOut = Schema.createMap(arrayOut);
130 app.addFieldType(mapOut, stg);
131 assertThatCode(() -> {
132 int ret = app.runApp();
133 assertEquals(0, ret);
134 }).doesNotThrowAnyException();
138 public static void cleanTest() {
139 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
140 ModelService.clear();