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 com.google.gson.Gson;
24 import org.json.simple.JSONArray;
25 import org.json.simple.JSONObject;
26 import org.json.simple.JSONValue;
27 import org.openecomp.sdc.be.model.Service;
28 import org.openecomp.sdc.be.model.User;
29 import org.openecomp.sdc.ci.tests.api.Urls;
30 import org.openecomp.sdc.ci.tests.config.Config;
31 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
32 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
33 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
34 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
35 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
36 import org.openecomp.sdc.ci.tests.utils.Utils;
37 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import java.io.IOException;
42 import java.util.HashMap;
45 public class ServiceRestUtils extends BaseRestUtils {
46 private static Logger logger = LoggerFactory.getLogger(ServiceRestUtils.class.getName());
47 // ****** CREATE *******
49 private static Gson gson = new Gson();
51 public static RestResponse deleteService(String serviceName, String version, User sdncModifierDetails)
54 Config config = Utils.getConfig();
55 String url = String.format(Urls.DELETE_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
56 config.getCatalogBePort(), serviceName, version);
58 String userId = sdncModifierDetails.getUserId();
59 RestResponse sendDelete = sendDelete(url, userId);
60 deleteMarkedServices(userId);
64 public static RestResponse markServiceToDelete(String resourceId, String userId) throws IOException {
66 Config config = Utils.getConfig();
67 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId);
68 RestResponse sendDelete = sendDelete(url, userId);
74 public static RestResponse deleteServiceById(String serviceId, String userId) throws IOException {
76 Config config = Utils.getConfig();
77 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
78 RestResponse sendDelete = sendDelete(url, userId);
79 deleteMarkedServices(userId);
83 public static void deleteMarkedServices(String userId) throws IOException {
85 Config config = Utils.getConfig();
86 url = String.format(Urls.DELETE_MARKED_SERVICES, config.getCatalogBeHost(), config.getCatalogBePort());
87 sendDelete(url, userId);
90 public static RestResponse createService(ServiceReqDetails service, User user) throws Exception {
92 Config config = Utils.getConfig();
93 String url = String.format(Urls.CREATE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort());
94 String serviceBodyJson = gson.toJson(service);
96 logger.debug("Send POST request to create service: {}", url);
97 logger.debug("Service body: {}", serviceBodyJson);
99 RestResponse res = sendPost(url, serviceBodyJson, user.getUserId(), acceptHeaderData);
100 if (res.getErrorCode() == STATUS_CODE_CREATED) {
101 service.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
102 service.setVersion(ResponseParser.getVersionFromResponse(res));
103 service.setUUID(ResponseParser.getUuidFromResponse(res));
104 // Creator details never change after component is created - Ella,
106 service.setCreatorUserId(user.getUserId());
107 service.setCreatorFullName(user.getFullName());
113 public static RestResponse updateService(ServiceReqDetails service, User user) throws Exception {
114 Config config = Utils.getConfig();
115 String url = String.format(Urls.UPDATE_SERVICE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
116 service.getUniqueId());
117 String serviceBodyJson = gson.toJson(service);
119 logger.debug("Send PUT request to create service: {}", url);
120 logger.debug("Service body: {}", serviceBodyJson);
122 RestResponse res = sendPut(url, serviceBodyJson, user.getUserId(), acceptHeaderData);
123 if (res.getErrorCode() == STATUS_CODE_CREATED) {
124 service.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
125 service.setVersion(ResponseParser.getVersionFromResponse(res));
131 public static RestResponse getService(String serviceId) throws IOException {
133 Config config = Utils.getConfig();
134 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
135 return getServiceFromUrl(url, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), false);
138 public static RestResponse getService(ServiceReqDetails serviceReqDetails, User sdncModifierDetails)
141 Config config = Utils.getConfig();
142 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
143 serviceReqDetails.getUniqueId());
144 return getServiceFromUrl(url, sdncModifierDetails, false);
147 public static RestResponse getService(String serviceId, User sdncModifierDetails) throws IOException {
149 Config config = Utils.getConfig();
150 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
151 return getServiceFromUrl(url, sdncModifierDetails, false);
154 public static RestResponse getServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
155 String serviceVersion) throws IOException {
156 Config config = Utils.getConfig();
157 String url = String.format(Urls.GET_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
158 config.getCatalogBePort(), serviceName, serviceVersion);
159 return getServiceFromUrl(url, sdncModifierDetails, false);
162 public static RestResponse getServiceFromUrl(String url, User sdncModifierDetails, boolean isCached)
164 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, isCached);
165 HttpRequest http = new HttpRequest();
166 logger.debug("Send GET request to create service: {}", url);
167 logger.debug("Service headers: {}", headersMap);
168 RestResponse sendGetServerRequest = http.httpSendGet(url, headersMap);
170 return sendGetServerRequest;
173 public static Map<String, String> prepareHeadersMap(User sdncModifierDetails, boolean isCached) {
174 Map<String, String> headersMap = new HashMap<>();
176 headersMap.put(HttpHeaderEnum.CACHE_CONTROL.getValue(), BaseRestUtils.cacheControlHeader);
178 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
179 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
180 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
184 public static RestResponse approveServiceDistribution(String serviceId, String userId) throws Exception {
185 return changeServiceDistributionState(serviceId, userId, Urls.APPROVE_DISTRIBUTION);
188 public static RestResponse rejectServiceDistribution(String serviceId, String userId) throws Exception {
189 return changeServiceDistributionState(serviceId, userId, Urls.REJECT_DISTRIBUTION);
193 public static RestResponse rejectServiceDistribution(String serviceId, String userId, String comment)
195 Config config = Utils.getConfig();
196 String url = String.format(Urls.REJECT_DISTRIBUTION, config.getCatalogBeHost(), config.getCatalogBePort(),
198 String userBodyJson = gson.toJson(comment);
199 return sendPost(url, userBodyJson, userId, BaseRestUtils.acceptHeaderData);
203 private static RestResponse changeServiceDistributionState(String serviceId, String userId, String distributionUrl)
205 Config config = Utils.getConfig();
206 String url = String.format(distributionUrl, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
207 String defComment = "{ userRemarks : \"this is an test\" }";
208 String userBodyJson = gson.toJson(defComment);
209 return sendPost(url, userBodyJson, userId, acceptHeaderData);
213 public static RestResponse getServiceLatestVersionList(User sdncModifierDetails) throws IOException {
215 Config config = Utils.getConfig();
216 String url = String.format(Urls.GET_SERVICE_lATEST_VERSION, config.getCatalogBeHost(),
217 config.getCatalogBePort());
219 return sendGet(url, sdncModifierDetails.getUserId());
223 public static RestResponse createServiceByHttpMethod(ServiceReqDetails serviceDetails, User sdncModifierDetails,
224 String method, String urls) throws IOException {
225 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
227 Config config = Utils.getConfig();
228 String serviceBodyJson = gson.toJson(serviceDetails);
229 HttpRequest http = new HttpRequest();
230 String url = String.format(urls, config.getCatalogBeHost(), config.getCatalogBePort());
231 // TODO: ADD AUTHENTICATION IN REQUEST
233 logger.debug("Send {} request to create user: {}",method,url);
234 logger.debug("User body: {}", serviceBodyJson);
235 logger.debug("User headers: {}", headersMap);
236 RestResponse sendCreateUserRequest = http.httpSendByMethod(url, method, serviceBodyJson, headersMap);
238 return sendCreateUserRequest;
242 public static RestResponse deleteServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
243 String serviceVersion) throws IOException {
244 Config config = Utils.getConfig();
246 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
248 HttpRequest http = new HttpRequest();
250 String url = String.format(Urls.DELETE_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
251 config.getCatalogBePort(), serviceName, serviceVersion);
252 RestResponse deleteResponse = http.httpSendDelete(url, headersMap);
254 deleteMarkedServices(sdncModifierDetails.getUserId());
255 return deleteResponse;
258 public static RestResponse getFollowed(User user) throws Exception {
259 Config config = Utils.getConfig();
261 HttpRequest httpRequest = new HttpRequest();
263 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
265 Map<String, String> headersMap = new HashMap<>();
266 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
267 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
268 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), user.getUserId());
270 RestResponse getResourceNotAbstarctResponse = httpRequest.httpSendGet(url, headersMap);
272 return getResourceNotAbstarctResponse;
275 public static JSONArray getListArrayFromRestResponse(RestResponse restResponse) {
276 String json = restResponse.getResponse();
277 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
278 JSONArray servicesArray = (JSONArray) jsonResp.get("services");
280 logger.debug("services= {}", servicesArray);
282 return servicesArray;
285 public static RestResponse getDistributionServiceList(Service service, User user) throws IOException {
287 Config config = Utils.getConfig();
288 String url = String.format(Urls.DISTRIBUTION_SERVICE_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), service.getUUID());
289 return getServiceFromUrl(url, user, false);