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