5215cbd4d852a32c0a0068a684c8d9fbf9ce6661
[clamp.git] / src / test / java / org / onap / clamp / clds / tosca / update / ConstraintTest.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 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;
35
36 public class ConstraintTest extends TestCase {
37
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"));
42
43     ToscaElement toscaElement = jsonTemplateManager.getToscaElements().get("onap.datatype.controlloop.Operation");
44
45     public ConstraintTest() throws IOException {
46     }
47
48     /**
49      * Test get value array.
50      */
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"),
57                 jsonTemplate);
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"),
65                 jsonTemplate);
66         reference = "{\"enum\":[\"VALID\",\"TERMINATED\"]}";
67         assertEquals(reference, String.valueOf(resultProcess));
68     }
69
70     /**
71      * Test get Specific length.
72      */
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"),
80                 jsonTemplate);
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);
86     }
87
88     /**
89      * Test get limit value.
90      */
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"),
98                 jsonTemplate);
99
100         int toTest = resultProcess.get("minItems").getAsInt();
101         assertEquals(5, toTest);
102         toTest = resultProcess.get("maxItems").getAsInt();
103         assertEquals(7, toTest);
104     }
105
106 }