552101f94f1352e9370be287986e145e0be8bd06
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.tools.model.generator.model2event;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25
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;
39
40 /**
41  * Test the Model2EventSchema.
42  */
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";
48
49     /**
50      * Set ups parameterService for the test.
51      */
52     @BeforeClass
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);
59         }
60     }
61
62     @Test
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();
69
70     }
71
72     @Test
73     public void testEventSchemaBadType() {
74         modelFile = "src/test/resources/SmallModel.json";
75         type = "default";
76         Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
77         assertThatCode(() -> {
78             int ret = app.runApp();
79             assertEquals(-1, ret);
80         }).doesNotThrowAnyException();
81     }
82
83     @Test
84     public void testEventSchemaStimuli() {
85         modelFile = "src/test/resources/SmallModel.json";
86         type = "stimuli";
87         Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
88         assertThatCode(() -> {
89             int ret = app.runApp();
90             assertEquals(0, ret);
91         }).doesNotThrowAnyException();
92     }
93
94     @Test
95     public void testEventSchemaResponse() {
96         modelFile = "src/test/resources/SmallModel.json";
97         type = "response";
98         Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
99         assertThatCode(() -> {
100             int ret = app.runApp();
101             assertEquals(0, ret);
102         }).doesNotThrowAnyException();
103     }
104
105     @Test
106     public void testEventSchemaInternal() {
107         modelFile = "src/test/resources/SmallModel.json";
108         type = "internal";
109         Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
110         assertThatCode(() -> {
111             int ret = app.runApp();
112             assertEquals(0, ret);
113         }).doesNotThrowAnyException();
114     }
115
116     @Test
117     public void testEventSchemaNotSimpleType() {
118         modelFile = "src/test/resources/ExecutionPropertiesRestTestPolicyModel.json";
119         type = "internal";
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");
122
123         Field stringField = new Field("string", Schema.create(Type.STRING), null, null);
124         Field enumField =
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();
135     }
136
137     @AfterClass
138     public static void cleanTest() {
139         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
140         ModelService.clear();
141     }
142 }