1e03513aa4b2214ec3c70667619737b630cb9562
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / tosca / constraints / ValidValuesConstraintTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.tosca.constraints;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.junit.jupiter.api.Test;
31 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
32
33 class ValidValuesConstraintTest {
34
35     private ValidValuesConstraint createStringTestSubject() {
36         List<Object> validValues = new ArrayList<>();
37         validValues.add("test1");
38         validValues.add("test2");
39         validValues.add("test3");
40         validValues.add("test4");
41         return new ValidValuesConstraint(validValues);
42     }
43
44     private ValidValuesConstraint createIntegerTestSubject() {
45         List<Object> validValues = new ArrayList<>();
46         validValues.add(1);
47         validValues.add(2);
48         validValues.add(3);
49         validValues.add(4);
50         return new ValidValuesConstraint(validValues);
51     }
52
53     @Test
54     void testGetValidValues() {
55         ValidValuesConstraint testSubject = createStringTestSubject();
56         List<Object> result = testSubject.getValidValues();
57
58         assertFalse(result.isEmpty());
59         assertEquals("test1", result.get(0));
60         assertEquals("test4", result.get(3));
61     }
62
63     @Test
64     void testSetValidValues() {
65         ValidValuesConstraint testSubject = createStringTestSubject();
66         List<Object> validValues = new ArrayList<>();
67         validValues.add("test5");
68         validValues.add("test6");
69         validValues.add("test7");
70         validValues.add("test8");
71         testSubject.setValidValues(validValues);
72
73         List<Object> result = testSubject.getValidValues();
74
75         assertEquals(4, result.size());
76         assertEquals("test5", result.get(0));
77         assertEquals("test8", result.get(3));
78     }
79
80     @Test
81     void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
82         ValidValuesConstraint testSubject = createStringTestSubject();
83         assertTrue(testSubject.validateValueType("string"));
84     }
85
86     @Test
87     void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
88         ValidValuesConstraint testSubject = createStringTestSubject();
89         assertFalse(testSubject.validateValueType("integer"));
90     }
91
92     @Test
93     void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
94         ValidValuesConstraint testSubject = createIntegerTestSubject();
95         assertTrue(testSubject.validateValueType("integer"));
96     }
97
98     @Test
99     void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
100         ValidValuesConstraint testSubject = createIntegerTestSubject();
101         assertFalse(testSubject.validateValueType("string"));
102     }
103
104     @Test
105     void testChangeStringConstraintValueTypeToIntegerThrow() {
106         String propertyType = "integer";
107         ValidValuesConstraint testSubject = createStringTestSubject();
108         Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> {
109             testSubject.changeConstraintValueTypeTo(propertyType);
110         });
111
112         String expectedMessage =
113             "validValues constraint has invalid values <" + testSubject.getValidValues() + "> property type is <" + propertyType + ">";
114         String actualMessage = exception.getMessage();
115
116         assertTrue(actualMessage.contains(expectedMessage));
117     }
118
119     @Test
120     void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException {
121         ValidValuesConstraint testSubject = createIntegerTestSubject();
122
123         testSubject.changeConstraintValueTypeTo("string");
124         List<Object> result = testSubject.getValidValues();
125
126         result.forEach(value -> assertTrue(value instanceof String));
127     }
128 }