re base code
[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 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;
40
41 import java.io.IOException;
42 import java.util.HashMap;
43 import java.util.Map;
44
45 public class ServiceRestUtils extends BaseRestUtils {
46         private static Logger logger = LoggerFactory.getLogger(ServiceRestUtils.class.getName());
47         // ****** CREATE *******
48
49         private static Gson gson = new Gson();
50
51         public static RestResponse deleteService(String serviceName, String version, User sdncModifierDetails)
52                         throws IOException {
53
54                 Config config = Utils.getConfig();
55                 String url = String.format(Urls.DELETE_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
56                                 config.getCatalogBePort(), serviceName, version);
57
58                 String userId = sdncModifierDetails.getUserId();
59                 RestResponse sendDelete = sendDelete(url, userId);
60                 deleteMarkedServices(userId);
61                 return sendDelete;
62         }
63         
64         public static RestResponse markServiceToDelete(String resourceId, String userId) throws IOException {
65
66                 Config config = Utils.getConfig();
67                 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId);
68                 RestResponse sendDelete = sendDelete(url, userId);
69
70                 return sendDelete;
71
72         }
73
74         public static RestResponse deleteServiceById(String serviceId, String userId) throws IOException {
75
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);
80                 return sendDelete;
81         }
82
83         public static void deleteMarkedServices(String userId) throws IOException {
84                 String url;
85                 Config config = Utils.getConfig();
86                 url = String.format(Urls.DELETE_MARKED_SERVICES, config.getCatalogBeHost(), config.getCatalogBePort());
87                 sendDelete(url, userId);
88         }
89
90         public static RestResponse createService(ServiceReqDetails service, User user) throws Exception {
91
92                 Config config = Utils.getConfig();
93                 String url = String.format(Urls.CREATE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort());
94                 String serviceBodyJson = gson.toJson(service);
95
96                 logger.debug("Send POST request to create service: {}", url);
97                 logger.debug("Service body: {}", serviceBodyJson);
98
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,
105                         // 12/1/2016
106                         service.setCreatorUserId(user.getUserId());
107                         service.setCreatorFullName(user.getFullName());
108                 }
109
110                 return res;
111         }
112
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);
118
119                 logger.debug("Send PUT request to create service: {}", url);
120                 logger.debug("Service body: {}", serviceBodyJson);
121
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));
126                 }
127
128                 return res;
129         }
130
131         public static RestResponse getService(String serviceId) throws IOException {
132
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);
136         }
137
138         public static RestResponse getService(ServiceReqDetails serviceReqDetails, User sdncModifierDetails)
139                         throws IOException {
140
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);
145         }
146
147         public static RestResponse getService(String serviceId, User sdncModifierDetails) throws IOException {
148
149                 Config config = Utils.getConfig();
150                 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
151                 return getServiceFromUrl(url, sdncModifierDetails, false);
152         }
153
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);
160         }
161
162         public static RestResponse getServiceFromUrl(String url, User sdncModifierDetails, boolean isCached)
163                         throws IOException {
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);
169
170                 return sendGetServerRequest;
171         }
172
173         public static Map<String, String> prepareHeadersMap(User sdncModifierDetails, boolean isCached) {
174                 Map<String, String> headersMap = new HashMap<>();
175                 if (isCached)
176                         headersMap.put(HttpHeaderEnum.CACHE_CONTROL.getValue(), BaseRestUtils.cacheControlHeader);
177
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());
181                 return headersMap;
182         }
183
184         public static RestResponse approveServiceDistribution(String serviceId, String userId) throws Exception {
185                 return changeServiceDistributionState(serviceId, userId, Urls.APPROVE_DISTRIBUTION);
186         }
187
188         public static RestResponse rejectServiceDistribution(String serviceId, String userId) throws Exception {
189                 return changeServiceDistributionState(serviceId, userId, Urls.REJECT_DISTRIBUTION);
190         }
191
192         // Benny
193         public static RestResponse rejectServiceDistribution(String serviceId, String userId, String comment)
194                         throws Exception {
195                 Config config = Utils.getConfig();
196                 String url = String.format(Urls.REJECT_DISTRIBUTION, config.getCatalogBeHost(), config.getCatalogBePort(),
197                                 serviceId);
198                 String userBodyJson = gson.toJson(comment);
199                 return sendPost(url, userBodyJson, userId, BaseRestUtils.acceptHeaderData);
200
201         }
202
203         private static RestResponse changeServiceDistributionState(String serviceId, String userId, String distributionUrl)
204                         throws Exception {
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);
210
211         }
212
213         public static RestResponse getServiceLatestVersionList(User sdncModifierDetails) throws IOException {
214
215                 Config config = Utils.getConfig();
216                 String url = String.format(Urls.GET_SERVICE_lATEST_VERSION, config.getCatalogBeHost(),
217                                 config.getCatalogBePort());
218
219                 return sendGet(url, sdncModifierDetails.getUserId());
220
221         }
222
223         public static RestResponse createServiceByHttpMethod(ServiceReqDetails serviceDetails, User sdncModifierDetails,
224                         String method, String urls) throws IOException {
225                 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
226
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
232                 logger.debug(url);
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);
237
238                 return sendCreateUserRequest;
239
240         }
241
242         public static RestResponse deleteServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
243                         String serviceVersion) throws IOException {
244                 Config config = Utils.getConfig();
245
246                 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
247
248                 HttpRequest http = new HttpRequest();
249
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);
253
254                 deleteMarkedServices(sdncModifierDetails.getUserId());
255                 return deleteResponse;
256         }
257
258         public static RestResponse getFollowed(User user) throws Exception {
259                 Config config = Utils.getConfig();
260
261                 HttpRequest httpRequest = new HttpRequest();
262
263                 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
264
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());
269
270                 RestResponse getResourceNotAbstarctResponse = httpRequest.httpSendGet(url, headersMap);
271
272                 return getResourceNotAbstarctResponse;
273         }
274
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");
279
280                 logger.debug("services= {}", servicesArray);
281
282                 return servicesArray;
283         }
284         
285         public static RestResponse getDistributionServiceList(Service service, User user) throws IOException {
286
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);
290         }
291
292 }