1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.sdc.backend.ci.tests.utils.rest;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.onap.sdc.backend.ci.tests.datatypes.http.HttpHeaderEnum;
import org.onap.sdc.backend.ci.tests.datatypes.http.HttpRequest;
import org.onap.sdc.backend.ci.tests.datatypes.http.RestResponse;
import org.onap.sdc.backend.ci.tests.utils.validation.BaseValidationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BaseRestUtils extends BaseValidationUtils {
public static final String contentTypeHeaderData = "application/json";
public static final String acceptHeaderData = "application/json";
public static final String acceptJsonHeader = "application/json";
public static final String acceptOctetHeader = "application/octet-stream";
public static final String acceptMultipartHeader = "application/octet-stream";
public static final String authorizationHeader = "Basic " + Base64.encodeBase64String("ci:123456".getBytes());
public static final String acceptOctetStream = "application/octet-stream";
public static final String ecomp = "ecomp";
public static final String authorizationPrefixString = "Basic ";
public static final String xEcompInstanceId = "a1bd39f6-d55e-45b2-9207-156216af5cb5";
public static final String cacheControlHeader = "no-cache";
public static final String RESOURCE_COMPONENT_TYPE = "resources";
public static final String PRODUCT_COMPONENT_TYPE = "products";
public static final String SERVICE_COMPONENT_TYPE = "services";
public static final int STATUS_CODE_SUCCESS = 200;
public static final int STATUS_CODE_CREATED = 201;
public static final int STATUS_CODE_DELETE = 204;
public static final int STATUS_CODE_NOT_FOUND = 404;
public static final int STATUS_CODE_SUCCESS_NO_CONTENT = 204;
public static final int STATUS_CODE_SUCCESS_DELETE = 204;
public static final int STATUS_CODE_INVALID_CONTENT = 400;
public static final int STATUS_CODE_MISSING_DATA = 400;
public static final int STATUS_CODE_MISSING_INFORMATION = 403;
public static final int STATUS_CODE_RESTRICTED_ACCESS = 403;
public static final int STATUS_CODE_ALREADY_EXISTS = 409;
public static final int STATUS_CODE_RESTRICTED_OPERATION = 409;
public static final int STATUS_CODE_COMPONENT_NAME_EXCEEDS_LIMIT = 400;
public static final int STATUS_CODE_MISSING_COMPONENT_NAME = 400;
public static final int STATUS_CODE_UNSUPPORTED_ERROR = 400;
public static final int STATUS_CODE_IMPORT_SUCCESS = 201;
public static final int STATUS_CODE_UPDATE_SUCCESS = 200;
public static final int RESTRICTED_OPERATION = 409;
public static final int STATUS_CODE_GET_SUCCESS = 200;
public static final String SUCCESS_MESSAGE = "OK";
private static Logger logger = LoggerFactory.getLogger(BaseRestUtils.class.getName());
private static byte[] encodeBase64;
// ************* PRIVATE METHODS ************************
public static Map<String, String> prepareHeadersMap(String userId) {
return prepareHeadersMap(userId, acceptHeaderData);
}
protected static Map<String, String> prepareHeadersMap(String userId, String accept) {
Map<String, String> headersMap = new HashMap<>();
if (contentTypeHeaderData != null) {
headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(), contentTypeHeaderData);
}
if (accept != null) {
headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), accept);
}
if (userId != null) {
headersMap.put(HttpHeaderEnum.USER_ID.getValue(), userId);
}
return headersMap;
}
// send request
// GET
protected static RestResponse sendGet(String url, String userId) throws IOException {
return sendGet(url, userId, null);
}
protected static RestResponse sendGet(String url, String userId, Map<String, String> additionalHeaders)
throws IOException {
Map<String, String> headersMap = prepareHeadersMap(userId);
if (additionalHeaders != null) {
headersMap.putAll(additionalHeaders);
}
HttpRequest http = new HttpRequest();
RestResponse getResourceResponse = http.httpSendGet(url, headersMap);
return getResourceResponse;
}
public static RestResponse sendGetAndRemoveHeaders(String url, String userId, List<String> headersToRemove)
throws IOException {
Map<String, String> headersMap = prepareHeadersMap(userId);
if (headersToRemove != null) {
for (String header : headersToRemove) {
headersMap.remove(header);
}
}
HttpRequest http = new HttpRequest();
RestResponse getResourceResponse = http.httpSendGet(url, headersMap);
return getResourceResponse;
}
// PUT
protected static RestResponse sendPut(String url, String userBodyJson, String userId, String cont)
throws IOException {
Map<String, String> headersMap = prepareHeadersMap(userId, cont);
HttpRequest http = new HttpRequest();
RestResponse updateResourceResponse = http.httpSendByMethod(url, "PUT", userBodyJson, headersMap);
return updateResourceResponse;
}
// POST
public static RestResponse sendPost(String url, String userBodyJson, String userId, String accept)
throws IOException {
return sendPost(url, userBodyJson, userId, accept, null);
}
protected static RestResponse sendPost(String url, String userBodyJson, String userId, String accept,
Map<String, String> additionalHeaders) throws IOException {
Map<String, String> headersMap = prepareHeadersMap(userId, accept);
if (additionalHeaders != null) {
headersMap.putAll(additionalHeaders);
}
HttpRequest http = new HttpRequest();
RestResponse postResourceResponse = http.httpSendPost(url, userBodyJson, headersMap);
return postResourceResponse;
}
// used form complex requests like import categories..
protected static RestResponse sendPost(String url, HttpEntity entity, String userId, String accept)
throws IOException {
RestResponse postResponse = new RestResponse();
CloseableHttpResponse response = null;
CloseableHttpClient client = null;
try {
client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("USER_ID", userId);
httpPost.setEntity(entity);
response = client.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
postResponse.setErrorCode(statusCode);
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
in.close();
} catch (Exception e) {
logger.debug("response body is null");
}
postResponse.setResponse(sb.toString());
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
logger.debug("failed to close client or response: ", e);
}
try {
if (client != null) {
client.close();
}
} catch (IOException e) {
logger.debug("failed to close client or response: ", e);
}
}
return postResponse;
}
// DELETE
protected static RestResponse sendDelete(String url, String userId) throws IOException {
// Map<String, String> headersMap = prepareHeadersMap(userId);
return sendDelete(url, userId, null);
}
protected static RestResponse sendDelete(String url, String userId, Map<String, String> additionalHeaders) throws IOException {
Map<String, String> headersMap = prepareHeadersMap(userId);
if (additionalHeaders != null) {
headersMap.putAll(additionalHeaders);
}
HttpRequest http = new HttpRequest();
RestResponse deleteResourceResponse = http.httpSendDelete(url, headersMap);
return deleteResourceResponse;
}
/*
* // ------ protected static Boolean checkErrorCode(RestResponse
* deleteResponse) { if (deleteResponse.getErrorCode() ==
* STATUS_CODE_SUCCESS || deleteResponse.getErrorCode() ==
* STATUS_CODE_DELETE) { return true; } return false; }
*
* // *** STATUS CODE VALIDATION UTIITIES **** public static void
* checkStatusCode(RestResponse response, String assertMessage, boolean AND,
* int... statuses) { int statusCode = response.getErrorCode(); for (int
* status : statuses) { if (AND && statusCode != status) {
* Assert.fail(assertMessage + " status: " + statusCode); } else if
* (statusCode == status) { return; } } if (!AND) {
* Assert.fail(assertMessage + " status: " + statusCode); } }
*
* public static void checkDeleteResponse(RestResponse response) {
* checkStatusCode(response,"delete request failed",false,STATUS_CODE_DELETE
* ,STATUS_CODE_NOT_FOUND, STATUS_CODE_SUCCESS); // STATUS_CODE_SUCCESS for
* deActivate user }
*
* public static void checkCreateResponse(RestResponse response) {
* checkStatusCode(response, "create request failed", false,
* STATUS_CODE_CREATED); }
*/
public static String encodeUrlForDownload(String url) {
return url.replaceAll(" ", "%20");
}
public static Map<String, String> addAuthorizeHeader(String userName, String password) {
String userCredentials = userName + ":" + password;
encodeBase64 = Base64.encodeBase64(userCredentials.getBytes());
String encodedUserCredentials = authorizationPrefixString + new String(encodeBase64);
Map<String, String> authorizationHeader = new HashMap<>();
authorizationHeader.put(HttpHeaderEnum.AUTHORIZATION.getValue(), encodedUserCredentials);
return authorizationHeader;
}
}
|