5788f03a6e080a7ed9c44933209b292666a930e9
[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.ConstraintValueDoNotMatchPropertyTypeException;
30 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
31
32 public class AbstractStringPropertyConstraintTest {
33
34     private static final String PROPERTY_VALUE = "propValue";
35
36     private final TestStringPropertyConstraint constraint = new TestStringPropertyConstraint();
37
38     @Test
39     public void testInitializeSuccess() throws ConstraintValueDoNotMatchPropertyTypeException {
40         // when
41         constraint.initialize(ToscaType.STRING);
42     }
43
44     @Test(expected = ConstraintValueDoNotMatchPropertyTypeException.class)
45     public void testInitializeFailure() throws ConstraintValueDoNotMatchPropertyTypeException {
46         // when
47         constraint.initialize(ToscaType.TIMESTAMP);
48     }
49
50     @Test
51     public void testValidateSuccess() throws ConstraintViolationException {
52         // when
53         constraint.validate(PROPERTY_VALUE);
54
55         // then
56         assertEquals(PROPERTY_VALUE, constraint.last);
57     }
58
59     @Test(expected = ConstraintViolationException.class)
60     public void testValidateNull() throws ConstraintViolationException {
61         // when
62         constraint.validate(null);
63     }
64
65     @Test(expected = ConstraintViolationException.class)
66     public void testValidateNotString() throws ConstraintViolationException {
67         // when
68         constraint.validate(Integer.valueOf(123));
69     }
70 }
71
72 class TestStringPropertyConstraint extends AbstractStringPropertyConstraint {
73
74     String last;
75
76     @Override
77     protected void doValidate(String propertyValue) {
78         last = propertyValue;
79     }
80
81     @Override
82     public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException exception, String propertyName) {
83         return null;
84     }
85 }