sanity fix after merge of BE code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / rest / ResponseParser.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.ci.tests.utils.rest;
22
23 import java.io.IOException;
24 import java.text.ParseException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Optional;
32
33 import org.apache.commons.codec.binary.Base64;
34 import org.apache.log4j.Logger;
35 import org.codehaus.jackson.JsonParseException;
36 import org.codehaus.jackson.Version;
37 import org.codehaus.jackson.map.DeserializationConfig;
38 import org.codehaus.jackson.map.JsonDeserializer;
39 import org.codehaus.jackson.map.JsonMappingException;
40 import org.codehaus.jackson.map.ObjectMapper;
41 import org.codehaus.jackson.map.module.SimpleModule;
42 import org.json.JSONArray;
43 import org.json.JSONException;
44 import org.json.simple.JSONObject;
45 import org.json.simple.JSONValue;
46 import org.openecomp.sdc.be.model.ArtifactDefinition;
47 import org.openecomp.sdc.be.model.Component;
48 import org.openecomp.sdc.be.model.ComponentInstance;
49 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
50 import org.openecomp.sdc.be.model.Product;
51 import org.openecomp.sdc.be.model.PropertyConstraint;
52 import org.openecomp.sdc.be.model.Resource;
53 import org.openecomp.sdc.be.model.Service;
54 import org.openecomp.sdc.be.model.category.CategoryDefinition;
55 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintJacksonDeserialiser;
56 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
57 import org.openecomp.sdc.ci.tests.datatypes.ResourceAssetStructure;
58 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
59 import org.openecomp.sdc.ci.tests.datatypes.ResourceRespJavaObject;
60 import org.openecomp.sdc.ci.tests.datatypes.ServiceDistributionStatus;
61 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
62 import org.openecomp.sdc.ci.tests.utils.Utils;
63 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
64
65 import com.fasterxml.jackson.databind.DeserializationFeature;
66 import com.google.gson.Gson;
67 import com.google.gson.JsonArray;
68 import com.google.gson.JsonElement;
69 import com.google.gson.JsonObject;
70 import com.google.gson.JsonParser;
71
72 public class ResponseParser {
73
74         // comment by Andrey, for test only
75         // public static void main(String[] args) {
76         // String response =
77         // "{\"uniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2\",\"resourceName\":\"importResource4test\",\"resourceVersion\":\"0.1\",\"creatorUserId\":\"jh0003\",\"creatorFullName\":\"Jimmy
78         // Hendrix\",\"lastUpdaterUserId\":\"jh0003\",\"lastUpdaterFullName\":\"Jimmy
79         // Hendrix\",\"creationDate\":1446742241514,\"lastUpdateDate\":1446742241514,\"description\":\"Represents
80         // a generic software component that can be managed and run by a Compute
81         // Node
82         // Type.\",\"icon\":\"defaulticon\",\"tags\":[\"importResource4test\"],\"category\":\"Generic/Infrastructure\",\"lifecycleState\":\"NOT_CERTIFIED_CHECKOUT\",\"derivedFrom\":[\"tosca.nodes.Root\"],\"artifacts\":{},\"deploymentArtifacts\":{},\"properties\":[{\"uniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2.port\",\"type\":\"integer\",\"required\":false,\"description\":\"the
83         // port the DBMS service will listen to for data and
84         // requests\",\"password\":false,\"name\":\"port\",\"parentUniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2\",\"definition\":true},{\"uniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2.root_password\",\"type\":\"string\",\"required\":false,\"description\":\"the
85         // optional root password for the DBMS
86         // service\",\"password\":false,\"name\":\"root_password\",\"parentUniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2\",\"definition\":true}],\"interfaces\":{\"standard\":{\"type\":\"tosca.interfaces.node.lifecycle.Standard\",\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard\",\"operations\":{\"stop\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.stop\",\"description\":\"Standard
87         // lifecycle stop
88         // operation.\",\"definition\":false},\"start\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.start\",\"description\":\"Standard
89         // lifecycle start
90         // operation.\",\"definition\":false},\"delete\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.delete\",\"description\":\"Standard
91         // lifecycle delete
92         // operation.\",\"definition\":false},\"create\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.create\",\"description\":\"Standard
93         // lifecycle create
94         // operation.\",\"definition\":false},\"configure\":{\"uniqueId\":\"tosca.interfaces.node.lifecycle.standard.configure\",\"description\":\"Standard
95         // lifecycle configure
96         // operation.\",\"definition\":false}},\"definition\":false}},\"capabilities\":{\"feature\":{\"uniqueId\":\"capability.8313348e-3623-4f4a-9b8f-d2fbadaf9a31.feature\",\"type\":\"tosca.capabilities.Node\"},\"feature2\":{\"uniqueId\":\"capability.52eb0139-a855-47b9-a0e6-c90f0a90b1d2.feature2\",\"type\":\"tosca.capabilities.Node\"}},\"requirements\":{\"dependency\":{\"uniqueId\":\"8313348e-3623-4f4a-9b8f-d2fbadaf9a31.dependency\",\"capability\":\"tosca.capabilities.Node\",\"node\":\"tosca.nodes.Root\",\"relationship\":\"tosca.relationships.DependsOn\"},\"dependency2\":{\"uniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2.dependency2\",\"capability\":\"tosca.capabilities.Node\",\"node\":\"tosca.nodes.importResource4test\",\"relationship\":\"tosca.relationships.DependsOn\"}},\"vendorName\":\"ATT
97         // (Tosca)\",\"vendorRelease\":\"1.0.0.wd03\",\"contactId\":\"jh0003\",\"systemName\":\"Importresource4test\",\"additionalInformation\":[{\"uniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2.additionalinformation\",\"lastCreatedCounter\":0,\"parentUniqueId\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2\",\"parameters\":[]}],\"allVersions\":{\"0.1\":\"52eb0139-a855-47b9-a0e6-c90f0a90b1d2\"},\"abstract\":false,\"highestVersion\":true,\"uuid\":\"2e91a2df-b066-49bb-abde-4c1c01e409db\"}";
98         // convertResourceResponseToJavaObject(response);
99         // }
100
101         private static final String INVARIANT_UUID = "invariantUUID";
102         public static final String UNIQUE_ID = "uniqueId";
103         public static final String VERSION = "version";
104         public static final String UUID = "uuid";
105         public static final String NAME = "name";
106         public static final String ORIGIN_TYPE = "originType";
107         public static final String TOSCA_RESOURCE_NAME = "toscaResourceName";
108
109         static Logger logger = Logger.getLogger(ResponseParser.class.getName());
110
111         public static String getValueFromJsonResponse(String response, String fieldName) {
112                 try {
113                         JSONObject jsonResp = (JSONObject) JSONValue.parse(response);
114                         Object fieldValue = jsonResp.get(fieldName);
115                         return fieldValue.toString();
116
117                 } catch (Exception e) {
118                         return null;
119                 }
120
121         }
122
123         public static String getUniqueIdFromResponse(RestResponse response) {
124                 return getValueFromJsonResponse(response.getResponse(), UNIQUE_ID);
125         }
126
127         public static String getInvariantUuid(RestResponse response) {
128                 return getValueFromJsonResponse(response.getResponse(), INVARIANT_UUID);
129         }
130
131         public static String getUuidFromResponse(RestResponse response) {
132                 return getValueFromJsonResponse(response.getResponse(), UUID);
133         }
134
135         public static String getNameFromResponse(RestResponse response) {
136                 return getValueFromJsonResponse(response.getResponse(), NAME);
137         }
138
139         public static String getVersionFromResponse(RestResponse response) {
140                 return ResponseParser.getValueFromJsonResponse(response.getResponse(), VERSION);
141         }
142
143         public static String getComponentTypeFromResponse(RestResponse response) {
144                 return ResponseParser.getValueFromJsonResponse(response.getResponse(), ORIGIN_TYPE);
145         }
146
147         public static String getToscaResourceNameFromResponse(RestResponse response) {
148                 return getValueFromJsonResponse(response.getResponse(), TOSCA_RESOURCE_NAME);
149         }
150
151         @SuppressWarnings("unchecked")
152         public static ResourceRespJavaObject parseJsonListReturnResourceDetailsObj(RestResponse restResponse,
153                         String resourceType, String searchPattern, String expectedResult) throws Exception {
154
155                 // Gson gson = new Gson;
156
157                 JsonElement jElement = new JsonParser().parse(restResponse.getResponse());
158                 JsonObject jObject = jElement.getAsJsonObject();
159                 JsonArray arrayOfObjects = (JsonArray) jObject.get(resourceType);
160                 Gson gson = new Gson();
161                 Map<String, Object> map = new HashMap<String, Object>();
162                 ResourceRespJavaObject jsonToJavaObject = new ResourceRespJavaObject();
163
164                 for (int counter = 0; counter < arrayOfObjects.size(); counter++) {
165                         JsonObject jHitObject = (JsonObject) arrayOfObjects.get(counter);
166
167                         map = (Map<String, Object>) gson.fromJson(jHitObject.toString(), map.getClass());
168                         if (map.get(searchPattern).toString().contains(expectedResult)) {
169
170                                 jsonToJavaObject = gson.fromJson(jObject, ResourceRespJavaObject.class);
171                                 break;
172                         }
173                 }
174                 return jsonToJavaObject;
175
176         }
177
178         public static Resource convertResourceResponseToJavaObject(String response) {
179
180                 ObjectMapper mapper = new ObjectMapper();
181                 final SimpleModule module = new SimpleModule("customerSerializationModule",
182                                 new Version(1, 0, 0, "static version"));
183                 JsonDeserializer<PropertyConstraint> deserializer = new PropertyConstraintJacksonDeserialiser();
184                 addDeserializer(module, PropertyConstraint.class, deserializer);
185
186                 mapper.registerModule(module);
187                 Resource resource = null;
188                 try {
189 //                      TODO Andrey L. uncomment line below in case to ignore on unknown properties, not recommended  
190                         mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
191                         resource = mapper.readValue(response, Resource.class);
192                         
193                         logger.debug(resource.toString());
194                 } catch (IOException e) {
195                         try {
196                                 List<Resource> resources = Arrays.asList(mapper.readValue(response.toString(), Resource[].class));
197                                 resource = resources.get(0);
198                         } catch (Exception e1) {
199                                 // TODO Auto-generated catch block
200                                 e1.printStackTrace();
201                         }
202                 }
203
204                 return resource;
205         }
206
207         public static ComponentInstanceProperty convertPropertyResponseToJavaObject(String response) {
208
209                 ObjectMapper mapper = new ObjectMapper();
210                 final SimpleModule module = new SimpleModule("customerSerializationModule",
211                                 new Version(1, 0, 0, "static version"));
212                 JsonDeserializer<PropertyConstraint> desrializer = new PropertyConstraintJacksonDeserialiser();
213                 addDeserializer(module, PropertyConstraint.class, desrializer);
214
215                 mapper.registerModule(module);
216                 ComponentInstanceProperty propertyDefinition = null;
217                 try {
218                         mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
219                         propertyDefinition = mapper.readValue(response, ComponentInstanceProperty.class);
220                         logger.debug(propertyDefinition.toString());
221                 } catch (IOException e) {
222                         // TODO Auto-generated catch block
223                         e.printStackTrace();
224                 }
225                 return propertyDefinition;
226         }
227
228         public static String toJson(Object object) {
229                 Gson gson = new Gson();
230                 return gson.toJson(object);
231         }
232
233         public static ArtifactDefinition convertArtifactDefinitionResponseToJavaObject(String response) {
234                 ObjectMapper mapper = new ObjectMapper();
235                 ArtifactDefinition artifactDefinition = null;
236                 try {
237                         mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
238                         artifactDefinition = mapper.readValue(response, ArtifactDefinition.class);
239                         logger.debug(artifactDefinition.toString());
240                 } catch (IOException e) {
241                         // TODO Auto-generated catch block
242                         e.printStackTrace();
243                 }
244
245                 return artifactDefinition;
246
247         }
248
249         public static ArtifactReqDetails convertArtifactReqDetailsToJavaObject(String response) {
250
251                 ArtifactReqDetails artifactReqDetails = null;
252                 Gson gson = new Gson();
253                 artifactReqDetails = gson.fromJson(response, ArtifactReqDetails.class);
254                 return artifactReqDetails;
255         }
256
257         public static <T> T parseToObject(String json, Class<T> clazz) {
258                 Gson gson = new Gson();
259                 T object;
260                 try {
261                         object = gson.fromJson(json, clazz);
262                 } catch (Exception e) {
263                         object = parseToObjectUsingMapper(json, clazz);
264                 }
265                 return object;
266         }
267
268         public static <T> T parseToObjectUsingMapper(String json, Class<T> clazz) {
269                 // Generic convert
270                 ObjectMapper mapper = new ObjectMapper();
271                 T object = null;
272                 final SimpleModule module = new SimpleModule("customerSerializationModule",
273                                 new Version(1, 0, 0, "static version"));
274                 JsonDeserializer<PropertyConstraint> desrializer = new PropertyConstraintJacksonDeserialiser();
275                 addDeserializer(module, PropertyConstraint.class, desrializer);
276                 mapper.registerModule(module);
277                 mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
278                 try {
279                         object = mapper.readValue(json, clazz);
280                         // System.out.println("Class: "+clazz.getSimpleName()+", json:
281                         // "+json);
282                 } catch (IOException e) {
283                         // TODO Auto-generated catch block
284                         e.printStackTrace();
285                 }
286
287                 return object;
288         }
289
290         public static ArtifactReqDetails convertArtifactDefinitionToArtifactReqDetailsObject(
291                         ArtifactDefinition artifactDefinition) {
292
293                 ArtifactReqDetails artifactReqDetails = null;
294                 Gson gson = new Gson();
295                 String artDef = gson.toJson(artifactDefinition);
296                 artifactReqDetails = gson.fromJson(artDef, ArtifactReqDetails.class);
297                 return artifactReqDetails;
298         }
299
300         public static <T> void addDeserializer(SimpleModule module, Class<T> clazz,
301                         final JsonDeserializer<T> deserializer) {
302                 module.addDeserializer(clazz, deserializer);
303         }
304
305         public static Service convertServiceResponseToJavaObject(String response) {
306
307                 ObjectMapper mapper = new ObjectMapper();
308                 final SimpleModule module = new SimpleModule("customerSerializationModule",
309                                 new Version(1, 0, 0, "static version"));
310                 JsonDeserializer<PropertyConstraint> deserializer = new PropertyConstraintJacksonDeserialiser();
311                 addDeserializer(module, PropertyConstraint.class, deserializer);
312
313                 mapper.registerModule(module);
314                 Service service = null;
315                 try {
316 //                      TODO Andrey L. uncomment line below in case to ignore on unknown properties, not recommended, added by Matvey
317                         mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
318                         service = mapper.readValue(response, Service.class);
319                         logger.debug(service.toString());
320                 } catch (IOException e) {
321                         // TODO Auto-generated catch block
322                         e.printStackTrace();
323                 }
324
325                 return service;
326         }
327
328         public static Product convertProductResponseToJavaObject(String response) {
329
330                 ObjectMapper mapper = new ObjectMapper();
331                 
332                 final SimpleModule module = new SimpleModule("customerSerializationModule",
333                                 new Version(1, 0, 0, "static version"));
334                 JsonDeserializer<PropertyConstraint> desrializer = new PropertyConstraintJacksonDeserialiser();
335                 addDeserializer(module, PropertyConstraint.class, desrializer);
336
337                 mapper.registerModule(module);
338                 
339                 Product product = null;
340                 try {
341                         mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
342                         product = mapper.readValue(response, Product.class);
343                         logger.debug(product.toString());
344                 } catch (IOException e) {
345                         // TODO Auto-generated catch block
346                         e.printStackTrace();
347                 }
348
349                 return product;
350         }
351
352         public static ComponentInstance convertComponentInstanceResponseToJavaObject(String response) {
353
354                 ObjectMapper mapper = new ObjectMapper();
355                 final SimpleModule module = new SimpleModule("customerSerializationModule",
356                                 new Version(1, 0, 0, "static version"));
357                 JsonDeserializer<PropertyConstraint> desrializer = new PropertyConstraintJacksonDeserialiser();
358                 addDeserializer(module, PropertyConstraint.class, desrializer);
359
360                 mapper.registerModule(module);
361                 ComponentInstance componentInstance = null;
362                 try {
363                         mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
364                         componentInstance = mapper.readValue(response, ComponentInstance.class);
365                         logger.debug(componentInstance.toString());
366                 } catch (IOException e) {
367                         // TODO Auto-generated catch block
368                         e.printStackTrace();
369                 }
370
371                 return componentInstance;
372         }
373
374         public static List<String> getValuesFromJsonArray(RestResponse message) throws Exception {
375                 List<String> artifactTypesArrayFromApi = new ArrayList<String>();
376
377                 org.json.JSONObject responseObject = new org.json.JSONObject(message.getResponse());
378                 JSONArray jArr = responseObject.getJSONArray("artifactTypes");
379
380                 for (int i = 0; i < jArr.length(); i++) {
381                         org.json.JSONObject jObj = jArr.getJSONObject(i);
382                         String value = jObj.get("name").toString();
383
384                         artifactTypesArrayFromApi.add(value);
385                 }
386                 return artifactTypesArrayFromApi;
387         }
388
389         public static String calculateMD5Header(ArtifactReqDetails artifactDetails) {
390                 Gson gson = new Gson();
391                 String jsonBody = gson.toJson(artifactDetails);
392                 // calculate MD5 for json body
393                 return calculateMD5(jsonBody);
394
395         }
396
397         public static String calculateMD5(String data) {
398                 String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
399                 // encode base-64 result
400                 byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes());
401                 String encodeBase64Str = new String(encodeBase64);
402                 return encodeBase64Str;
403
404         }
405
406         public static List<Map<String, Object>> getAuditFromMessage(Map auditingMessage) {
407                 List<Map<String, Object>> auditList = new ArrayList<Map<String, Object>>();
408                 // JsonElement jElement = new JsonParser().parse(auditingMessage);
409                 // JsonObject jObject = jElement.getAsJsonObject();
410                 // JsonObject hitsObject = (JsonObject) jObject.get("hits");
411                 // JsonArray hitsArray = (JsonArray) hitsObject.get("hits");
412                 //
413                 // Iterator<JsonElement> hitsIterator = hitsArray.iterator();
414                 // while(hitsIterator.hasNext())
415                 // {
416                 // JsonElement nextHit = hitsIterator.next();
417                 // JsonObject jHitObject = nextHit.getAsJsonObject();
418                 // JsonObject jSourceObject = (JsonObject) jHitObject.get("_source");
419                 //
420                 // Gson gson=new Gson();
421                 // String auditUnparsed = jSourceObject.toString();
422                 //
423                 // Map<String,Object> map = new HashMap<String,Object>();
424                 // map = (Map<String,Object>) gson.fromJson(auditUnparsed,
425                 // map.getClass());
426
427                 auditList.add(auditingMessage);
428                 // }
429                 return auditList;
430         }
431
432         public static List<CategoryDefinition> parseCategories(RestResponse getAllCategoriesRest) {
433
434                 List<CategoryDefinition> categories = new ArrayList<>();
435                 try {
436                         JsonElement jElement = new JsonParser().parse(getAllCategoriesRest.getResponse());
437                         JsonArray cagegories = jElement.getAsJsonArray();
438                         Iterator<JsonElement> iter = cagegories.iterator();
439                         while (iter.hasNext()) {
440                                 JsonElement next = iter.next();
441                                 CategoryDefinition category = ResponseParser.parseToObject(next.toString(), CategoryDefinition.class);
442                                 categories.add(category);
443                         }
444
445                 } catch (Exception e) {
446                         e.printStackTrace();
447                 }
448
449                 return categories;
450         }
451
452         public static JSONArray getListFromJson(RestResponse res, String field) throws JSONException {
453                 String valueFromJsonResponse = getValueFromJsonResponse(res.getResponse(), field);
454                 JSONArray jArr = new JSONArray(valueFromJsonResponse);
455
456                 return jArr;
457         }
458
459         public static List<String> getDerivedListFromJson(RestResponse res) throws JSONException {
460                 JSONArray listFromJson = getListFromJson(res, "derivedList");
461                 List<String> lst = new ArrayList<String>();
462                 for (int i = 0; i < listFromJson.length(); i++) {
463                         lst.add(listFromJson.getString(i));
464                 }
465
466                 return lst;
467         }
468
469         public static Map<String, Object> convertStringToMap(String obj) {
470                 Map<String, Object> object = (Map<String, Object>) JSONValue.parse(obj);
471                 return object;
472         }
473
474         public static List<Map<String, Object>> getListOfMapsFromJson(RestResponse res, String field) throws Exception {
475                 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
476                 JSONArray listFromJson = getListFromJson(res, field);
477                 for (int i = 0; i < listFromJson.length(); i++) {
478                         Map<String, Object> convertStringToMap = convertStringToMap(listFromJson.getString(i));
479                         list.add(convertStringToMap);
480                 }
481                 return list;
482
483         }
484
485         public static Map<String, Object> getJsonValueAsMap(RestResponse response, String key) {
486                 String valueField = getValueFromJsonResponse(response.getResponse(), key);
487                 Map<String, Object> convertToMap = convertStringToMap(valueField);
488                 return convertToMap;
489         }
490
491         public static String getJsonObjectValueByKey(String metadata, String key) {
492                 JsonElement jelement = new JsonParser().parse(metadata);
493
494                 JsonObject jobject = jelement.getAsJsonObject();
495                 Object obj = jobject.get(key);
496                 if (obj == null) {
497                         return null;
498                 } else {
499                         return obj.toString();
500                 }
501         }
502
503         public static Map<String, List<Component>> convertCatalogResponseToJavaObject(String response) {
504
505                 // Map<String, ArrayList<Component>> map = new HashMap<String,
506                 // ArrayList<Component>>();
507                 Map<String, List<Component>> map = new HashMap<String, List<Component>>();
508
509                 JsonElement jElement = new JsonParser().parse(response);
510                 JsonObject jObject = jElement.getAsJsonObject();
511                 JsonArray jArrReousrces = jObject.getAsJsonArray("resources");
512                 JsonArray jArrServices = jObject.getAsJsonArray("services");
513                 JsonArray jArrProducts = jObject.getAsJsonArray("products");
514                 
515                 if (jArrReousrces != null && jArrServices != null && jArrProducts != null){
516                         
517                 
518                 //////// RESOURCE/////////////////////////////
519                 ArrayList<Component> restResponseArray = new ArrayList<>();
520                 Component component = null;
521                 for (int i = 0; i < jArrReousrces.size(); i++) {
522                         String resourceString = (String) jArrReousrces.get(i).toString();
523                         component = ResponseParser.convertResourceResponseToJavaObject(resourceString);
524                         restResponseArray.add(component);
525                 }
526
527                 map.put("resources", restResponseArray);
528
529                 ///////// SERVICE/////////////////////////////
530
531                 restResponseArray = new ArrayList<>();
532                 component = null;
533                 for (int i = 0; i < jArrServices.size(); i++) {
534                         String resourceString = (String) jArrServices.get(i).toString();
535                         component = ResponseParser.convertServiceResponseToJavaObject(resourceString);
536                         restResponseArray.add(component);
537                 }
538
539                 map.put("services", restResponseArray);
540
541                 ///////// PRODUCT/////////////////////////////
542                 restResponseArray = new ArrayList<>();
543                 component = null;
544                 for (int i = 0; i < jArrProducts.size(); i++) {
545                         String resourceString = (String) jArrProducts.get(i).toString();
546                         component = ResponseParser.convertProductResponseToJavaObject(resourceString);
547                         restResponseArray.add(component);
548                 }
549
550                 map.put("products", restResponseArray);
551                 
552         }
553                 else {
554                         map.put("resources", new ArrayList<>());
555                         map.put("services", new ArrayList<>());
556                         map.put("products", new ArrayList<>());
557                 }
558
559                 return map;
560
561         }
562         
563         
564         public static Map<Long, ServiceDistributionStatus> convertServiceDistributionStatusToObject(String response) throws ParseException {
565
566                 Map<Long, ServiceDistributionStatus> serviceDistributionStatusMap = new HashMap<Long, ServiceDistributionStatus>();
567                 ServiceDistributionStatus serviceDistributionStatusObject = null;
568                 
569                 JsonElement jElement = new JsonParser().parse(response);
570                 JsonObject jObject = jElement.getAsJsonObject();
571                 JsonArray jDistrStatusArray = jObject.getAsJsonArray("distributionStatusOfServiceList");
572                 
573                 for (int i = 0; i < jDistrStatusArray.size(); i++){
574                         Gson gson = new Gson();
575                         String servDistrStatus = gson.toJson(jDistrStatusArray.get(i));
576                         serviceDistributionStatusObject = gson.fromJson(servDistrStatus, ServiceDistributionStatus.class);
577                         serviceDistributionStatusMap.put(Utils.getEpochTimeFromUTC(serviceDistributionStatusObject.getTimestamp()), serviceDistributionStatusObject);
578                 }
579
580                 return serviceDistributionStatusMap;
581                 
582         }
583         
584         public static Map<String, String> getPropertiesNameType(RestResponse restResponse)
585                         throws JSONException {
586                 Map<String, String> propertiesMap = new HashMap<String, String>();
587                 JSONArray propertiesList = getListFromJson(restResponse, "properties");
588                 for (int i = 0; i < propertiesList.length() ; i ++){
589                         JSONObject  prop = (JSONObject) JSONValue.parse(propertiesList.get(i).toString());
590                         String propName = prop.get("name").toString();
591                         String propType = prop.get("type").toString();
592                         propertiesMap.put(propName, propType);
593                 }
594                 
595                 return propertiesMap;
596         }
597         
598         public static ResourceAssetStructure getDataOutOfSearchExternalAPIResponseForResourceName(String response, String resourceName) {
599                 Gson gson = new Gson();
600                 JsonElement jsonElement = new JsonParser().parse(response);
601                 JsonArray jsonArray = jsonElement.getAsJsonArray();
602                 for(JsonElement jElement: jsonArray) {
603                         ResourceAssetStructure parsedResponse = gson.fromJson(jElement, ResourceAssetStructure.class);
604                         
605                         if(resourceName.contains(parsedResponse.getName()) && parsedResponse.getName().contains(resourceName)) {
606                                 return parsedResponse;
607                         }
608                 }
609                 
610                 return null;
611         }
612 }