2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utils.rest;
23 import java.io.IOException;
24 import java.util.HashMap;
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;
39 import com.google.gson.Gson;
41 public class UserRestUtils extends BaseRestUtils {
43 static Gson gson = new Gson();
45 static Logger logger = LoggerFactory.getLogger(UserRestUtils.class.getName());
46 static String contentTypeHeaderData = "application/json";
47 static String acceptHeaderDate = "application/json";
49 public UserRestUtils() {
52 StartTest.enableLogger();
55 public static RestResponse createUser(User sdncUserDetails, User sdncModifierDetails) throws IOException {
57 Config config = Utils.getConfig();
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());
64 String userBodyJson = gson.toJson(sdncUserDetails);
65 HttpRequest http = new HttpRequest();
66 String url = String.format(Urls.CREATE_USER, config.getCatalogBeHost(), config.getCatalogBePort());
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);
73 return sendCreateUserRequest;
77 public static RestResponse deactivateUser(User sdncUserDetails, User sdncModifierDetails) throws IOException {
78 return deleteUser(sdncUserDetails, sdncModifierDetails, true);
81 public static RestResponse deActivateUser(User sdncUserDetails, User sdncModifierDetails) throws IOException {
82 return deleteUser(sdncUserDetails, sdncModifierDetails, false);
85 public static RestResponse deleteUser(User sdncUserDetails, User sdncModifierDetails, boolean isForceDelete)
88 Config config = Utils.getConfig();
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());
95 headersMap.put(User.FORCE_DELETE_HEADER_FLAG, User.FORCE_DELETE_HEADER_FLAG);
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;
106 public static RestResponse updateUser(User sdncUserDetails, User sdncModifierDetails)
107 throws IOException, CloneNotSupportedException {
109 Config config = Utils.getConfig();
110 User user = new User(sdncModifierDetails);
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());
117 user.setUserId(StringUtils.EMPTY);
118 user.setRole(StringUtils.EMPTY);
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);
128 return sendUpdateUserRequest;
132 public static RestResponse updateUserRole(User sdncUserDetails, User sdncModifierDetails, String userIdToUpdate)
135 Config config = Utils.getConfig();
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());
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(),
148 RestResponse sendUpdateUserRequest = http.httpSendPost(url, userBodyJson, headersMap);
150 return sendUpdateUserRequest;
154 public static RestResponse getUser(User sdncUserDetails, User sdncModifierDetails) throws IOException {
156 Config config = Utils.getConfig();
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;
170 public static RestResponse getUserRole(User sdncUserDetails, User sdncModifierDetails) throws IOException {
172 Config config = Utils.getConfig();
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;
188 public static RestResponse getAllAdminUsers(User sdncModifierDetails) throws IOException {
190 Config config = Utils.getConfig();
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());
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);
205 return sendGetUserRequest;
210 public static RestResponse getUsersByRoles(User sdncModifierDetails, String roles) throws IOException {
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();
220 url = String.format(Urls.GET_ALL_USERS, config.getCatalogBeHost(), config.getCatalogBePort());
222 url = String.format(Urls.GET_USERS_BY_ROLES, config.getCatalogBeHost(), config.getCatalogBePort(), roles);
225 logger.debug("Send following url: {} and headers: {}",url, headersMap.toString());
226 RestResponse sendGetUserRequest = http.httpSendGet(url, headersMap);
227 return sendGetUserRequest;
230 public static RestResponse getUsersByRolesHttpCspAtuUidIsMissing(User sdncModifierDetails, String roles)
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(),
243 "Send following url without USER_ID header : " + url + " headers: " + headersMap.toString());
245 RestResponse sendGetUserRequest = http.httpSendGet(url, headersMap);
246 return sendGetUserRequest;
249 public static RestResponse authorizedUserTowardsCatalogBe(User sdncUserDetails) throws IOException {
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());
258 if (sdncUserDetails.getLastName() != null) {
259 headersMap.put(HttpHeaderEnum.HTTP_CSP_LASTNAME.getValue(), sdncUserDetails.getLastName());
261 if (sdncUserDetails.getEmail() != null) {
262 headersMap.put(HttpHeaderEnum.HTTP_CSP_EMAIL.getValue(), sdncUserDetails.getEmail());
265 logger.debug("headersMap: {}", headersMap.toString());
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);
274 public static RestResponse authorizedUserTowardsCatalogBeQA(User sdncUserDetails) throws IOException {
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());
283 if (sdncUserDetails.getLastName() != null) {
284 headersMap.put(HttpHeaderEnum.HTTP_CSP_LASTNAME.getValue(), sdncUserDetails.getLastName());
286 if (sdncUserDetails.getEmail() != null) {
287 headersMap.put(HttpHeaderEnum.HTTP_CSP_EMAIL.getValue(), sdncUserDetails.getEmail());
290 logger.debug("headersMap: {}", headersMap.toString());
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);