[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / rest / EcompUserRestUtils.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.List;
26 import java.util.Map;
27
28 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
29 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
30 import org.openecomp.sdc.ci.tests.api.Urls;
31 import org.openecomp.sdc.ci.tests.config.Config;
32 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
33 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
34 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
35 import org.openecomp.sdc.ci.tests.run.StartTest;
36 import org.openecomp.sdc.ci.tests.utils.Utils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import com.google.gson.Gson;
41
42 public class EcompUserRestUtils extends BaseRestUtils {
43
44         static Gson gson = new Gson();
45
46         static Logger logger = LoggerFactory.getLogger(EcompUserRestUtils.class.getName());
47         static String contentTypeHeaderData = "application/json";
48         static String acceptHeaderDate = "application/json";
49         static String ecompUsername = "12345";
50         static String ecompPassword = "12345";
51
52         public EcompUserRestUtils() {
53                 super();
54
55                 StartTest.enableLogger();
56         }
57
58         public static RestResponse pushUser(EcompUser ecompUser) throws IOException {
59                 Config config = Utils.getConfig();
60
61                 Map<String, String> headersMap = new HashMap<String, String>();
62                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
63                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
64                 headersMap.put(HttpHeaderEnum.ECOMP_USERNAME.getValue(), ecompUsername);
65                 headersMap.put(HttpHeaderEnum.ECOMP_PASSWORD.getValue(), ecompPassword);
66
67                 String userBodyJson = gson.toJson(ecompUser);
68
69                 HttpRequest http = new HttpRequest();
70                 String url = String.format(Urls.ECOMP_PUSH_USER, config.getCatalogBeHost(), config.getCatalogBePort());
71
72                 logger.debug("Send POST request to create user: {}", url);
73                 logger.debug("User body: {}", userBodyJson);
74                 logger.debug("User headers: {}", headersMap);
75
76                 RestResponse sendPushUserResponse = http.httpSendPost(url, userBodyJson, headersMap);
77
78                 return sendPushUserResponse;
79         }
80
81         /*
82          * loginId - equals to userId
83          */
84         public static RestResponse editUser(String loginId, EcompUser ecompUser) throws IOException {
85                 Config config = Utils.getConfig();
86
87                 Map<String, String> headersMap = new HashMap<String, String>();
88                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
89                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
90                 headersMap.put(HttpHeaderEnum.ECOMP_USERNAME.getValue(), ecompUsername);
91                 headersMap.put(HttpHeaderEnum.ECOMP_PASSWORD.getValue(), ecompPassword);
92
93                 String userBodyJson = gson.toJson(ecompUser);
94
95                 HttpRequest http = new HttpRequest();
96                 String url = String.format(Urls.ECOMP_EDIT_USER, config.getCatalogBeHost(), config.getCatalogBePort(), loginId);
97
98                 logger.debug("Send POST request to edit user: {}", url);
99                 logger.debug("User body: {}", userBodyJson);
100                 logger.debug("User headers: {}", headersMap);
101
102                 RestResponse sendEditUserResponse = http.httpSendPost(url, userBodyJson, headersMap);
103
104                 return sendEditUserResponse;
105         }
106
107         /*
108          * loginId - equals to userId
109          */
110         public static RestResponse getUser(String loginId) throws IOException {
111                 Config config = Utils.getConfig();
112
113                 Map<String, String> headersMap = new HashMap<String, String>();
114                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
115                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
116                 headersMap.put(HttpHeaderEnum.ECOMP_USERNAME.getValue(), ecompUsername);
117                 headersMap.put(HttpHeaderEnum.ECOMP_PASSWORD.getValue(), ecompPassword);
118
119                 HttpRequest http = new HttpRequest();
120                 String url = String.format(Urls.ECOMP_GET_USER, config.getCatalogBeHost(), config.getCatalogBePort(), loginId);
121
122                 logger.debug("Send GET request to get user: {}", url);
123                 logger.debug("User headers: {}", headersMap);
124
125                 RestResponse sendGetUserRequest = http.httpSendGet(url, headersMap);
126
127                 return sendGetUserRequest;
128         }
129
130         public static RestResponse getAllUsers() throws IOException {
131                 Config config = Utils.getConfig();
132
133                 Map<String, String> headersMap = new HashMap<String, String>();
134                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
135                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
136                 headersMap.put(HttpHeaderEnum.ECOMP_USERNAME.getValue(), ecompUsername);
137                 headersMap.put(HttpHeaderEnum.ECOMP_PASSWORD.getValue(), ecompPassword);
138
139                 HttpRequest http = new HttpRequest();
140                 String url = String.format(Urls.ECOMP_GET_ALL_USERS, config.getCatalogBeHost(), config.getCatalogBePort());
141
142                 logger.debug("Send POST request to get all users: {}", url);
143                 logger.debug("User headers: {}", headersMap);
144
145                 RestResponse sendGetAllUsersRequest = http.httpSendGet(url, headersMap);
146
147                 return sendGetAllUsersRequest;
148         }
149
150         public static RestResponse getAllAvailableRoles() throws IOException {
151                 Config config = Utils.getConfig();
152
153                 Map<String, String> headersMap = new HashMap<String, String>();
154                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
155                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
156                 headersMap.put(HttpHeaderEnum.ECOMP_USERNAME.getValue(), ecompUsername);
157                 headersMap.put(HttpHeaderEnum.ECOMP_PASSWORD.getValue(), ecompPassword);
158
159                 HttpRequest http = new HttpRequest();
160                 String url = String.format(Urls.ECOMP_GET_ALL_AVAILABLE_ROLES, config.getCatalogBeHost(),
161                                 config.getCatalogBePort());
162
163                 logger.debug("Send GET request to get all available roles: {}", url);
164                 logger.debug("User headers: {}", headersMap);
165
166                 RestResponse sendUpdateUserRequest = http.httpSendGet(url, headersMap);
167
168                 return sendUpdateUserRequest;
169         }
170
171         /*
172          * loginId - equals to userId
173          */
174         public static RestResponse pushUserRoles(String loginId, List<EcompRole> roles) throws IOException {
175                 Config config = Utils.getConfig();
176
177                 Map<String, String> headersMap = new HashMap<String, String>();
178                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
179                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
180                 headersMap.put(HttpHeaderEnum.ECOMP_USERNAME.getValue(), ecompUsername);
181                 headersMap.put(HttpHeaderEnum.ECOMP_PASSWORD.getValue(), ecompPassword);
182
183                 String roleBodyJson = gson.toJson(roles);
184                 if(roleBodyJson.equals("[{}]")) {
185                         roleBodyJson = "[]";
186                 }
187
188                 HttpRequest http = new HttpRequest();
189                 String url = String.format(Urls.ECOMP_PUSH_USER_ROLES, config.getCatalogBeHost(), config.getCatalogBePort(),
190                                 loginId);
191
192                 logger.debug("Send POST request to push user role: {}", url);
193                 logger.debug("Roles body: {}", roleBodyJson);
194                 logger.debug("Request headers: {}", headersMap);
195
196                 RestResponse sendpushUserRolesResponse = http.httpSendPost(url, roleBodyJson, headersMap);
197
198                 return sendpushUserRolesResponse;
199         }
200
201         /*
202          * loginId - equals to userId
203          */
204         public static RestResponse getUserRoles(String loginId) throws IOException {
205                 Config config = Utils.getConfig();
206
207                 Map<String, String> headersMap = new HashMap<String, String>();
208                 headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
209                 headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
210                 headersMap.put(HttpHeaderEnum.ECOMP_USERNAME.getValue(), ecompUsername);
211                 headersMap.put(HttpHeaderEnum.ECOMP_PASSWORD.getValue(), ecompPassword);
212
213                 HttpRequest http = new HttpRequest();
214                 String url = String.format(Urls.ECOMP_GET_USER_ROLES, config.getCatalogBeHost(), config.getCatalogBePort(),
215                                 loginId);
216
217                 logger.debug("Send GET request to get user roles: {}", url);
218                 logger.debug("User headers: {}", headersMap);
219
220                 RestResponse sendGetUserRolesRequest = http.httpSendGet(url, headersMap);
221
222                 return sendGetUserRolesRequest;
223         }
224
225         // TODO !!!!!!!!!!!!!!
226         /*
227          * Ask Eli if implementation of users is needed DELETE ECOMP USER
228          */
229
230         /*
231          * public static void main(String[] args) { EcompUser ecompUser = new
232          * EcompUser(); ecompUser.setFirstName("Test");
233          * ecompUser.setLastName("Testovich");
234          * ecompUser.setActive(true);
235          * 
236          * EcompRole roleToUpdate = new EcompRole(); roleToUpdate.setId(new
237          * Long(6)); roleToUpdate.setName("PRODUCT_STRATEGIST"); List<EcompRole>
238          * listOfRoles = new LinkedList<>(); listOfRoles.add(roleToUpdate);
239          * 
240          * try {
241          * System.out.println("\n-----------------------------\n Testing pushUser");
242          * System.out.println(pushUser(ecompUser));
243          * System.out.println("\n-----------------------------\n Testing editUser");
244          * System.out.println("\n-----------------------------\n Testing getUser");
245          * // System.out.println(getUser(ecompUser.getLoginId())); System.out.
246          * println("\n-----------------------------\n Testing getAllUsers"); //
247          * System.out.println(getAllUsers()); System.out.
248          * println("\n-----------------------------\n Testing getAllAvailableRoles"
249          * ); // System.out.println(getAllAvailableRoles().toString()); System.out.
250          * println("\n-----------------------------\n Testing pushUserRoles");
251          * TODO Auto-generated catch block e.printStackTrace(); } }
252          */
253 }