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 com.google.gson.JsonObject;
28 import java.io.IOException;
29 import javax.transaction.Transactional;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.onap.clamp.clds.Application;
33 import org.onap.clamp.clds.util.ResourceFileUtil;
34 import org.onap.clamp.tosca.Dictionary;
35 import org.onap.clamp.tosca.DictionaryElement;
36 import org.onap.clamp.tosca.DictionaryService;
37 import org.skyscreamer.jsonassert.JSONAssert;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
42 import static org.junit.Assert.assertNotNull;
43 import static org.junit.jupiter.api.Assertions.assertEquals;
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 = ResourceFileUtil.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 ResourceFileUtil.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 ResourceFileUtil.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 ResourceFileUtil.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 ResourceFileUtil.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 ResourceFileUtil.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 // Set up dictionary elements
129 Dictionary dictionaryTest = new Dictionary();
130 dictionaryTest.setName("Context");
131 dictionaryTest.setSecondLevelDictionary(0);
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);
140 dictionaryService.saveOrUpdateDictionary(dictionaryTest);
142 Dictionary dictionaryTest1 = new Dictionary();
143 dictionaryTest1.setName("EventDictionary");
144 dictionaryTest1.setSecondLevelDictionary(0);
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);
153 dictionaryService.saveOrUpdateDictionary(dictionaryTest1);
155 Dictionary dictionaryTest2 = new Dictionary();
156 dictionaryTest2.setName("Operators");
157 dictionaryTest2.setSecondLevelDictionary(0);
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);
167 String toscaModelYaml =
168 ResourceFileUtil.getResourceAsString("tosca/tosca_metadata_clamp_possible_values.yaml");
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);
179 assertNotNull(parsedJsonSchema);
180 assertEquals("onap.policies.monitoring.cdap.tca.hi.lo.app", policyModelType);
181 assertEquals("tca", acronym);
182 JSONAssert.assertEquals(
184 .getResourceAsString("tosca/tosca_metadata_clamp_possible_values_json_schema.json"),
185 parsedJsonSchema, true);