[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / rest / ServiceRestUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.utils.rest;
22
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Map;
26
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;
43
44 import com.google.gson.Gson;
45
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 *******
52
53         private static Gson gson = new Gson();
54
55         public static RestResponse deleteService(String serviceName, String version, User sdncModifierDetails)
56                         throws IOException {
57
58                 Config config = Utils.getConfig();
59                 String url = String.format(Urls.DELETE_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
60                                 config.getCatalogBePort(), serviceName, version);
61
62                 String userId = sdncModifierDetails.getUserId();
63                 RestResponse sendDelete = sendDelete(url, userId);
64                 deleteMarkedServices(userId);
65                 return sendDelete;
66         }
67         
68         public static RestResponse markServiceToDelete(String resourceId, String userId) throws IOException {
69
70                 Config config = Utils.getConfig();
71                 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
72                                 resourceId);
73                 RestResponse sendDelete = sendDelete(url, userId);
74
75                 return sendDelete;
76
77         }
78
79         public static RestResponse deleteServiceById(String serviceId, String userId) throws IOException {
80
81                 Config config = Utils.getConfig();
82                 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
83                                 serviceId);
84                 RestResponse sendDelete = sendDelete(url, userId);
85                 deleteMarkedServices(userId);
86                 return sendDelete;
87         }
88
89         public static void deleteMarkedServices(String userId) throws IOException {
90                 String url;
91                 Config config = Utils.getConfig();
92                 url = String.format(Urls.DELETE_MARKED_SERVICES, config.getCatalogBeHost(), config.getCatalogBePort());
93                 sendDelete(url, userId);
94         }
95
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);
100
101                 logger.debug("Send POST request to create service: {}", url);
102                 logger.debug("Service body: {}", serviceBodyJson);
103
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,
110                         // 12/1/2016
111                         service.setCreatorUserId(user.getUserId());
112                         service.setCreatorFullName(user.getFullName());
113                 }
114
115                 return res;
116         }
117
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);
123
124                 logger.debug("Send PUT request to create service: {}", url);
125                 logger.debug("Service body: {}", serviceBodyJson);
126
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));
131                 }
132
133                 return res;
134         }
135
136         public static RestResponse getService(String serviceId) throws IOException {
137
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);
141         }
142
143         public static RestResponse getService(ServiceReqDetails serviceReqDetails, User sdncModifierDetails)
144                         throws IOException {
145
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);
150         }
151
152         public static RestResponse getService(String serviceId, User sdncModifierDetails) throws IOException {
153
154                 Config config = Utils.getConfig();
155                 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
156                 return getServiceFromUrl(url, sdncModifierDetails, false);
157         }
158
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);
165         }
166
167         public static RestResponse getServiceFromUrl(String url, User sdncModifierDetails, boolean isCached)
168                         throws IOException {
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);
174
175                 return sendGetServerRequest;
176         }
177
178         public static Map<String, String> prepareHeadersMap(User sdncModifierDetails, boolean isCached) {
179                 Map<String, String> headersMap = new HashMap<String, String>();
180                 if (isCached)
181                         headersMap.put(HttpHeaderEnum.CACHE_CONTROL.getValue(), cacheControl);
182
183                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
184                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
185                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
186                 return headersMap;
187         }
188
189         public static RestResponse approveServiceDistribution(String serviceId, String userId) throws Exception {
190                 return changeServiceDistributionState(serviceId, userId, Urls.APPROVE_DISTRIBUTION);
191         }
192
193         public static RestResponse rejectServiceDistribution(String serviceId, String userId) throws Exception {
194                 return changeServiceDistributionState(serviceId, userId, Urls.REJECT_DISTRIBUTION);
195         }
196
197         // Benny
198         public static RestResponse rejectServiceDistribution(String serviceId, String userId, String comment)
199                         throws Exception {
200                 Config config = Utils.getConfig();
201                 String url = String.format(Urls.REJECT_DISTRIBUTION, config.getCatalogBeHost(), config.getCatalogBePort(),
202                                 serviceId);
203                 String userBodyJson = gson.toJson(comment);
204                 return sendPost(url, userBodyJson, userId, acceptHeaderData);
205
206         }
207
208         private static RestResponse changeServiceDistributionState(String serviceId, String userId, String distributionUrl)
209                         throws Exception {
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);
215
216         }
217
218         public static RestResponse getServiceLatestVersionList(User sdncModifierDetails) throws IOException {
219
220                 Config config = Utils.getConfig();
221                 String url = String.format(Urls.GET_SERVICE_lATEST_VERSION, config.getCatalogBeHost(),
222                                 config.getCatalogBePort());
223
224                 return sendGet(url, sdncModifierDetails.getUserId());
225
226         }
227
228         public static RestResponse createServiceByHttpMethod(ServiceReqDetails serviceDetails, User sdncModifierDetails,
229                         String method, String urls) throws IOException {
230                 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
231
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
237                 logger.debug(url);
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);
242
243                 return sendCreateUserRequest;
244
245         }
246
247         public static RestResponse deleteServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
248                         String serviceVersion) throws IOException {
249                 Config config = Utils.getConfig();
250
251                 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
252
253                 HttpRequest http = new HttpRequest();
254
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);
258
259                 return deleteResponse;
260         }
261
262         public static RestResponse getFollowed(User user) throws Exception {
263                 Config config = Utils.getConfig();
264
265                 HttpRequest httpRequest = new HttpRequest();
266
267                 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
268
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());
273
274                 RestResponse getResourceNotAbstarctResponse = httpRequest.httpSendGet(url, headersMap);
275
276                 return getResourceNotAbstarctResponse;
277         }
278
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");
283
284                 logger.debug("services= {}", servicesArray);
285
286                 return servicesArray;
287         }
288         
289         public static RestResponse getDistributionServiceList(Service service, User user) throws IOException {
290
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);
294         }
295
296 }