2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
25 package org.onap.clamp.clds.tosca;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertEquals;
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.ResourceFileUtils;
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;
45 @RunWith(SpringJUnit4ClassRunner.class)
46 @SpringBootTest(classes = Application.class)
47 public class ToscaYamlToJsonConvertorTestItCase {
50 private DictionaryService dictionaryService;
53 private ToscaYamlToJsonConvertor toscaYamlToJsonConvertor;
56 * This Test validates TOSCA yaml to JSON Schema conversion based on JSON Editor
59 * @throws IOException In case of issue when opening the tosca yaml file and
63 public final void testParseToscaYaml() throws IOException {
64 String toscaModelYaml = ResourceFileUtils.getResourceAsString("tosca/tosca_example.yaml");
65 ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor();
67 String parsedJsonSchema =
68 convertor.parseToscaYaml(toscaModelYaml, "onap.policies.monitoring.cdap.tca.hi.lo.app");
69 assertNotNull(parsedJsonSchema);
70 JSONAssert.assertEquals(
71 ResourceFileUtils.getResourceAsString("tosca/policy-yaml-to-json.json"),
72 parsedJsonSchema, true);
76 * This Test validates TOSCA yaml with constraints to JSON Schema conversion
77 * based on JSON Editor Schema.
79 * @throws IOException In case of issue when opening the tosca yaml file and
83 public final void testParseToscaYamlWithConstraints() throws IOException {
84 String toscaModelYaml =
85 ResourceFileUtils.getResourceAsString("tosca/tosca-with-constraints.yaml");
86 ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor();
88 String parsedJsonSchema =
89 convertor.parseToscaYaml(toscaModelYaml, "onap.policies.monitoring.example.app");
90 assertNotNull(parsedJsonSchema);
91 JSONAssert.assertEquals(
92 ResourceFileUtils.getResourceAsString("tosca/policy-yaml-to-json-with-constraints.json"),
93 parsedJsonSchema, true);
97 * This Test validates TOSCA yaml with different datatypes to JSON Schema
98 * conversion based on JSON Editor Schema.
100 * @throws IOException In case of issue when opening the tosca yaml file and
101 * converted json file
104 public final void testParseToscaYamlWithTypes() throws IOException {
105 String toscaModelYaml =
106 ResourceFileUtils.getResourceAsString("tosca/tosca-with-datatypes.yaml");
107 ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor();
109 String parsedJsonSchema =
110 convertor.parseToscaYaml(toscaModelYaml, "onap.policies.monitoring.example.app");
111 assertNotNull(parsedJsonSchema);
112 JSONAssert.assertEquals(
113 ResourceFileUtils.getResourceAsString("tosca/policy-yaml-to-json-with-datatypes.json"),
114 parsedJsonSchema, true);
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.
121 * @throws IOException In case of issue when opening the tosca yaml file and
122 * converted json file
126 public final void testMetadataClampPossibleValues() throws IOException {
128 String toscaModelYaml =
129 ResourceFileUtils.getResourceAsString("tosca/tosca_metadata_clamp_possible_values.yaml");
131 JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(toscaModelYaml);
132 assertNotNull(jsonObject);
133 String policyModelType = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
134 ToscaSchemaConstants.METADATA_POLICY_MODEL_TYPE);
135 String acronym = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
136 ToscaSchemaConstants.METADATA_ACRONYM);
137 String parsedJsonSchema =
138 toscaYamlToJsonConvertor.parseToscaYaml(toscaModelYaml, policyModelType);
140 assertNotNull(parsedJsonSchema);
141 assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyModelType);
142 assertEquals("tca", acronym);
143 JSONAssert.assertEquals(
145 .getResourceAsString("tosca/tosca_metadata_clamp_possible_values_json_schema.json"),
146 parsedJsonSchema, true);
150 private void setupDictionary() {
152 // Set up dictionary elements
153 Dictionary dictionaryTest = new Dictionary();
154 dictionaryTest.setName("Context");
155 dictionaryTest.setSecondLevelDictionary(0);
157 DictionaryElement element = new DictionaryElement();
158 element.setName("PROD");
159 element.setShortName("PROD");
160 element.setType("string");
161 element.setDescription("Production");
162 dictionaryTest.addDictionaryElements(element);
164 dictionaryService.saveOrUpdateDictionary(dictionaryTest);
166 Dictionary dictionaryTest1 = new Dictionary();
167 dictionaryTest1.setName("EventDictionary");
168 dictionaryTest1.setSecondLevelDictionary(0);
170 DictionaryElement element1 = new DictionaryElement();
171 element1.setName("alarmCondition");
172 element1.setShortName("alarmCondition");
173 element1.setType("string");
174 element1.setDescription("Alarm Condition");
175 dictionaryTest1.addDictionaryElements(element1);
177 dictionaryTest1 = dictionaryService.saveOrUpdateDictionary(dictionaryTest1);
179 DictionaryElement element3 = new DictionaryElement();
180 element3.setName("timeEpoch");
181 element3.setShortName("timeEpoch");
182 element3.setType("datetime");
183 element3.setDescription("Time Epoch");
184 dictionaryTest1.addDictionaryElements(element3);
186 dictionaryService.saveOrUpdateDictionary(dictionaryTest1);
188 Dictionary dictionaryTest2 = new Dictionary();
189 dictionaryTest2.setName("Operators");
190 dictionaryTest2.setSecondLevelDictionary(0);
192 DictionaryElement element2 = new DictionaryElement();
193 element2.setName("equals");
194 element2.setShortName("equals");
195 element2.setType("string|datetime");
196 element2.setDescription("equals");
197 dictionaryTest2.addDictionaryElements(element2);
198 dictionaryService.saveOrUpdateDictionary(dictionaryTest2);