re base code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / rest / ResourceRestUtils.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 com.google.gson.Gson;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParser;
27 import org.apache.http.client.ClientProtocolException;
28 import org.json.JSONException;
29 import org.json.simple.JSONArray;
30 import org.json.simple.JSONObject;
31 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
32 import org.openecomp.sdc.be.model.*;
33 import org.openecomp.sdc.be.resources.data.RelationshipInstData;
34 import org.openecomp.sdc.ci.tests.api.Urls;
35 import org.openecomp.sdc.ci.tests.config.Config;
36 import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
37 import org.openecomp.sdc.ci.tests.datatypes.ImportReqDetails;
38 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
39 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
40 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
41 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
42 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
43 import org.openecomp.sdc.ci.tests.utils.Utils;
44 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
45 import org.openecomp.sdc.common.util.GeneralUtility;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import java.io.FileNotFoundException;
50 import java.io.IOException;
51 import java.util.ArrayList;
52 import java.util.HashMap;
53 import java.util.List;
54 import java.util.Map;
55
56 import static org.testng.AssertJUnit.assertEquals;
57
58 public class ResourceRestUtils extends BaseRestUtils {
59         private static Logger logger = LoggerFactory.getLogger(ResourceRestUtils.class.getName());
60
61         // ****** CREATE *******
62
63         public static RestResponse createResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
64                         throws Exception {
65
66                 Config config = Utils.getConfig();
67                 String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
68
69                 String userId = sdncModifierDetails.getUserId();
70
71                 Map<String, String> headersMap = prepareHeadersMap(userId);
72
73                 Gson gson = new Gson();
74                 String userBodyJson = gson.toJson(resourceDetails);
75                 String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
76                 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
77                 HttpRequest http = new HttpRequest();
78                 RestResponse createResourceResponse = http.httpSendPost(url, userBodyJson, headersMap);
79                 if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
80                         resourceDetails.setUUID(ResponseParser.getUuidFromResponse(createResourceResponse));
81                         resourceDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
82                         resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
83                         String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(createResourceResponse.getResponse(),
84                                         "lastUpdaterUserId");
85                         resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
86                         String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(createResourceResponse.getResponse(),
87                                         "lastUpdaterFullName");
88                         resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
89                         // Creator details never change after component is created - Ella,
90                         // 12/1/2016
91                         resourceDetails.setCreatorUserId(userId);
92                         resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
93                 }
94                 return createResourceResponse;
95
96         }
97
98         public static RestResponse createImportResource(ImportReqDetails importReqDetails, User sdncModifierDetails,
99                         Map<String, String> additionalHeaders) throws JSONException, IOException {
100
101                 Config config = Utils.getConfig();
102                 String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
103                 String userId = sdncModifierDetails.getUserId();
104
105                 Gson gson = new Gson();
106                 String resourceImportBodyJson = gson.toJson(importReqDetails);
107                 HttpRequest http = new HttpRequest();
108
109                 Map<String, String> headersMap = prepareHeadersMap(userId);
110                 if (additionalHeaders != null) {
111                         headersMap.putAll(additionalHeaders);
112                 } else {
113                         headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), ArtifactRestUtils.calculateMD5(resourceImportBodyJson));
114                 }
115
116                 RestResponse createResourceResponse = http.httpSendPost(url, resourceImportBodyJson, headersMap);
117                 if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
118                         importReqDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
119                         importReqDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
120                         // Creator details never change after component is created - Ella,
121                         // 12/1/2016
122                         importReqDetails.setCreatorUserId(userId);
123                         importReqDetails.setCreatorFullName(sdncModifierDetails.getFullName());
124                         importReqDetails
125                                         .setToscaResourceName(ResponseParser.getToscaResourceNameFromResponse(createResourceResponse));
126                         importReqDetails.setDerivedList(ResponseParser.getDerivedListFromJson(createResourceResponse));
127                 }
128                 return createResourceResponse;
129
130         }
131
132         // ***** DELETE ****
133         public static RestResponse deleteResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
134                         String version) throws IOException {
135
136                 if (resourceDetails.getUniqueId() != null) {
137                         Config config = Utils.getConfig();
138                         String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
139                                         config.getCatalogBePort(), resourceDetails.getName(), version);
140                         return sendDelete(url, sdncModifierDetails.getUserId());
141                 } else {
142                         return null;
143                 }
144
145         }
146
147         public static RestResponse markResourceToDelete(String resourceId, String userId) throws IOException {
148
149                 Config config = Utils.getConfig();
150                 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
151                                 resourceId);
152                 RestResponse sendDelete = sendDelete(url, userId);
153
154                 return sendDelete;
155
156         }
157
158         public static RestResponse deleteResource(String resourceId, String userId) throws IOException {
159
160                 Config config = Utils.getConfig();
161                 String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
162                                 resourceId);
163                 RestResponse sendDelete = sendDelete(url, userId);
164
165                 deleteMarkedResources(userId);
166
167                 return sendDelete;
168
169         }
170
171         public static void deleteMarkedResources(String userId) throws IOException {
172                 String url;
173                 Config config = Utils.getConfig();
174                 url = String.format(Urls.DELETE_MARKED_RESOURCES, config.getCatalogBeHost(), config.getCatalogBePort());
175                 sendDelete(url, userId);
176         }
177
178         public static RestResponse deleteResourceByNameAndVersion(User sdncModifierDetails, String resourceName,
179                         String resourceVersion) throws IOException {
180                 Config config = Utils.getConfig();
181                 String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
182                                 config.getCatalogBePort(), resourceName, resourceVersion);
183                 RestResponse sendDelete = sendDelete(url, sdncModifierDetails.getUserId());
184
185                 deleteMarkedResources(sdncModifierDetails.getUserId());
186
187                 return sendDelete;
188         }
189
190         public static Boolean deleteResourceByNameAndVersion(String resourceName, String resourceVersion)
191                         throws IOException {
192                 RestResponse deleteResponse = ResourceRestUtils.deleteResourceByNameAndVersion(
193                                 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceName, resourceVersion);
194                 return checkErrorCode(deleteResponse);
195         }
196
197         public static Boolean removeResource(String resourceId)
198                         throws FileNotFoundException, IOException, ClientProtocolException {
199                 RestResponse response = deleteResource(resourceId,
200                                 ElementFactory.getDefaultUser(UserRoleEnum.ADMIN).getUserId());
201                 return checkErrorCode(response);
202         }
203
204         // ************** GET *************
205         public static RestResponse getResource(User sdncModifierDetails, String uniqueId) throws IOException {
206
207                 Config config = Utils.getConfig();
208                 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), uniqueId);
209                 return sendGet(url, sdncModifierDetails.getUserId());
210         }
211
212         public static RestResponse getModule(User sdncModifierDetails, String componentId, String moduleId)
213                         throws IOException {
214                 Config config = Utils.getConfig();
215                 String url = String.format(Urls.GET_MODULE_BY_ID, config.getCatalogBeHost(), config.getCatalogBePort(),
216                                 componentId, moduleId);
217                 return sendGet(url, sdncModifierDetails.getUserId());
218         }
219
220         public static RestResponse getLatestResourceFromCsarUuid(User sdncModifierDetails, String csarUuid)
221                         throws IOException {
222
223                 Config config = Utils.getConfig();
224                 String url = String.format(Urls.GET_RESOURCE_BY_CSAR_UUID, config.getCatalogBeHost(), config.getCatalogBePort(),
225                                 csarUuid);
226                 return sendGet(url, sdncModifierDetails.getUserId());
227         }
228
229         public static RestResponse getResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
230                         throws IOException {
231
232                 Config config = Utils.getConfig();
233                 String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
234                                 resourceDetails.getUniqueId());
235                 return sendGet(url, sdncModifierDetails.getUserId());
236         }
237
238         public static RestResponse getResourceByNameAndVersion(String userId, String resourceName, String resourceVersion)
239                         throws IOException {
240
241                 Config config = Utils.getConfig();
242                 String url = String.format(Urls.GET_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
243                                 config.getCatalogBePort(), resourceName, resourceVersion);
244
245                 return sendGet(url, userId);
246         }
247
248         public static RestResponse getResourceList(User sdncModifierDetails) throws IOException {
249
250                 Config config = Utils.getConfig();
251                 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
252
253                 return sendGet(url, sdncModifierDetails.getUserId());
254
255         }
256         
257         public static RestResponse getResourceListFilterByCategory(User sdncModifierDetails, String componentType, String category) throws IOException {
258
259                 Config config = Utils.getConfig();
260                 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "category=" + category);
261                 
262                 Map<String, String> headersMap =  prepareHeadersMap(sdncModifierDetails.getUserId());
263                 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
264                 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
265
266                 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
267
268         }
269         
270         public static RestResponse getResourceListFilterBySubCategory(User sdncModifierDetails, String componentType, String subcategory) throws IOException {
271
272                 Config config = Utils.getConfig();
273                 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "subCategory=" + subcategory);
274                 
275                 Map<String, String> headersMap =  prepareHeadersMap(sdncModifierDetails.getUserId());
276                 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
277                 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
278
279                 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
280
281         }
282         
283         public static RestResponse getResourceListFilterByCriteria(User sdncModifierDetails, String componentType, String criteria, String value) throws IOException {
284
285                 Config config = Utils.getConfig();
286                 String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, criteria + "=" + value);
287                 
288                 Map<String, String> headersMap =  prepareHeadersMap(sdncModifierDetails.getUserId());
289                 headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
290                 headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);
291
292                 return sendGet(url, sdncModifierDetails.getUserId(), headersMap);
293
294         }
295
296         public static RestResponse getResource(String resourceId) throws ClientProtocolException, IOException {
297                 return getResource(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceId);
298         }
299
300         public static RestResponse getLatestResourceFromCsarUuid(String csarUuid)
301                         throws ClientProtocolException, IOException {
302                 return getLatestResourceFromCsarUuid(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), csarUuid);
303         }
304
305         public static RestResponse getResourceLatestVersionList(User sdncModifierDetails) throws IOException {
306
307                 Config config = Utils.getConfig();
308                 String url = String.format(Urls.GET_RESOURCE_lATEST_VERSION, config.getCatalogBeHost(),
309                                 config.getCatalogBePort());
310
311                 return sendGet(url, sdncModifierDetails.getUserId());
312
313         }
314
315         public static RestResponse putAllCategoriesTowardsCatalogFeWithUuidNotAllowed(String uuid) throws IOException {
316
317                 Config config = Utils.getConfig();
318                 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
319                                 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
320
321                 Map<String, String> headersMap = new HashMap<String, String>();
322                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
323                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
324                 headersMap.put(HttpHeaderEnum.X_ECOMP_REQUEST_ID_HEADER.getValue(), uuid);
325                 HttpRequest http = new HttpRequest();
326
327                 logger.debug("Send PUT request to get all categories (should be 405): {}", url);
328                 return http.httpSendByMethod(url, "PUT", null, headersMap);
329         }
330
331         public static RestResponse getAllTagsTowardsCatalogBe() throws IOException {
332
333                 Config config = Utils.getConfig();
334                 HttpRequest http = new HttpRequest();
335                 String url = String.format(Urls.GET_ALL_TAGS, config.getCatalogBeHost(), config.getCatalogBePort());
336
337                 Map<String, String> headersMap = new HashMap<String, String>();
338                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
339                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
340                 
341                 return http.httpSendGet(url, headersMap);
342
343         }
344
345         public static RestResponse getAllPropertyScopesTowardsCatalogBe() throws IOException {
346
347                 Config config = Utils.getConfig();
348                 HttpRequest http = new HttpRequest();
349                 String url = String.format(Urls.GET_PROPERTY_SCOPES_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
350
351                 Map<String, String> headersMap = new HashMap<>();
352                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
353                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
354                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
355
356                 return http.httpSendGet(url, headersMap);
357
358         }
359
360         public static RestResponse getAllArtifactTypesTowardsCatalogBe() throws IOException {
361
362                 Config config = Utils.getConfig();
363                 HttpRequest http = new HttpRequest();
364                 String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());
365
366                 Map<String, String> headersMap = new HashMap<>();
367
368                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), acceptJsonHeader);
369                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
370                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
371
372                 return http.httpSendGet(url, headersMap);
373
374         }
375
376         public static RestResponse getConfigurationTowardsCatalogBe() throws IOException {
377
378                 Config config = Utils.getConfig();
379                 HttpRequest http = new HttpRequest();
380                 String url = String.format(Urls.GET_CONFIGURATION, config.getCatalogBeHost(), config.getCatalogBePort());
381
382                 Map<String, String> headersMap = new HashMap<>();
383                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), acceptJsonHeader);
384                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
385                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");
386
387                 return http.httpSendGet(url, headersMap);
388
389         }
390
391         public static RestResponse sendOptionsTowardsCatalogFeWithUuid() throws IOException {
392
393                 Config config = Utils.getConfig();
394                 String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
395                                 BaseRestUtils.RESOURCE_COMPONENT_TYPE);
396
397                 Map<String, String> headersMap = new HashMap<String, String>();
398                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
399                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
400                 HttpRequest http = new HttpRequest();
401
402                 logger.debug("Send OPTIONS request for categories: {}", url);
403                 return http.httpSendByMethod(url, "OPTIONS", null, headersMap);
404         }
405
406         // ********** UPDATE *************
407         public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
408                         User sdncModifierDetails, String uniqueId, String encoding) throws Exception {
409                 Config config = Utils.getConfig();
410                 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
411                                 uniqueId);
412
413                 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
414
415                 Gson gson = new Gson();
416                 String userBodyJson = gson.toJson(updatedResourceDetails);
417                 String userId = sdncModifierDetails.getUserId();
418
419                 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
420
421                 updatedResourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
422                 updatedResourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
423
424                 return updateResourceResponse;
425         }
426
427         public static RestResponse updateResourceTEST(Resource resource, User sdncModifierDetails, String uniqueId,
428                         String encoding) throws Exception {
429                 Config config = Utils.getConfig();
430                 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
431                                 uniqueId);
432
433                 String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);
434
435                 Gson gson = new Gson();
436                 String userBodyJson = gson.toJson(resource);
437                 String userId = sdncModifierDetails.getUserId();
438
439                 RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);
440
441                 // String resourceUniqueId =
442                 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
443                 // "uniqueId");
444                 // updatedResourceDetails.setUniqueId(resourceUniqueId);
445                 // String resourceVersion =
446                 // ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
447                 // "version");
448                 // updatedResourceDetails.setUniqueId(resourceVersion);
449
450                 return updateResourceResponse;
451         }
452
453         public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
454                         User sdncModifierDetails, String uniqueId) throws Exception {
455                 return updateResourceMetadata(updatedResourceDetails, sdncModifierDetails, uniqueId, "");
456         }
457
458         public static RestResponse updateResourceMetadata(String json, User sdncModifierDetails, String resourceId)
459                         throws IOException {
460                 Config config = Utils.getConfig();
461                 String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
462                                 resourceId);
463                 String userId = sdncModifierDetails.getUserId();
464
465                 RestResponse updateResourceResponse = sendPut(url, json, userId, contentTypeHeaderData);
466
467                 return updateResourceResponse;
468         }
469
470         public static RestResponse updateResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
471                         String resourceId) throws IOException {
472
473                 String userId = sdncModifierDetails.getUserId();
474                 Config config = Utils.getConfig();
475                 String url = String.format(Urls.UPDATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
476                                 resourceId);
477
478                 Map<String, String> headersMap = prepareHeadersMap(userId);
479
480                 Gson gson = new Gson();
481                 String userBodyJson = gson.toJson(resourceDetails);
482                 String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
483                 headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
484                 HttpRequest http = new HttpRequest();
485                 RestResponse updateResourceResponse = http.httpSendPut(url, userBodyJson, headersMap);
486                 if (updateResourceResponse.getErrorCode() == STATUS_CODE_UPDATE_SUCCESS) {
487                         resourceDetails.setUUID(ResponseParser.getUuidFromResponse(updateResourceResponse));
488                         resourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
489                         resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
490                         String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
491                                         "lastUpdaterUserId");
492                         resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
493                         String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
494                                         "lastUpdaterFullName");
495                         resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
496                         resourceDetails.setCreatorUserId(userId);
497                         resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
498                 }
499                 return updateResourceResponse;
500         }
501
502         public static RestResponse createResourceInstance(ResourceReqDetails resourceDetails, User modifier,
503                         String vfResourceUniqueId) throws Exception {
504                 ComponentInstanceReqDetails resourceInstanceReqDetails = ElementFactory
505                                 .getComponentResourceInstance(resourceDetails);
506                 RestResponse createResourceInstanceResponse = ComponentInstanceRestUtils.createComponentInstance(
507                                 resourceInstanceReqDetails, modifier, vfResourceUniqueId, ComponentTypeEnum.RESOURCE);
508                 ResourceRestUtils.checkCreateResponse(createResourceInstanceResponse);
509                 return createResourceInstanceResponse;
510         }
511
512         public static RestResponse associateResourceInstances(JSONObject body, User sdncModifierDetails,
513                         Component component) throws IOException {
514
515                 Config config = Utils.getConfig();
516                 Gson gson = new Gson();
517                 String bodyJson = gson.toJson(body);
518                 component.getComponentType();
519                 String componentType = ComponentTypeEnum.findParamByType(component.getComponentType());
520                 String url = String.format(Urls.ASSOCIATE__RESOURCE_INSTANCE, config.getCatalogBeHost(),
521                                 config.getCatalogBePort(), componentType, component.getUniqueId());
522                 return sendPost(url, bodyJson, sdncModifierDetails.getUserId(), null);
523
524         }
525
526         public static RestResponse getFollowedList(User sdncModifierDetails) throws Exception {
527                 Config config = Utils.getConfig();
528                 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
529                 return sendGet(url, sdncModifierDetails.getUserId());
530         }
531
532         public static List<Resource> restResponseToResourceObjectList(String restResponse) {
533                 JsonElement jelement = new JsonParser().parse(restResponse);
534                 JsonArray jsonArray = jelement.getAsJsonArray();
535                 List<Resource> restResponseArray = new ArrayList<>();
536                 Resource resource = null;
537                 for (int i = 0; i < jsonArray.size(); i++) {
538                         String resourceString = (String) jsonArray.get(i).toString();
539                         resource = ResponseParser.convertResourceResponseToJavaObject(resourceString);
540                         restResponseArray.add(resource);
541                 }
542
543                 return restResponseArray;
544
545         }
546
547         public static Resource getResourceObjectFromResourceListByUid(List<Resource> resourceList, String uid) {
548                 if (resourceList != null && resourceList.size() > 0) {
549                         for (Resource resource : resourceList) {
550                                 if (resource.getUniqueId().equals(uid))
551                                         return resource;
552                         }
553                 } else
554                         return null;
555                 return null;
556         }
557
558         // =======================================resource
559         // associate==================================================
560         public static RestResponse associate2ResourceInstances(Component container, ComponentInstance fromNode,
561                         ComponentInstance toNode, String assocType, User sdncUserDetails) throws IOException {
562                 return associate2ResourceInstances(container, fromNode.getUniqueId(), toNode.getUniqueId(), assocType,
563                                 sdncUserDetails);
564         }
565
566         public static RestResponse associate2ResourceInstances(Component component, String fromNode, String toNode,
567                         String assocType, User sdncUserDetails) throws IOException {
568
569                 RelationshipInstData relationshipInstData = new RelationshipInstData();
570                 Map<String, List<CapabilityDefinition>> capabilitiesMap = component.getCapabilities();
571                 Map<String, List<RequirementDefinition>> requirementMap = component.getRequirements();
572                 List<CapabilityDefinition> capabilitiesList = capabilitiesMap.get(assocType);
573                 List<RequirementDefinition> requirementList = requirementMap.get(assocType);
574
575                 RequirementDefinition requirementDefinitionFrom = getRequirementDefinitionByOwnerId(requirementList, fromNode);
576                 CapabilityDefinition capabilityDefinitionTo = getCapabilityDefinitionByOwnerId(capabilitiesList, toNode);
577                 relationshipInstData.setCapabilityOwnerId(capabilityDefinitionTo.getOwnerId());
578                 relationshipInstData.setCapabiltyId(capabilityDefinitionTo.getUniqueId());
579                 relationshipInstData.setRequirementOwnerId(requirementDefinitionFrom.getOwnerId());
580                 relationshipInstData.setRequirementId(requirementDefinitionFrom.getUniqueId());
581
582                 JSONObject assocBody = assocBuilder(relationshipInstData, capabilityDefinitionTo, requirementDefinitionFrom,
583                                 toNode, fromNode);
584                 return ResourceRestUtils.associateResourceInstances(assocBody, sdncUserDetails, component);
585
586         }
587
588         private static JSONObject assocBuilder(RelationshipInstData relationshipInstData,
589                         CapabilityDefinition capabilityDefinitionTo, RequirementDefinition requirementDefinitionFrom, String toNode,
590                         String fromNode) {
591
592                 String type = capabilityDefinitionTo.getType();
593                 String requirement = requirementDefinitionFrom.getName();
594                 String capability = requirementDefinitionFrom.getName();
595
596                 JSONObject wrapper = new JSONObject();
597                 JSONArray relationshipsArray = new JSONArray();
598                 JSONObject relationship = new JSONObject();
599                 JSONObject simpleObject = new JSONObject();
600
601                 relationship.put("type", type);
602                 simpleObject.put("relationship", relationship);
603                 simpleObject.put("requirement", requirement);
604                 simpleObject.put("capability", capability);
605                 simpleObject.put("capabilityUid", relationshipInstData.getCapabiltyId());
606                 simpleObject.put("capabilityOwnerId", relationshipInstData.getCapabilityOwnerId());
607                 simpleObject.put("requirementOwnerId", relationshipInstData.getRequirementOwnerId());
608                 simpleObject.put("requirementUid", relationshipInstData.getRequirementId());
609                 relationshipsArray.add(simpleObject);
610
611                 ArrayList<Object> relationships = new ArrayList<Object>(relationshipsArray);
612                 wrapper.put("fromNode", fromNode);
613                 wrapper.put("toNode", toNode);
614                 wrapper.put("relationships", relationships);
615                 return wrapper;
616
617         }
618
619         private static CapabilityDefinition getCapabilityDefinitionByOwnerId(
620                         List<CapabilityDefinition> capabilityDefinitionList, String ownerId) {
621
622                 for (CapabilityDefinition capabilityDefinition : capabilityDefinitionList) {
623                         if (capabilityDefinition.getOwnerId().equals(ownerId)) {
624                                 return capabilityDefinition;
625                         }
626                 }
627                 return null;
628         }
629
630         private static RequirementDefinition getRequirementDefinitionByOwnerId(
631                         List<RequirementDefinition> requirementDefinitionList, String ownerId) {
632
633                 for (RequirementDefinition requirementDefinition : requirementDefinitionList) {
634                         if (requirementDefinition.getOwnerId().equals(ownerId)) {
635                                 return requirementDefinition;
636                         }
637                 }
638                 return null;
639         }
640
641         public static String getRiUniqueIdByRiName(Component component, String resourceInstanceName) {
642
643                 List<ComponentInstance> componentInstances = component.getComponentInstances();
644                 String name = null;
645                 for (ComponentInstance componentInstance : componentInstances) {
646                         if (componentInstance.getName().equals(resourceInstanceName)) {
647                                 name = componentInstance.getUniqueId();
648                                 break;
649                         }
650                 }
651                 return name;
652         }
653
654         public static Resource convertResourceGetResponseToJavaObject(ResourceReqDetails resourceDetails)
655                         throws IOException {
656                 RestResponse response = ResourceRestUtils.getResource(resourceDetails,
657                                 ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
658                 assertEquals("Check response code after get resource", 200, response.getErrorCode().intValue());
659                 return ResponseParser.convertResourceResponseToJavaObject(response.getResponse());
660         }
661
662         public static RestResponse changeResourceInstanceVersion(String containerUniqueId, String instanceToReplaceUniqueId,
663                         String newResourceUniqueId, User sdncModifierDetails, ComponentTypeEnum componentType) throws IOException {
664                 return ProductRestUtils.changeServiceInstanceVersion(containerUniqueId, instanceToReplaceUniqueId,
665                                 newResourceUniqueId, sdncModifierDetails, componentType);
666         }
667
668 }