Fix in_range constraint validation gives unhelpful error message
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / constraints / InRangeConstraint.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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 package org.openecomp.sdc.be.model.tosca.constraints;
21
22 import com.google.common.collect.Lists;
23 import java.util.List;
24 import javax.validation.constraints.NotNull;
25 import lombok.Getter;
26 import lombok.NoArgsConstructor;
27 import lombok.NonNull;
28 import lombok.Setter;
29 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
30 import org.openecomp.sdc.be.model.PropertyConstraint;
31 import org.openecomp.sdc.be.model.tosca.ToscaType;
32 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
33 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
34 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
35 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
36
37 @NoArgsConstructor
38 @Getter
39 @Setter
40 public class InRangeConstraint extends AbstractPropertyConstraint {
41
42     @NonNull
43     private List<Object> inRange;
44     private Comparable min;
45     private Comparable max;
46
47     public InRangeConstraint(List<Object> inRange) {
48         this.inRange = inRange;
49     }
50
51     @Override
52     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
53         // Perform verification that the property type is supported for
54
55         // comparison
56         ConstraintUtil.checkComparableType(propertyType);
57         if (inRange == null || inRange.size() != 2) {
58             throw new ConstraintValueDoNotMatchPropertyTypeException("In range constraint must have two elements.");
59         }
60         String minRawText = String.valueOf(inRange.get(0));
61         String maxRawText = String.valueOf(inRange.get(1));
62         if (!propertyType.isValidValue(minRawText)) {
63             throw new ConstraintValueDoNotMatchPropertyTypeException(
64                 "Invalid min value for in range constraint [" + minRawText + "] as it does not follow the property type [" + propertyType + "]");
65         }
66         if (!propertyType.isValidValue(maxRawText)) {
67             throw new ConstraintValueDoNotMatchPropertyTypeException(
68                 "Invalid max value for in range constraint [" + maxRawText + "] as it does not follow the property type [" + propertyType + "]");
69         }
70         min = ConstraintUtil.convertToComparable(propertyType, minRawText);
71         max = ConstraintUtil.convertToComparable(propertyType, maxRawText);
72     }
73
74     @Override
75     public void validate(Object propertyValue) throws ConstraintViolationException {
76         if (propertyValue == null) {
77             throw new ConstraintViolationException("Value to check is null");
78         }
79         if (!(min.getClass().isAssignableFrom(propertyValue.getClass()))) {
80             throw new ConstraintViolationException(
81                 "Value to check is not comparable to range type, value type [" + propertyValue.getClass() + "], range type [" + min.getClass() + "]");
82         }
83         if (min.compareTo(propertyValue) > 0 || max.compareTo(propertyValue) < 0) {
84             throw new ConstraintViolationException("The value [" + propertyValue + "] is out of range " + inRange);
85         }
86     }
87
88     @Override
89     public ConstraintType getConstraintType() {
90         return ConstraintType.IN_RANGE;
91     }
92
93     @Override
94     public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
95     }
96
97     @NotNull
98     public Object getRangeMinValue() {
99         if (inRange != null) {
100             return inRange.get(0);
101         } else {
102             return null;
103         }
104     }
105
106     public void setRangeMinValue(Object minValue) {
107         if (inRange == null) {
108             inRange = Lists.newArrayList(minValue, null);
109         } else {
110             inRange.set(0, minValue);
111         }
112     }
113
114     @NotNull
115     public Object getRangeMaxValue() {
116         if (inRange != null) {
117             return inRange.get(1);
118         } else {
119             return null;
120         }
121     }
122
123     public void setRangeMaxValue(Object maxValue) {
124         if (inRange == null) {
125             inRange = Lists.newArrayList(null, maxValue);
126         } else {
127             inRange.set(1, maxValue);
128         }
129     }
130
131     @Override
132     public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
133         return getErrorMessage(toscaType, e, propertyName, "%s property value must be in a range of %s", String.valueOf(min),
134             String.valueOf(max));
135     }
136
137     public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
138         ToscaType toscaType = ToscaType.getToscaType(propertyType);
139         if (toscaType == null) {
140             throw new ConstraintValueDoNotMatchPropertyTypeException(
141                 "inRange constraint has invalid values <" + inRange + "> property type is <" + propertyType + ">");
142         }
143         if (inRange == null) {
144             throw new ConstraintValueDoNotMatchPropertyTypeException(
145                 "inRange constraint has invalid value <> property type is <" + propertyType + ">");
146         }
147         for (Object value : inRange) {
148             if (!toscaType.isValueTypeValid(value)) {
149                 return false;
150             }
151         }
152         return true;
153     }
154
155     public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
156         ToscaType toscaType = ToscaType.getToscaType(propertyType);
157         try {
158             inRange.replaceAll(obj -> toscaType.convert(String.valueOf(obj)));
159         } catch (Exception e) {
160             throw new ConstraintValueDoNotMatchPropertyTypeException(
161                     "inRange constraint has invalid values <" + inRange + "> property type is <" + propertyType + ">");
162         }
163     }
164 }