49384121556496276aeae6275ecd4748cf5b662e
[clamp.git] / src / test / java / org / onap / clamp / clds / tosca / update / ToscaConverterManagerTest.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 ToscaConverterManagerTest 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         ToscaConverterManager toscaConverterManager =
43                 new ToscaConverterManager(
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/default-tosca-types.yaml"),
48                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
49         assertNull(toscaConverterManager.getParseToJson());
50         String componentName = "onap.policies.monitoring.cdap.tca.hi.lo.app";
51         toscaConverterManager.startConversionToJson(componentName);
52         assertNotNull(toscaConverterManager.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         ToscaConverterManager toscaConverterManager =
63                 new ToscaConverterManager(
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/default-tosca-types.yaml"),
68                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
69         assertNull(toscaConverterManager.getParseToJson());
70         String componentName = "onap.policies.controlloop.guard.common.FrequencyLimiter";
71         toscaConverterManager.startConversionToJson(componentName);
72         assertNotNull(toscaConverterManager.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         ToscaConverterManager toscaConverterManager =
83                 new ToscaConverterManager(
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/default-tosca-types.yaml"),
88                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
89         assertNull(toscaConverterManager.getParseToJson());
90         String componentName = "onap.policies.controlloop.operational.common.Apex";
91         toscaConverterManager.startConversionToJson(componentName);
92         assertNotNull(toscaConverterManager.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         ToscaConverterManager toscaConverterManager =
103                 new ToscaConverterManager(
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/default-tosca-types.yaml"),
108                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
109         assertNull(toscaConverterManager.getParseToJson());
110         String componentName = "onap.policies.controlloop.operational.common.Drools";
111         toscaConverterManager.startConversionToJson(componentName);
112         assertNotNull(toscaConverterManager.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         ToscaConverterManager toscaConverterManager =
123                 new ToscaConverterManager(
124                         ResourceFileUtil.getResourceAsString("tosca/new-converter/sampleOperationalPolicies.yaml"),
125                         ResourceFileUtil.getResourceAsString("clds/tosca_update/default-tosca-types.yaml"),
126                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
127         assertNull(toscaConverterManager.getParseToJson());
128         String componentName = "onap.policies.controlloop.operational.common.Drools";
129         toscaConverterManager.startConversionToJson(componentName);
130         assertNotNull(toscaConverterManager.getParseToJson());
131     }
132
133     /**
134      * Test addTemplate.
135      *
136      * @throws IOException In case of failure
137      */
138     public void testAddTemplate() throws IOException {
139         ToscaConverterManager toscaConverterManager =
140                 new ToscaConverterManager(
141                         ResourceFileUtil.getResourceAsString("tosca/new-converter/sampleOperationalPolicies.yaml"),
142                         ResourceFileUtil.getResourceAsString("clds/tosca_update/default-tosca-types.yaml"),
143                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
144         int count = toscaConverterManager.getTemplates().size();
145         List<TemplateField> templateFields = new ArrayList<>(Arrays.asList(new TemplateField("type"), new TemplateField("description"),
146                 new TemplateField(
147                 "required"),
148                 new TemplateField("metadata"), new TemplateField("constraints")));
149         toscaConverterManager.addTemplate("test", templateFields);
150         assertNotSame(count, toscaConverterManager.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         ToscaConverterManager toscaConverterManager =
160                 new ToscaConverterManager(
161                         ResourceFileUtil.getResourceAsString("tosca/new-converter/sampleOperationalPolicies.yaml"),
162                         ResourceFileUtil.getResourceAsString("clds/tosca_update/default-tosca-types.yaml"),
163                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
164         int count = toscaConverterManager.getTemplates().size();
165         toscaConverterManager.removeTemplate("string");
166         assertNotSame(count, toscaConverterManager.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         ToscaConverterManager toscaConverterManager =
176                 new ToscaConverterManager(
177                         ResourceFileUtil.getResourceAsString("tosca/new-converter/sampleOperationalPolicies.yaml"),
178                         ResourceFileUtil.getResourceAsString("clds/tosca_update/default-tosca-types.yaml"),
179                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
180         int count = toscaConverterManager.getTemplates().get("integer").getTemplateFields().size();
181         toscaConverterManager.updateTemplate("integer", new TemplateField("type"), false);
182         assertNotSame(count, toscaConverterManager.getTemplates().get("integer").getTemplateFields().size());
183     }
184
185     /**
186      * Test has template.
187      *
188      * @throws IOException In case of failure
189      */
190     public void testHasTemplate() throws IOException {
191         ToscaConverterManager toscaConverterManager =
192                 new ToscaConverterManager(
193                         ResourceFileUtil.getResourceAsString("tosca/new-converter/sampleOperationalPolicies.yaml"),
194                         ResourceFileUtil.getResourceAsString("clds/tosca_update/default-tosca-types.yaml"),
195                         ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"));
196         boolean has = true;
197         List<TemplateField> templateFieldsString =
198                 new ArrayList<>(Arrays.asList(new TemplateField("type"), new TemplateField("description"), new TemplateField("required"),
199                         new TemplateField("metadata"), new TemplateField("constraints")));
200         Template templateTest = new Template("String", templateFieldsString);
201         has = toscaConverterManager.hasTemplate(templateTest);
202         assertEquals(false, has);
203     }
204
205 }