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