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