new unit tests for sdc-dao
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / validators / ListValidator.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
21 package org.openecomp.sdc.be.model.tosca.validators;
22
23 import java.util.Map;
24
25 import org.openecomp.sdc.be.config.BeEcompErrorManager;
26 import org.openecomp.sdc.be.model.DataTypeDefinition;
27 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
28 import org.openecomp.sdc.common.util.JsonUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.gson.JsonArray;
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonParser;
35 import com.google.gson.JsonSyntaxException;
36
37 public class ListValidator implements PropertyTypeValidator {
38
39         private static ListValidator listValidator = new ListValidator();
40
41         private static Logger log = LoggerFactory.getLogger(ListValidator.class.getName());
42
43         private static JsonParser jsonParser = new JsonParser();
44
45         private static DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();
46
47         public static ListValidator getInstance() {
48                 return listValidator;
49         }
50
51         @Override
52         public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
53
54                 log.debug("Going to validate value {} with inner type {}", value, innerType);
55
56                 if (value == null || value == "") {
57                         return true;
58                 }
59                 if (innerType == null) {
60                         return false;
61                 }
62
63                 PropertyTypeValidator innerValidator;
64
65                 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
66
67                 if (innerToscaType != null) {
68                         switch (innerToscaType) {
69                         case STRING:
70                                 innerValidator = ToscaPropertyType.STRING.getValidator();
71                                 break;
72                         case INTEGER:
73                                 innerValidator = ToscaPropertyType.INTEGER.getValidator();
74                                 break;
75                         case FLOAT:
76                                 innerValidator = ToscaPropertyType.FLOAT.getValidator();
77                                 break;
78                         case BOOLEAN:
79                                 innerValidator = ToscaPropertyType.BOOLEAN.getValidator();
80                                 break;
81                         case JSON:
82                                 innerValidator = ToscaPropertyType.JSON.getValidator();
83                                 break;
84                         default:
85                                 log.debug("inner Tosca Type is unknown. {}", innerToscaType);
86                                 return false;
87                         }
88
89                 } else {
90                         log.debug("inner Tosca Type is: {}", innerType);
91
92                         boolean isValid = validateComplexInnerType(value, innerType, allDataTypes);
93                         log.debug("Finish to validate value {} of list with inner type {}. result is {}",value,innerType,isValid);
94                         return isValid;
95                 }
96
97                 try {
98                         JsonArray jo = (JsonArray) jsonParser.parse(value);
99                         if(ToscaPropertyType.JSON == innerToscaType)
100                                 return true;
101                         int size = jo.size();
102                         for (int i = 0; i < size; i++) {
103                                 JsonElement currentValue = jo.get(i);
104                                 String element = JsonUtils.toString(currentValue);
105                                 if (!innerValidator.isValid(element, null, allDataTypes)) {
106                                         log.debug("validation of element : {} failed", element);
107                                         return false;
108                                 }
109
110                         }
111                         return true;
112
113                 } catch (JsonSyntaxException e) {
114                         log.debug("Failed to parse json : {}", value, e);
115                         BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Validator");
116                 }
117
118                 return false;
119
120         }
121
122         @Override
123         public boolean isValid(String value, String innerType) {
124                 return isValid(value, innerType, null);
125         }
126
127         private boolean validateComplexInnerType(String value, String innerType,
128                         Map<String, DataTypeDefinition> allDataTypes) {
129
130                 DataTypeDefinition innerDataTypeDefinition = allDataTypes.get(innerType);
131                 if (innerDataTypeDefinition == null) {
132                         log.debug("Data type {} cannot be found in our data types.", innerType);
133                         return false;
134                 }
135
136                 try {
137
138                         JsonArray jo = (JsonArray) jsonParser.parse(value);
139                         int size = jo.size();
140                         for (int i = 0; i < size; i++) {
141                                 JsonElement currentValue = jo.get(i);
142                                 if (currentValue != null) {
143                                         String element = JsonUtils.toString(currentValue);
144                                         boolean isValid = dataTypeValidatorConverter.isValid(element, innerDataTypeDefinition,
145                                                         allDataTypes);
146                                         if (isValid == false) {
147                                                 log.debug("Cannot parse value {} from type {} in list parameter",currentValue,innerType);
148                                                 return false;
149                                         }
150                                 }
151                         }
152
153                 } catch (Exception e) {
154                         log.debug("Error when parsing JSON of object of type ", e);
155                         return false;
156                 }
157
158                 return true;
159         }
160 }