summaryrefslogtreecommitdiffstats
path: root/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraintTest.java
blob: 6fd578856d2d04cf0b63883e0bc8ecd28adaeb91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*-
 * ============LICENSE_START=======================================================
 * ONAP SDC
 * ================================================================================
 * Copyright (C) 2019 Samsung. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 */

package org.openecomp.sdc.be.model.tosca.constraints;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.openecomp.sdc.be.model.PropertyConstraint;
import org.openecomp.sdc.be.model.tosca.ToscaType;
import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
import org.openecomp.sdc.be.model.tosca.version.Version;

public class AbstractPropertyConstraintTest {

    private final TestPropertyConstraint constraint = new TestPropertyConstraint();

    @Test
    public void testValidateBoolean() throws ConstraintViolationException {
        // given
        final String value = "faLsE";

        // when
        constraint.validate(ToscaType.BOOLEAN, value);

        // then
        assertEquals(Boolean.valueOf(value), constraint.last);
    }

    @Test
    public void testValidateFloat() throws ConstraintViolationException {
        // given
        final String value = "123.456";

        // when
        constraint.validate(ToscaType.FLOAT, value);

        // then
        assertEquals(Float.valueOf(value), constraint.last);
    }

    @Test
    public void testValidateVersion() throws ConstraintViolationException {
        // given
        final String value = "12.34.1002-Release7";

        // when
        constraint.validate(ToscaType.VERSION, value);

        // then
        assertEquals(new Version(value), constraint.last);
    }

    @Test
    public void testGetErrorMessageIsInstanceOf() {
        // given
        final ConstraintFunctionalException exception = new ConstraintViolationException("exc");

        // when
        final String message = constraint.getErrorMessage(ToscaType.SCALAR_UNIT, exception,
                "area", "Message: %s --> %s", "prop1", "prop2");

        // then
        assertEquals("Message: area --> [prop1, prop2]", message);
    }

    @Test
    public void testGetErrorMessageOtherType() {
        // given
        final ConstraintFunctionalException exception = new ConstraintFunctionalException("exc");

        // when
        final String message = constraint.getErrorMessage(ToscaType.SCALAR_UNIT_FREQUENCY, exception,
                "freq", null);

        // then
        assertEquals("Unsupported value provided for freq property supported value type is scalar-unit.frequency.",
                message);
    }
}

class TestPropertyConstraint extends AbstractPropertyConstraint {

    Object last;

    @Override
    public void validate(Object propertyValue) {
        last = propertyValue;
    }

    @Override
    public ConstraintType getConstraintType() {
        return null;
    }

    @Override
    public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {

    }

    @Override
    public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException exception, String propertyName) {
        return null;
    }
}