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