Adding unit tests
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / converters / ToscaValueBaseConverter.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.converters;
22
23 import com.google.gson.*;
24 import org.openecomp.sdc.be.model.DataTypeDefinition;
25 import org.openecomp.sdc.be.model.PropertyDefinition;
26 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
28
29 import java.util.*;
30 import java.util.Map.Entry;
31
32 public class ToscaValueBaseConverter {
33     protected Gson gson = new Gson();
34     private static final Logger log = Logger.getLogger(ToscaValueBaseConverter.class.getName());
35
36     protected Map<String, PropertyDefinition> getAllProperties(DataTypeDefinition dataTypeDefinition) {
37
38         Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
39
40         while (dataTypeDefinition != null) {
41
42             List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
43             if (currentParentsProps != null) {
44                 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
45             }
46
47             dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
48         }
49
50         return allParentsProps;
51     }
52
53     public ToscaPropertyType isScalarType(DataTypeDefinition dataTypeDef) {
54
55         ToscaPropertyType result = null;
56
57         DataTypeDefinition dataType = dataTypeDef;
58
59         while (dataType != null) {
60
61             String name = dataType.getName();
62             ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(name);
63             if (typeIfScalar != null) {
64                 result = typeIfScalar;
65                 break;
66             }
67
68             dataType = dataType.getDerivedFrom();
69         }
70
71         return result;
72     }
73
74     public Object handleComplexJsonValue(JsonElement elementValue) {
75         Object jsonValue = null;
76
77         Map<String, Object> value = new HashMap<>();
78         if (elementValue.isJsonObject()) {
79             JsonObject jsonOb = elementValue.getAsJsonObject();
80             Set<Entry<String, JsonElement>> entrySet = jsonOb.entrySet();
81             Iterator<Entry<String, JsonElement>> iteratorEntry = entrySet.iterator();
82             while (iteratorEntry.hasNext()) {
83                 Entry<String, JsonElement> entry = iteratorEntry.next();
84                 if (entry.getValue().isJsonArray()) {
85                     List<Object> array = handleJsonArray(entry.getValue());
86                     value.put(entry.getKey(), array);
87                 } else {
88                     Object object;
89                     if (entry.getValue().isJsonPrimitive()) {
90                         object = json2JavaPrimitive(entry.getValue().getAsJsonPrimitive());
91                     } else {
92                         object = handleComplexJsonValue(entry.getValue());
93                     }
94                     value.put(entry.getKey(), object);
95                 }
96             }
97             jsonValue = value;
98         } else {
99             if (elementValue.isJsonArray()) {
100                 jsonValue = handleJsonArray(elementValue);
101             } else {
102                 if (elementValue.isJsonPrimitive()) {
103                     jsonValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
104                 } else {
105                     log.debug("not supported json type ");
106                 }
107             }
108         }
109
110         return jsonValue;
111     }
112
113     private List<Object> handleJsonArray(JsonElement entry) {
114         List<Object> array = new ArrayList<>();
115         JsonArray jsonArray = entry.getAsJsonArray();
116         Iterator<JsonElement> iterator = jsonArray.iterator();
117         while (iterator.hasNext()) {
118             Object object;
119             JsonElement element = iterator.next();
120             if (element.isJsonPrimitive()) {
121                 object = json2JavaPrimitive(element.getAsJsonPrimitive());
122             } else {
123                 object = handleComplexJsonValue(element);
124             }
125             array.add(object);
126         }
127         return array;
128     }
129
130     public Object json2JavaPrimitive(JsonPrimitive prim) {
131         if (prim.isBoolean()) {
132             return prim.getAsBoolean();
133         } else if (prim.isString()) {
134             return prim.getAsString();
135         } else if (prim.isNumber()) {
136             String strRepesentation = prim.getAsString();
137             if (strRepesentation.contains(".")) {
138                 return prim.getAsDouble();
139             } else {
140                 return prim.getAsInt();
141             }
142         } else {
143             throw new IllegalStateException();
144         }
145     }
146
147     /**
148      * checks is received Object empty or equals null or not It is relevant only
149      * if received Object is instance of String, Map or List class.
150      *
151      * @param convertedValue
152      * @return
153      */
154     public static boolean isEmptyObjectValue(Object convertedValue) {
155         if ((convertedValue == null)
156             || (convertedValue instanceof String && ((String) convertedValue).isEmpty())
157             || (convertedValue instanceof Map && ((Map) convertedValue).isEmpty())
158             || (convertedValue instanceof List && ((List) convertedValue).isEmpty()))
159         {
160             return true;
161         }
162         return false;
163     }
164 }