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