[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / rest / UserRestUtils.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.apache.commons.lang.StringUtils;
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.http.HttpHeaderEnum;
32 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
33 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
34 import org.openecomp.sdc.ci.tests.run.StartTest;
35 import org.openecomp.sdc.ci.tests.utils.Utils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import com.google.gson.Gson;
40
41 public class UserRestUtils extends BaseRestUtils {
42
43         static Gson gson = new Gson();
44
45         static Logger logger = LoggerFactory.getLogger(UserRestUtils.class.getName());
46         static String contentTypeHeaderData = "application/json";
47         static String acceptHeaderDate = "application/json";
48
49         public UserRestUtils() {
50                 super();
51
52                 StartTest.enableLogger();
53         }
54
55         public static RestResponse createUser(User sdncUserDetails, User sdncModifierDetails) throws IOException {
56
57                 Config config = Utils.getConfig();
58
59                 Map<String, String> headersMap = new HashMap<String, String>();
60                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
61                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
62                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
63
64                 String userBodyJson = gson.toJson(sdncUserDetails);
65                 HttpRequest http = new HttpRequest();
66                 String url = String.format(Urls.CREATE_USER, config.getCatalogBeHost(), config.getCatalogBePort());
67
68                 logger.debug("Send POST request to create user: {}", url);
69                 logger.debug("User body: {}", userBodyJson);
70                 logger.debug("User headers: {}", headersMap);
71                 RestResponse sendCreateUserRequest = http.httpSendPost(url, userBodyJson, headersMap);
72
73                 return sendCreateUserRequest;
74
75         }
76
77         public static RestResponse deactivateUser(User sdncUserDetails, User sdncModifierDetails) throws IOException {
78                 return deleteUser(sdncUserDetails, sdncModifierDetails, true);
79         }
80
81         public static RestResponse deActivateUser(User sdncUserDetails, User sdncModifierDetails) throws IOException {
82                 return deleteUser(sdncUserDetails, sdncModifierDetails, false);
83         }
84
85         public static RestResponse deleteUser(User sdncUserDetails, User sdncModifierDetails, boolean isForceDelete)
86                         throws IOException {
87
88                 Config config = Utils.getConfig();
89
90                 Map<String, String> headersMap = new HashMap<String, String>();
91                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
92                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
93                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
94                 if (isForceDelete) {
95                         headersMap.put(User.FORCE_DELETE_HEADER_FLAG, User.FORCE_DELETE_HEADER_FLAG);
96                 }
97
98                 HttpRequest http = new HttpRequest();
99                 String url = String.format(Urls.DELETE_USER, config.getCatalogBeHost(), config.getCatalogBePort(),
100                                 sdncUserDetails.getUserId());
101                 RestResponse sendDeleteUserRequest = http.httpSendDelete(url, headersMap);
102                 return sendDeleteUserRequest;
103
104         }
105
106         public static RestResponse updateUser(User sdncUserDetails, User sdncModifierDetails)
107                         throws IOException, CloneNotSupportedException {
108
109                 Config config = Utils.getConfig();
110                 User user = new User(sdncModifierDetails);
111
112                 Map<String, String> headersMap = new HashMap<String, String>();
113                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
114                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
115                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
116
117                 user.setUserId(StringUtils.EMPTY);
118                 user.setRole(StringUtils.EMPTY);
119
120                 Gson gson = new Gson();
121                 String userBodyJson = gson.toJson(user);
122                 logger.debug("userBodyJson: {}", userBodyJson);
123                 HttpRequest http = new HttpRequest();
124                 String url = String.format(Urls.UPDATE_USER, config.getCatalogBeHost(), config.getCatalogBePort(),
125                                 sdncModifierDetails.getUserId());
126                 RestResponse sendUpdateUserRequest = http.httpSendPost(url, userBodyJson, headersMap);
127
128                 return sendUpdateUserRequest;
129         }
130
131         /// Benny
132         public static RestResponse updateUserRole(User sdncUserDetails, User sdncModifierDetails, String userIdToUpdate)
133                         throws IOException {
134
135                 Config config = Utils.getConfig();
136
137                 Map<String, String> headersMap = new HashMap<String, String>();
138                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
139                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
140                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
141
142                 Gson gson = new Gson();
143                 String userBodyJson = gson.toJson(sdncUserDetails);
144                 logger.debug("userBodyJson: {}", userBodyJson);
145                 HttpRequest http = new HttpRequest();
146                 String url = String.format(Urls.UPDATE_USER_ROLE, config.getCatalogBeHost(), config.getCatalogBePort(),
147                                 userIdToUpdate);
148                 RestResponse sendUpdateUserRequest = http.httpSendPost(url, userBodyJson, headersMap);
149
150                 return sendUpdateUserRequest;
151
152         }
153
154         public static RestResponse getUser(User sdncUserDetails, User sdncModifierDetails) throws IOException {
155
156                 Config config = Utils.getConfig();
157
158                 Map<String, String> headersMap = new HashMap<String, String>();
159                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
160                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
161                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
162                 HttpRequest http = new HttpRequest();
163                 String url = String.format(Urls.GET_USER, config.getCatalogBeHost(), config.getCatalogBePort(),
164                                 sdncUserDetails.getUserId());
165                 RestResponse sendGetUserRequest = http.httpSendGet(url, headersMap);
166                 return sendGetUserRequest;
167
168         }
169         
170         public static RestResponse getUserRole(User sdncUserDetails, User sdncModifierDetails) throws IOException {
171
172                 Config config = Utils.getConfig();
173
174                 Map<String, String> headersMap = new HashMap<String, String>();
175                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
176                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
177                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
178                 HttpRequest http = new HttpRequest();
179                 String url = String.format(Urls.GET_USER_ROLE, config.getCatalogBeHost(), config.getCatalogBePort(),
180                                 sdncUserDetails.getUserId());
181                 RestResponse sendGetUserRequest = http.httpSendGet(url, headersMap);
182                 return sendGetUserRequest;
183
184         }
185         
186         
187
188         public static RestResponse getAllAdminUsers(User sdncModifierDetails) throws IOException {
189
190                 Config config = Utils.getConfig();
191
192                 Map<String, String> headersMap = new HashMap<String, String>();
193                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
194                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
195                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
196
197                 // Gson gson = new Gson();
198                 // String userBodyJson = gson.toJson(sdncModifierDetails);
199                 // System.out.println(userBodyJson);
200                 HttpRequest http = new HttpRequest();
201                 String url = String.format(Urls.GET_ALL_ADMIN_USERS, config.getCatalogBeHost(), config.getCatalogBePort());
202                 logger.debug("Send following url: {} and headers: {}",url, headersMap.toString());
203                 RestResponse sendGetUserRequest = http.httpSendGet(url, headersMap);
204
205                 return sendGetUserRequest;
206
207         }
208
209         // US571255
210         public static RestResponse getUsersByRoles(User sdncModifierDetails, String roles) throws IOException {
211
212                 Config config = Utils.getConfig();
213                 Map<String, String> headersMap = new HashMap<String, String>();
214                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
215                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
216                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
217                 HttpRequest http = new HttpRequest();
218                 String url;
219                 if (roles == "/") {
220                         url = String.format(Urls.GET_ALL_USERS, config.getCatalogBeHost(), config.getCatalogBePort());
221                 } else {
222                         url = String.format(Urls.GET_USERS_BY_ROLES, config.getCatalogBeHost(), config.getCatalogBePort(), roles);
223
224                 }
225                 logger.debug("Send following url: {} and headers: {}",url, headersMap.toString());
226                 RestResponse sendGetUserRequest = http.httpSendGet(url, headersMap);
227                 return sendGetUserRequest;
228         }
229
230         public static RestResponse getUsersByRolesHttpCspAtuUidIsMissing(User sdncModifierDetails, String roles)
231                         throws Exception {
232
233                 Config config = Utils.getConfig();
234                 Map<String, String> headersMap = new HashMap<String, String>();
235                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
236                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
237                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncModifierDetails.getUserId());
238                 headersMap.remove("USER_ID");
239                 HttpRequest http = new HttpRequest();
240                 String url = String.format(Urls.GET_USERS_BY_ROLES, config.getCatalogBeHost(), config.getCatalogBePort(),
241                                 roles);
242                 logger.debug(
243                                 "Send following url without USER_ID header : " + url + "  headers: " + headersMap.toString());
244
245                 RestResponse sendGetUserRequest = http.httpSendGet(url, headersMap);
246                 return sendGetUserRequest;
247         }
248
249         public static RestResponse authorizedUserTowardsCatalogBe(User sdncUserDetails) throws IOException {
250
251                 Map<String, String> headersMap = new HashMap<String, String>();
252                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
253                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
254                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails.getUserId());
255                 if (sdncUserDetails.getFirstName() != null) {
256                         headersMap.put(HttpHeaderEnum.HTTP_CSP_FIRSTNAME.getValue(), sdncUserDetails.getFirstName());
257                 }
258                 if (sdncUserDetails.getLastName() != null) {
259                         headersMap.put(HttpHeaderEnum.HTTP_CSP_LASTNAME.getValue(), sdncUserDetails.getLastName());
260                 }
261                 if (sdncUserDetails.getEmail() != null) {
262                         headersMap.put(HttpHeaderEnum.HTTP_CSP_EMAIL.getValue(), sdncUserDetails.getEmail());
263                 }
264
265                 logger.debug("headersMap: {}", headersMap.toString());
266
267                 Config config = Utils.getConfig();
268                 HttpRequest http = new HttpRequest();
269                 String url = String.format(Urls.AUTHORIZE_USER, config.getCatalogBeHost(), config.getCatalogBePort());
270                 logger.debug("Send GET request to login as seal user : {}", url);
271                 return http.httpSendGet(url, headersMap);
272         }
273
274         public static RestResponse authorizedUserTowardsCatalogBeQA(User sdncUserDetails) throws IOException {
275
276                 Map<String, String> headersMap = new HashMap<String, String>();
277                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), "application/json");
278                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), "application/json");
279                 headersMap.put(HttpHeaderEnum.USER_ID.getValue(), sdncUserDetails.getUserId());
280                 if (sdncUserDetails.getFirstName() != null) {
281                         headersMap.put(HttpHeaderEnum.HTTP_CSP_FIRSTNAME.getValue(), sdncUserDetails.getFirstName());
282                 }
283                 if (sdncUserDetails.getLastName() != null) {
284                         headersMap.put(HttpHeaderEnum.HTTP_CSP_LASTNAME.getValue(), sdncUserDetails.getLastName());
285                 }
286                 if (sdncUserDetails.getEmail() != null) {
287                         headersMap.put(HttpHeaderEnum.HTTP_CSP_EMAIL.getValue(), sdncUserDetails.getEmail());
288                 }
289
290                 logger.debug("headersMap: {}", headersMap.toString());
291
292                 Config config = Utils.getConfig();
293                 HttpRequest http = new HttpRequest();
294                 String url = String.format(Urls.AUTHORIZE_USER, config.getCatalogBeHost(), config.getCatalogBePort());
295                 logger.debug("Send GET request to login as seal user : {}", url);
296                 return http.httpSendGet(url, headersMap);
297         }
298
299 }