2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.tosca.update;
26 import com.google.gson.JsonObject;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import junit.framework.TestCase;
30 import org.onap.clamp.clds.tosca.update.elements.ToscaElement;
31 import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty;
32 import org.onap.clamp.clds.tosca.update.templates.JsonTemplate;
33 import org.onap.clamp.clds.tosca.update.templates.JsonTemplateManager;
34 import org.onap.clamp.clds.util.ResourceFileUtil;
36 public class ConstraintTest extends TestCase {
38 JsonTemplateManager jsonTemplateManager = new JsonTemplateManager(
39 ResourceFileUtil.getResourceAsString("tosca/new-converter/constraints.yaml"),
40 ResourceFileUtil.getResourceAsString("clds/tosca-converter/default-tosca-types.yaml"),
41 ResourceFileUtil.getResourceAsString("clds/tosca-converter/templates.json"));
43 ToscaElement toscaElement = jsonTemplateManager.getToscaElements().get("onap.datatype.controlloop.Operation");
45 public ConstraintTest() throws IOException {
49 * Test get value array.
51 public void testGetValuesArray() {
52 ToscaElementProperty toscaElementProperty = toscaElement.getProperties().get("timeout");
53 JsonTemplate jsonTemplate = jsonTemplateManager.getJsonSchemaTemplates().get("integer");
54 JsonObject resultProcess = new JsonObject();
55 toscaElementProperty.addConstraintsAsJson(resultProcess,
56 (ArrayList<Object>) toscaElementProperty.getItems().get("constraints"),
58 String reference = "{\"enum\":[3,4,5.5,6,10]}";
59 assertEquals(reference, String.valueOf(resultProcess));
60 toscaElementProperty = toscaElement.getProperties().get("success");
61 jsonTemplate = jsonTemplateManager.getJsonSchemaTemplates().get("string");
62 resultProcess = new JsonObject();
63 toscaElementProperty.addConstraintsAsJson(resultProcess,
64 (ArrayList<Object>) toscaElementProperty.getItems().get("constraints"),
66 reference = "{\"enum\":[\"VALID\",\"TERMINATED\"]}";
67 assertEquals(reference, String.valueOf(resultProcess));
71 * Test get Specific length.
73 public void testGetSpecificLength() {
74 //Test for string type, same process for array
75 ToscaElementProperty toscaElementProperty = toscaElement.getProperties().get("id");
76 JsonTemplate jsonTemplate = jsonTemplateManager.getJsonSchemaTemplates().get("string");
77 JsonObject resultProcess = new JsonObject();
78 toscaElementProperty.addConstraintsAsJson(resultProcess,
79 (ArrayList<Object>) toscaElementProperty.getItems().get("constraints"),
81 int specificLength = 8;
82 int toTest = resultProcess.get("minLength").getAsInt();
83 assertEquals(specificLength, toTest);
84 toTest = resultProcess.get("maxLength").getAsInt();
85 assertEquals(specificLength, toTest);
89 * Test get limit value.
91 public void testGetLimitValue() {
92 //Test for array type, same process for string
93 ToscaElementProperty toscaElementProperty = toscaElement.getProperties().get("description");
94 JsonTemplate jsonTemplate = jsonTemplateManager.getJsonSchemaTemplates().get("array");
95 JsonObject resultProcess = new JsonObject();
96 toscaElementProperty.addConstraintsAsJson(resultProcess,
97 (ArrayList<Object>) toscaElementProperty.getItems().get("constraints"),
100 int toTest = resultProcess.get("minItems").getAsInt();
101 assertEquals(5, toTest);
102 toTest = resultProcess.get("maxItems").getAsInt();
103 assertEquals(7, toTest);