2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
22 package org.openecomp.sdc.be.model.tosca.constraints;
24 import static org.junit.Assert.assertEquals;
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.ConstraintViolationException;
32 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
33 import org.openecomp.sdc.be.model.tosca.version.Version;
35 public class AbstractPropertyConstraintTest {
37 private final TestPropertyConstraint constraint = new TestPropertyConstraint();
40 public void testValidateBoolean() throws ConstraintViolationException {
42 final String value = "faLsE";
45 constraint.validate(ToscaType.BOOLEAN, value);
48 assertEquals(Boolean.valueOf(value), constraint.last);
52 public void testValidateFloat() throws ConstraintViolationException {
54 final String value = "123.456";
57 constraint.validate(ToscaType.FLOAT, value);
60 assertEquals(Float.valueOf(value), constraint.last);
64 public void testValidateVersion() throws ConstraintViolationException {
66 final String value = "12.34.1002-Release7";
69 constraint.validate(ToscaType.VERSION, value);
72 assertEquals(new Version(value), constraint.last);
76 public void testGetErrorMessageIsInstanceOf() {
78 final ConstraintFunctionalException exception = new ConstraintViolationException("exc");
81 final String message = constraint.getErrorMessage(ToscaType.SCALAR_UNIT, exception,
82 "area", "Message: %s --> %s", "prop1", "prop2");
85 assertEquals("Message: area --> [prop1, prop2]", message);
89 public void testGetErrorMessageOtherType() {
91 final ConstraintFunctionalException exception = new ConstraintFunctionalException("exc");
94 final String message = constraint.getErrorMessage(ToscaType.SCALAR_UNIT_FREQUENCY, exception,
98 assertEquals("Unsupported value provided for freq property supported value type is scalar-unit.frequency.",
103 class TestPropertyConstraint extends AbstractPropertyConstraint {
108 public void validate(Object propertyValue) {
109 last = propertyValue;
113 public ConstraintType getConstraintType() {
118 public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
123 public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException exception, String propertyName) {