[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci-dev / src / main / java / org / openecomp / sdc / uici / tests / utilities / RestCDUtils.java
1 package org.openecomp.sdc.uici.tests.utilities;
2
3 import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.retryMethodOnResult;
4 import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.swallowException;
5 import static org.testng.AssertJUnit.assertTrue;
6
7 import java.util.HashMap;
8 import java.util.Iterator;
9 import java.util.Map;
10 import java.util.function.Function;
11 import java.util.function.Supplier;
12
13 import org.apache.http.HttpStatus;
14 import org.codehaus.jackson.map.ObjectMapper;
15 import org.codehaus.jettison.json.JSONArray;
16 import org.codehaus.jettison.json.JSONObject;
17
18 import org.openecomp.sdc.be.model.User;
19 import org.openecomp.sdc.ci.tests.datatypes.ComponentReqDetails;
20 import org.openecomp.sdc.ci.tests.datatypes.ProductReqDetails;
21 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
22 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
23 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
24 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
25 import org.openecomp.sdc.ci.tests.utils.rest.ProductRestUtils;
26 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
27 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
28 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
29
30 public class RestCDUtils {
31
32         private static void setResourceUniqueIdAndUUID(ComponentReqDetails element, RestResponse getResourceResponse) {
33                 element.setUniqueId(ResponseParser.getUniqueIdFromResponse(getResourceResponse));
34                 element.setUUID(ResponseParser.getUuidFromResponse(getResourceResponse));
35         }
36
37         public static RestResponse getResourceByNameAndVersionRetryOnFail(String userId, String resourceName,
38                         String resourceVersion) {
39                 Supplier<RestResponse> resourceGetter = () -> swallowException(
40                                 () -> ResourceRestUtils.getResourceByNameAndVersion(userId, resourceName, resourceVersion));
41                 Function<RestResponse, Boolean> validator = restRes -> restRes.getErrorCode() == HttpStatus.SC_OK;
42                 return retryMethodOnResult(resourceGetter, validator);
43         }
44
45         public static RestResponse getResource(ResourceReqDetails resource) {
46                 try {
47                         System.out.println("trying to get resource");
48                         RestResponse getResourceResponse = null;
49                         String reourceUniqueId = resource.getUniqueId();
50
51                         if (reourceUniqueId != null) {
52                                 GeneralUIUtils.sleep(1000);
53                                 getResourceResponse = ResourceRestUtils.getResource(reourceUniqueId);
54                                 if (getResourceResponse.getErrorCode().intValue() == HttpStatus.SC_OK) {
55                                         System.out.println("succeeded to get resource");
56                                 }
57                                 return getResourceResponse;
58                         }
59                         JSONObject getResourceJSONObject = null;
60                         getResourceResponse = getResourceByNameAndVersionRetryOnFail(UserRoleEnum.ADMIN.getUserId(),
61                                         resource.getName(), resource.getVersion());
62                         if (getResourceResponse.getErrorCode().intValue() == HttpStatus.SC_OK) {
63                                 JSONArray jArray = new JSONArray(getResourceResponse.getResponse());
64                                 for (int i = 0; i < jArray.length(); i++) {
65                                         getResourceJSONObject = jArray.getJSONObject(i);
66                                         String resourceType = ResponseParser.getValueFromJsonResponse(getResourceJSONObject.toString(),
67                                                         "resourceType");
68                                         if (resourceType.equals(resource.getResourceType())) {
69                                                 getResourceResponse.setResponse(getResourceJSONObject.toString());
70                                                 setResourceUniqueIdAndUUID(resource, getResourceResponse);
71                                                 System.out.println("succeeded to get resource");
72                                                 return getResourceResponse;
73                                         }
74                                 }
75                         }
76
77                         return getResourceResponse;
78                 } catch (Exception e) {
79                         throw new RuntimeException(e);
80                 }
81         }
82
83         public static RestResponse getService(ServiceReqDetails service, User user) {
84                 Supplier<RestResponse> serviceFetcher = () -> swallowException(
85                                 () -> ServiceRestUtils.getServiceByNameAndVersion(user, service.getName(), service.getVersion()));
86                 Function<RestResponse, Boolean> verifier = restResponse -> restResponse.getErrorCode()
87                                 .intValue() == HttpStatus.SC_OK;
88                 RestResponse getServiceResponse = retryMethodOnResult(serviceFetcher, verifier);
89
90                 if (getServiceResponse.getErrorCode().intValue() == HttpStatus.SC_OK) {
91                         setResourceUniqueIdAndUUID(service, getServiceResponse);
92                 }
93                 return getServiceResponse;
94         }
95
96         public static RestResponse getProduct(ProductReqDetails product, User user) throws Exception {
97                 Thread.sleep(3500);
98                 RestResponse getProductResponse = ProductRestUtils.getProductByNameAndVersion(product.getName(),
99                                 product.getVersion(), user.getUserId());
100                 if (getProductResponse.getErrorCode().intValue() == 200) {
101                         setResourceUniqueIdAndUUID(product, getProductResponse);
102                 }
103                 return getProductResponse;
104         }
105
106         public static Map<String, String> getAllElementVersionsFromResponse(RestResponse getResource) throws Exception {
107                 Map<String, String> versionsMap = new HashMap<String, String>();
108                 try {
109                         ObjectMapper mapper = new ObjectMapper();
110
111                         JSONObject object = new JSONObject(getResource.getResponse());
112                         versionsMap = mapper.readValue(object.get("allVersions").toString(), Map.class);
113
114                 } catch (Exception e) {
115                         e.printStackTrace();
116                         return versionsMap;
117
118                 }
119
120                 return versionsMap;
121         }
122
123         public static void deleteElementVersions(Map<String, String> elementVersions, boolean isBeforeTest, Object clazz,
124                         User user) throws Exception {
125                 Iterator<String> iterator = elementVersions.keySet().iterator();
126                 while (iterator.hasNext()) {
127                         String singleVersion = iterator.next();
128                         String uniqueId = elementVersions.get(singleVersion);
129                         RestResponse deleteResponse = null;
130                         if (clazz instanceof ServiceReqDetails) {
131                                 deleteResponse = ServiceRestUtils.deleteServiceById(uniqueId, user.getUserId());
132                         } else if (clazz instanceof ResourceReqDetails) {
133                                 deleteResponse = ResourceRestUtils.deleteResource(uniqueId, user.getUserId());
134                         } else if (clazz instanceof ProductReqDetails) {
135                                 deleteResponse = ProductRestUtils.deleteProduct(uniqueId, user.getUserId());
136                         }
137
138                         if (isBeforeTest) {
139                                 assertTrue(deleteResponse.getErrorCode().intValue() == 204
140                                                 || deleteResponse.getErrorCode().intValue() == 404);
141                         } else {
142                                 assertTrue(deleteResponse.getErrorCode().intValue() == 204);
143                         }
144                 }
145         }
146
147         public static void deleteAllResourceVersionsAfterTest(ComponentReqDetails componentDetails,
148                         RestResponse getObjectResponse, User user) throws Exception {
149                 deleteAllComponentVersion(false, componentDetails, getObjectResponse, user);
150         }
151
152         public static void deleteAllResourceVersionsBeforeTest(ComponentReqDetails componentDetails,
153                         RestResponse getObjectResponse, User user) throws Exception {
154                 deleteAllComponentVersion(true, componentDetails, getObjectResponse, user);
155         }
156
157         public static void deleteAllComponentVersion(boolean isBeforeTest, ComponentReqDetails componentDetails,
158                         RestResponse getObjectResponse, User user) throws Exception {
159                 if (getObjectResponse.getErrorCode().intValue() == 404)
160                         return;
161                 Map<String, String> componentVersionsMap = getAllElementVersionsFromResponse(getObjectResponse);
162                 System.out.println("deleting...");
163                 deleteElementVersions(componentVersionsMap, isBeforeTest, componentDetails, user);
164                 componentDetails.setUniqueId(null);
165         }
166
167 }