Fix for radio buttons
[sdc.git] / asdc-tests / src / main / java / org / openecomp / sdc / ci / tests / utils / general / AtomicOperationUtils.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.general;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.apache.commons.codec.binary.Base64;
36 import org.apache.commons.lang3.tuple.Pair;
37 import org.json.JSONException;
38 import org.openecomp.sdc.be.datatypes.elements.ConsumerDataDefinition;
39 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
40 import org.openecomp.sdc.be.model.ArtifactDefinition;
41 import org.openecomp.sdc.be.model.Component;
42 import org.openecomp.sdc.be.model.ComponentInstance;
43 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
44 import org.openecomp.sdc.be.model.DistributionStatusEnum;
45 import org.openecomp.sdc.be.model.Product;
46 import org.openecomp.sdc.be.model.Resource;
47 import org.openecomp.sdc.be.model.Service;
48 import org.openecomp.sdc.be.model.User;
49 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
50 import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
51 import org.openecomp.sdc.ci.tests.datatypes.ImportReqDetails;
52 import org.openecomp.sdc.ci.tests.datatypes.ProductReqDetails;
53 import org.openecomp.sdc.ci.tests.datatypes.PropertyReqDetails;
54 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
55 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
56 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
57 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
58 import org.openecomp.sdc.ci.tests.datatypes.enums.NormativeTypesEnum;
59 import org.openecomp.sdc.ci.tests.datatypes.enums.PropertyTypeEnum;
60 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
61 import org.openecomp.sdc.ci.tests.datatypes.enums.ServiceCategoriesEnum;
62 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
63 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
64 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
65 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
66 import org.openecomp.sdc.ci.tests.utils.rest.ComponentInstanceRestUtils;
67 import org.openecomp.sdc.ci.tests.utils.rest.ConsumerRestUtils;
68 import org.openecomp.sdc.ci.tests.utils.rest.LifecycleRestUtils;
69 import org.openecomp.sdc.ci.tests.utils.rest.ProductRestUtils;
70 import org.openecomp.sdc.ci.tests.utils.rest.PropertyRestUtils;
71 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
72 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
73 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
74 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
75
76 import com.google.gson.Gson;
77
78 import fj.data.Either;
79
80 public final class AtomicOperationUtils {
81
82         private AtomicOperationUtils() {
83                 throw new UnsupportedOperationException();
84         }
85
86         // *********** RESOURCE ****************
87         /**
88          * Import a vfc From tosca file
89          * 
90          * @param filePath
91          * @param fileName
92          * @return
93          * @throws IOException
94          * @throws JSONException
95          */
96         public static Either<Resource, RestResponse> importResource(String filePath, String fileName) {
97                 try {
98                         User designer = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
99                         ImportReqDetails importReqDetails = ElementFactory.getDefaultImportResource("ciTmpVFC");
100                         importReqDetails = ImportUtils.getImportResourceDetailsByPathAndName(importReqDetails, filePath, fileName);
101                         RestResponse importResourceResponse = ResourceRestUtils.createImportResource(importReqDetails, designer, null);
102                         return buildResourceFromResponse(importResourceResponse);
103                 } catch (Exception e) {
104                         throw new AtomicOperationException(e);
105                 }
106         }
107
108         public static Either<Resource, RestResponse> createResourceByType(ResourceTypeEnum resourceType, UserRoleEnum userRole, Boolean validateState) {
109                 try {
110                         User defaultUser = ElementFactory.getDefaultUser(userRole);
111                         ResourceReqDetails defaultResource = ElementFactory.getDefaultResourceByType(resourceType, defaultUser);
112                         RestResponse resourceResp = ResourceRestUtils.createResource(defaultResource, defaultUser);
113
114                         if (validateState) {
115                                 assertTrue(resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED);
116                         }
117
118                         if (resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
119                                 Resource resourceResponseObject = ResponseParser.convertResourceResponseToJavaObject(resourceResp.getResponse());
120                                 return Either.left(resourceResponseObject);
121                         }
122                         return Either.right(resourceResp);
123                 } catch (Exception e) {
124                         throw new AtomicOperationException(e);
125                 }
126         }
127
128         public static Either<Resource, RestResponse> createResourcesByTypeNormTypeAndCatregory(ResourceTypeEnum resourceType, NormativeTypesEnum normativeTypes, ResourceCategoryEnum resourceCategory, UserRoleEnum userRole, Boolean validateState)
129                         throws Exception {
130                 User defaultUser = ElementFactory.getDefaultUser(userRole);
131                 ResourceReqDetails defaultResource = ElementFactory.getDefaultResourceByTypeNormTypeAndCatregory(resourceType, normativeTypes, resourceCategory, defaultUser);
132                 RestResponse resourceResp = ResourceRestUtils.createResource(defaultResource, defaultUser);
133
134                 if (validateState) {
135                         assertTrue(resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED);
136                 }
137
138                 if (resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
139                         // Resource resourceResponseObject = ResponseParser
140                         // .convertResourceResponseToJavaObject(resourceResp.getResponse());
141                         Resource resourceResponseObject = ResponseParser.parseToObjectUsingMapper(resourceResp.getResponse(), Resource.class);
142                         return Either.left(resourceResponseObject);
143                 }
144                 return Either.right(resourceResp);
145         }
146
147         public static Either<Resource, RestResponse> createResourcesByCustomNormativeTypeAndCatregory(ResourceTypeEnum resourceType, Resource resourceNormativeType, ResourceCategoryEnum resourceCategory, UserRoleEnum userRole, Boolean validateState)
148                         throws Exception {
149                 User defaultUser = ElementFactory.getDefaultUser(userRole);
150                 ResourceReqDetails defaultResource = ElementFactory.getDefaultResourceByTypeNormTypeAndCatregory(resourceType, resourceNormativeType, resourceCategory, defaultUser);
151                 RestResponse resourceResp = ResourceRestUtils.createResource(defaultResource, defaultUser);
152
153                 if (validateState) {
154                         assertTrue("actual result: " + resourceResp.getResponseMessage(), resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED);
155                 }
156
157                 if (resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
158                         // Resource resourceResponseObject = ResponseParser
159                         // .convertResourceResponseToJavaObject(resourceResp.getResponse());
160                         Resource resourceResponseObject = ResponseParser.parseToObjectUsingMapper(resourceResp.getResponse(), Resource.class);
161                         return Either.left(resourceResponseObject);
162                 }
163                 return Either.right(resourceResp);
164         }
165
166         // *********** SERVICE ****************
167
168         public static Either<Service, RestResponse> createDefaultService(UserRoleEnum userRole, Boolean validateState) throws Exception {
169                 User defaultUser = ElementFactory.getDefaultUser(userRole);
170                 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService(defaultUser);
171                 RestResponse createServiceResp = ServiceRestUtils.createService(serviceDetails, defaultUser);
172
173                 if (validateState) {
174                         assertTrue(createServiceResp.getErrorCode() == ServiceRestUtils.STATUS_CODE_CREATED);
175                 }
176
177                 if (createServiceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
178                         Service serviceResponseObject = ResponseParser.convertServiceResponseToJavaObject(createServiceResp.getResponse());
179                         return Either.left(serviceResponseObject);
180                 }
181                 return Either.right(createServiceResp);
182         }
183
184         public static Either<Service, RestResponse> createServiceByCategory(ServiceCategoriesEnum category, UserRoleEnum userRole, Boolean validateState) throws Exception {
185                 User defaultUser = ElementFactory.getDefaultUser(userRole);
186                 ServiceReqDetails serviceDetails = ElementFactory.getDefaultService(category, defaultUser);
187                 RestResponse createServiceResp = ServiceRestUtils.createService(serviceDetails, defaultUser);
188
189                 if (validateState) {
190                         assertTrue(createServiceResp.getErrorCode() == ServiceRestUtils.STATUS_CODE_CREATED);
191                 }
192
193                 if (createServiceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
194                         Service serviceResponseObject = ResponseParser.convertServiceResponseToJavaObject(createServiceResp.getResponse());
195                         return Either.left(serviceResponseObject);
196                 }
197                 return Either.right(createServiceResp);
198         }
199
200         // *********** PRODUCT ****************
201
202         public static Either<Product, RestResponse> createDefaultProduct(UserRoleEnum userRole, Boolean validateState) throws Exception {
203                 User defaultUser = ElementFactory.getDefaultUser(userRole);
204                 ProductReqDetails defaultProduct = ElementFactory.getDefaultProduct();
205                 RestResponse createProductResp = ProductRestUtils.createProduct(defaultProduct, defaultUser);
206
207                 if (validateState) {
208                         assertTrue(createProductResp.getErrorCode() == ProductRestUtils.STATUS_CODE_CREATED);
209                 }
210
211                 if (createProductResp.getErrorCode() == ProductRestUtils.STATUS_CODE_CREATED) {
212                         Product productResponseJavaObject = ResponseParser.convertProductResponseToJavaObject(createProductResp.getResponse());
213                         return Either.left(productResponseJavaObject);
214                 }
215                 return Either.right(createProductResp);
216         }
217
218         // public static ComponentReqDetails
219         // convertCompoentToComponentReqDetails(Component component){
220         //
221         // ComponentReqDetails componentReqDetails =
222         // ElementFactory.getDefaultService();
223         // componentReqDetails.setName(component.getName());
224         // componentReqDetails.setDescription(component.getDescription());
225         // componentReqDetails.setTags(component.getTags());
226         // componentReqDetails.setContactId(component.getContactId());
227         // componentReqDetails.setIcon(component.getIcon());
228         // componentReqDetails.setUniqueId(component.getUniqueId());
229         // componentReqDetails.setCreatorUserId(component.getCreatorUserId());
230         // componentReqDetails.setCreatorFullName(component.getCreatorFullName());
231         // componentReqDetails.setLastUpdaterUserId(component.getLastUpdaterUserId());
232         // componentReqDetails.setLastUpdaterFullName(component.getLastUpdaterFullName());
233         // componentReqDetails.setCreationDate(component.getCreationDate());
234         // componentReqDetails.setLastUpdateDate(component.getLastUpdateDate());
235         // componentReqDetails.setLifecycleState(component.getLifecycleState());
236         // componentReqDetails.setVersion(component.getVersion());
237         // componentReqDetails.setUuid(component.getUUID());
238         // componentReqDetails.setCategories(component.getCategories());
239         // componentReqDetails.setProjectCode(component.getProjectCode());
240         //
241         // return componentReqDetails;
242         // }
243
244         // *********** LIFECYCLE ***************
245
246         public static Pair<Component, RestResponse> changeComponentState(Component component, UserRoleEnum userRole, LifeCycleStatesEnum targetState, Boolean validateState) throws Exception {
247
248                 Boolean isValidationFailed = false;
249                 RestResponse lifeCycleStatesResponse = null;
250                 User defaultUser = ElementFactory.getDefaultUser(userRole);
251
252                 LifeCycleStatesEnum curentCompState = LifeCycleStatesEnum.findByCompState(component.getLifecycleState().toString());
253
254                 if (curentCompState == targetState) {
255                         component = getCompoenntObject(component, userRole);
256                         return Pair.of(component, null);
257                 }
258                 // List<LifeCycleStatesEnum> lifeCycleStatesEnumOrigList = new
259                 // ArrayList<LifeCycleStatesEnum>(EnumSet.allOf(LifeCycleStatesEnum.class));
260
261                 ArrayList<String> lifeCycleStatesEnumList = new ArrayList<String>();
262                 if (curentCompState.equals(LifeCycleStatesEnum.CHECKIN) && targetState.equals(LifeCycleStatesEnum.CHECKOUT)) {
263                         lifeCycleStatesEnumList.add(LifeCycleStatesEnum.CHECKIN.toString());
264                         lifeCycleStatesEnumList.add(LifeCycleStatesEnum.CHECKOUT.toString());
265                 } else {
266                         lifeCycleStatesEnumList.add(LifeCycleStatesEnum.CHECKOUT.toString());
267                         lifeCycleStatesEnumList.add(LifeCycleStatesEnum.CHECKIN.toString());
268                         lifeCycleStatesEnumList.add(LifeCycleStatesEnum.CERTIFICATIONREQUEST.toString());
269                         lifeCycleStatesEnumList.add(LifeCycleStatesEnum.STARTCERTIFICATION.toString());
270                         lifeCycleStatesEnumList.add(LifeCycleStatesEnum.CERTIFY.toString());
271                 }
272                 for (int i = 0; i < lifeCycleStatesEnumList.size(); i++) {
273                         if (lifeCycleStatesEnumList.get(i).equals(curentCompState.name())) {
274                                 int a;
275                                 a = (i == lifeCycleStatesEnumList.size() - 1) ? 0 : i + 1;
276
277                                 for (int n = a; n < lifeCycleStatesEnumList.size(); n++) {
278                                         if (lifeCycleStatesEnumList.get(n).equals(LifeCycleStatesEnum.STARTCERTIFICATION.name()) || lifeCycleStatesEnumList.get(n).equals(LifeCycleStatesEnum.CERTIFY.name())) {
279                                                 defaultUser = ElementFactory.getDefaultUser(UserRoleEnum.TESTER);
280                                         } else
281                                                 defaultUser = ElementFactory.getDefaultUser(userRole);
282
283                                         lifeCycleStatesResponse = LifecycleRestUtils.changeComponentState(component, defaultUser, LifeCycleStatesEnum.findByState(lifeCycleStatesEnumList.get(n)));
284                                         if (lifeCycleStatesResponse.getErrorCode() != LifecycleRestUtils.STATUS_CODE_SUCCESS)
285                                                 isValidationFailed = true;
286                                         if (lifeCycleStatesEnumList.get(n).equals(targetState.toString()) || isValidationFailed == true) {
287                                                 break;
288                                         }
289                                 }
290                         }
291
292                 }
293                 Component componentJavaObject = getCompoenntObject(component, userRole);
294
295                 if (validateState == true && isValidationFailed == true) {
296                         assertTrue("change state failed" + lifeCycleStatesResponse.getResponse(), false);
297
298                         return Pair.of(componentJavaObject, lifeCycleStatesResponse);
299                 }
300
301                 if (isValidationFailed == true) {
302                         return Pair.of(componentJavaObject, lifeCycleStatesResponse);
303                 }
304
305                 return Pair.of(componentJavaObject, lifeCycleStatesResponse);
306         }
307
308         public static RestResponse distributeService(Component component, Boolean validateState) throws Exception {
309
310                 Service service = (Service) component;
311
312                 User opsUser = ElementFactory.getDefaultUser(UserRoleEnum.OPS);
313                 User governotUser = ElementFactory.getDefaultUser(UserRoleEnum.GOVERNOR);
314
315                 ServiceReqDetails serviceDetails = new ServiceReqDetails(service);
316                 RestResponse distributionService = null;
317
318                 RestResponse approveDistribution = LifecycleRestUtils.changeDistributionStatus(serviceDetails, null, governotUser, "approveService", DistributionStatusEnum.DISTRIBUTION_APPROVED);
319                 if (approveDistribution.getErrorCode() == 200) {
320                         distributionService = LifecycleRestUtils.changeDistributionStatus(serviceDetails, null, opsUser, "approveService", DistributionStatusEnum.DISTRIBUTED);
321                 }
322
323                 if (validateState) {
324                         assertTrue(approveDistribution.getErrorCode() == ProductRestUtils.STATUS_CODE_SUCCESS);
325                         assertTrue(distributionService.getErrorCode() == ProductRestUtils.STATUS_CODE_SUCCESS);
326                         return distributionService;
327                 }
328
329                 return distributionService;
330
331         }
332         
333         public static RestResponse approveAndRejectServiceForDistribution(Component component) throws Exception {
334
335                 Service service = (Service) component;
336
337                 User opsUser = ElementFactory.getDefaultUser(UserRoleEnum.OPS);
338                 opsUser.setRole("OPS");
339                 User governotUser = ElementFactory.getDefaultUser(UserRoleEnum.GOVERNOR);
340
341                 ServiceReqDetails serviceDetails = new ServiceReqDetails(service);
342                 RestResponse distributionService = null;
343
344                 RestResponse approveDistribution = LifecycleRestUtils.changeDistributionStatus(serviceDetails, null, governotUser, "approveService", DistributionStatusEnum.DISTRIBUTION_APPROVED);
345                 if (approveDistribution.getErrorCode() == 200) {
346                         distributionService = LifecycleRestUtils.changeDistributionStatus(serviceDetails, null, opsUser, "rejectService", DistributionStatusEnum.DISTRIBUTION_REJECTED);
347                 }
348                 
349                 assertEquals(approveDistribution.getErrorCode(), new Integer(ProductRestUtils.STATUS_CODE_SUCCESS));
350                 assertEquals(distributionService.getErrorCode(), new Integer(ProductRestUtils.STATUS_CODE_SUCCESS));
351
352                 return distributionService;
353
354         }
355
356         // *********** ARTIFACTS *****************
357
358         public static Either<ArtifactDefinition, RestResponse> uploadArtifactByType(ArtifactTypeEnum artifactType, Component component, UserRoleEnum userRole, Boolean deploymentTrue, Boolean validateState) throws Exception {
359
360                 User defaultUser = ElementFactory.getDefaultUser(userRole);
361                 ArtifactReqDetails artifactDetails = ElementFactory.getArtifactByType(null, artifactType, deploymentTrue);
362                 if (deploymentTrue == false)
363                         artifactDetails.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL.getType());
364                 RestResponse uploadArtifactResp = ArtifactRestUtils.uploadArtifact(artifactDetails, component, defaultUser);
365
366                 if (validateState) {
367                         assertTrue("artifact upload failed: " + artifactDetails.getArtifactName(), uploadArtifactResp.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
368                 }
369
370                 if (uploadArtifactResp.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS) {
371                         ArtifactDefinition artifactJavaObject = ResponseParser.convertArtifactDefinitionResponseToJavaObject(uploadArtifactResp.getResponse());
372                         return Either.left(artifactJavaObject);
373                 }
374                 return Either.right(uploadArtifactResp);
375         }
376
377         // *********** CONTAINERS *****************
378         /**
379          * Adds Component instance to Component
380          * 
381          * @param compInstParent
382          * @param compContainer
383          * @return
384          */
385         public static Either<ComponentInstance, RestResponse> addComponentInstanceToComponentContainer(Component compInstParent, Component compContainer) {
386                 return addComponentInstanceToComponentContainer(compInstParent, compContainer, UserRoleEnum.DESIGNER, false);
387         }
388
389         public static Either<ComponentInstance, RestResponse> addComponentInstanceToComponentContainer(Component compInstParent, Component compContainer, UserRoleEnum userRole, Boolean validateState) {
390                 try {
391                         User defaultUser = ElementFactory.getDefaultUser(userRole);
392                         ComponentInstanceReqDetails componentInstanceDetails = ElementFactory.getComponentInstance(compInstParent);
393                         RestResponse createComponentInstance = ComponentInstanceRestUtils.createComponentInstance(componentInstanceDetails, defaultUser, compContainer);
394
395                         if (validateState) {
396                                 assertTrue(createComponentInstance.getErrorCode() == ServiceRestUtils.STATUS_CODE_CREATED);
397                         }
398
399                         if (createComponentInstance.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
400                                 ComponentInstance componentInstance = ResponseParser.convertComponentInstanceResponseToJavaObject(createComponentInstance.getResponse());
401                                 return Either.left(componentInstance);
402                         }
403                         return Either.right(createComponentInstance);
404                 } catch (Exception e) {
405                         throw new AtomicOperationException(e);
406                 }
407         }
408
409         public static Resource getResourceObject(Component containerDetails, UserRoleEnum userRole) throws Exception {
410                 // User defaultUser = ElementFactory.getDefaultUser(userRole);
411                 RestResponse restResponse = ResourceRestUtils.getResource(containerDetails.getUniqueId());
412                 Resource container = ResponseParser.convertResourceResponseToJavaObject(restResponse.getResponse());
413                 return container;
414         }
415         
416         public static Resource getResourceObjectByNameAndVersion(UserRoleEnum sdncModifierDetails, String resourceName, String resourceVersion) throws Exception {
417                 User defaultUser = ElementFactory.getDefaultUser(sdncModifierDetails);
418                 RestResponse resourceResponse = ResourceRestUtils.getResourceByNameAndVersion(defaultUser.getUserId(), resourceName, resourceVersion);
419                 Resource container = ResponseParser.convertResourceResponseToJavaObject(resourceResponse.getResponse());
420                 return container;
421         }
422
423         public static Service getServiceObject(Component containerDetails, UserRoleEnum userRole) throws Exception {
424                 User defaultUser = ElementFactory.getDefaultUser(userRole);
425                 RestResponse serviceResponse = ServiceRestUtils.getService(containerDetails.getUniqueId(), defaultUser);
426                 Service container = ResponseParser.convertServiceResponseToJavaObject(serviceResponse.getResponse());
427                 return container;
428         }
429         
430         public static Service getServiceObjectByNameAndVersion(UserRoleEnum sdncModifierDetails, String serviceName, String serviceVersion) throws Exception {
431                 User defaultUser = ElementFactory.getDefaultUser(sdncModifierDetails);
432                 RestResponse serviceResponse = ServiceRestUtils.getServiceByNameAndVersion(defaultUser, serviceName, serviceVersion);
433                 Service container = ResponseParser.convertServiceResponseToJavaObject(serviceResponse.getResponse());
434                 return container;
435         }
436
437         public static Product getProductObject(Component containerDetails, UserRoleEnum userRole) throws Exception {
438                 User defaultUser = ElementFactory.getDefaultUser(userRole);
439                 RestResponse productRest = ProductRestUtils.getProduct(containerDetails.getUniqueId(), defaultUser.getUserId());
440                 Product container = ResponseParser.convertProductResponseToJavaObject(productRest.getResponse());
441                 return container;
442         }
443
444         public static Component getCompoenntObject(Component containerDetails, UserRoleEnum userRole) throws Exception {
445                 User defaultUser = ElementFactory.getDefaultUser(userRole);
446
447                 switch (containerDetails.getComponentType()) {
448                 case RESOURCE:
449                         RestResponse restResponse = ResourceRestUtils.getResource(containerDetails.getUniqueId());
450                         containerDetails = ResponseParser.convertResourceResponseToJavaObject(restResponse.getResponse());
451                         break;
452                 case SERVICE:
453                         RestResponse serviceResponse = ServiceRestUtils.getService(containerDetails.getUniqueId(), defaultUser);
454                         containerDetails = ResponseParser.convertServiceResponseToJavaObject(serviceResponse.getResponse());
455                         break;
456                 case PRODUCT:
457                         RestResponse productRest = ProductRestUtils.getProduct(containerDetails.getUniqueId(), defaultUser.getUserId());
458                         containerDetails = ResponseParser.convertProductResponseToJavaObject(productRest.getResponse());
459                         break;
460                 default:
461                         break;
462                 }
463                 return containerDetails;
464         }
465
466         public static Component convertReposnseToComponentObject(Component containerDetails, RestResponse restresponse) throws Exception {
467
468                 switch (containerDetails.getComponentType()) {
469                 case RESOURCE:
470                         containerDetails = ResponseParser.convertResourceResponseToJavaObject(restresponse.getResponse());
471                         break;
472                 case SERVICE:
473                         containerDetails = ResponseParser.convertServiceResponseToJavaObject(restresponse.getResponse());
474                         break;
475                 case PRODUCT:
476                         containerDetails = ResponseParser.convertProductResponseToJavaObject(restresponse.getResponse());
477                         break;
478                 default:
479                         break;
480                 }
481                 return containerDetails;
482         }
483
484         public static RestResponse associate2ResourceInstances(Component containerDetails, ComponentInstance fromNode, ComponentInstance toNode, String assocType, UserRoleEnum userRole, Boolean validateState) throws IOException {
485                 User defaultUser = ElementFactory.getDefaultUser(userRole);
486                 RestResponse associate2ResourceInstancesResponse = ResourceRestUtils.associate2ResourceInstances(containerDetails, fromNode, toNode, assocType, defaultUser);
487
488                 if (validateState) {
489                         assertTrue(associate2ResourceInstancesResponse.getErrorCode() == ServiceRestUtils.STATUS_CODE_SUCCESS);
490                 }
491
492                 return associate2ResourceInstancesResponse;
493         }
494
495         public static Either<Pair<Component, ComponentInstance>, RestResponse> changeComponentInstanceVersion(Component containerDetails, ComponentInstance componentInstanceToReplace, Component newInstance, UserRoleEnum userRole, Boolean validateState)
496                         throws Exception {
497                 User defaultUser = ElementFactory.getDefaultUser(userRole);
498
499                 RestResponse changeComponentInstanceVersionResp = ComponentInstanceRestUtils.changeComponentInstanceVersion(containerDetails, componentInstanceToReplace, newInstance, defaultUser);
500                 if (validateState) {
501                         assertTrue("change ComponentInstance version failed: " + changeComponentInstanceVersionResp.getResponseMessage(), changeComponentInstanceVersionResp.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
502                 }
503
504                 if (changeComponentInstanceVersionResp.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS) {
505
506                         Component compoenntObject = AtomicOperationUtils.getCompoenntObject(containerDetails, userRole);
507                         ComponentInstance componentInstanceJavaObject = ResponseParser.convertComponentInstanceResponseToJavaObject(changeComponentInstanceVersionResp.getResponse());
508
509                         return Either.left(Pair.of(compoenntObject, componentInstanceJavaObject));
510                 }
511
512                 return Either.right(changeComponentInstanceVersionResp);
513         }
514
515         // *********** PROPERTIES *****************
516
517         public static Either<ComponentInstanceProperty, RestResponse> addCustomPropertyToResource(PropertyReqDetails propDetails, Resource resourceDetails, UserRoleEnum userRole, Boolean validateState) throws Exception {
518
519                 User defaultUser = ElementFactory.getDefaultUser(userRole);
520                 Map<String, PropertyReqDetails> propertyToSend = new HashMap<String, PropertyReqDetails>();
521                 propertyToSend.put(propDetails.getName(), propDetails);
522                 Gson gson = new Gson();
523                 RestResponse addPropertyResponse = PropertyRestUtils.createProperty(resourceDetails.getUniqueId(), gson.toJson(propertyToSend), defaultUser);
524
525                 if (validateState) {
526                         assertTrue("add property to resource failed: " + addPropertyResponse.getErrorCode(), addPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_CREATED);
527                 }
528
529                 if (addPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_CREATED) {
530                         ComponentInstanceProperty compInstProp = null;
531                         String property = ResponseParser.getJsonObjectValueByKey(addPropertyResponse.getResponse(), propDetails.getName());
532                         compInstProp = (ResponseParser.convertPropertyResponseToJavaObject(property));
533                         return Either.left(compInstProp);
534                 }
535                 return Either.right(addPropertyResponse);
536         }
537
538         // Benny
539         public static Either<ComponentInstanceProperty, RestResponse> updatePropertyOfResource(PropertyReqDetails propDetails, Resource resourceDetails, String propertyUniqueId, UserRoleEnum userRole, Boolean validateState) throws Exception {
540
541                 User defaultUser = ElementFactory.getDefaultUser(userRole);
542                 Map<String, PropertyReqDetails> propertyToSend = new HashMap<String, PropertyReqDetails>();
543                 propertyToSend.put(propDetails.getName(), propDetails);
544                 Gson gson = new Gson();
545                 RestResponse addPropertyResponse = PropertyRestUtils.updateProperty(resourceDetails.getUniqueId(), propertyUniqueId, gson.toJson(propertyToSend), defaultUser);
546
547                 if (validateState) {
548                         assertTrue("add property to resource failed: " + addPropertyResponse.getResponseMessage(), addPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
549                 }
550
551                 if (addPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS) {
552                         ComponentInstanceProperty compInstProp = null;
553                         String property = ResponseParser.getJsonObjectValueByKey(addPropertyResponse.getResponse(), propDetails.getName());
554                         compInstProp = (ResponseParser.convertPropertyResponseToJavaObject(property));
555                         return Either.left(compInstProp);
556                 }
557                 return Either.right(addPropertyResponse);
558         }
559
560         public static RestResponse deletePropertyOfResource(String resourceId, String propertyId, UserRoleEnum userRole) throws Exception {
561                 User defaultUser = ElementFactory.getDefaultUser(userRole);
562                 return PropertyRestUtils.deleteProperty(resourceId, propertyId, defaultUser);
563         }
564
565         public static Either<ComponentInstanceProperty, RestResponse> addDefaultPropertyToResource(PropertyTypeEnum propertyType, Resource resourceDetails, UserRoleEnum userRole, Boolean validateState) throws Exception {
566
567                 User defaultUser = ElementFactory.getDefaultUser(userRole);
568                 PropertyReqDetails propDetails = ElementFactory.getPropertyDetails(propertyType);
569                 Map<String, PropertyReqDetails> propertyToSend = new HashMap<String, PropertyReqDetails>();
570                 propertyToSend.put(propDetails.getName(), propDetails);
571                 Gson gson = new Gson();
572                 RestResponse addPropertyResponse = PropertyRestUtils.createProperty(resourceDetails.getUniqueId(), gson.toJson(propertyToSend), defaultUser);
573
574                 if (validateState) {
575                         assertTrue("add property to resource failed: " + addPropertyResponse.getResponseMessage(), addPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_CREATED);
576                 }
577
578                 if (addPropertyResponse.getErrorCode() == BaseRestUtils.STATUS_CODE_CREATED) {
579                         ComponentInstanceProperty compInstProp = null;
580                         String property = ResponseParser.getJsonObjectValueByKey(addPropertyResponse.getResponse(), propDetails.getName());
581                         compInstProp = (ResponseParser.convertPropertyResponseToJavaObject(property));
582
583                         return Either.left(compInstProp);
584                 }
585                 return Either.right(addPropertyResponse);
586         }
587
588         public static RestResponse createDefaultConsumer(Boolean validateState) {
589                 try {
590                         ConsumerDataDefinition defaultConsumerDefinition = ElementFactory.getDefaultConsumerDetails();
591                         RestResponse createResponse = ConsumerRestUtils.createConsumer(defaultConsumerDefinition, ElementFactory.getDefaultUser(UserRoleEnum.ADMIN));
592                         BaseRestUtils.checkCreateResponse(createResponse);
593
594                         if (validateState) {
595                                 assertTrue(createResponse.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED);
596                         }
597                         return createResponse;
598                 } catch (Exception e) {
599                         throw new AtomicOperationException(e);
600                 }
601         }
602
603         /**
604          * Builds Resource From rest response
605          * 
606          * @param resourceResp
607          * @return
608          */
609         public static Either<Resource, RestResponse> buildResourceFromResponse(RestResponse resourceResp) {
610                 Either<Resource, RestResponse> result;
611                 if (resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
612                         Resource resourceResponseObject = ResponseParser.convertResourceResponseToJavaObject(resourceResp.getResponse());
613                         result = Either.left(resourceResponseObject);
614                 } else {
615                         result = Either.right(resourceResp);
616                 }
617                 return result;
618         }
619
620         private static class AtomicOperationException extends RuntimeException {
621                 private AtomicOperationException(Exception e) {
622                         super(e);
623                 }
624
625                 private static final long serialVersionUID = 1L;
626         };
627         
628         /**
629          * Import resource from CSAR
630          * 
631          * @param resourceType
632          * @param userRole
633          * @param fileName
634          * @param filePath
635          * @return Resource
636          * @throws Exception
637          */
638         public static Resource importResourceFromCSAR(ResourceTypeEnum resourceType, UserRoleEnum userRole, String fileName, String... filePath) throws Exception {
639                 // Get the CSARs path
640                 String realFilePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "CI" + File.separator + "csars" ;
641                 if (filePath != null && filePath.length > 0) {
642                         realFilePath = filePath.toString();
643                 }
644                 
645                 // Create default import resource & user
646                 ImportReqDetails resourceDetails = ElementFactory.getDefaultImportResource();
647                 User sdncModifierDetails = ElementFactory.getDefaultUser(userRole);
648                 
649                 byte[] data = null;
650                 Path path = Paths.get(realFilePath + File.separator + fileName);
651                 data = Files.readAllBytes(path);
652                 String payloadName = fileName;
653                 String payloadData = Base64.encodeBase64String(data);
654                 resourceDetails.setPayloadData(payloadData);
655                 resourceDetails.setCsarUUID(payloadName);
656                 resourceDetails.setPayloadName(payloadName);
657                 resourceDetails.setResourceType(resourceType.name());
658                 
659                 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
660                 BaseRestUtils.checkCreateResponse(createResource);
661                 return ResponseParser.parseToObjectUsingMapper(createResource.getResponse(), Resource.class);           
662         };
663         
664         public static Either<Resource, RestResponse> importResourceByFileName(ResourceTypeEnum resourceType, UserRoleEnum userRole, String fileName, Boolean validateState, String... filePath) throws IOException {
665
666                 String realFilePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "CI" + File.separator + "csars" ;
667                 if (filePath != null && filePath.length > 0) {
668                         realFilePath = filePath.toString();
669                 }
670
671                 try {
672                         User defaultUser = ElementFactory.getDefaultUser(userRole);
673                         ResourceReqDetails defaultResource = ElementFactory.getDefaultResource(defaultUser);                    
674                         ImportReqDetails defaultImportResource = ElementFactory.getDefaultImportResource(defaultResource);
675                         ImportUtils.getImportResourceDetailsByPathAndName(defaultImportResource, realFilePath, fileName);
676                         RestResponse resourceResp = ResourceRestUtils.createResource(defaultImportResource, defaultUser);
677
678                         if (validateState) {
679                                 assertTrue(resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED);
680                         }
681
682                         if (resourceResp.getErrorCode() == ResourceRestUtils.STATUS_CODE_CREATED) {
683                                 Resource resourceResponseObject = ResponseParser.convertResourceResponseToJavaObject(resourceResp.getResponse());
684                                 return Either.left(resourceResponseObject);
685                         }
686                         return Either.right(resourceResp);
687                 } catch (Exception e) {
688                         throw new AtomicOperationException(e);
689                 }
690         }
691
692 }