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.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.ConstraintViolationException;
31 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
32 import org.openecomp.sdc.be.model.tosca.version.Version;
34 public class AbstractPropertyConstraintTest {
36 private final TestPropertyConstraint constraint = new TestPropertyConstraint();
39 public void testValidateBoolean() throws ConstraintViolationException {
41 final String value = "faLsE";
44 constraint.validate(ToscaType.BOOLEAN, value);
47 assertEquals(Boolean.valueOf(value), constraint.last);
51 public void testValidateFloat() throws ConstraintViolationException {
53 final String value = "123.456";
56 constraint.validate(ToscaType.FLOAT, value);
59 assertEquals(Float.valueOf(value), constraint.last);
63 public void testValidateVersion() throws ConstraintViolationException {
65 final String value = "12.34.1002-Release7";
68 constraint.validate(ToscaType.VERSION, value);
71 assertEquals(new Version(value), constraint.last);
75 public void testGetErrorMessageIsInstanceOf() {
77 final ConstraintFunctionalException exception = new ConstraintViolationException("exc");
80 final String message = constraint.getErrorMessage(ToscaType.SCALAR_UNIT, exception,
81 "area", "Message: %s --> %s", "prop1", "prop2");
84 assertEquals("Message: area --> [prop1, prop2]", message);
88 public void testGetErrorMessageOtherType() {
90 final ConstraintFunctionalException exception = new ConstraintFunctionalException("exc");
93 final String message = constraint.getErrorMessage(ToscaType.SCALAR_UNIT_FREQUENCY, exception,
97 assertEquals("Unsupported value provided for freq property supported value type is scalar-unit.frequency.",
102 class TestPropertyConstraint extends AbstractPropertyConstraint {
107 public void validate(Object propertyValue) {
108 last = propertyValue;
112 public ConstraintType getConstraintType() {
117 public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
122 public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException exception, String propertyName) {