aboutsummaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/rest/ResourceRestUtils.java
blob: 656ece54f0ef559b3097664a85b5182ffb4171e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.ci.tests.utils.rest;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.model.*;
import org.openecomp.sdc.be.resources.data.RelationshipInstData;
import org.openecomp.sdc.ci.tests.api.Urls;
import org.openecomp.sdc.ci.tests.config.Config;
import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.ImportReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.utils.Utils;
import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
import org.openecomp.sdc.common.util.GeneralUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.testng.AssertJUnit.assertEquals;

public class ResourceRestUtils extends BaseRestUtils {
	private static Logger logger = LoggerFactory.getLogger(ResourceRestUtils.class.getName());

	// ****** CREATE *******

	public static RestResponse createResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
			throws Exception {

		Config config = Utils.getConfig();
		String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());

		String userId = sdncModifierDetails.getUserId();

		Map<String, String> headersMap = prepareHeadersMap(userId);

		Gson gson = new Gson();
		String userBodyJson = gson.toJson(resourceDetails);
		String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
		headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
		HttpRequest http = new HttpRequest();
		RestResponse createResourceResponse = http.httpSendPost(url, userBodyJson, headersMap);
		if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
			resourceDetails.setUUID(ResponseParser.getUuidFromResponse(createResourceResponse));
			resourceDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
			resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
			String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(createResourceResponse.getResponse(),
					"lastUpdaterUserId");
			resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
			String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(createResourceResponse.getResponse(),
					"lastUpdaterFullName");
			resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
			// Creator details never change after component is created - Ella,
			// 12/1/2016
			resourceDetails.setCreatorUserId(userId);
			resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
		}
		return createResourceResponse;

	}

	public static RestResponse createImportResource(ImportReqDetails importReqDetails, User sdncModifierDetails,
			Map<String, String> additionalHeaders) throws JSONException, IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.CREATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort());
		String userId = sdncModifierDetails.getUserId();

		Gson gson = new Gson();
		String resourceImportBodyJson = gson.toJson(importReqDetails);
		HttpRequest http = new HttpRequest();

		Map<String, String> headersMap = prepareHeadersMap(userId);
		if (additionalHeaders != null) {
			headersMap.putAll(additionalHeaders);
		} else {
			headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), ArtifactRestUtils.calculateMD5(resourceImportBodyJson));
		}

		RestResponse createResourceResponse = http.httpSendPost(url, resourceImportBodyJson, headersMap);
		if (createResourceResponse.getErrorCode() == STATUS_CODE_CREATED) {
			importReqDetails.setVersion(ResponseParser.getVersionFromResponse(createResourceResponse));
			importReqDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(createResourceResponse));
			// Creator details never change after component is created - Ella,
			// 12/1/2016
			importReqDetails.setCreatorUserId(userId);
			importReqDetails.setCreatorFullName(sdncModifierDetails.getFullName());
			importReqDetails
					.setToscaResourceName(ResponseParser.getToscaResourceNameFromResponse(createResourceResponse));
			importReqDetails.setDerivedList(ResponseParser.getDerivedListFromJson(createResourceResponse));
		}
		return createResourceResponse;

	}

	// ***** DELETE ****
	public static RestResponse deleteResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
			String version) throws IOException {

		if (resourceDetails.getUniqueId() != null) {
			Config config = Utils.getConfig();
			String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
					config.getCatalogBePort(), resourceDetails.getName(), version);
			return sendDelete(url, sdncModifierDetails.getUserId());
		} else {
			return null;
		}

	}

	public static RestResponse markResourceToDelete(String resourceId, String userId) throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
				resourceId);
		RestResponse sendDelete = sendDelete(url, userId);

		return sendDelete;

	}

	public static RestResponse deleteResource(String resourceId, String userId) throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.DELETE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
				resourceId);
		RestResponse sendDelete = sendDelete(url, userId);

		deleteMarkedResources(userId);

		return sendDelete;

	}

	public static void deleteMarkedResources(String userId) throws IOException {
		String url;
		Config config = Utils.getConfig();
		url = String.format(Urls.DELETE_MARKED_RESOURCES, config.getCatalogBeHost(), config.getCatalogBePort());
		sendDelete(url, userId);
	}

	public static RestResponse deleteResourceByNameAndVersion(User sdncModifierDetails, String resourceName,
			String resourceVersion) throws IOException {
		Config config = Utils.getConfig();
		String url = String.format(Urls.DELETE_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
				config.getCatalogBePort(), resourceName, resourceVersion);
		RestResponse sendDelete = sendDelete(url, sdncModifierDetails.getUserId());

		deleteMarkedResources(sdncModifierDetails.getUserId());

		return sendDelete;
	}

	public static Boolean deleteResourceByNameAndVersion(String resourceName, String resourceVersion)
			throws IOException {
		RestResponse deleteResponse = ResourceRestUtils.deleteResourceByNameAndVersion(
				ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceName, resourceVersion);
		return checkErrorCode(deleteResponse);
	}

	public static Boolean removeResource(String resourceId)
			throws FileNotFoundException, IOException, ClientProtocolException {
		RestResponse response = deleteResource(resourceId,
				ElementFactory.getDefaultUser(UserRoleEnum.ADMIN).getUserId());
		return checkErrorCode(response);
	}

	// ************** GET *************
	public static RestResponse getResource(User sdncModifierDetails, String uniqueId) throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(), uniqueId);
		return sendGet(url, sdncModifierDetails.getUserId());
	}

	public static RestResponse getModule(User sdncModifierDetails, String componentId, String moduleId)
			throws IOException {
		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_MODULE_BY_ID, config.getCatalogBeHost(), config.getCatalogBePort(),
				componentId, moduleId);
		return sendGet(url, sdncModifierDetails.getUserId());
	}

	public static RestResponse getLatestResourceFromCsarUuid(User sdncModifierDetails, String csarUuid)
			throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_RESOURCE_BY_CSAR_UUID, config.getCatalogBeHost(), config.getCatalogBePort(),
				csarUuid);
		return sendGet(url, sdncModifierDetails.getUserId());
	}

	public static RestResponse getResource(ResourceReqDetails resourceDetails, User sdncModifierDetails)
			throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
				resourceDetails.getUniqueId());
		return sendGet(url, sdncModifierDetails.getUserId());
	}

	public static RestResponse getResourceByNameAndVersion(String userId, String resourceName, String resourceVersion)
			throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_RESOURCE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
				config.getCatalogBePort(), resourceName, resourceVersion);

		return sendGet(url, userId);
	}

	public static RestResponse getResourceList(User sdncModifierDetails) throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());

		return sendGet(url, sdncModifierDetails.getUserId());

	}
	
	public static RestResponse getResourceListFilterByCategory(User sdncModifierDetails, String componentType, String category) throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "category=" + category);
		
		Map<String, String> headersMap =  prepareHeadersMap(sdncModifierDetails.getUserId());
		headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
		headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);

		return sendGet(url, sdncModifierDetails.getUserId(), headersMap);

	}
	
	public static RestResponse getResourceListFilterBySubCategory(User sdncModifierDetails, String componentType, String subcategory) throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, "subCategory=" + subcategory);
		
		Map<String, String> headersMap =  prepareHeadersMap(sdncModifierDetails.getUserId());
		headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
		headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);

		return sendGet(url, sdncModifierDetails.getUserId(), headersMap);

	}
	
	public static RestResponse getResourceListFilterByCriteria(User sdncModifierDetails, String componentType, String criteria, String value) throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_FILTERED_ASSET_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), componentType, criteria + "=" + value);
		
		Map<String, String> headersMap =  prepareHeadersMap(sdncModifierDetails.getUserId());
		headersMap.put(HttpHeaderEnum.AUTHORIZATION.getValue(), authorizationHeader);
		headersMap.put(HttpHeaderEnum.X_ECOMP_INSTANCE_ID.getValue(), BaseRestUtils.xEcompInstanceId);

		return sendGet(url, sdncModifierDetails.getUserId(), headersMap);

	}

	public static RestResponse getResource(String resourceId) throws ClientProtocolException, IOException {
		return getResource(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), resourceId);
	}

	public static RestResponse getLatestResourceFromCsarUuid(String csarUuid)
			throws ClientProtocolException, IOException {
		return getLatestResourceFromCsarUuid(ElementFactory.getDefaultUser(UserRoleEnum.ADMIN), csarUuid);
	}

	public static RestResponse getResourceLatestVersionList(User sdncModifierDetails) throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_RESOURCE_lATEST_VERSION, config.getCatalogBeHost(),
				config.getCatalogBePort());

		return sendGet(url, sdncModifierDetails.getUserId());

	}

	public static RestResponse putAllCategoriesTowardsCatalogFeWithUuidNotAllowed(String uuid) throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
				BaseRestUtils.RESOURCE_COMPONENT_TYPE);

		Map<String, String> headersMap = new HashMap<String, String>();
		headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
		headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
		headersMap.put(HttpHeaderEnum.X_ECOMP_REQUEST_ID_HEADER.getValue(), uuid);
		HttpRequest http = new HttpRequest();

		logger.debug("Send PUT request to get all categories (should be 405): {}", url);
		return http.httpSendByMethod(url, "PUT", null, headersMap);
	}

	public static RestResponse getAllTagsTowardsCatalogBe() throws IOException {

		Config config = Utils.getConfig();
		HttpRequest http = new HttpRequest();
		String url = String.format(Urls.GET_ALL_TAGS, config.getCatalogBeHost(), config.getCatalogBePort());

		Map<String, String> headersMap = new HashMap<String, String>();
		headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
		headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
		
		return http.httpSendGet(url, headersMap);

	}

	public static RestResponse getAllPropertyScopesTowardsCatalogBe() throws IOException {

		Config config = Utils.getConfig();
		HttpRequest http = new HttpRequest();
		String url = String.format(Urls.GET_PROPERTY_SCOPES_LIST, config.getCatalogBeHost(), config.getCatalogBePort());

		Map<String, String> headersMap = new HashMap<>();
		headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
		headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
		headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");

		return http.httpSendGet(url, headersMap);

	}

	public static RestResponse getAllArtifactTypesTowardsCatalogBe() throws IOException {

		Config config = Utils.getConfig();
		HttpRequest http = new HttpRequest();
		String url = String.format(Urls.GET_ALL_ARTIFACTS, config.getCatalogBeHost(), config.getCatalogBePort());

		Map<String, String> headersMap = new HashMap<>();

		headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), acceptJsonHeader);
		headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
		headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");

		return http.httpSendGet(url, headersMap);

	}

	public static RestResponse getConfigurationTowardsCatalogBe() throws IOException {

		Config config = Utils.getConfig();
		HttpRequest http = new HttpRequest();
		String url = String.format(Urls.GET_CONFIGURATION, config.getCatalogBeHost(), config.getCatalogBePort());

		Map<String, String> headersMap = new HashMap<>();
		headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), acceptJsonHeader);
		headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptJsonHeader);
		headersMap.put(HttpHeaderEnum.USER_ID.getValue(), "cs0008");

		return http.httpSendGet(url, headersMap);

	}

	public static RestResponse sendOptionsTowardsCatalogFeWithUuid() throws IOException {

		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_ALL_CATEGORIES_FE, config.getCatalogFeHost(), config.getCatalogFePort(),
				BaseRestUtils.RESOURCE_COMPONENT_TYPE);

		Map<String, String> headersMap = new HashMap<String, String>();
		headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
		headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderData);
		HttpRequest http = new HttpRequest();

		logger.debug("Send OPTIONS request for categories: {}", url);
		return http.httpSendByMethod(url, "OPTIONS", null, headersMap);
	}

	// ********** UPDATE *************
	public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
			User sdncModifierDetails, String uniqueId, String encoding) throws Exception {
		Config config = Utils.getConfig();
		String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
				uniqueId);

		String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);

		Gson gson = new Gson();
		String userBodyJson = gson.toJson(updatedResourceDetails);
		String userId = sdncModifierDetails.getUserId();

		RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);

		updatedResourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
		updatedResourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));

		return updateResourceResponse;
	}

	public static RestResponse updateResourceTEST(Resource resource, User sdncModifierDetails, String uniqueId,
			String encoding) throws Exception {
		Config config = Utils.getConfig();
		String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
				uniqueId);

		String ContentTypeString = String.format("%s;%s", contentTypeHeaderData, encoding);

		Gson gson = new Gson();
		String userBodyJson = gson.toJson(resource);
		String userId = sdncModifierDetails.getUserId();

		RestResponse updateResourceResponse = sendPut(url, userBodyJson, userId, ContentTypeString);

		// String resourceUniqueId =
		// ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
		// "uniqueId");
		// updatedResourceDetails.setUniqueId(resourceUniqueId);
		// String resourceVersion =
		// ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
		// "version");
		// updatedResourceDetails.setUniqueId(resourceVersion);

		return updateResourceResponse;
	}

	public static RestResponse updateResourceMetadata(ResourceReqDetails updatedResourceDetails,
			User sdncModifierDetails, String uniqueId) throws Exception {
		return updateResourceMetadata(updatedResourceDetails, sdncModifierDetails, uniqueId, "");
	}

	public static RestResponse updateResourceMetadata(String json, User sdncModifierDetails, String resourceId)
			throws IOException {
		Config config = Utils.getConfig();
		String url = String.format(Urls.UPDATE_RESOURCE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
				resourceId);
		String userId = sdncModifierDetails.getUserId();

		RestResponse updateResourceResponse = sendPut(url, json, userId, contentTypeHeaderData);

		return updateResourceResponse;
	}

	public static RestResponse updateResource(ResourceReqDetails resourceDetails, User sdncModifierDetails,
			String resourceId) throws IOException {

		String userId = sdncModifierDetails.getUserId();
		Config config = Utils.getConfig();
		String url = String.format(Urls.UPDATE_RESOURCE, config.getCatalogBeHost(), config.getCatalogBePort(),
				resourceId);

		Map<String, String> headersMap = prepareHeadersMap(userId);

		Gson gson = new Gson();
		String userBodyJson = gson.toJson(resourceDetails);
		String calculateMD5 = GeneralUtility.calculateMD5Base64EncodedByString(userBodyJson);
		headersMap.put(HttpHeaderEnum.Content_MD5.getValue(), calculateMD5);
		HttpRequest http = new HttpRequest();
		RestResponse updateResourceResponse = http.httpSendPut(url, userBodyJson, headersMap);
		if (updateResourceResponse.getErrorCode() == STATUS_CODE_UPDATE_SUCCESS) {
			resourceDetails.setUUID(ResponseParser.getUuidFromResponse(updateResourceResponse));
			resourceDetails.setVersion(ResponseParser.getVersionFromResponse(updateResourceResponse));
			resourceDetails.setUniqueId(ResponseParser.getUniqueIdFromResponse(updateResourceResponse));
			String lastUpdaterUserId = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
					"lastUpdaterUserId");
			resourceDetails.setLastUpdaterUserId(lastUpdaterUserId);
			String lastUpdaterFullName = ResponseParser.getValueFromJsonResponse(updateResourceResponse.getResponse(),
					"lastUpdaterFullName");
			resourceDetails.setLastUpdaterFullName(lastUpdaterFullName);
			resourceDetails.setCreatorUserId(userId);
			resourceDetails.setCreatorFullName(sdncModifierDetails.getFullName());
		}
		return updateResourceResponse;
	}

	public static RestResponse createResourceInstance(ResourceReqDetails resourceDetails, User modifier,
			String vfResourceUniqueId) throws Exception {
		ComponentInstanceReqDetails resourceInstanceReqDetails = ElementFactory
				.getComponentResourceInstance(resourceDetails);
		RestResponse createResourceInstanceResponse = ComponentInstanceRestUtils.createComponentInstance(
				resourceInstanceReqDetails, modifier, vfResourceUniqueId, ComponentTypeEnum.RESOURCE);
		ResourceRestUtils.checkCreateResponse(createResourceInstanceResponse);
		return createResourceInstanceResponse;
	}

	public static RestResponse associateResourceInstances(JSONObject body, User sdncModifierDetails,
			Component component) throws IOException {

		Config config = Utils.getConfig();
		Gson gson = new Gson();
		String bodyJson = gson.toJson(body);
		component.getComponentType();
		String componentType = ComponentTypeEnum.findParamByType(component.getComponentType());
		String url = String.format(Urls.ASSOCIATE__RESOURCE_INSTANCE, config.getCatalogBeHost(),
				config.getCatalogBePort(), componentType, component.getUniqueId());
		return sendPost(url, bodyJson, sdncModifierDetails.getUserId(), null);

	}

	public static RestResponse getFollowedList(User sdncModifierDetails) throws Exception {
		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
		return sendGet(url, sdncModifierDetails.getUserId());
	}

	public static List<Resource> restResponseToResourceObjectList(String restResponse) {
		JsonElement jelement = new JsonParser().parse(restResponse);
		JsonArray jsonArray = jelement.getAsJsonArray();
		List<Resource> restResponseArray = new ArrayList<>();
		Resource resource = null;
		for (int i = 0; i < jsonArray.size(); i++) {
			String resourceString = (String) jsonArray.get(i).toString();
			resource = ResponseParser.convertResourceResponseToJavaObject(resourceString);
			restResponseArray.add(resource);
		}

		return restResponseArray;

	}

	public static Resource getResourceObjectFromResourceListByUid(List<Resource> resourceList, String uid) {
		if (resourceList != null && resourceList.size() > 0) {
			for (Resource resource : resourceList) {
				if (resource.getUniqueId().equals(uid))
					return resource;
			}
		} else
			return null;
		return null;
	}

	// =======================================resource
	// associate==================================================
	public static RestResponse associate2ResourceInstances(Component container, ComponentInstance fromNode,
			ComponentInstance toNode, String assocType, User sdncUserDetails) throws IOException {
		return associate2ResourceInstances(container, fromNode.getUniqueId(), toNode.getUniqueId(), assocType,
				sdncUserDetails);
	}

	public static RestResponse associate2ResourceInstances(Component component, String fromNode, String toNode,
			String assocType, User sdncUserDetails) throws IOException {

		RelationshipInstData relationshipInstData = new RelationshipInstData();
		Map<String, List<CapabilityDefinition>> capabilitiesMap = component.getCapabilities();
		Map<String, List<RequirementDefinition>> requirementMap = component.getRequirements();
		List<CapabilityDefinition> capabilitiesList = capabilitiesMap.get(assocType);
		List<RequirementDefinition> requirementList = requirementMap.get(assocType);

		RequirementDefinition requirementDefinitionFrom = getRequirementDefinitionByOwnerId(requirementList, fromNode);
		CapabilityDefinition capabilityDefinitionTo = getCapabilityDefinitionByOwnerId(capabilitiesList, toNode);
		relationshipInstData.setCapabilityOwnerId(capabilityDefinitionTo.getOwnerId());
		relationshipInstData.setCapabiltyId(capabilityDefinitionTo.getUniqueId());
		relationshipInstData.setRequirementOwnerId(requirementDefinitionFrom.getOwnerId());
		relationshipInstData.setRequirementId(requirementDefinitionFrom.getUniqueId());

		JSONObject assocBody = assocBuilder(relationshipInstData, capabilityDefinitionTo, requirementDefinitionFrom,
				toNode, fromNode);
		return ResourceRestUtils.associateResourceInstances(assocBody, sdncUserDetails, component);

	}

	private static JSONObject assocBuilder(RelationshipInstData relationshipInstData,
			CapabilityDefinition capabilityDefinitionTo, RequirementDefinition requirementDefinitionFrom, String toNode,
			String fromNode) {

		String type = capabilityDefinitionTo.getType();
		String requirement = requirementDefinitionFrom.getName();
		String capability = requirementDefinitionFrom.getName();

		JSONObject wrapper = new JSONObject();
		JSONArray relationshipsArray = new JSONArray();
		JSONObject relationship = new JSONObject();
		JSONObject simpleObject = new JSONObject();

		relationship.put("type", type);
		simpleObject.put("relationship", relationship);
		simpleObject.put("requirement", requirement);
		simpleObject.put("capability", capability);
		simpleObject.put("capabilityUid", relationshipInstData.getCapabiltyId());
		simpleObject.put("capabilityOwnerId", relationshipInstData.getCapabilityOwnerId());
		simpleObject.put("requirementOwnerId", relationshipInstData.getRequirementOwnerId());
		simpleObject.put("requirementUid", relationshipInstData.getRequirementId());
		relationshipsArray.add(simpleObject);

		ArrayList<Object> relationships = new ArrayList<Object>(relationshipsArray);
		wrapper.put("fromNode", fromNode);
		wrapper.put("toNode", toNode);
		wrapper.put("relationships", relationships);
		return wrapper;

	}

	private static CapabilityDefinition getCapabilityDefinitionByOwnerId(
			List<CapabilityDefinition> capabilityDefinitionList, String ownerId) {

		for (CapabilityDefinition capabilityDefinition : capabilityDefinitionList) {
			if (capabilityDefinition.getOwnerId().equals(ownerId)) {
				return capabilityDefinition;
			}
		}
		return null;
	}

	private static RequirementDefinition getRequirementDefinitionByOwnerId(
			List<RequirementDefinition> requirementDefinitionList, String ownerId) {

		for (RequirementDefinition requirementDefinition : requirementDefinitionList) {
			if (requirementDefinition.getOwnerId().equals(ownerId)) {
				return requirementDefinition;
			}
		}
		return null;
	}

	public static String getRiUniqueIdByRiName(Component component, String resourceInstanceName) {

		List<ComponentInstance> componentInstances = component.getComponentInstances();
		String name = null;
		for (ComponentInstance componentInstance : componentInstances) {
			if (componentInstance.getName().equals(resourceInstanceName)) {
				name = componentInstance.getUniqueId();
				break;
			}
		}
		return name;
	}

	public static Resource convertResourceGetResponseToJavaObject(ResourceReqDetails resourceDetails)
			throws IOException {
		RestResponse response = ResourceRestUtils.getResource(resourceDetails,
				ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
		assertEquals("Check response code after get resource", 200, response.getErrorCode().intValue());
		return ResponseParser.convertResourceResponseToJavaObject(response.getResponse());
	}

	public static RestResponse changeResourceInstanceVersion(String containerUniqueId, String instanceToReplaceUniqueId,
			String newResourceUniqueId, User sdncModifierDetails, ComponentTypeEnum componentType) throws IOException {
		return ProductRestUtils.changeServiceInstanceVersion(containerUniqueId, instanceToReplaceUniqueId,
				newResourceUniqueId, sdncModifierDetails, componentType);
	}

}