b4f8b12458deb2315f84e545542abaf957165b40
[clamp.git] / src / test / java / org / onap / clamp / clds / tosca / update / ToscaConverterWithDictionarySupportItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
7   * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.tosca.update;
25
26 import com.google.gson.JsonObject;
27 import java.io.IOException;
28 import javax.transaction.Transactional;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.clamp.clds.Application;
32 import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParserWithDictionarySupport;
33 import org.onap.clamp.clds.tosca.update.templates.JsonTemplateManager;
34 import org.onap.clamp.clds.util.JsonUtils;
35 import org.onap.clamp.clds.util.ResourceFileUtil;
36 import org.onap.clamp.tosca.Dictionary;
37 import org.onap.clamp.tosca.DictionaryElement;
38 import org.onap.clamp.tosca.DictionaryService;
39 import org.skyscreamer.jsonassert.JSONAssert;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
43
44 @RunWith(SpringJUnit4ClassRunner.class)
45 @SpringBootTest(classes = Application.class)
46 public class ToscaConverterWithDictionarySupportItCase {
47
48     @Autowired
49     private DictionaryService dictionaryService;
50
51     @Autowired
52     private ToscaMetadataParserWithDictionarySupport toscaMetadataParserWithDictionarySupport;
53
54     /**
55      * This Test validates Tosca yaml with metadata tag that contains policy_model_type and acronym
56      * parameters which defines the Tosca Policy name and its short name.
57      *
58      * @throws IOException In case of issue when opening the tosca yaml file and
59      *                     converted json file
60      */
61     @Test
62     @Transactional
63     public final void testMetadataClampPossibleValues() throws IOException, UnknownComponentException {
64
65         // Set up dictionary elements
66         Dictionary dictionaryTest = new Dictionary();
67         dictionaryTest.setName("Context");
68         dictionaryTest.setSecondLevelDictionary(0);
69
70         DictionaryElement element = new DictionaryElement();
71         element.setName("PROD");
72         element.setShortName("PROD");
73         element.setType("string");
74         element.setDescription("Production");
75         dictionaryTest.addDictionaryElements(element);
76
77         dictionaryService.saveOrUpdateDictionary(dictionaryTest);
78
79         Dictionary dictionaryTest1 = new Dictionary();
80         dictionaryTest1.setName("EventDictionary");
81         dictionaryTest1.setSecondLevelDictionary(0);
82
83         DictionaryElement element1 = new DictionaryElement();
84         element1.setName("alarmCondition");
85         element1.setShortName("alarmCondition");
86         element1.setType("string");
87         element1.setDescription("Alarm Condition");
88         dictionaryTest1.addDictionaryElements(element1);
89
90         dictionaryService.saveOrUpdateDictionary(dictionaryTest1);
91
92         Dictionary dictionaryTest2 = new Dictionary();
93         dictionaryTest2.setName("Operators");
94         dictionaryTest2.setSecondLevelDictionary(0);
95
96         DictionaryElement element2 = new DictionaryElement();
97         element2.setName("equals");
98         element2.setShortName("equals");
99         element2.setType("string");
100         element2.setDescription("equals");
101         dictionaryTest2.addDictionaryElements(element2);
102         dictionaryService.saveOrUpdateDictionary(dictionaryTest2);
103
104         JsonTemplateManager jsonTemplateManager =
105                 new JsonTemplateManager(
106                         ResourceFileUtil.getResourceAsString("tosca/tosca_metadata_clamp_possible_values.yaml"),
107                         ResourceFileUtil.getResourceAsString("clds/tosca-converter/default-tosca-types.yaml"),
108                         ResourceFileUtil.getResourceAsString("clds/tosca-converter/templates.json"));
109
110         JsonObject jsonSchema = jsonTemplateManager.getJsonSchemaForPolicyType(
111                 "onap.policies.monitoring.cdap.tca.hi.lo.app", toscaMetadataParserWithDictionarySupport);
112
113         JSONAssert.assertEquals(
114                 ResourceFileUtil
115                         .getResourceAsString("tosca/new-converter/tca-with-metadata.json"),
116                 JsonUtils.GSON.toJson(jsonSchema), true);
117     }
118 }