Add unit test for vfc-nfvo-wfengine
[vfc/nfvo/wfengine.git] / activiti-extension / src / main / java / org / onap / workflow / activitiext / restservicetask / GsonUtil.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.workflow.activitiext.restservicetask;
17
18 import java.util.Map.Entry;
19
20 import org.activiti.engine.delegate.DelegateExecution;
21 import org.onap.workflow.activitiext.common.ConstString;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonPrimitive;
29
30 public class GsonUtil {
31         
32         private static final Logger logger = LoggerFactory.getLogger(GsonUtil.class);
33         
34         public static JsonObject formatJsonObject(String key, JsonObject obj, DelegateExecution execution) {
35
36                 JsonObject res = new JsonObject();
37                 
38                 for (Entry<String, JsonElement> entry : obj.entrySet()) {
39                         
40                         if (entry.getValue() instanceof JsonObject) {
41                                 
42                                 JsonObject tempObj = (JsonObject) entry.getValue();
43                                 JsonElement valueSource = tempObj.get(ConstString.PARAMETER_VALUESOURCE);
44                                 if(valueSource == null || valueSource.getAsString().equals(ConstString.PARAMETER_VALUE_SOURCE_DEFINITION))
45                                 {
46                                         res.add(entry.getKey(), formatJsonObject(entry.getKey(), entry.getValue().getAsJsonObject(), execution));
47                                 }else{
48                                         res.addProperty(entry.getKey(), getValue(tempObj, execution));
49                                 }
50
51                         } else if (entry.getValue() instanceof JsonArray) {
52
53                                 JsonArray objArray = entry.getValue().getAsJsonArray();
54                                 JsonArray resArray = formatJsonArray(objArray, execution);
55                                 res.add(entry.getKey(), resArray);
56                         }
57                 }
58
59                 return res;
60         }
61
62         public static JsonArray formatJsonArray(JsonArray objArray, DelegateExecution execution) {
63
64                 JsonArray array = new JsonArray();
65
66                 for (JsonElement element : objArray) {
67
68                         if (element.isJsonObject()) {
69                                 
70                                 JsonObject tempObj = (JsonObject) element;
71                                 JsonElement valueSource = tempObj.get(ConstString.PARAMETER_VALUESOURCE);
72                                 if(valueSource == null || valueSource.getAsString().equals(ConstString.PARAMETER_VALUE_SOURCE_DEFINITION))
73                                 {
74                                         array.add(formatJsonObject(null, element.getAsJsonObject(), execution));
75                                 }else{
76                                         array.add(new JsonPrimitive(getValue(tempObj, execution)));
77                                 }
78                                 
79                         } else if (element.isJsonArray()) {
80
81                                 array = formatJsonArray(element.getAsJsonArray(), execution);
82                         }
83                 }
84
85                 return array;
86         }
87
88         /**
89          * obtain jsonObject value
90          * @param obj
91          * @param execution
92          * @return
93          */
94         public static String getValue(JsonObject obj, DelegateExecution execution)
95         {
96                 String result = new String();
97                 try {
98                         String valueSource = obj.get(ConstString.PARAMETER_VALUESOURCE).getAsString();
99                         String value = obj.get(ConstString.PARAMETER_VALUE).getAsString();
100                         if(ConstString.PARAMETER_VALUE_SOURCE_STRING.equals(valueSource))
101                         {
102                                 result = value;
103                         }else if(ConstString.PARAMETER_VALUE_SOURCE_PLAN.equals(valueSource))
104                         {
105                                 result = HttpUtil.parsePlanExpression(value, execution);
106                         }else if(ConstString.PARAMETER_VALUE_SOURCE_TOPOLOGY.equals(valueSource))
107                         {
108                                 result = HttpUtil.parseTopologyExpression(value, execution);
109                         }
110                 } catch (Exception e) {
111                         logger.error("jsonObject {} is illegal!", obj.toString(), e);
112                         throw e;
113                 }
114                 
115                 return result;
116         }
117 }