Sync Integ to Master
[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         // ****** CREATE *******
49
50         private static Gson gson = new Gson();
51
52         public static RestResponse deleteService(String serviceName, String version, User sdncModifierDetails)
53                         throws IOException {
54
55                 Config config = Utils.getConfig();
56                 String url = String.format(Urls.DELETE_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
57                                 config.getCatalogBePort(), serviceName, version);
58
59                 String userId = sdncModifierDetails.getUserId();
60                 RestResponse sendDelete = sendDelete(url, userId);
61                 deleteMarkedServices(userId);
62                 return sendDelete;
63         }
64         
65         public static RestResponse markServiceToDelete(String resourceId, String userId) throws IOException {
66
67                 Config config = Utils.getConfig();
68                 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), resourceId);
69                 RestResponse sendDelete = sendDelete(url, userId);
70
71                 return sendDelete;
72
73         }
74
75         public static RestResponse deleteServiceById(String serviceId, String userId) throws IOException {
76
77                 Config config = Utils.getConfig();
78                 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
79                 RestResponse sendDelete = sendDelete(url, userId);
80                 deleteMarkedServices(userId);
81                 return sendDelete;
82         }
83
84         public static void deleteMarkedServices(String userId) throws IOException {
85                 String url;
86                 Config config = Utils.getConfig();
87                 url = String.format(Urls.DELETE_MARKED_SERVICES, config.getCatalogBeHost(), config.getCatalogBePort());
88                 sendDelete(url, userId);
89         }
90
91         public static RestResponse createService(ServiceReqDetails service, User user) throws Exception {
92
93                 Config config = Utils.getConfig();
94                 String url = String.format(Urls.CREATE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort());
95                 String serviceBodyJson = gson.toJson(service);
96
97                 logger.debug("Send POST request to create service: {}", url);
98                 logger.debug("Service body: {}", serviceBodyJson);
99
100                 RestResponse res = sendPost(url, serviceBodyJson, user.getUserId(), acceptHeaderData);
101                 if (res.getErrorCode() == STATUS_CODE_CREATED) {
102                         service.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
103                         service.setVersion(ResponseParser.getVersionFromResponse(res));
104                         service.setUUID(ResponseParser.getUuidFromResponse(res));
105                         // Creator details never change after component is created - Ella,
106                         // 12/1/2016
107                         service.setCreatorUserId(user.getUserId());
108                         service.setCreatorFullName(user.getFullName());
109                 }
110
111                 return res;
112         }
113
114         public static RestResponse updateService(ServiceReqDetails service, User user) throws Exception {
115                 Config config = Utils.getConfig();
116                 String url = String.format(Urls.UPDATE_SERVICE_METADATA, config.getCatalogBeHost(), config.getCatalogBePort(),
117                                 service.getUniqueId());
118                 String serviceBodyJson = gson.toJson(service);
119
120                 logger.debug("Send PUT request to create service: {}", url);
121                 logger.debug("Service body: {}", serviceBodyJson);
122
123                 RestResponse res = sendPut(url, serviceBodyJson, user.getUserId(), acceptHeaderData);
124                 if (res.getErrorCode() == STATUS_CODE_CREATED) {
125                         service.setUniqueId(ResponseParser.getUniqueIdFromResponse(res));
126                         service.setVersion(ResponseParser.getVersionFromResponse(res));
127                 }
128
129                 return res;
130         }
131
132         public static RestResponse getService(String serviceId) throws IOException {
133
134                 Config config = Utils.getConfig();
135                 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
136                 return getServiceFromUrl(url, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER), false);
137         }
138
139         public static RestResponse getService(ServiceReqDetails serviceReqDetails, User sdncModifierDetails)
140                         throws IOException {
141
142                 Config config = Utils.getConfig();
143                 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
144                                 serviceReqDetails.getUniqueId());
145                 return getServiceFromUrl(url, sdncModifierDetails, false);
146         }
147
148         public static RestResponse getService(String serviceId, User sdncModifierDetails) throws IOException {
149
150                 Config config = Utils.getConfig();
151                 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
152                 return getServiceFromUrl(url, sdncModifierDetails, false);
153         }
154
155         public static RestResponse getServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
156                         String serviceVersion) throws IOException {
157                 Config config = Utils.getConfig();
158                 String url = String.format(Urls.GET_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
159                                 config.getCatalogBePort(), serviceName, serviceVersion);
160                 return getServiceFromUrl(url, sdncModifierDetails, false);
161         }
162
163         public static RestResponse getServiceFromUrl(String url, User sdncModifierDetails, boolean isCached)
164                         throws IOException {
165                 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, isCached);
166                 HttpRequest http = new HttpRequest();
167                 logger.debug("Send GET request to create service: {}", url);
168                 logger.debug("Service headers: {}", headersMap);
169                 RestResponse sendGetServerRequest = http.httpSendGet(url, headersMap);
170
171                 return sendGetServerRequest;
172         }
173
174         public static Map<String, String> prepareHeadersMap(User sdncModifierDetails, boolean isCached) {
175                 Map<String, String> headersMap = new HashMap<>();
176                 if (isCached)
177                         headersMap.put(HttpHeaderEnum.CACHE_CONTROL.getValue(), BaseRestUtils.cacheControlHeader);
178
179                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
180                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
181                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
182                 return headersMap;
183         }
184
185         public static RestResponse approveServiceDistribution(String serviceId, String userId) throws Exception {
186                 return changeServiceDistributionState(serviceId, userId, Urls.APPROVE_DISTRIBUTION);
187         }
188
189         public static RestResponse rejectServiceDistribution(String serviceId, String userId) throws Exception {
190                 return changeServiceDistributionState(serviceId, userId, Urls.REJECT_DISTRIBUTION);
191         }
192
193         // Benny
194         public static RestResponse rejectServiceDistribution(String serviceId, String userId, String comment)
195                         throws Exception {
196                 Config config = Utils.getConfig();
197                 String url = String.format(Urls.REJECT_DISTRIBUTION, config.getCatalogBeHost(), config.getCatalogBePort(),
198                                 serviceId);
199                 String userBodyJson = gson.toJson(comment);
200                 return sendPost(url, userBodyJson, userId, BaseRestUtils.acceptHeaderData);
201
202         }
203
204         private static RestResponse changeServiceDistributionState(String serviceId, String userId, String distributionUrl)
205                         throws Exception {
206                 Config config = Utils.getConfig();
207                 String url = String.format(distributionUrl, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
208                 String defComment = "{ userRemarks : \"this is an test\" }";
209                 String userBodyJson = gson.toJson(defComment);
210                 return sendPost(url, userBodyJson, userId, acceptHeaderData);
211
212         }
213
214         public static RestResponse getServiceLatestVersionList(User sdncModifierDetails) throws IOException {
215
216                 Config config = Utils.getConfig();
217                 String url = String.format(Urls.GET_SERVICE_lATEST_VERSION, config.getCatalogBeHost(),
218                                 config.getCatalogBePort());
219
220                 return sendGet(url, sdncModifierDetails.getUserId());
221
222         }
223
224         public static RestResponse createServiceByHttpMethod(ServiceReqDetails serviceDetails, User sdncModifierDetails,
225                         String method, String urls) throws IOException {
226                 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
227
228                 Config config = Utils.getConfig();
229                 String serviceBodyJson = gson.toJson(serviceDetails);
230                 HttpRequest http = new HttpRequest();
231                 String url = String.format(urls, config.getCatalogBeHost(), config.getCatalogBePort());
232                 // TODO: ADD AUTHENTICATION IN REQUEST
233                 logger.debug(url);
234                 logger.debug("Send {} request to create user: {}",method,url);
235                 logger.debug("User body: {}", serviceBodyJson);
236                 logger.debug("User headers: {}", headersMap);
237                 RestResponse sendCreateUserRequest = http.httpSendByMethod(url, method, serviceBodyJson, headersMap);
238
239                 return sendCreateUserRequest;
240
241         }
242
243         public static RestResponse deleteServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
244                         String serviceVersion) throws IOException {
245                 Config config = Utils.getConfig();
246
247                 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
248
249                 HttpRequest http = new HttpRequest();
250
251                 String url = String.format(Urls.DELETE_SERVICE_BY_NAME_AND_VERSION, config.getCatalogBeHost(),
252                                 config.getCatalogBePort(), serviceName, serviceVersion);
253                 RestResponse deleteResponse = http.httpSendDelete(url, headersMap);
254
255                 deleteMarkedServices(sdncModifierDetails.getUserId());
256                 return deleteResponse;
257         }
258
259         public static RestResponse getFollowed(User user) throws Exception {
260                 Config config = Utils.getConfig();
261
262                 HttpRequest httpRequest = new HttpRequest();
263
264                 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
265
266                 Map<String, String> headersMap = new HashMap<>();
267                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), BaseRestUtils.contentTypeHeaderData);
268                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), BaseRestUtils.acceptHeaderData);
269                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), user.getUserId());
270
271                 RestResponse getResourceNotAbstarctResponse = httpRequest.httpSendGet(url, headersMap);
272
273                 return getResourceNotAbstarctResponse;
274         }
275
276         public static JSONArray getListArrayFromRestResponse(RestResponse restResponse) {
277                 String json = restResponse.getResponse();
278                 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
279                 JSONArray servicesArray = (JSONArray) jsonResp.get("services");
280
281                 logger.debug("services= {}", servicesArray);
282
283                 return servicesArray;
284         }
285         
286         public static RestResponse getDistributionServiceList(Service service, User user) throws IOException {
287
288                 Config config = Utils.getConfig();
289                 String url = String.format(Urls.DISTRIBUTION_SERVICE_LIST, config.getCatalogBeHost(), config.getCatalogBePort(), service.getUUID());
290                 return getServiceFromUrl(url, user, false);
291         }
292
293 }