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