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