2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utilities;
23 import com.aventstack.extentreports.Status;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.codehaus.jettison.json.JSONObject;
26 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.User;
29 import org.openecomp.sdc.be.model.category.CategoryDefinition;
30 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
31 import org.openecomp.sdc.ci.tests.config.Config;
32 import org.openecomp.sdc.ci.tests.datatypes.ComponentReqDetails;
33 import org.openecomp.sdc.ci.tests.datatypes.ProductReqDetails;
34 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
35 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
36 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
37 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
38 import org.openecomp.sdc.ci.tests.execute.setup.DriverFactory;
39 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
40 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
41 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
42 import org.openecomp.sdc.ci.tests.utils.rest.CategoryRestUtils;
43 import org.openecomp.sdc.ci.tests.utils.rest.ProductRestUtils;
44 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
45 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
46 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
47 import org.openecomp.sdc.ci.tests.utils.rest.UserRestUtils;
49 import java.io.IOException;
50 import java.net.InetAddress;
51 import java.net.UnknownHostException;
52 import java.util.HashMap;
53 import java.util.Iterator;
54 import java.util.List;
56 import java.util.stream.Collectors;
58 import static org.testng.AssertJUnit.assertTrue;
60 public class RestCDUtils {
62 private static void setResourceUniqueIdAndUUID(ComponentReqDetails element, RestResponse getResourceResponse) {
63 element.setUniqueId(ResponseParser.getUniqueIdFromResponse(getResourceResponse));
64 element.setUUID(ResponseParser.getUuidFromResponse(getResourceResponse));
67 public static RestResponse getResource(ResourceReqDetails resource, User user) {
68 final String getResourceMsg = "Trying to get resource named " + resource.getName() + " with version " + resource.getVersion();
69 final String succeedGetResourceMsg = "Succeeded to get resource named " + resource.getName() + " with version " + resource.getVersion();
70 final String failedGetResourceMsg = "Failed to get resource named " + resource.getName() + " with version " + resource.getVersion();
72 ExtentTestActions.log(Status.INFO, getResourceMsg);
73 System.out.println(getResourceMsg);
74 GeneralUIUtils.sleep(1000);
75 RestResponse getResourceResponse = null;
76 String reourceUniqueId = resource.getUniqueId();
77 if (reourceUniqueId != null) {
78 getResourceResponse = ResourceRestUtils.getResource(reourceUniqueId);
79 if (getResourceResponse.getErrorCode().intValue() == 200) {
80 ExtentTestActions.log(Status.INFO, succeedGetResourceMsg);
81 System.out.println(succeedGetResourceMsg);
83 return getResourceResponse;
85 JSONObject getResourceJSONObject = null;
86 getResourceResponse = ResourceRestUtils.getResourceByNameAndVersion(user.getUserId(), resource.getName(), resource.getVersion());
87 if (getResourceResponse.getErrorCode().intValue() == 200) {
88 // JSONArray jArray = new JSONArray(getResourceResponse.getResponse());
89 // for (int i = 0; i < jArray.length(); i++) {
90 // getResourceJSONObject = jArray.getJSONObject(i);
91 // String resourceType = ResponseParser.getValueFromJsonResponse(getResourceJSONObject.toString(), "resourceType");
92 // if (resourceType.equals(resource.getResourceType())) {
93 // getResourceResponse.setResponse(getResourceJSONObject.toString());
94 setResourceUniqueIdAndUUID(resource, getResourceResponse);
95 ExtentTestActions.log(Status.INFO, succeedGetResourceMsg);
96 System.out.println(succeedGetResourceMsg);
97 return getResourceResponse;
101 ExtentTestActions.log(Status.INFO, failedGetResourceMsg);
102 return getResourceResponse;
103 } catch (Exception e) {
104 throw new RuntimeException(e);
108 public static RestResponse getService(ServiceReqDetails service, User user) {
111 RestResponse getServiceResponse = ServiceRestUtils.getServiceByNameAndVersion(user, service.getName(),
112 service.getVersion());
113 if (getServiceResponse.getErrorCode().intValue() == 200) {
114 setResourceUniqueIdAndUUID(service, getServiceResponse);
116 return getServiceResponse;
117 } catch (Exception e) {
118 throw new RuntimeException(e);
123 public static RestResponse getProduct(ProductReqDetails product, User user) {
126 RestResponse getProductResponse = ProductRestUtils.getProductByNameAndVersion(product.getName(),
127 product.getVersion(), user.getUserId());
128 if (getProductResponse.getErrorCode().intValue() == 200) {
129 setResourceUniqueIdAndUUID(product, getProductResponse);
131 return getProductResponse;
132 } catch (Exception e) {
133 throw new RuntimeException(e);
137 public static Map<String, String> getAllElementVersionsFromResponse(RestResponse getResource) throws Exception {
138 Map<String, String> versionsMap = new HashMap<String, String>();
140 ObjectMapper mapper = new ObjectMapper();
142 JSONObject object = new JSONObject(getResource.getResponse());
143 versionsMap = mapper.readValue(object.get("allVersions").toString(), Map.class);
145 } catch (Exception e) {
154 public static void deleteElementVersions(Map<String, String> elementVersions, boolean isBeforeTest, Object clazz,
155 User user) throws Exception {
156 Iterator<String> iterator = elementVersions.keySet().iterator();
157 while (iterator.hasNext()) {
158 String singleVersion = iterator.next();
159 String uniqueId = elementVersions.get(singleVersion);
160 RestResponse deleteResponse = null;
161 if (clazz instanceof ServiceReqDetails) {
162 deleteResponse = ServiceRestUtils.deleteServiceById(uniqueId, user.getUserId());
163 } else if (clazz instanceof ResourceReqDetails) {
164 deleteResponse = ResourceRestUtils.deleteResource(uniqueId, user.getUserId());
165 } else if (clazz instanceof ProductReqDetails) {
166 deleteResponse = ProductRestUtils.deleteProduct(uniqueId, user.getUserId());
170 assertTrue(deleteResponse.getErrorCode().intValue() == 204
171 || deleteResponse.getErrorCode().intValue() == 404);
173 assertTrue(deleteResponse.getErrorCode().intValue() == 204);
178 public static void deleteAllResourceVersionsAfterTest(ComponentReqDetails componentDetails,
179 RestResponse getObjectResponse, User user) {
181 deleteAllComponentVersion(false, componentDetails, getObjectResponse, user);
182 } catch (Exception e) {
187 public static void deleteAllResourceVersionsBeforeTest(ComponentReqDetails componentDetails,
188 RestResponse getObjectResponse, User user) throws Exception {
189 deleteAllComponentVersion(true, componentDetails, getObjectResponse, user);
192 public static void deleteAllComponentVersion(boolean isBeforeTest, ComponentReqDetails componentDetails,
193 RestResponse getObjectResponse, User user) throws Exception {
194 if (getObjectResponse.getErrorCode().intValue() == 404)
196 Map<String, String> componentVersionsMap = getAllElementVersionsFromResponse(getObjectResponse);
197 System.out.println("deleting...");
198 deleteElementVersions(componentVersionsMap, isBeforeTest, componentDetails, user);
199 componentDetails.setUniqueId(null);
204 public static String getExecutionHostAddress() {
206 String computerName = null;
208 computerName = InetAddress.getLocalHost().getHostAddress().replaceAll("\\.", "·");
209 System.out.println(computerName);
210 if (computerName.indexOf(".") > -1)
211 computerName = computerName.substring(0,
212 computerName.indexOf(".")).toUpperCase();
213 } catch (UnknownHostException e) {
214 System.out.println("Uknown hostAddress");
216 return computerName != null ? computerName : "Uknown hostAddress";
219 public static Map<String, List<Component>> getCatalogAsMap() throws IOException {
220 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
221 RestResponse catalog = CatalogRestUtils.getCatalog(defaultAdminUser.getUserId());
222 Map<String, List<Component>> convertCatalogResponseToJavaObject = ResponseParser
223 .convertCatalogResponseToJavaObject(catalog.getResponse());
224 return convertCatalogResponseToJavaObject;
227 public static Map<String, List<CategoryDefinition>> getCategories() throws Exception {
229 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
231 Map<String,List<CategoryDefinition>> map = new HashMap<String,List<CategoryDefinition>>();
234 RestResponse allResourceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.RESOURCE_PARAM_NAME);
235 RestResponse allServiceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.SERVICE_PARAM_NAME);
237 List<CategoryDefinition> parsedResourceCategories = ResponseParser.parseCategories(allResourceCategories);
238 List<CategoryDefinition> parsedServiceCategories = ResponseParser.parseCategories(allServiceCategories);
240 map.put(ComponentTypeEnum.RESOURCE_PARAM_NAME, parsedResourceCategories);
241 map.put(ComponentTypeEnum.SERVICE_PARAM_NAME, parsedServiceCategories);
246 public static void deleteCreatedComponents(Map<String, List<Component>> map) throws IOException {
248 System.out.println("going to delete all created components...");
250 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
251 final String userId = defaultAdminUser.getUserId();
253 List<Component> resourcesArrayList = map.get("products");
254 List<String> collect = resourcesArrayList.stream().filter(s -> s.getName().startsWith(ElementFactory.getProductPrefix())).
255 map(e -> e.getUniqueId()).
256 collect(Collectors.toList());
257 for (String uId : collect) {
258 ProductRestUtils.deleteProduct(uId, userId);
261 resourcesArrayList = map.get("services");
262 collect = resourcesArrayList.stream().filter(s -> s.getName().startsWith(ElementFactory.getServicePrefix())).
263 map(e -> e.getUniqueId()).
264 collect(Collectors.toList());
265 for (String uId : collect) {
266 ServiceRestUtils.markServiceToDelete(uId, userId);
268 ServiceRestUtils.deleteMarkedServices(userId);
270 resourcesArrayList = map.get("resources");
271 collect = resourcesArrayList.stream().filter(s -> s.getName().startsWith(ElementFactory.getResourcePrefix())).
272 map(e -> e.getUniqueId()).
273 collect(Collectors.toList());
274 for (String uId : collect) {
275 ResourceRestUtils.markResourceToDelete(uId, userId);
277 ResourceRestUtils.deleteMarkedResources(userId);
285 public static void deleteCategoriesByList(List<CategoryDefinition> listCategories, String componentType, User user) throws Exception {
287 for (CategoryDefinition categoryDefinition : listCategories) {
288 if (categoryDefinition.getName().toLowerCase().startsWith("ci")) {
289 List<SubCategoryDefinition> subcategories = categoryDefinition.getSubcategories();
290 if (subcategories != null) {
291 for (SubCategoryDefinition subCategoryDefinition : subcategories) {
293 CategoryRestUtils.deleteSubCategory(subCategoryDefinition.getUniqueId(),
294 categoryDefinition.getUniqueId(), user.getUserId(),
299 CategoryRestUtils.deleteCategory(categoryDefinition.getUniqueId(), user.getUserId(),
306 public static String getUserRole(User reqUser, User user){
308 RestResponse getUserRoleResp = UserRestUtils.getUserRole(reqUser, user);
309 JSONObject jObject = new JSONObject(getUserRoleResp.getResponse());
310 return jObject.getString("role");
317 public static RestResponse getUser(User reqUser, User user){
319 return UserRestUtils.getUser(reqUser, user);
326 /*************************************/
328 public static void deleteOnDemand() throws IOException {
329 Config config = DriverFactory.getConfig();
330 if(!config.getSystemUnderDebug()){
331 deleteCreatedComponents(getCatalogAsMap());
333 System.out.println("Accordindig to configuration components will not be deleted, in case to unable option to delete, please change systemUnderDebug parameter value to false ...");
337 public static void deleteCategories(User user) throws Exception {
338 Map<String, List<CategoryDefinition>> categoriesMap = getCategories();
339 List<CategoryDefinition> listCategories = categoriesMap.get(ComponentTypeEnum.RESOURCE_PARAM_NAME);
340 deleteCategoriesByList(listCategories, ComponentTypeEnum.RESOURCE_PARAM_NAME, user);
341 listCategories = categoriesMap.get(ComponentTypeEnum.SERVICE_PARAM_NAME);
342 deleteCategoriesByList(listCategories, ComponentTypeEnum.SERVICE_PARAM_NAME, user);