9c7eccd0fc05dc1cffe02a86892897788cf937d5
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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.model2event;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertEquals;
26
27 import java.util.Arrays;
28 import org.apache.avro.Schema;
29 import org.apache.avro.Schema.Field;
30 import org.apache.avro.Schema.Type;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
35 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
36 import org.onap.policy.apex.context.parameters.SchemaParameters;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
38 import org.onap.policy.apex.model.basicmodel.service.ModelService;
39 import org.onap.policy.common.parameters.ParameterService;
40 import org.stringtemplate.v4.STGroupFile;
41
42 /**
43  * Test the Model2EventSchema.
44  */
45 public class Model2EventSchemaTest {
46     String modelFile = "src/test/resources/blankSchema.json";
47     String type = "stimuli";
48     /** The name of the application. */
49     public static final String APP_NAME = "gen-model2eventSchema";
50
51     /**
52      * Set ups parameterService for the test.
53      */
54     @BeforeClass
55     public static void prepareForTest() {
56         final SchemaParameters schemaParameters = new SchemaParameters();
57         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
58         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
59         if (!ParameterService.contains(ContextParameterConstants.SCHEMA_GROUP_NAME)) {
60             ParameterService.register(schemaParameters);
61         }
62     }
63
64     @Test
65     public void testEventSchemaBadModelFile() {
66         Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
67         assertThatCode(() -> {
68             int ret = app.runApp();
69             assertEquals(-1, ret);
70         }).doesNotThrowAnyException();
71
72     }
73
74     @Test
75     public void testEventSchemaBadType() {
76         modelFile = "src/test/resources/SmallModel.json";
77         type = "default";
78         Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
79         assertThatCode(() -> {
80             int ret = app.runApp();
81             assertEquals(-1, ret);
82         }).doesNotThrowAnyException();
83     }
84
85     @Test
86     public void testEventSchemaStimuli() throws ApexException {
87         modelFile = "src/test/resources/SmallModel.json";
88
89         String[] types = {"stimuli", "response", "internal"};
90         for (String type2: types) {
91             type = type2;
92             Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
93             assertEquals(type, 0, app.runApp());
94         }
95     }
96
97     @Test
98     public void testEventSchemaNotSimpleType() {
99         modelFile = "src/test/resources/ExecutionPropertiesRestTestPolicyModel.json";
100         type = "internal";
101         Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME);
102         final STGroupFile stg = new STGroupFile("org/onap/policy/apex/tools/model/generator/event-json.stg");
103
104         Field stringField = new Field("string", Schema.create(Type.STRING), null, null);
105         Field enumField =
106                 new Field("enum", Schema.createEnum("my_enum", "doc", null, Arrays.asList("a", "b", "c")), null, null);
107         Schema schema = Schema.createRecord("my_record", "doc", "mytest", false);
108         schema.setFields(Arrays.asList(stringField, enumField));
109         Schema arrayOut = Schema.createArray(schema);
110         Schema mapOut = Schema.createMap(arrayOut);
111         app.addFieldType(mapOut, stg);
112         assertThatCode(() -> {
113             int ret = app.runApp();
114             assertEquals(0, ret);
115         }).doesNotThrowAnyException();
116     }
117
118     @AfterClass
119     public static void cleanTest() {
120         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
121         ModelService.clear();
122     }
123 }