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 org.codehaus.jettison.json.JSONObject;
25 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
26 import org.openecomp.sdc.be.model.Component;
27 import org.openecomp.sdc.be.model.User;
28 import org.openecomp.sdc.be.model.category.CategoryDefinition;
29 import org.openecomp.sdc.ci.tests.config.Config;
30 import org.openecomp.sdc.ci.tests.datatypes.ComponentReqDetails;
31 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
32 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
33 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
34 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
35 import org.openecomp.sdc.ci.tests.execute.setup.DriverFactory;
36 import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
37 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
38 import org.openecomp.sdc.ci.tests.utils.rest.*;
40 import java.io.IOException;
41 import java.net.InetAddress;
42 import java.net.UnknownHostException;
43 import java.util.HashMap;
44 import java.util.List;
46 import java.util.stream.Collectors;
48 public class RestCDUtils {
50 private static void setResourceUniqueIdAndUUID(ComponentReqDetails element, RestResponse getResourceResponse) {
51 element.setUniqueId(ResponseParser.getUniqueIdFromResponse(getResourceResponse));
52 element.setUUID(ResponseParser.getUuidFromResponse(getResourceResponse));
55 public static RestResponse getResource(ResourceReqDetails resource, User user) {
56 final String getResourceMsg = "Trying to get resource named " + resource.getName() + " with version " + resource.getVersion();
57 final String succeedGetResourceMsg = "Succeeded to get resource named " + resource.getName() + " with version " + resource.getVersion();
58 final String failedGetResourceMsg = "Failed to get resource named " + resource.getName() + " with version " + resource.getVersion();
60 ExtentTestActions.log(Status.INFO, getResourceMsg);
61 System.out.println(getResourceMsg);
62 GeneralUIUtils.sleep(1000);
63 RestResponse getResourceResponse = null;
64 String resourceUniqueId = resource.getUniqueId();
65 if (resourceUniqueId != null) {
66 getResourceResponse = ResourceRestUtils.getResource(resourceUniqueId);
67 if (getResourceResponse.getErrorCode().intValue() == 200) {
68 ExtentTestActions.log(Status.INFO, succeedGetResourceMsg);
69 System.out.println(succeedGetResourceMsg);
71 return getResourceResponse;
73 JSONObject getResourceJSONObject = null;
74 getResourceResponse = ResourceRestUtils.getResourceByNameAndVersion(user.getUserId(), resource.getName(), resource.getVersion());
75 if (getResourceResponse.getErrorCode().intValue() == 200) {
76 setResourceUniqueIdAndUUID(resource, getResourceResponse);
77 ExtentTestActions.log(Status.INFO, succeedGetResourceMsg);
78 System.out.println(succeedGetResourceMsg);
79 return getResourceResponse;
81 ExtentTestActions.log(Status.INFO, failedGetResourceMsg);
82 return getResourceResponse;
83 } catch (Exception e) {
84 throw new RuntimeException(e);
88 public static RestResponse getService(ServiceReqDetails service, User user) {
91 RestResponse getServiceResponse = ServiceRestUtils.getServiceByNameAndVersion(user, service.getName(),
92 service.getVersion());
93 if (getServiceResponse.getErrorCode().intValue() == 200) {
94 setResourceUniqueIdAndUUID(service, getServiceResponse);
96 return getServiceResponse;
97 } catch (Exception e) {
98 throw new RuntimeException(e);
103 public static String getExecutionHostAddress() {
105 String computerName = null;
107 computerName = InetAddress.getLocalHost().getHostAddress().replaceAll("\\.", "·");
108 System.out.println(computerName);
109 if (computerName.indexOf(".") > -1)
110 computerName = computerName.substring(0,
111 computerName.indexOf(".")).toUpperCase();
112 } catch (UnknownHostException e) {
113 System.out.println("Uknown hostAddress");
115 return computerName != null ? computerName : "Uknown hostAddress";
118 public static Map<String, List<Component>> getCatalogAsMap() throws IOException {
119 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
120 RestResponse catalog = CatalogRestUtils.getCatalog(defaultAdminUser.getUserId());
121 return ResponseParser.convertCatalogResponseToJavaObject(catalog.getResponse());
124 public static Map<String, List<CategoryDefinition>> getCategories() throws Exception {
126 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
128 Map<String, List<CategoryDefinition>> map = new HashMap<>();
131 RestResponse allResourceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.RESOURCE_PARAM_NAME);
132 RestResponse allServiceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.SERVICE_PARAM_NAME);
134 List<CategoryDefinition> parsedResourceCategories = ResponseParser.parseCategories(allResourceCategories);
135 List<CategoryDefinition> parsedServiceCategories = ResponseParser.parseCategories(allServiceCategories);
137 map.put(ComponentTypeEnum.RESOURCE_PARAM_NAME, parsedResourceCategories);
138 map.put(ComponentTypeEnum.SERVICE_PARAM_NAME, parsedServiceCategories);
144 public static void deleteCreatedComponents(Map<String, List<Component>> map) throws IOException {
146 System.out.println("going to delete all created components...");
148 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
149 final String userId = defaultAdminUser.getUserId();
152 List<Component> resourcesArrayList = map.get("resources");
153 List<String> collect = resourcesArrayList.stream().filter(s -> s.getName().startsWith(ElementFactory.getResourcePrefix())).
154 map(e -> e.getUniqueId()).
155 collect(Collectors.toList());
156 for (String uId : collect) {
157 ResourceRestUtils.markResourceToDelete(uId, userId);
160 ResourceRestUtils.deleteMarkedResources(userId);
162 resourcesArrayList = map.get("services");
163 collect = resourcesArrayList.stream().
164 filter(e -> e != null).
165 filter(e -> e.getName() != null).
166 filter(s -> s.getName().startsWith(ElementFactory.getServicePrefix())).
167 filter(e -> e.getUniqueId() != null).
168 map(e -> e.getUniqueId()).
169 collect(Collectors.toList());
170 for (String uId : collect) {
171 ServiceRestUtils.markServiceToDelete(uId, userId);
173 ServiceRestUtils.deleteMarkedServices(userId);
177 public static String getUserRole(User reqUser, User user) {
179 RestResponse getUserRoleResp = UserRestUtils.getUserRole(reqUser, user);
180 JSONObject jObject = new JSONObject(getUserRoleResp.getResponse());
181 return jObject.getString("role");
182 } catch (Exception e) {
187 public static RestResponse getUser(User reqUser, User user) {
189 return UserRestUtils.getUser(reqUser, user);
190 } catch (Exception e) {
195 /*************************************/
197 public static void deleteOnDemand() throws IOException {
198 Config config = DriverFactory.getConfig();
199 if (!config.getSystemUnderDebug()) {
200 deleteCreatedComponents(getCatalogAsMap());
202 System.out.println("According to configuration components will not be deleted, in case to unable option to delete, please change systemUnderDebug parameter value to false ...");