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.onap.sdc.frontend.ci.tests.utilities;
23 import com.aventstack.extentreports.Status;
24 import java.io.IOException;
25 import java.net.InetAddress;
26 import java.net.UnknownHostException;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.stream.Collectors;
31 import org.apache.http.HttpStatus;
32 import org.codehaus.jettison.json.JSONObject;
33 import org.onap.sdc.backend.ci.tests.config.Config;
34 import org.onap.sdc.backend.ci.tests.datatypes.ComponentReqDetails;
35 import org.onap.sdc.backend.ci.tests.datatypes.ResourceReqDetails;
36 import org.onap.sdc.backend.ci.tests.datatypes.ServiceReqDetails;
37 import org.onap.sdc.backend.ci.tests.datatypes.http.RestResponse;
38 import org.onap.sdc.backend.ci.tests.utils.general.ElementFactory;
39 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
40 import org.openecomp.sdc.be.model.Component;
41 import org.openecomp.sdc.be.model.User;
42 import org.openecomp.sdc.be.model.category.CategoryDefinition;
43 import org.onap.sdc.backend.ci.tests.datatypes.enums.UserRoleEnum;
44 import org.onap.sdc.frontend.ci.tests.execute.setup.DriverFactory;
45 import org.onap.sdc.frontend.ci.tests.execute.setup.ExtentTestActions;
46 import org.onap.sdc.backend.ci.tests.utils.rest.CatalogRestUtils;
47 import org.onap.sdc.backend.ci.tests.utils.rest.CategoryRestUtils;
48 import org.onap.sdc.backend.ci.tests.utils.rest.ResourceRestUtils;
49 import org.onap.sdc.backend.ci.tests.utils.rest.ResponseParser;
50 import org.onap.sdc.backend.ci.tests.utils.rest.ServiceRestUtils;
51 import org.onap.sdc.backend.ci.tests.utils.rest.UserRestUtils;
53 public class RestCDUtils {
55 private static final int SLEEP_DURATION = 1000;
57 private static void setResourceUniqueIdAndUUID(ComponentReqDetails element, RestResponse getResourceResponse) {
58 element.setUniqueId(ResponseParser.getUniqueIdFromResponse(getResourceResponse));
59 element.setUUID(ResponseParser.getUuidFromResponse(getResourceResponse));
62 public static RestResponse getResource(ResourceReqDetails resource, User user) {
63 final String getResourceMsg = "Trying to get resource named " + resource.getName() + " with version " + resource.getVersion();
64 final String succeedGetResourceMsg = "Succeeded to get resource named " + resource.getName() + " with version " + resource.getVersion();
65 final String failedGetResourceMsg = "Failed to get resource named " + resource.getName() + " with version " + resource.getVersion();
67 ExtentTestActions.log(Status.INFO, getResourceMsg);
68 System.out.println(getResourceMsg);
69 GeneralUIUtils.sleep(SLEEP_DURATION);
70 RestResponse getResourceResponse = null;
71 String resourceUniqueId = resource.getUniqueId();
72 if (resourceUniqueId != null) {
73 getResourceResponse = ResourceRestUtils.getResource(resourceUniqueId);
74 if (getResourceResponse.getErrorCode().intValue() == HttpStatus.SC_OK) {
75 ExtentTestActions.log(Status.INFO, succeedGetResourceMsg);
76 System.out.println(succeedGetResourceMsg);
78 return getResourceResponse;
80 JSONObject getResourceJSONObject = null;
81 getResourceResponse = ResourceRestUtils.getResourceByNameAndVersion(user.getUserId(), resource.getName(), resource.getVersion());
82 if (getResourceResponse.getErrorCode().intValue() == HttpStatus.SC_OK) {
83 setResourceUniqueIdAndUUID(resource, getResourceResponse);
84 ExtentTestActions.log(Status.INFO, succeedGetResourceMsg);
85 System.out.println(succeedGetResourceMsg);
86 return getResourceResponse;
88 ExtentTestActions.log(Status.INFO, failedGetResourceMsg);
89 return getResourceResponse;
90 } catch (Exception e) {
91 throw new RuntimeException(e);
95 public static RestResponse getService(ServiceReqDetails service, User user) {
96 final int threadSleepTime = 3500;
98 Thread.sleep(threadSleepTime);
99 RestResponse getServiceResponse = ServiceRestUtils.getServiceByNameAndVersion(user, service.getName(),
100 service.getVersion());
101 if (getServiceResponse.getErrorCode().intValue() == HttpStatus.SC_OK) {
102 setResourceUniqueIdAndUUID(service, getServiceResponse);
104 return getServiceResponse;
105 } catch (Exception e) {
106 throw new RuntimeException(e);
111 public static String getExecutionHostAddress() {
113 String computerName = null;
115 computerName = InetAddress.getLocalHost().getHostAddress().replaceAll("\\.", "·");
116 System.out.println(computerName);
117 if (computerName.contains(".")) {
118 computerName = computerName.substring(0, computerName.indexOf(".")).toUpperCase();
120 } catch (UnknownHostException e) {
121 System.out.println("Uknown hostAddress");
123 return computerName != null ? computerName : "Uknown hostAddress";
126 public static Map<String, List<Component>> getCatalogAsMap() throws IOException {
127 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
128 RestResponse catalog = CatalogRestUtils.getCatalog(defaultAdminUser.getUserId());
129 return ResponseParser.convertCatalogResponseToJavaObject(catalog.getResponse());
132 public static Map<String, List<CategoryDefinition>> getCategories() throws Exception {
134 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
136 Map<String, List<CategoryDefinition>> map = new HashMap<>();
139 RestResponse allResourceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.RESOURCE_PARAM_NAME);
140 RestResponse allServiceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.SERVICE_PARAM_NAME);
142 List<CategoryDefinition> parsedResourceCategories = ResponseParser.parseCategories(allResourceCategories);
143 List<CategoryDefinition> parsedServiceCategories = ResponseParser.parseCategories(allServiceCategories);
145 map.put(ComponentTypeEnum.RESOURCE_PARAM_NAME, parsedResourceCategories);
146 map.put(ComponentTypeEnum.SERVICE_PARAM_NAME, parsedServiceCategories);
152 public static void deleteCreatedComponents(Map<String, List<Component>> map) throws IOException {
154 System.out.println("going to delete all created components...");
156 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
157 final String userId = defaultAdminUser.getUserId();
160 List<Component> resourcesArrayList = map.get("resources");
161 List<String> collect = resourcesArrayList.stream().filter(s -> s.getName().startsWith(ElementFactory.getResourcePrefix())).
162 map(e -> e.getUniqueId()).
163 collect(Collectors.toList());
164 for (String uId : collect) {
165 ResourceRestUtils.markResourceToDelete(uId, userId);
168 ResourceRestUtils.deleteMarkedResources(userId);
170 resourcesArrayList = map.get("services");
171 collect = resourcesArrayList.stream().
172 filter(e -> e != null).
173 filter(e -> e.getName() != null).
174 filter(s -> s.getName().startsWith(ElementFactory.getServicePrefix())).
175 filter(e -> e.getUniqueId() != null).
176 map(e -> e.getUniqueId()).
177 collect(Collectors.toList());
178 for (String uId : collect) {
179 ServiceRestUtils.markServiceToDelete(uId, userId);
181 ServiceRestUtils.deleteMarkedServices(userId);
185 public static String getUserRole(User reqUser, User user) {
187 RestResponse getUserRoleResp = UserRestUtils.getUserRole(reqUser, user);
188 JSONObject jObject = new JSONObject(getUserRoleResp.getResponse());
189 return jObject.getString("role");
190 } catch (Exception e) {
195 public static RestResponse getUser(User reqUser, User user) {
197 return UserRestUtils.getUser(reqUser, user);
198 } catch (Exception e) {
203 /*************************************/
205 public static void deleteOnDemand() throws IOException {
206 Config config = DriverFactory.getConfig();
207 if (!config.getSystemUnderDebug()) {
208 deleteCreatedComponents(getCatalogAsMap());
210 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 ...");