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