Support TOSCA functions in Node Filters
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / tosca / constraints / AbstractPropertyConstraintTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP SDC
4  * ================================================================================
5  * Copyright (C) 2019 Samsung. 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
22 package org.openecomp.sdc.be.model.tosca.constraints;
23
24 import static org.junit.Assert.assertEquals;
25
26 import org.junit.Test;
27 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
28 import org.openecomp.sdc.be.model.PropertyConstraint;
29 import org.openecomp.sdc.be.model.tosca.ToscaType;
30 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
31 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
32 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
33 import org.openecomp.sdc.be.model.tosca.version.Version;
34
35 public class AbstractPropertyConstraintTest {
36
37     private final TestPropertyConstraint constraint = new TestPropertyConstraint();
38
39     @Test
40     public void testValidateBoolean() throws ConstraintViolationException {
41         // given
42         final String value = "faLsE";
43
44         // when
45         constraint.validate(ToscaType.BOOLEAN, value);
46
47         // then
48         assertEquals(Boolean.valueOf(value), constraint.last);
49     }
50
51     @Test
52     public void testValidateFloat() throws ConstraintViolationException {
53         // given
54         final String value = "123.456";
55
56         // when
57         constraint.validate(ToscaType.FLOAT, value);
58
59         // then
60         assertEquals(Float.valueOf(value), constraint.last);
61     }
62
63     @Test
64     public void testValidateVersion() throws ConstraintViolationException {
65         // given
66         final String value = "12.34.1002-Release7";
67
68         // when
69         constraint.validate(ToscaType.VERSION, value);
70
71         // then
72         assertEquals(new Version(value), constraint.last);
73     }
74
75     @Test
76     public void testGetErrorMessageIsInstanceOf() {
77         // given
78         final ConstraintFunctionalException exception = new ConstraintViolationException("exc");
79
80         // when
81         final String message = constraint.getErrorMessage(ToscaType.SCALAR_UNIT, exception,
82                 "area", "Message: %s --> %s", "prop1", "prop2");
83
84         // then
85         assertEquals("Message: area --> [prop1, prop2]", message);
86     }
87
88     @Test
89     public void testGetErrorMessageOtherType() {
90         // given
91         final ConstraintFunctionalException exception = new ConstraintFunctionalException("exc");
92
93         // when
94         final String message = constraint.getErrorMessage(ToscaType.SCALAR_UNIT_FREQUENCY, exception,
95                 "freq", null);
96
97         // then
98         assertEquals("Unsupported value provided for freq property supported value type is scalar-unit.frequency.",
99                 message);
100     }
101 }
102
103 class TestPropertyConstraint extends AbstractPropertyConstraint {
104
105     Object last;
106
107     @Override
108     public void validate(Object propertyValue) {
109         last = propertyValue;
110     }
111
112     @Override
113     public ConstraintType getConstraintType() {
114         return null;
115     }
116
117     @Override
118     public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
119
120     }
121
122     @Override
123     public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException exception, String propertyName) {
124         return null;
125     }
126 }