Fix the CDS calls
[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.execution.ToscaMetadataExecutor;
33 import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParserWithDictionarySupport;
34 import org.onap.clamp.clds.tosca.update.templates.JsonTemplateManager;
35 import org.onap.clamp.clds.util.JsonUtils;
36 import org.onap.clamp.clds.util.ResourceFileUtil;
37 import org.onap.clamp.loop.service.Service;
38 import org.onap.clamp.tosca.Dictionary;
39 import org.onap.clamp.tosca.DictionaryElement;
40 import org.onap.clamp.tosca.DictionaryService;
41 import org.skyscreamer.jsonassert.JSONAssert;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
45
46 @RunWith(SpringJUnit4ClassRunner.class)
47 @SpringBootTest(classes = Application.class)
48 public class ToscaConverterWithDictionarySupportItCase {
49
50     @Autowired
51     private DictionaryService dictionaryService;
52
53     @Autowired
54     private ToscaMetadataParserWithDictionarySupport toscaMetadataParserWithDictionarySupport;
55
56     @Autowired
57     private ToscaMetadataExecutor toscaMetadataExecutor;
58
59     /**
60      * This Test validates Tosca yaml with metadata tag that contains policy_model_type and acronym
61      * parameters which defines the Tosca Policy name and its short name.
62      *
63      * @throws IOException In case of issue when opening the tosca yaml file and
64      *                     converted json file
65      */
66     @Test
67     @Transactional
68     public final void testMetadataClampPossibleValues() throws IOException, UnknownComponentException {
69
70         // Set up dictionary elements
71         Dictionary dictionaryTest = new Dictionary();
72         dictionaryTest.setName("Context");
73         dictionaryTest.setSecondLevelDictionary(0);
74
75         DictionaryElement element = new DictionaryElement();
76         element.setName("PROD");
77         element.setShortName("PROD");
78         element.setType("string");
79         element.setDescription("Production");
80         dictionaryTest.addDictionaryElements(element);
81
82         dictionaryService.saveOrUpdateDictionary(dictionaryTest);
83
84         Dictionary dictionaryTest1 = new Dictionary();
85         dictionaryTest1.setName("EventDictionary");
86         dictionaryTest1.setSecondLevelDictionary(0);
87
88         DictionaryElement element1 = new DictionaryElement();
89         element1.setName("alarmCondition");
90         element1.setShortName("alarmCondition");
91         element1.setType("string");
92         element1.setDescription("Alarm Condition");
93         dictionaryTest1.addDictionaryElements(element1);
94
95         dictionaryService.saveOrUpdateDictionary(dictionaryTest1);
96
97         Dictionary dictionaryTest2 = new Dictionary();
98         dictionaryTest2.setName("Operators");
99         dictionaryTest2.setSecondLevelDictionary(0);
100
101         DictionaryElement element2 = new DictionaryElement();
102         element2.setName("equals");
103         element2.setShortName("equals");
104         element2.setType("string");
105         element2.setDescription("equals");
106         dictionaryTest2.addDictionaryElements(element2);
107         dictionaryService.saveOrUpdateDictionary(dictionaryTest2);
108
109         JsonTemplateManager jsonTemplateManager =
110                 new JsonTemplateManager(
111                         ResourceFileUtil.getResourceAsString("tosca/new-converter/tosca_metadata_clamp_possible_values.yaml"),
112                         ResourceFileUtil.getResourceAsString("clds/tosca-converter/default-tosca-types.yaml"),
113                         ResourceFileUtil.getResourceAsString("clds/tosca-converter/templates.json"));
114
115         JsonObject jsonSchema = jsonTemplateManager.getJsonSchemaForPolicyType(
116                 "onap.policies.monitoring.cdap.tca.hi.lo.app", toscaMetadataParserWithDictionarySupport, null);
117
118         JSONAssert.assertEquals(
119                 ResourceFileUtil
120                         .getResourceAsString("tosca/new-converter/tca-with-metadata.json"),
121                 JsonUtils.GSON.toJson(jsonSchema), true);
122     }
123
124     @Test
125     @Transactional
126     public final void testMetadataClampPossibleValueWithExecutor() throws IOException, UnknownComponentException {
127         Service service = new Service(ResourceFileUtil.getResourceAsString("tosca/service-details.json"),
128                 ResourceFileUtil.getResourceAsString("tosca/resource-details.json"));
129         JsonTemplateManager jsonTemplateManager =
130                 new JsonTemplateManager(
131                         ResourceFileUtil.getResourceAsString("http-cache/example/policy/api/v1/policytypes/onap"
132                                 + ".policies.controlloop.operational.common.Apex/versions/1.0"
133                                 + ".0?connectionTimeToLive=5000/.file"),
134                         ResourceFileUtil.getResourceAsString("clds/tosca-converter/default-tosca-types.yaml"),
135                         ResourceFileUtil.getResourceAsString("clds/tosca-converter/templates.json"));
136
137         JsonObject jsonSchema = jsonTemplateManager.getJsonSchemaForPolicyType(
138                 "onap.policies.controlloop.operational.common.Apex", toscaMetadataParserWithDictionarySupport, service);
139
140         JSONAssert.assertEquals(
141                 ResourceFileUtil
142                         .getResourceAsString("tosca/new-converter/tosca_apex_with_metadata.json"),
143                 JsonUtils.GSON.toJson(jsonSchema), true);
144     }
145 }