Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / constraints / MinLength.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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
21 package org.onap.sdc.toscaparser.api.elements.constraints;
22
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
25
26 import java.util.Collections;
27 import java.util.LinkedHashMap;
28
29 public class MinLength extends Constraint {
30     // Constraint class for "min_length"
31
32     // Constrains the property or parameter to a value of a minimum length.
33
34     @Override
35     protected void setValues() {
36
37         setConstraintKey(MIN_LENGTH);
38
39         addValidTypes(Collections.singletonList("Integer"));
40
41         validPropTypes.add(Schema.STRING);
42         validPropTypes.add(Schema.MAP);
43
44     }
45
46     public MinLength(String name, String type, Object c) {
47         super(name, type, c);
48
49         if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
50             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE113", "InvalidSchemaError: The property \"min_length\" expects an integer"));
51         }
52     }
53
54     @SuppressWarnings("unchecked")
55     @Override
56     protected boolean isValid(Object value) {
57         if (value instanceof String && constraintValue instanceof Integer
58                 && ((String) value).length() >= (Integer) constraintValue) {
59             return true;
60         } else {
61             return value instanceof LinkedHashMap && constraintValue instanceof Integer
62                     && ((LinkedHashMap<String, Object>) value).size() >= (Integer) constraintValue;
63         }
64     }
65
66     @Override
67     protected String errMsg(Object value) {
68         return String.format("Length of value \"%s\" of property \"%s\" must be at least \"%s\"",
69                 value.toString(), propertyName, constraintValue.toString());
70     }
71
72 }
73
74 /*python
75
76 class MinLength(Constraint):
77         """Constraint class for "min_length"
78
79         Constrains the property or parameter to a value to a minimum length.
80         """
81
82         constraint_key = Constraint.MIN_LENGTH
83
84         valid_types = (int, )
85
86         valid_prop_types = (Schema.STRING, Schema.MAP)
87
88         def __init__(self, property_name, property_type, constraint):
89             super(MinLength, self).__init__(property_name, property_type,
90                                             constraint)
91             if not isinstance(self.constraint_value, self.valid_types):
92                 ValidationIsshueCollector.appendException(
93                     InvalidSchemaError(message=_('The property "min_length" '
94                                                  'expects an integer.')))
95
96         def _is_valid(self, value):
97             if ((isinstance(value, str) or isinstance(value, dict)) and
98                len(value) >= self.constraint_value):
99                 return True
100
101             return False
102
103         def _err_msg(self, value):
104             return (_('Length of value "%(pvalue)s" of property "%(pname)s" '
105                       'must be at least "%(cvalue)s".') %
106                     dict(pname=self.property_name,
107                          pvalue=value,
108                          cvalue=self.constraint_value))
109 */