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 static org.testng.AssertJUnit.assertTrue;
25 import java.io.IOException;
26 import java.net.InetAddress;
27 import java.net.UnknownHostException;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
32 import java.util.stream.Collectors;
34 import org.codehaus.jackson.map.ObjectMapper;
35 import org.codehaus.jettison.json.JSONArray;
36 import org.codehaus.jettison.json.JSONObject;
37 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
38 import org.openecomp.sdc.be.model.Component;
39 import org.openecomp.sdc.be.model.User;
40 import org.openecomp.sdc.be.model.category.CategoryDefinition;
41 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
42 import org.openecomp.sdc.ci.tests.config.Config;
43 import org.openecomp.sdc.ci.tests.datatypes.ComponentReqDetails;
44 import org.openecomp.sdc.ci.tests.datatypes.ProductReqDetails;
45 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
46 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
47 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
48 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
49 import org.openecomp.sdc.ci.tests.execute.setup.DriverFactory;
50 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
51 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
52 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
53 import org.openecomp.sdc.ci.tests.utils.rest.CategoryRestUtils;
54 import org.openecomp.sdc.ci.tests.utils.rest.ProductRestUtils;
55 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
56 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
57 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
58 import org.openecomp.sdc.ci.tests.utils.rest.UserRestUtils;
60 import com.aventstack.extentreports.Status;
62 public class RestCDUtils {
64 private static void setResourceUniqueIdAndUUID(ComponentReqDetails element, RestResponse getResourceResponse) {
65 element.setUniqueId(ResponseParser.getUniqueIdFromResponse(getResourceResponse));
66 element.setUUID(ResponseParser.getUuidFromResponse(getResourceResponse));
69 public static RestResponse getResource(ResourceReqDetails resource, User user) {
70 final String getResourceMsg = "Trying to get resource named " + resource.getName() + " with version " + resource.getVersion();
71 final String succeedGetResourceMsg = "Succeeded to get resource named " + resource.getName() + " with version " + resource.getVersion();
72 final String failedGetResourceMsg = "Failed to get resource named " + resource.getName() + " with version " + resource.getVersion();
74 ExtentTestActions.log(Status.INFO, getResourceMsg);
75 System.out.println(getResourceMsg);
76 GeneralUIUtils.sleep(1000);
77 RestResponse getResourceResponse = null;
78 String reourceUniqueId = resource.getUniqueId();
79 if (reourceUniqueId != null) {
80 getResourceResponse = ResourceRestUtils.getResource(reourceUniqueId);
81 if (getResourceResponse.getErrorCode().intValue() == 200) {
82 ExtentTestActions.log(Status.INFO, succeedGetResourceMsg);
83 System.out.println(succeedGetResourceMsg);
85 return getResourceResponse;
87 JSONObject getResourceJSONObject = null;
88 getResourceResponse = ResourceRestUtils.getResourceByNameAndVersion(user.getUserId(), resource.getName(), resource.getVersion());
89 if (getResourceResponse.getErrorCode().intValue() == 200) {
90 // JSONArray jArray = new JSONArray(getResourceResponse.getResponse());
91 // for (int i = 0; i < jArray.length(); i++) {
92 // getResourceJSONObject = jArray.getJSONObject(i);
93 // String resourceType = ResponseParser.getValueFromJsonResponse(getResourceJSONObject.toString(), "resourceType");
94 // if (resourceType.equals(resource.getResourceType())) {
95 // getResourceResponse.setResponse(getResourceJSONObject.toString());
96 setResourceUniqueIdAndUUID(resource, getResourceResponse);
97 ExtentTestActions.log(Status.INFO, succeedGetResourceMsg);
98 System.out.println(succeedGetResourceMsg);
99 return getResourceResponse;
103 ExtentTestActions.log(Status.INFO, failedGetResourceMsg);
104 return getResourceResponse;
105 } catch (Exception e) {
106 throw new RuntimeException(e);
110 public static RestResponse getService(ServiceReqDetails service, User user) {
113 RestResponse getServiceResponse = ServiceRestUtils.getServiceByNameAndVersion(user, service.getName(),
114 service.getVersion());
115 if (getServiceResponse.getErrorCode().intValue() == 200) {
116 setResourceUniqueIdAndUUID(service, getServiceResponse);
118 return getServiceResponse;
119 } catch (Exception e) {
120 throw new RuntimeException(e);
125 public static RestResponse getProduct(ProductReqDetails product, User user) {
128 RestResponse getProductResponse = ProductRestUtils.getProductByNameAndVersion(product.getName(),
129 product.getVersion(), user.getUserId());
130 if (getProductResponse.getErrorCode().intValue() == 200) {
131 setResourceUniqueIdAndUUID(product, getProductResponse);
133 return getProductResponse;
134 } catch (Exception e) {
135 throw new RuntimeException(e);
139 public static Map<String, String> getAllElementVersionsFromResponse(RestResponse getResource) throws Exception {
140 Map<String, String> versionsMap = new HashMap<String, String>();
142 ObjectMapper mapper = new ObjectMapper();
144 JSONObject object = new JSONObject(getResource.getResponse());
145 versionsMap = mapper.readValue(object.get("allVersions").toString(), Map.class);
147 } catch (Exception e) {
156 public static void deleteElementVersions(Map<String, String> elementVersions, boolean isBeforeTest, Object clazz,
157 User user) throws Exception {
158 Iterator<String> iterator = elementVersions.keySet().iterator();
159 while (iterator.hasNext()) {
160 String singleVersion = iterator.next();
161 String uniqueId = elementVersions.get(singleVersion);
162 RestResponse deleteResponse = null;
163 if (clazz instanceof ServiceReqDetails) {
164 deleteResponse = ServiceRestUtils.deleteServiceById(uniqueId, user.getUserId());
165 } else if (clazz instanceof ResourceReqDetails) {
166 deleteResponse = ResourceRestUtils.deleteResource(uniqueId, user.getUserId());
167 } else if (clazz instanceof ProductReqDetails) {
168 deleteResponse = ProductRestUtils.deleteProduct(uniqueId, user.getUserId());
172 assertTrue(deleteResponse.getErrorCode().intValue() == 204
173 || deleteResponse.getErrorCode().intValue() == 404);
175 assertTrue(deleteResponse.getErrorCode().intValue() == 204);
180 public static void deleteAllResourceVersionsAfterTest(ComponentReqDetails componentDetails,
181 RestResponse getObjectResponse, User user) {
183 deleteAllComponentVersion(false, componentDetails, getObjectResponse, user);
184 } catch (Exception e) {
189 public static void deleteAllResourceVersionsBeforeTest(ComponentReqDetails componentDetails,
190 RestResponse getObjectResponse, User user) throws Exception {
191 deleteAllComponentVersion(true, componentDetails, getObjectResponse, user);
194 public static void deleteAllComponentVersion(boolean isBeforeTest, ComponentReqDetails componentDetails,
195 RestResponse getObjectResponse, User user) throws Exception {
196 if (getObjectResponse.getErrorCode().intValue() == 404)
198 Map<String, String> componentVersionsMap = getAllElementVersionsFromResponse(getObjectResponse);
199 System.out.println("deleting...");
200 deleteElementVersions(componentVersionsMap, isBeforeTest, componentDetails, user);
201 componentDetails.setUniqueId(null);
206 public static String getExecutionHostAddress() {
208 String computerName = null;
210 computerName = InetAddress.getLocalHost().getHostAddress().replaceAll("\\.", "·");
211 System.out.println(computerName);
212 if (computerName.indexOf(".") > -1)
213 computerName = computerName.substring(0,
214 computerName.indexOf(".")).toUpperCase();
215 } catch (UnknownHostException e) {
216 System.out.println("Uknown hostAddress");
218 return computerName != null ? computerName : "Uknown hostAddress";
221 public static Map<String, List<Component>> getCatalogAsMap() throws IOException {
222 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
223 RestResponse catalog = CatalogRestUtils.getCatalog(defaultAdminUser.getUserId());
224 Map<String, List<Component>> convertCatalogResponseToJavaObject = ResponseParser
225 .convertCatalogResponseToJavaObject(catalog.getResponse());
226 return convertCatalogResponseToJavaObject;
229 public static Map<String, List<CategoryDefinition>> getCategories() throws Exception {
231 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
233 Map<String,List<CategoryDefinition>> map = new HashMap<String,List<CategoryDefinition>>();
236 RestResponse allResourceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.RESOURCE_PARAM_NAME);
237 RestResponse allServiceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.SERVICE_PARAM_NAME);
239 List<CategoryDefinition> parsedResourceCategories = ResponseParser.parseCategories(allResourceCategories);
240 List<CategoryDefinition> parsedServiceCategories = ResponseParser.parseCategories(allServiceCategories);
242 map.put(ComponentTypeEnum.RESOURCE_PARAM_NAME, parsedResourceCategories);
243 map.put(ComponentTypeEnum.SERVICE_PARAM_NAME, parsedServiceCategories);
248 public static void deleteCreatedComponents(Map<String, List<Component>> map) throws IOException {
250 System.out.println("going to delete all created components...");
252 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
253 final String userId = defaultAdminUser.getUserId();
255 List<Component> resourcesArrayList = map.get("products");
256 List<String> collect = resourcesArrayList.stream().filter(s -> s.getName().startsWith("Ci")).map(e -> e.getUniqueId())
257 .collect(Collectors.toList());
258 for (String uId : collect) {
259 ProductRestUtils.deleteProduct(uId, userId);
262 resourcesArrayList = map.get("services");
263 collect = resourcesArrayList.stream().filter(s -> s.getName().startsWith("ci")).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("ci"))
272 .map(e -> e.getUniqueId()).collect(Collectors.toList());
273 for (String uId : collect) {
274 ResourceRestUtils.markResourceToDelete(uId, userId);
276 ResourceRestUtils.deleteMarkedResources(userId);
284 public static void deleteCategoriesByList(List<CategoryDefinition> listCategories, String componentType, User user) throws Exception {
286 for (CategoryDefinition categoryDefinition : listCategories) {
287 if (categoryDefinition.getName().toLowerCase().startsWith("ci")) {
288 List<SubCategoryDefinition> subcategories = categoryDefinition.getSubcategories();
289 if (subcategories != null) {
290 for (SubCategoryDefinition subCategoryDefinition : subcategories) {
292 CategoryRestUtils.deleteSubCategory(subCategoryDefinition.getUniqueId(),
293 categoryDefinition.getUniqueId(), user.getUserId(),
298 CategoryRestUtils.deleteCategory(categoryDefinition.getUniqueId(), user.getUserId(),
305 public static String getUserRole(User reqUser, User user){
307 RestResponse getUserRoleResp = UserRestUtils.getUserRole(reqUser, user);
308 JSONObject jObject = new JSONObject(getUserRoleResp.getResponse());
309 return jObject.getString("role");
316 public static RestResponse getUser(User reqUser, User user){
318 return UserRestUtils.getUser(reqUser, user);
325 /*************************************/
327 public static void deleteOnDemand() throws IOException {
328 Config config = DriverFactory.getConfig();
329 if(!config.getSystemUnderDebug()){
330 deleteCreatedComponents(getCatalogAsMap());
332 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 ...");
336 public static void deleteCategories(User user) throws Exception {
337 Map<String, List<CategoryDefinition>> categoriesMap = getCategories();
338 List<CategoryDefinition> listCategories = categoriesMap.get(ComponentTypeEnum.RESOURCE_PARAM_NAME);
339 deleteCategoriesByList(listCategories, ComponentTypeEnum.RESOURCE_PARAM_NAME, user);
340 listCategories = categoriesMap.get(ComponentTypeEnum.SERVICE_PARAM_NAME);
341 deleteCategoriesByList(listCategories, ComponentTypeEnum.SERVICE_PARAM_NAME, user);