db6fd5c032f919a4110e61eb0c040e1272e7a137
[clamp.git] / src / test / java / org / onap / clamp / clds / tosca / ToscaYamlToJsonConvertorTestItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * Modifications Copyright (C) 2019 Huawei Technologies Co., Ltd.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END============================================
21  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.clds.tosca;
26
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertEquals;
29
30 import com.google.gson.JsonObject;
31 import java.io.IOException;
32 import javax.transaction.Transactional;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.onap.clamp.clds.Application;
36 import org.onap.clamp.clds.util.ResourceFileUtil;
37 import org.onap.clamp.tosca.Dictionary;
38 import org.onap.clamp.tosca.DictionaryElement;
39 import org.onap.clamp.tosca.DictionaryService;
40 import org.skyscreamer.jsonassert.JSONAssert;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
44
45 @RunWith(SpringJUnit4ClassRunner.class)
46 @SpringBootTest(classes = Application.class)
47 public class ToscaYamlToJsonConvertorTestItCase {
48
49     @Autowired
50     private DictionaryService dictionaryService;
51
52     @Autowired
53     private ToscaYamlToJsonConvertor toscaYamlToJsonConvertor;
54
55     /**
56      * This Test validates TOSCA yaml to JSON Schema conversion based on JSON Editor
57      * Schema.
58      *
59      * @throws IOException In case of issue when opening the tosca yaml file and
60      *         converted json file
61      */
62     @Test
63     public final void testParseToscaYaml() throws IOException {
64         String toscaModelYaml = ResourceFileUtil.getResourceAsString("tosca/tosca_example.yaml");
65         ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor();
66
67         String parsedJsonSchema =
68             convertor.parseToscaYaml(toscaModelYaml, "onap.policies.monitoring.cdap.tca.hi.lo.app");
69         assertNotNull(parsedJsonSchema);
70         JSONAssert.assertEquals(
71             ResourceFileUtil.getResourceAsString("tosca/policy-yaml-to-json.json"),
72             parsedJsonSchema, true);
73     }
74
75     /**
76      * This Test validates TOSCA yaml with constraints to JSON Schema conversion
77      * based on JSON Editor Schema.
78      *
79      * @throws IOException In case of issue when opening the tosca yaml file and
80      *         converted json file
81      */
82     @Test
83     public final void testParseToscaYamlWithConstraints() throws IOException {
84         String toscaModelYaml =
85             ResourceFileUtil.getResourceAsString("tosca/tosca-with-constraints.yaml");
86         ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor();
87
88         String parsedJsonSchema =
89             convertor.parseToscaYaml(toscaModelYaml, "onap.policies.monitoring.example.app");
90         assertNotNull(parsedJsonSchema);
91         JSONAssert.assertEquals(
92             ResourceFileUtil.getResourceAsString("tosca/policy-yaml-to-json-with-constraints.json"),
93             parsedJsonSchema, true);
94     }
95
96     /**
97      * This Test validates TOSCA yaml with different datatypes to JSON Schema
98      * conversion based on JSON Editor Schema.
99      *
100      * @throws IOException In case of issue when opening the tosca yaml file and
101      *         converted json file
102      */
103     @Test
104     public final void testParseToscaYamlWithTypes() throws IOException {
105         String toscaModelYaml =
106             ResourceFileUtil.getResourceAsString("tosca/tosca-with-datatypes.yaml");
107         ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor();
108
109         String parsedJsonSchema =
110             convertor.parseToscaYaml(toscaModelYaml, "onap.policies.monitoring.example.app");
111         assertNotNull(parsedJsonSchema);
112         JSONAssert.assertEquals(
113             ResourceFileUtil.getResourceAsString("tosca/policy-yaml-to-json-with-datatypes.json"),
114             parsedJsonSchema, true);
115     }
116
117     /**
118      * This Test validates Tosca yaml with metadata tag that contains policy_model_type and acronym
119      * parameters which defines the Tosca Policy name and its short name.
120      *
121      * @throws IOException In case of issue when opening the tosca yaml file and
122      *         converted json file
123      */
124     @Test
125     @Transactional
126     public final void testMetadataClampPossibleValues() throws IOException {
127
128         // Set up dictionary elements
129         Dictionary dictionaryTest = new Dictionary();
130         dictionaryTest.setName("Context");
131         dictionaryTest.setSecondLevelDictionary(0);
132
133         DictionaryElement element = new DictionaryElement();
134         element.setName("PROD");
135         element.setShortName("PROD");
136         element.setType("string");
137         element.setDescription("Production");
138         dictionaryTest.addDictionaryElements(element);
139
140         dictionaryService.saveOrUpdateDictionary(dictionaryTest);
141
142         Dictionary dictionaryTest1 = new Dictionary();
143         dictionaryTest1.setName("EventDictionary");
144         dictionaryTest1.setSecondLevelDictionary(0);
145
146         DictionaryElement element1 = new DictionaryElement();
147         element1.setName("alarmCondition");
148         element1.setShortName("alarmCondition");
149         element1.setType("string");
150         element1.setDescription("Alarm Condition");
151         dictionaryTest1.addDictionaryElements(element1);
152
153         dictionaryService.saveOrUpdateDictionary(dictionaryTest1);
154
155         Dictionary dictionaryTest2 = new Dictionary();
156         dictionaryTest2.setName("Operators");
157         dictionaryTest2.setSecondLevelDictionary(0);
158
159         DictionaryElement element2 = new DictionaryElement();
160         element2.setName("equals");
161         element2.setShortName("equals");
162         element2.setType("string");
163         element2.setDescription("equals");
164         dictionaryTest2.addDictionaryElements(element2);
165         dictionaryService.saveOrUpdateDictionary(dictionaryTest2);
166
167         String toscaModelYaml =
168             ResourceFileUtil.getResourceAsString("tosca/tosca_metadata_clamp_possible_values.yaml");
169
170         JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(toscaModelYaml);
171         assertNotNull(jsonObject);
172         String policyModelType = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
173             ToscaSchemaConstants.METADATA_POLICY_MODEL_TYPE);
174         String acronym = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
175             ToscaSchemaConstants.METADATA_ACRONYM);
176         String parsedJsonSchema =
177             toscaYamlToJsonConvertor.parseToscaYaml(toscaModelYaml, policyModelType);
178
179         assertNotNull(parsedJsonSchema);
180         assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyModelType);
181         assertEquals("tca", acronym);
182         JSONAssert.assertEquals(
183             ResourceFileUtil
184                 .getResourceAsString("tosca/tosca_metadata_clamp_possible_values_json_schema.json"),
185             parsedJsonSchema, true);
186     }
187
188 }