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 deleteServiceById(String serviceId, 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);
74 deleteMarkedServices(userId);
78 public static void deleteMarkedServices(String userId) throws IOException {
80 Config config = Utils.getConfig();
81 url = String.format(Urls.DELETE_MARKED_SERVICES, config.getCatalogBeHost(), config.getCatalogBePort());
82 sendDelete(url, userId);
85 public static RestResponse createService(ServiceReqDetails service, User user) throws Exception {
86 Config config = Utils.getConfig();
87 String url = String.format(Urls.CREATE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort());
88 String serviceBodyJson = gson.toJson(service);
90 logger.debug("Send POST request to create service: {}",url);
91 logger.debug("Service body: {}",serviceBodyJson);
93 RestResponse res = sendPost(url, serviceBodyJson, user.getUserId(), acceptHeaderData);
94 if (res.getErrorCode() == STATUS_CODE_CREATED) {
95 service.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
96 service.setVersion(ResponseParser.getVersionFromResponse(res));
97 service.setUUID(ResponseParser.getUuidFromResponse(res));
98 // Creator details never change after component is created - Ella,
100 service.setCreatorUserId(user.getUserId());
101 service.setCreatorFullName(user.getFullName());
107 public static RestResponse updateService(ServiceReqDetails service, User user) throws Exception {
108 Config config = Utils.getConfig();
109 String url = String.format(Urls.UPDATE_SERVICE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
110 service.getUniqueId());
111 String serviceBodyJson = gson.toJson(service);
113 logger.debug("Send PUT request to create service: {}",url);
114 logger.debug("Service body: {}",serviceBodyJson);
116 RestResponse res = sendPut(url, serviceBodyJson, user.getUserId(), acceptHeaderData);
117 if (res.getErrorCode() == STATUS_CODE_CREATED) {
118 service.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
119 service.setVersion(ResponseParser.getVersionFromResponse(res));
125 public static RestResponse getService(String serviceId) throws IOException {
127 Config config = Utils.getConfig();
128 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
129 return getServiceFromUrl(url, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), false);
132 public static RestResponse getService(ServiceReqDetails serviceReqDetails, User sdncModifierDetails)
135 Config config = Utils.getConfig();
136 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
137 serviceReqDetails.getUniqueId());
138 return getServiceFromUrl(url, sdncModifierDetails, false);
141 public static RestResponse getService(String serviceId, User sdncModifierDetails) throws IOException {
143 Config config = Utils.getConfig();
144 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
145 return getServiceFromUrl(url, sdncModifierDetails, false);
148 public static RestResponse getServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
149 String serviceVersion) throws IOException {
150 Config config = Utils.getConfig();
151 String url = String.format(Urls.GET_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
152 config.getCatalogBePort(), serviceName, serviceVersion);
153 return getServiceFromUrl(url, sdncModifierDetails, false);
156 public static RestResponse getServiceFromUrl(String url, User sdncModifierDetails, boolean isCached)
158 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, isCached);
159 HttpRequest http = new HttpRequest();
160 logger.debug("Send GET request to create service: {}",url);
161 logger.debug("Service headers: {}",headersMap);
162 RestResponse sendGetServerRequest = http.httpSendGet(url, headersMap);
164 return sendGetServerRequest;
167 public static Map<String, String> prepareHeadersMap(User sdncModifierDetails, boolean isCached) {
168 Map<String, String> headersMap = new HashMap<String, String>();
170 headersMap.put(HttpHeaderEnum.CACHE_CONTROL.getValue(), cacheControl);
172 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
173 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
174 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
178 public static RestResponse approveServiceDistribution(String serviceId, String userId) throws Exception {
179 return changeServiceDistributionState(serviceId, userId, Urls.APPROVE_DISTRIBUTION);
182 public static RestResponse rejectServiceDistribution(String serviceId, String userId) throws Exception {
183 return changeServiceDistributionState(serviceId, userId, Urls.REJECT_DISTRIBUTION);
187 public static RestResponse rejectServiceDistribution(String serviceId, String userId, String comment)
189 Config config = Utils.getConfig();
190 String url = String.format(Urls.REJECT_DISTRIBUTION, config.getCatalogBeHost(), config.getCatalogBePort(),
192 String userBodyJson = gson.toJson(comment);
193 return sendPost(url, userBodyJson, userId, acceptHeaderData);
197 private static RestResponse changeServiceDistributionState(String serviceId, String userId, String distributionUrl)
199 Config config = Utils.getConfig();
200 String url = String.format(distributionUrl, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
201 String defComment = "{ userRemarks : \"this is an test\" }";
202 String userBodyJson = gson.toJson(defComment);
203 return sendPost(url, userBodyJson, userId, acceptHeaderData);
207 public static RestResponse getServiceLatestVersionList(User sdncModifierDetails) throws IOException {
209 Config config = Utils.getConfig();
210 String url = String.format(Urls.GET_SERVICE_lATEST_VERSION, config.getCatalogBeHost(),
211 config.getCatalogBePort());
213 return sendGet(url, sdncModifierDetails.getUserId());
217 public static RestResponse createServiceByHttpMethod(ServiceReqDetails serviceDetails, User sdncModifierDetails,
218 String method, String urls) throws IOException {
219 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
221 Config config = Utils.getConfig();
222 String serviceBodyJson = gson.toJson(serviceDetails);
223 HttpRequest http = new HttpRequest();
224 String url = String.format(urls, config.getCatalogBeHost(), config.getCatalogBePort());
225 // TODO: ADD AUTHENTICATION IN REQUEST
227 logger.debug("Send {} request to create user: {}",method,url);
228 logger.debug("User body: {}",serviceBodyJson);
229 logger.debug("User headers: {}",headersMap);
230 RestResponse sendCreateUserRequest = http.httpSendByMethod(url, method, serviceBodyJson, headersMap);
232 return sendCreateUserRequest;
236 public static RestResponse deleteServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
237 String serviceVersion) throws IOException {
238 Config config = Utils.getConfig();
240 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
242 HttpRequest http = new HttpRequest();
244 String url = String.format(Urls.DELETE_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
245 config.getCatalogBePort(), serviceName, serviceVersion);
246 RestResponse deleteResponse = http.httpSendDelete(url, headersMap);
248 return deleteResponse;
251 public static RestResponse getFollowed(User user) throws Exception {
252 Config config = Utils.getConfig();
254 HttpRequest httpRequest = new HttpRequest();
256 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
258 Map<String, String> headersMap = new HashMap<String, String>();
259 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
260 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
261 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), user.getUserId());
263 RestResponse getResourceNotAbstarctResponse = httpRequest.httpSendGet(url, headersMap);
265 return getResourceNotAbstarctResponse;
268 public static JSONArray getListArrayFromRestResponse(RestResponse restResponse) {
269 String json = restResponse.getResponse();
270 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
271 JSONArray servicesArray = (JSONArray) jsonResp.get("services");
273 logger.debug("services= {}",servicesArray);
275 return servicesArray;
278 public static RestResponse getDistributionServiceList(Service service, User user) throws IOException {
280 Config config = Utils.getConfig();
281 String url = String.format(Urls.DISTRIBUTION_SERVICE_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), service.getUUID());
282 return getServiceFromUrl(url, user, false);