Fix for radio buttons
[sdc.git] / asdc-tests / src / main / java / org / openecomp / sdc / ci / tests / utils / rest / BaseRestUtils.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.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.commons.codec.binary.Base64;
31 import org.apache.http.HttpEntity;
32 import org.apache.http.client.methods.CloseableHttpResponse;
33 import org.apache.http.client.methods.HttpPost;
34 import org.apache.http.impl.client.CloseableHttpClient;
35 import org.apache.http.impl.client.HttpClients;
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.validation.BaseValidationUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class BaseRestUtils extends BaseValidationUtils {
44         public static final String contentTypeHeaderData = "application/json";
45         public static final String acceptHeaderData = "application/json";
46         public static final String acceptJsonHeader = "application/json";
47         public static final String acceptOctetHeader = "application/octet-stream";
48         public static final String authorizationHeader = "Basic " + Base64.encodeBase64String("ci:123456".getBytes());
49         public static final String acceptOctetStream = "application/octet-stream";
50         public static final String ecomp = "ecomp";
51         public static final String authorizationPrefixString = "Basic ";
52
53         public static final String RESOURCE_COMPONENT_TYPE = "resources";
54         public static final String PRODUCT_COMPONENT_TYPE = "products";
55         public static final String SERVICE_COMPONENT_TYPE = "services";
56
57         public static final int STATUS_CODE_SUCCESS = 200;
58         public static final int STATUS_CODE_CREATED = 201;
59         public static final int STATUS_CODE_DELETE = 204;
60         public static final int STATUS_CODE_NOT_FOUND = 404;
61         public static final int STATUS_CODE_SUCCESS_NO_CONTENT = 204;
62         public static final int STATUS_CODE_SUCCESS_DELETE = 204;
63         public static final int STATUS_CODE_INVALID_CONTENT = 400;
64         public static final int STATUS_CODE_MISSING_DATA = 400;
65         public static final int STATUS_CODE_MISSING_INFORMATION = 403;
66         public static final int STATUS_CODE_RESTRICTED_ACCESS = 403;
67         public static final int STATUS_CODE_ALREADY_EXISTS = 409;
68         public static final int STATUS_CODE_RESTRICTED_OPERATION = 409;
69         public static final int STATUS_CODE_COMPONENT_NAME_EXCEEDS_LIMIT = 400;
70         public static final int STATUS_CODE_MISSING_COMPONENT_NAME = 400;
71         public static final int STATUS_CODE_UNSUPPORTED_ERROR = 400;
72         public static final int STATUS_CODE_IMPORT_SUCCESS = 201;
73         public static final int STATUS_CODE_UPDATE_SUCCESS = 200;
74         public static final int RESTRICTED_OPERATION = 409;
75         public static final int STATUS_CODE_GET_SUCCESS = 200;
76
77         public static final String SUCCESS_MESSAGE = "OK";
78         private static Logger logger = LoggerFactory.getLogger(BaseRestUtils.class.getName());
79
80         private static byte[] encodeBase64;
81
82         // ************* PRIVATE METHODS ************************
83
84         protected static Map<String, String> prepareHeadersMap(String userId) {
85                 return prepareHeadersMap(userId, acceptHeaderData);
86         }
87
88         protected static Map<String, String> prepareHeadersMap(String userId, String accept) {
89                 Map<String, String> headersMap = new HashMap<String, String>();
90                 if (contentTypeHeaderData != null) {
91                         headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
92                 }
93                 if (accept != null) {
94                         headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), accept);
95                 }
96                 if (userId != null) {
97                         headersMap.put(HttpHeaderEnum.USER_ID.getValue(), userId);
98                 }
99
100                 return headersMap;
101         }
102
103         // send request
104         // GET
105         protected static RestResponse sendGet(String url, String userId) throws IOException {
106                 return sendGet(url, userId, null);
107         }
108
109         protected static RestResponse sendGet(String url, String userId, Map<String, String> additionalHeaders)
110                         throws IOException {
111                 Map<String, String> headersMap = prepareHeadersMap(userId);
112                 if (additionalHeaders != null) {
113                         headersMap.putAll(additionalHeaders);
114                 }
115
116                 HttpRequest http = new HttpRequest();
117                 RestResponse getResourceResponse = http.httpSendGet(url, headersMap);
118                 return getResourceResponse;
119         }
120
121         public static RestResponse sendGetAndRemoveHeaders(String url, String userId, List<String> headersToRemove)
122                         throws IOException {
123                 Map<String, String> headersMap = prepareHeadersMap(userId);
124                 if (headersToRemove != null) {
125                         for (String header : headersToRemove) {
126                                 headersMap.remove(header);
127                         }
128                 }
129
130                 HttpRequest http = new HttpRequest();
131                 RestResponse getResourceResponse = http.httpSendGet(url, headersMap);
132                 return getResourceResponse;
133         }
134
135         // PUT
136         protected static RestResponse sendPut(String url, String userBodyJson, String userId, String cont)
137                         throws IOException {
138                 Map<String, String> headersMap = prepareHeadersMap(userId, cont);
139
140                 HttpRequest http = new HttpRequest();
141                 RestResponse updateResourceResponse = http.httpSendByMethod(url, "PUT", userBodyJson, headersMap);
142                 return updateResourceResponse;
143         }
144
145         // POST
146         public static RestResponse sendPost(String url, String userBodyJson, String userId, String accept)
147                         throws IOException {
148                 return sendPost(url, userBodyJson, userId, accept, null);
149         }
150
151         protected static RestResponse sendPost(String url, String userBodyJson, String userId, String accept,
152                         Map<String, String> additionalHeaders) throws IOException {
153                 Map<String, String> headersMap = prepareHeadersMap(userId, accept);
154                 if (additionalHeaders != null) {
155                         headersMap.putAll(additionalHeaders);
156                 }
157                 HttpRequest http = new HttpRequest();
158                 RestResponse postResourceResponse = http.httpSendPost(url, userBodyJson, headersMap);
159                 return postResourceResponse;
160         }
161
162         // used form complex requests like import categories..
163         protected static RestResponse sendPost(String url, HttpEntity entity, String userId, String accept)
164                         throws IOException {
165                 RestResponse postResponse = new RestResponse();
166                 CloseableHttpResponse response = null;
167                 CloseableHttpClient client = null;
168                 try {
169                         client = HttpClients.createDefault();
170                         HttpPost httpPost = new HttpPost(url);
171
172                         httpPost.addHeader("USER_ID", userId);
173                         httpPost.setEntity(entity);
174                         response = client.execute(httpPost);
175                         HttpEntity responseEntity = response.getEntity();
176                         int statusCode = response.getStatusLine().getStatusCode();
177
178                         postResponse.setErrorCode(statusCode);
179                         StringBuffer sb = new StringBuffer();
180                         try {
181                                 BufferedReader in = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
182                                 String inputLine;
183                                 while ((inputLine = in.readLine()) != null) {
184                                         sb.append(inputLine);
185                                 }
186                                 in.close();
187                         } catch (Exception e) {
188                                 logger.debug("response body is null");
189                         }
190                         postResponse.setResponse(sb.toString());
191                 } finally {
192                         try {
193                                 if (response != null) {
194                                         response.close();
195                                 }
196
197                         } catch (IOException e) {
198                                 logger.debug("failed to close client or response: ", e);
199                         }
200                         try {
201                                 if (client != null) {
202                                         client.close();
203                                 }
204                         } catch (IOException e) {
205                                 logger.debug("failed to close client or response: ", e);
206                         }
207                 }
208                 return postResponse;
209         }
210
211         // DELETE
212         protected static RestResponse sendDelete(String url, String userId) throws IOException {
213                 return sendDelete(url, userId, null);
214         }
215         
216         protected static RestResponse sendDelete(String url, String userId, Map<String, String> additionalHeaders) throws IOException {
217                 Map<String, String> headersMap = prepareHeadersMap(userId);
218                 if (additionalHeaders != null) {
219                         headersMap.putAll(additionalHeaders);
220                 }
221                 
222                 HttpRequest http = new HttpRequest();
223                 RestResponse deleteResourceResponse = http.httpSendDelete(url, headersMap);
224                 return deleteResourceResponse;
225         }
226
227         /*
228          * // ------ protected static Boolean checkErrorCode(RestResponse
229          * deleteResponse) { if (deleteResponse.getErrorCode() ==
230          * STATUS_CODE_SUCCESS || deleteResponse.getErrorCode() ==
231          * STATUS_CODE_DELETE) { return true; } return false; }
232          * 
233          * // *** STATUS CODE VALIDATION UTIITIES **** public static void
234          * checkStatusCode(RestResponse response, String assertMessage, boolean AND,
235          * int... statuses) { int statusCode = response.getErrorCode(); for (int
236          * status : statuses) { if (AND && statusCode != status) {
237          * Assert.fail(assertMessage + " status: " + statusCode); } else if
238          * (statusCode == status) { return; } } if (!AND) {
239          * Assert.fail(assertMessage + " status: " + statusCode); } }
240          * 
241          * public static void checkDeleteResponse(RestResponse response) {
242          * checkStatusCode(response,"delete request failed",false,STATUS_CODE_DELETE
243          * ,STATUS_CODE_NOT_FOUND, STATUS_CODE_SUCCESS); // STATUS_CODE_SUCCESS for
244          * deActivate user }
245          * 
246          * public static void checkCreateResponse(RestResponse response) {
247          * checkStatusCode(response, "create request failed", false,
248          * STATUS_CODE_CREATED); }
249          */
250         public static String encodeUrlForDownload(String url) {
251                 return url.replaceAll(" ", "%20");
252         }
253
254         public static Map<String, String> addAuthorizeHeader(String userName, String password) {
255                 String userCredentials = userName + ":" + password;
256                 encodeBase64 = Base64.encodeBase64(userCredentials.getBytes());
257                 String encodedUserCredentials = authorizationPrefixString + new String(encodeBase64);
258                 Map<String, String> authorizationHeader = new HashMap<String, String>();
259                 authorizationHeader.put(HttpHeaderEnum.AUTHORIZATION.getValue(), encodedUserCredentials);
260                 return authorizationHeader;
261         }
262
263 }