Rework tosca converter
[clamp.git] / src / test / java / org / onap / clamp / clds / tosca / update / TemplateManagementTest.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 java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import junit.framework.TestCase;
31 import org.onap.clamp.clds.util.ResourceFileUtil;
32
33 public class TemplateManagementTest extends TestCase {
34
35     /**
36      * Test the launch translation wit operational policies.
37      *
38      * @throws IOException               In case of failure
39      * @throws UnknownComponentException In case of failure
40      */
41     public void testLaunchTranslationTca() throws IOException, UnknownComponentException {
42         TemplateManagement templateManagement =
43                 new TemplateManagement(
44                         ResourceFileUtil.getResourceAsString("http-cache/example/policy/api/v1/policytypes/onap"
45                                 + ".policies.monitoring.cdap.tca.hi.lo.app/versions/1.0.0?"
46                                 + "connectionTimeToLive=5000/.file"), ResourceFileUtil.getResourceAsString(
47                         "clds/tosca_update/defaultToscaTypes.yaml"),
48                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
49         assertNull(templateManagement.getParseToJson());
50         String componentName = "onap.policies.monitoring.cdap.tca.hi.lo.app";
51         templateManagement.launchTranslation(componentName);
52         assertNotNull(templateManagement.getParseToJson());
53     }
54
55     /**
56      * Test the launch translation wit operational policies.
57      *
58      * @throws IOException               In case of failure
59      * @throws UnknownComponentException In case of failure
60      */
61     public void testLaunchTranslationFrequencyLimiter() throws IOException, UnknownComponentException {
62         TemplateManagement templateManagement =
63                 new TemplateManagement(
64                         ResourceFileUtil.getResourceAsString("http-cache/example/policy/api/v1/policytypes/onap"
65                                 + ".policies.controlloop.guard.common.FrequencyLimiter/versions/1.0.0?"
66                                 + "connectionTimeToLive=5000/.file"), ResourceFileUtil.getResourceAsString(
67                         "clds/tosca_update/defaultToscaTypes.yaml"),
68                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
69         assertNull(templateManagement.getParseToJson());
70         String componentName = "onap.policies.controlloop.guard.common.FrequencyLimiter";
71         templateManagement.launchTranslation(componentName);
72         assertNotNull(templateManagement.getParseToJson());
73     }
74
75     /**
76      * Test the launch translation wit operational policies.
77      *
78      * @throws IOException               In case of failure
79      * @throws UnknownComponentException In case of failure
80      */
81     public void testLaunchTranslationApex() throws IOException, UnknownComponentException {
82         TemplateManagement templateManagement =
83                 new TemplateManagement(
84                         ResourceFileUtil.getResourceAsString("http-cache/example/policy/api/v1/policytypes/onap"
85                                 + ".policies.controlloop.operational.common.Apex/versions/1.0.0?"
86                                 + "connectionTimeToLive=5000/.file"), ResourceFileUtil.getResourceAsString(
87                         "clds/tosca_update/defaultToscaTypes.yaml"),
88                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
89         assertNull(templateManagement.getParseToJson());
90         String componentName = "onap.policies.controlloop.operational.common.Apex";
91         templateManagement.launchTranslation(componentName);
92         assertNotNull(templateManagement.getParseToJson());
93     }
94
95     /**
96      * Test the launch translation wit operational policies.
97      *
98      * @throws IOException               In case of failure
99      * @throws UnknownComponentException In case of failure
100      */
101     public void testLaunchTranslationDrools() throws IOException, UnknownComponentException {
102         TemplateManagement templateManagement =
103                 new TemplateManagement(
104                         ResourceFileUtil.getResourceAsString("http-cache/example/policy/api/v1/policytypes/onap"
105                                 + ".policies.controlloop.operational.common.Drools/versions/1.0.0?"
106                                 + "connectionTimeToLive=5000/.file"), ResourceFileUtil.getResourceAsString(
107                         "clds/tosca_update/defaultToscaTypes.yaml"),
108                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
109         assertNull(templateManagement.getParseToJson());
110         String componentName = "onap.policies.controlloop.operational.common.Drools";
111         templateManagement.launchTranslation(componentName);
112         assertNotNull(templateManagement.getParseToJson());
113     }
114
115     /**
116      * Test the launch translation.
117      *
118      * @throws IOException               In case of failure
119      * @throws UnknownComponentException In case of failure
120      */
121     public void testLaunchTranslation() throws IOException, UnknownComponentException {
122         TemplateManagement templateManagement =
123                 new TemplateManagement(
124                         ResourceFileUtil.getResourceAsString("tosca/new-converter/sampleOperationalPolicies.yaml"),
125                         ResourceFileUtil.getResourceAsString("clds/tosca_update/defaultToscaTypes.yaml"),
126                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
127         assertNull(templateManagement.getParseToJson());
128         String componentName = "onap.policies.controlloop.operational.common.Drools";
129         templateManagement.launchTranslation(componentName);
130         assertNotNull(templateManagement.getParseToJson());
131     }
132
133     /**
134      * Test addTemplate.
135      *
136      * @throws IOException In case of failure
137      */
138     public void testAddTemplate() throws IOException {
139         TemplateManagement templateManagement =
140                 new TemplateManagement(
141                         ResourceFileUtil.getResourceAsString("tosca/new-converter/sampleOperationalPolicies.yaml"),
142                         ResourceFileUtil.getResourceAsString("clds/tosca_update/defaultToscaTypes.yaml"),
143                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
144         int count = templateManagement.getTemplates().size();
145         List<Field> templateFields = new ArrayList<>(Arrays.asList(new Field("type"), new Field("description"),
146                 new Field(
147                 "required"),
148                 new Field("metadata"), new Field("constraints")));
149         templateManagement.addTemplate("test", templateFields);
150         assertNotSame(count, templateManagement.getTemplates().size());
151     }
152
153     /**
154      * test Remove template.
155      *
156      * @throws IOException In case of failure
157      */
158     public void testRemoveTemplate() throws IOException {
159         TemplateManagement templateManagement =
160                 new TemplateManagement(
161                         ResourceFileUtil.getResourceAsString("tosca/new-converter/sampleOperationalPolicies.yaml"),
162                         ResourceFileUtil.getResourceAsString("clds/tosca_update/defaultToscaTypes.yaml"),
163                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
164         int count = templateManagement.getTemplates().size();
165         templateManagement.removeTemplate("string");
166         assertNotSame(count, templateManagement.getTemplates().size());
167     }
168
169     /**
170      * Test update template.
171      *
172      * @throws IOException In case of failure
173      */
174     public void testUpdateTemplate() throws IOException {
175         TemplateManagement templateManagement =
176                 new TemplateManagement(
177                         ResourceFileUtil.getResourceAsString("tosca/new-converter/sampleOperationalPolicies.yaml"),
178                         ResourceFileUtil.getResourceAsString("clds/tosca_update/defaultToscaTypes.yaml"),
179                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
180         int count = templateManagement.getTemplates().get("integer").getFields().size();
181         templateManagement.updateTemplate("integer", new Field("type"), false);
182         assertNotSame(count, templateManagement.getTemplates().get("integer").getFields().size());
183     }
184
185     /**
186      * Test has template.
187      *
188      * @throws IOException In case of failure
189      */
190     public void testHasTemplate() throws IOException {
191         TemplateManagement templateManagement =
192                 new TemplateManagement(
193                         ResourceFileUtil.getResourceAsString("tosca/new-converter/sampleOperationalPolicies.yaml"),
194                         ResourceFileUtil.getResourceAsString("clds/tosca_update/defaultToscaTypes.yaml"),
195                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
196         boolean has = true;
197         List<Field> templateFieldsString =
198                 new ArrayList<>(Arrays.asList(new Field("type"), new Field("description"), new Field("required"),
199                         new Field("metadata"), new Field("constraints")));
200         Template templateTest = new Template("String", templateFieldsString);
201         has = templateManagement.hasTemplate(templateTest);
202         assertEquals(false, has);
203     }
204
205 }