2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.tosca.constraints;
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;
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;
34 public class ValidValuesConstraintTest {
36 private ValidValuesConstraint createStringTestSubject() {
37 List<Object> validValues = new ArrayList<>();
38 validValues.add("test1");
39 validValues.add("test2");
40 validValues.add("test3");
41 validValues.add("test4");
42 return new ValidValuesConstraint(validValues);
45 private ValidValuesConstraint createIntegerTestSubject() {
46 List<Object> validValues = new ArrayList<>();
51 return new ValidValuesConstraint(validValues);
55 public void testGetValidValues() {
56 ValidValuesConstraint testSubject = createStringTestSubject();
57 List<Object> result = testSubject.getValidValues();
59 assertFalse(result.isEmpty());
60 assertEquals("test1", result.get(0));
61 assertEquals("test4", result.get(3));
65 public void testSetValidValues() {
66 ValidValuesConstraint testSubject = createStringTestSubject();
67 List<Object> validValues = new ArrayList<>();
68 validValues.add("test5");
69 validValues.add("test6");
70 validValues.add("test7");
71 validValues.add("test8");
72 testSubject.setValidValues(validValues);
74 List<Object> result = testSubject.getValidValues();
76 assertEquals(4, result.size());
77 assertEquals("test5", result.get(0));
78 assertEquals("test8", result.get(3));
82 public void testValidateValueTypeStringTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
83 ValidValuesConstraint testSubject = createStringTestSubject();
84 Boolean validTypes = testSubject.validateValueType("string");
85 assertTrue(validTypes);
89 public void testValidateValueTypeStringFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
90 ValidValuesConstraint testSubject = createStringTestSubject();
91 Boolean validTypes = testSubject.validateValueType("integer");
92 assertFalse(validTypes);
96 public void testValidateValueTypeIntegerTrue() throws ConstraintValueDoNotMatchPropertyTypeException {
97 ValidValuesConstraint testSubject = createIntegerTestSubject();
98 Boolean validTypes = testSubject.validateValueType("integer");
99 assertTrue(validTypes);
103 public void testValidateValueTypeIntegerFalse() throws ConstraintValueDoNotMatchPropertyTypeException {
104 ValidValuesConstraint testSubject = createIntegerTestSubject();
105 Boolean validTypes = testSubject.validateValueType("string");
106 assertFalse(validTypes);
110 public void testChangeStringConstraintValueTypeToIntegerThrow() {
111 String propertyType = "integer";
112 ValidValuesConstraint testSubject = createStringTestSubject();
113 Exception exception = assertThrows(ConstraintValueDoNotMatchPropertyTypeException.class, () -> {
114 testSubject.changeConstraintValueTypeTo(propertyType);
117 String expectedMessage =
118 "validValues constraint has invalid values <" + testSubject.getValidValues() + "> property type is <" + propertyType + ">";
119 String actualMessage = exception.getMessage();
121 assertTrue(actualMessage.contains(expectedMessage));
125 public void testChangeIntegerConstraintValueTypeToString() throws ConstraintValueDoNotMatchPropertyTypeException {
126 ValidValuesConstraint testSubject = createIntegerTestSubject();
128 testSubject.changeConstraintValueTypeTo("string");
129 List<Object> result = testSubject.getValidValues();
131 result.forEach(value -> assertTrue(value instanceof String));