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 private final static String cacheControl = "no-cache";
49 private final static String contentTypeHeaderData = "application/json";
50 private final static String acceptHeaderDate = "application/json";
51 // ****** CREATE *******
53 private static Gson gson = new Gson();
55 public static RestResponse deleteService(String serviceName, String version, User sdncModifierDetails)
58 Config config = Utils.getConfig();
59 String url = String.format(Urls.DELETE_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
60 config.getCatalogBePort(), serviceName, version);
62 String userId = sdncModifierDetails.getUserId();
63 RestResponse sendDelete = sendDelete(url, userId);
64 deleteMarkedServices(userId);
68 public static RestResponse markServiceToDelete(String resourceId, String userId) throws IOException {
70 Config config = Utils.getConfig();
71 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
73 RestResponse sendDelete = sendDelete(url, userId);
79 public static RestResponse deleteServiceById(String serviceId, String userId) throws IOException {
81 Config config = Utils.getConfig();
82 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
84 RestResponse sendDelete = sendDelete(url, userId);
85 deleteMarkedServices(userId);
89 public static void deleteMarkedServices(String userId) throws IOException {
91 Config config = Utils.getConfig();
92 url = String.format(Urls.DELETE_MARKED_SERVICES, config.getCatalogBeHost(), config.getCatalogBePort());
93 sendDelete(url, userId);
96 public static RestResponse createService(ServiceReqDetails service, User user) throws Exception {
97 Config config = Utils.getConfig();
98 String url = String.format(Urls.CREATE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort());
99 String serviceBodyJson = gson.toJson(service);
101 logger.debug("Send POST request to create service: {}", url);
102 logger.debug("Service body: {}", serviceBodyJson);
104 RestResponse res = sendPost(url, serviceBodyJson, user.getUserId(), acceptHeaderData);
105 if (res.getErrorCode() == STATUS_CODE_CREATED) {
106 service.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
107 service.setVersion(ResponseParser.getVersionFromResponse(res));
108 service.setUUID(ResponseParser.getUuidFromResponse(res));
109 // Creator details never change after component is created - Ella,
111 service.setCreatorUserId(user.getUserId());
112 service.setCreatorFullName(user.getFullName());
118 public static RestResponse updateService(ServiceReqDetails service, User user) throws Exception {
119 Config config = Utils.getConfig();
120 String url = String.format(Urls.UPDATE_SERVICE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
121 service.getUniqueId());
122 String serviceBodyJson = gson.toJson(service);
124 logger.debug("Send PUT request to create service: {}", url);
125 logger.debug("Service body: {}", serviceBodyJson);
127 RestResponse res = sendPut(url, serviceBodyJson, user.getUserId(), acceptHeaderData);
128 if (res.getErrorCode() == STATUS_CODE_CREATED) {
129 service.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
130 service.setVersion(ResponseParser.getVersionFromResponse(res));
136 public static RestResponse getService(String serviceId) throws IOException {
138 Config config = Utils.getConfig();
139 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
140 return getServiceFromUrl(url, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), false);
143 public static RestResponse getService(ServiceReqDetails serviceReqDetails, User sdncModifierDetails)
146 Config config = Utils.getConfig();
147 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
148 serviceReqDetails.getUniqueId());
149 return getServiceFromUrl(url, sdncModifierDetails, false);
152 public static RestResponse getService(String serviceId, User sdncModifierDetails) throws IOException {
154 Config config = Utils.getConfig();
155 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
156 return getServiceFromUrl(url, sdncModifierDetails, false);
159 public static RestResponse getServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
160 String serviceVersion) throws IOException {
161 Config config = Utils.getConfig();
162 String url = String.format(Urls.GET_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
163 config.getCatalogBePort(), serviceName, serviceVersion);
164 return getServiceFromUrl(url, sdncModifierDetails, false);
167 public static RestResponse getServiceFromUrl(String url, User sdncModifierDetails, boolean isCached)
169 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, isCached);
170 HttpRequest http = new HttpRequest();
171 logger.debug("Send GET request to create service: {}", url);
172 logger.debug("Service headers: {}", headersMap);
173 RestResponse sendGetServerRequest = http.httpSendGet(url, headersMap);
175 return sendGetServerRequest;
178 public static Map<String, String> prepareHeadersMap(User sdncModifierDetails, boolean isCached) {
179 Map<String, String> headersMap = new HashMap<String, String>();
181 headersMap.put(HttpHeaderEnum.CACHE_CONTROL.getValue(), cacheControl);
183 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
184 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
185 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
189 public static RestResponse approveServiceDistribution(String serviceId, String userId) throws Exception {
190 return changeServiceDistributionState(serviceId, userId, Urls.APPROVE_DISTRIBUTION);
193 public static RestResponse rejectServiceDistribution(String serviceId, String userId) throws Exception {
194 return changeServiceDistributionState(serviceId, userId, Urls.REJECT_DISTRIBUTION);
198 public static RestResponse rejectServiceDistribution(String serviceId, String userId, String comment)
200 Config config = Utils.getConfig();
201 String url = String.format(Urls.REJECT_DISTRIBUTION, config.getCatalogBeHost(), config.getCatalogBePort(),
203 String userBodyJson = gson.toJson(comment);
204 return sendPost(url, userBodyJson, userId, acceptHeaderData);
208 private static RestResponse changeServiceDistributionState(String serviceId, String userId, String distributionUrl)
210 Config config = Utils.getConfig();
211 String url = String.format(distributionUrl, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
212 String defComment = "{ userRemarks : \"this is an test\" }";
213 String userBodyJson = gson.toJson(defComment);
214 return sendPost(url, userBodyJson, userId, acceptHeaderData);
218 public static RestResponse getServiceLatestVersionList(User sdncModifierDetails) throws IOException {
220 Config config = Utils.getConfig();
221 String url = String.format(Urls.GET_SERVICE_lATEST_VERSION, config.getCatalogBeHost(),
222 config.getCatalogBePort());
224 return sendGet(url, sdncModifierDetails.getUserId());
228 public static RestResponse createServiceByHttpMethod(ServiceReqDetails serviceDetails, User sdncModifierDetails,
229 String method, String urls) throws IOException {
230 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
232 Config config = Utils.getConfig();
233 String serviceBodyJson = gson.toJson(serviceDetails);
234 HttpRequest http = new HttpRequest();
235 String url = String.format(urls, config.getCatalogBeHost(), config.getCatalogBePort());
236 // TODO: ADD AUTHENTICATION IN REQUEST
238 logger.debug("Send {} request to create user: {}",method,url);
239 logger.debug("User body: {}", serviceBodyJson);
240 logger.debug("User headers: {}", headersMap);
241 RestResponse sendCreateUserRequest = http.httpSendByMethod(url, method, serviceBodyJson, headersMap);
243 return sendCreateUserRequest;
247 public static RestResponse deleteServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
248 String serviceVersion) throws IOException {
249 Config config = Utils.getConfig();
251 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
253 HttpRequest http = new HttpRequest();
255 String url = String.format(Urls.DELETE_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
256 config.getCatalogBePort(), serviceName, serviceVersion);
257 RestResponse deleteResponse = http.httpSendDelete(url, headersMap);
259 return deleteResponse;
262 public static RestResponse getFollowed(User user) throws Exception {
263 Config config = Utils.getConfig();
265 HttpRequest httpRequest = new HttpRequest();
267 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
269 Map<String, String> headersMap = new HashMap<String, String>();
270 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
271 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
272 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), user.getUserId());
274 RestResponse getResourceNotAbstarctResponse = httpRequest.httpSendGet(url, headersMap);
276 return getResourceNotAbstarctResponse;
279 public static JSONArray getListArrayFromRestResponse(RestResponse restResponse) {
280 String json = restResponse.getResponse();
281 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
282 JSONArray servicesArray = (JSONArray) jsonResp.get("services");
284 logger.debug("services= {}", servicesArray);
286 return servicesArray;
289 public static RestResponse getDistributionServiceList(Service service, User user) throws IOException {
291 Config config = Utils.getConfig();
292 String url = String.format(Urls.DISTRIBUTION_SERVICE_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), service.getUUID());
293 return getServiceFromUrl(url, user, false);