Fix for radio buttons
[sdc.git] / asdc-tests / 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 deleteServiceById(String serviceId, String userId) throws IOException {
69
70                 Config config = Utils.getConfig();
71                 String url = String.format(Urls.DELETE_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(),
72                                 serviceId);
73                 RestResponse sendDelete = sendDelete(url, userId);
74                 deleteMarkedServices(userId);
75                 return sendDelete;
76         }
77
78         public static void deleteMarkedServices(String userId) throws IOException {
79                 String url;
80                 Config config = Utils.getConfig();
81                 url = String.format(Urls.DELETE_MARKED_SERVICES, config.getCatalogBeHost(), config.getCatalogBePort());
82                 sendDelete(url, userId);
83         }
84
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);
89
90                 logger.debug("Send POST request to create service: {}",url);
91                 logger.debug("Service body: {}",serviceBodyJson);
92
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,
99                         // 12/1/2016
100                         service.setCreatorUserId(user.getUserId());
101                         service.setCreatorFullName(user.getFullName());
102                 }
103
104                 return res;
105         }
106
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);
112
113                 logger.debug("Send PUT request to create service: {}",url);
114                 logger.debug("Service body: {}",serviceBodyJson);
115
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));
120                 }
121
122                 return res;
123         }
124
125         public static RestResponse getService(String serviceId) throws IOException {
126
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);
130         }
131
132         public static RestResponse getService(ServiceReqDetails serviceReqDetails, User sdncModifierDetails)
133                         throws IOException {
134
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);
139         }
140
141         public static RestResponse getService(String serviceId, User sdncModifierDetails) throws IOException {
142
143                 Config config = Utils.getConfig();
144                 String url = String.format(Urls.GET_SERVICE, config.getCatalogBeHost(), config.getCatalogBePort(), serviceId);
145                 return getServiceFromUrl(url, sdncModifierDetails, false);
146         }
147
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);
154         }
155
156         public static RestResponse getServiceFromUrl(String url, User sdncModifierDetails, boolean isCached)
157                         throws IOException {
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);
163
164                 return sendGetServerRequest;
165         }
166
167         public static Map<String, String> prepareHeadersMap(User sdncModifierDetails, boolean isCached) {
168                 Map<String, String> headersMap = new HashMap<String, String>();
169                 if (isCached)
170                         headersMap.put(HttpHeaderEnum.CACHE_CONTROL.getValue(), cacheControl);
171
172                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
173                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
174                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
175                 return headersMap;
176         }
177
178         public static RestResponse approveServiceDistribution(String serviceId, String userId) throws Exception {
179                 return changeServiceDistributionState(serviceId, userId, Urls.APPROVE_DISTRIBUTION);
180         }
181
182         public static RestResponse rejectServiceDistribution(String serviceId, String userId) throws Exception {
183                 return changeServiceDistributionState(serviceId, userId, Urls.REJECT_DISTRIBUTION);
184         }
185
186         // Benny
187         public static RestResponse rejectServiceDistribution(String serviceId, String userId, String comment)
188                         throws Exception {
189                 Config config = Utils.getConfig();
190                 String url = String.format(Urls.REJECT_DISTRIBUTION, config.getCatalogBeHost(), config.getCatalogBePort(),
191                                 serviceId);
192                 String userBodyJson = gson.toJson(comment);
193                 return sendPost(url, userBodyJson, userId, acceptHeaderData);
194
195         }
196
197         private static RestResponse changeServiceDistributionState(String serviceId, String userId, String distributionUrl)
198                         throws Exception {
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);
204
205         }
206
207         public static RestResponse getServiceLatestVersionList(User sdncModifierDetails) throws IOException {
208
209                 Config config = Utils.getConfig();
210                 String url = String.format(Urls.GET_SERVICE_lATEST_VERSION, config.getCatalogBeHost(),
211                                 config.getCatalogBePort());
212
213                 return sendGet(url, sdncModifierDetails.getUserId());
214
215         }
216
217         public static RestResponse createServiceByHttpMethod(ServiceReqDetails serviceDetails, User sdncModifierDetails,
218                         String method, String urls) throws IOException {
219                 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
220
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
226                 logger.debug(url);
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);
231
232                 return sendCreateUserRequest;
233
234         }
235
236         public static RestResponse deleteServiceByNameAndVersion(User sdncModifierDetails, String serviceName,
237                         String serviceVersion) throws IOException {
238                 Config config = Utils.getConfig();
239
240                 Map<String, String> headersMap = prepareHeadersMap(sdncModifierDetails, true);
241
242                 HttpRequest http = new HttpRequest();
243
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);
247
248                 return deleteResponse;
249         }
250
251         public static RestResponse getFollowed(User user) throws Exception {
252                 Config config = Utils.getConfig();
253
254                 HttpRequest httpRequest = new HttpRequest();
255
256                 String url = String.format(Urls.GET_FOLLWED_LIST, config.getCatalogBeHost(), config.getCatalogBePort());
257
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());
262
263                 RestResponse getResourceNotAbstarctResponse = httpRequest.httpSendGet(url, headersMap);
264
265                 return getResourceNotAbstarctResponse;
266         }
267
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");
272
273                 logger.debug("services= {}",servicesArray);
274
275                 return servicesArray;
276         }
277         
278         public static RestResponse getDistributionServiceList(Service service, User user) throws IOException {
279
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);
283         }
284
285 }