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.utils.rest;
23 import java.io.IOException;
24 import java.util.HashMap;
27 import org.json.simple.JSONArray;
28 import org.json.simple.JSONObject;
29 import org.json.simple.JSONValue;
30 import org.openecomp.sdc.be.model.Service;
31 import org.openecomp.sdc.be.model.User;
32 import org.openecomp.sdc.ci.tests.api.Urls;
33 import org.openecomp.sdc.ci.tests.config.Config;
34 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
35 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
36 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
37 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
38 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
39 import org.openecomp.sdc.ci.tests.utils.Utils;
40 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import com.google.gson.Gson;
46 public class ServiceRestUtils extends BaseRestUtils {
47 private static Logger logger = LoggerFactory.getLogger(ServiceRestUtils.class.getName());
48 // ****** CREATE *******
50 private static Gson gson = new Gson();
52 public static RestResponse deleteService(String serviceName, String version, User sdncModifierDetails)
55 Config config = Utils.getConfig();
56 String url = String.format(Urls.DELETE_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
57 config.getCatalogBePort(), serviceName, version);
59 String userId = sdncModifierDetails.getUserId();
60 RestResponse sendDelete = sendDelete(url, userId);
61 deleteMarkedServices(userId);
65 public static RestResponse markServiceToDelete(String resourceId, String userId) throws IOException {
67 Config config = Utils.getConfig();
68 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId);
69 RestResponse sendDelete = sendDelete(url, userId);
75 public static RestResponse deleteServiceById(String serviceId, String userId) throws IOException {
77 Config config = Utils.getConfig();
78 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
79 RestResponse sendDelete = sendDelete(url, userId);
80 deleteMarkedServices(userId);
84 public static void deleteMarkedServices(String userId) throws IOException {
86 Config config = Utils.getConfig();
87 url = String.format(Urls.DELETE_MARKED_SERVICES, config.getCatalogBeHost(), config.getCatalogBePort());
88 sendDelete(url, userId);
91 public static RestResponse createService(ServiceReqDetails service, User user) throws Exception {
93 Config config = Utils.getConfig();
94 String url = String.format(Urls.CREATE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort());
95 String serviceBodyJson = gson.toJson(service);
97 logger.debug("Send POST request to create service: {}", url);
98 logger.debug("Service body: {}", serviceBodyJson);
100 RestResponse res = sendPost(url, serviceBodyJson, user.getUserId(), acceptHeaderData);
101 if (res.getErrorCode() == STATUS_CODE_CREATED) {
102 service.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
103 service.setVersion(ResponseParser.getVersionFromResponse(res));
104 service.setUUID(ResponseParser.getUuidFromResponse(res));
105 // Creator details never change after component is created - Ella,
107 service.setCreatorUserId(user.getUserId());
108 service.setCreatorFullName(user.getFullName());
114 public static RestResponse updateService(ServiceReqDetails service, User user) throws Exception {
115 Config config = Utils.getConfig();
116 String url = String.format(Urls.UPDATE_SERVICE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
117 service.getUniqueId());
118 String serviceBodyJson = gson.toJson(service);
120 logger.debug("Send PUT request to create service: {}", url);
121 logger.debug("Service body: {}", serviceBodyJson);
123 RestResponse res = sendPut(url, serviceBodyJson, user.getUserId(), acceptHeaderData);
124 if (res.getErrorCode() == STATUS_CODE_CREATED) {
125 service.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
126 service.setVersion(ResponseParser.getVersionFromResponse(res));
132 public static RestResponse getService(String serviceId) throws IOException {
134 Config config = Utils.getConfig();
135 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
136 return getServiceFromUrl(url, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), false);
139 public static RestResponse getService(ServiceReqDetails serviceReqDetails, User sdncModifierDetails)
142 Config config = Utils.getConfig();
143 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
144 serviceReqDetails.getUniqueId());
145 return getServiceFromUrl(url, sdncModifierDetails, false);
148 public static RestResponse getService(String serviceId, User sdncModifierDetails) throws IOException {
150 Config config = Utils.getConfig();
151 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
152 return getServiceFromUrl(url, sdncModifierDetails, false);
155 public static RestResponse getServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
156 String serviceVersion) throws IOException {
157 Config config = Utils.getConfig();
158 String url = String.format(Urls.GET_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
159 config.getCatalogBePort(), serviceName, serviceVersion);
160 return getServiceFromUrl(url, sdncModifierDetails, false);
163 public static RestResponse getServiceFromUrl(String url, User sdncModifierDetails, boolean isCached)
165 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, isCached);
166 HttpRequest http = new HttpRequest();
167 logger.debug("Send GET request to create service: {}", url);
168 logger.debug("Service headers: {}", headersMap);
169 RestResponse sendGetServerRequest = http.httpSendGet(url, headersMap);
171 return sendGetServerRequest;
174 public static Map<String, String> prepareHeadersMap(User sdncModifierDetails, boolean isCached) {
175 Map<String, String> headersMap = new HashMap<>();
177 headersMap.put(HttpHeaderEnum.CACHE_CONTROL.getValue(), BaseRestUtils.cacheControlHeader);
179 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
180 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
181 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
185 public static RestResponse approveServiceDistribution(String serviceId, String userId) throws Exception {
186 return changeServiceDistributionState(serviceId, userId, Urls.APPROVE_DISTRIBUTION);
189 public static RestResponse rejectServiceDistribution(String serviceId, String userId) throws Exception {
190 return changeServiceDistributionState(serviceId, userId, Urls.REJECT_DISTRIBUTION);
194 public static RestResponse rejectServiceDistribution(String serviceId, String userId, String comment)
196 Config config = Utils.getConfig();
197 String url = String.format(Urls.REJECT_DISTRIBUTION, config.getCatalogBeHost(), config.getCatalogBePort(),
199 String userBodyJson = gson.toJson(comment);
200 return sendPost(url, userBodyJson, userId, BaseRestUtils.acceptHeaderData);
204 private static RestResponse changeServiceDistributionState(String serviceId, String userId, String distributionUrl)
206 Config config = Utils.getConfig();
207 String url = String.format(distributionUrl, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
208 String defComment = "{ userRemarks : \"this is an test\" }";
209 String userBodyJson = gson.toJson(defComment);
210 return sendPost(url, userBodyJson, userId, acceptHeaderData);
214 public static RestResponse getServiceLatestVersionList(User sdncModifierDetails) throws IOException {
216 Config config = Utils.getConfig();
217 String url = String.format(Urls.GET_SERVICE_lATEST_VERSION, config.getCatalogBeHost(),
218 config.getCatalogBePort());
220 return sendGet(url, sdncModifierDetails.getUserId());
224 public static RestResponse createServiceByHttpMethod(ServiceReqDetails serviceDetails, User sdncModifierDetails,
225 String method, String urls) throws IOException {
226 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
228 Config config = Utils.getConfig();
229 String serviceBodyJson = gson.toJson(serviceDetails);
230 HttpRequest http = new HttpRequest();
231 String url = String.format(urls, config.getCatalogBeHost(), config.getCatalogBePort());
232 // TODO: ADD AUTHENTICATION IN REQUEST
234 logger.debug("Send {} request to create user: {}",method,url);
235 logger.debug("User body: {}", serviceBodyJson);
236 logger.debug("User headers: {}", headersMap);
237 RestResponse sendCreateUserRequest = http.httpSendByMethod(url, method, serviceBodyJson, headersMap);
239 return sendCreateUserRequest;
243 public static RestResponse deleteServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
244 String serviceVersion) throws IOException {
245 Config config = Utils.getConfig();
247 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
249 HttpRequest http = new HttpRequest();
251 String url = String.format(Urls.DELETE_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
252 config.getCatalogBePort(), serviceName, serviceVersion);
253 RestResponse deleteResponse = http.httpSendDelete(url, headersMap);
255 deleteMarkedServices(sdncModifierDetails.getUserId());
256 return deleteResponse;
259 public static RestResponse getFollowed(User user) throws Exception {
260 Config config = Utils.getConfig();
262 HttpRequest httpRequest = new HttpRequest();
264 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
266 Map<String, String> headersMap = new HashMap<>();
267 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
268 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
269 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), user.getUserId());
271 RestResponse getResourceNotAbstarctResponse = httpRequest.httpSendGet(url, headersMap);
273 return getResourceNotAbstarctResponse;
276 public static JSONArray getListArrayFromRestResponse(RestResponse restResponse) {
277 String json = restResponse.getResponse();
278 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
279 JSONArray servicesArray = (JSONArray) jsonResp.get("services");
281 logger.debug("services= {}", servicesArray);
283 return servicesArray;
286 public static RestResponse getDistributionServiceList(Service service, User user) throws IOException {
288 Config config = Utils.getConfig();
289 String url = String.format(Urls.DISTRIBUTION_SERVICE_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), service.getUUID());
290 return getServiceFromUrl(url, user, false);