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