-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.java
More file actions
266 lines (208 loc) · 9.15 KB
/
Service.java
File metadata and controls
266 lines (208 loc) · 9.15 KB
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
package api.service;
import api.interfaces.IRoutes;
import api.service.utils.RequestParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.config.EncoderConfig;
import io.restassured.http.Cookie;
import io.restassured.http.Cookies;
import io.restassured.response.Response;
import io.restassured.specification.QueryableRequestSpecification;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.SpecificationQuerier;
import utils.Logger;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static io.restassured.RestAssured.given;
public final class Service {
private RequestParser requestParser;
private static RequestSpecBuilder requestSpecBuilder;
private RequestSpecification requestSpecification;
private static Response response;
private Class<?> declaringClass;
private Object[] enumFields;
private static IRoutes _route;
public static Service init() {
requestSpecBuilder = new RequestSpecBuilder();
return new Service();
}
private <E extends Enum<E>> void initializeRoute(Enum<E> route) throws Exception {
if(_route == null || declaringClass != route.getDeclaringClass()) {
declaringClass = route.getDeclaringClass();
enumFields = declaringClass.getEnumConstants();
}
getEnumField(route);
initRequestSpecs();
}
@SuppressWarnings(value = "all")
private <E extends Enum<E>> void getEnumField(Enum<E> route) throws Exception {
if(enumFields == null) {
Logger.exception("Enum has no declared fields!");
}
for(Object c : enumFields) {
if(route.name().equals(c.getClass().getDeclaredMethod("field_name").invoke(c))) {
_route = (IRoutes) c;
break;
}
_route = null;
}
if(_route == null) {
Logger.exception("Case not implemented yet for services: " + route);
}
}
private void initRequestSpecs() {
EncoderConfig encoderconfig = new EncoderConfig();
requestSpecBuilder.setBaseUri(_route.service_url());
requestSpecBuilder.setBasePath(_route.endpoint_path());
requestSpecBuilder.setUrlEncodingEnabled(false);
requestSpecBuilder.setConfig(RestAssured.config().encoderConfig(encoderconfig.appendDefaultContentCharsetToContentTypeIfUndefined(false)));
}
public Service headers(Map<String, String> headers) {
requestSpecBuilder.addHeaders(headers);
return this;
}
public Service cookie(Cookie cookie) {
requestSpecBuilder.addCookie(cookie);
return this;
}
public Service cookies(Map<String, String> cookies) {
requestSpecBuilder.addCookies(cookies);
return this;
}
public Service cookies(Cookies cookies) {
requestSpecBuilder.addCookies(cookies);
return this;
}
public Service queryParam(String key, String value) {
requestSpecBuilder.addQueryParam(key, value);
return this;
}
public Service queryParams(Map<String, ?> queryParams) {
requestSpecBuilder.addQueryParams(queryParams);
return this;
}
public Service pathParam(String key, String value) {
requestSpecBuilder.addPathParam(key, value);
return this;
}
public Service pathParams(Map<String, ?> pathParams) {
requestSpecBuilder.addPathParams(pathParams);
return this;
}
public Service formParam(String key, String value) {
requestSpecBuilder.addFormParam(key, value);
return this;
}
public Service formParams(Map<String, ?> formParams) {
requestSpecBuilder.addFormParams(formParams);
return this;
}
public Service body(Object body) {
requestSpecBuilder.setBody(body);
return this;
}
public Service path(String path) {
requestSpecBuilder.setBasePath(path);
return this;
}
public <E extends Enum<E>> Service get(Enum<E> route, Map<String, String> additionalHeaders) throws Exception {
initializeRoute(route);
requestParser = getRequest(additionalHeaders);
setRequestSpecifications();
// RestAssured.useRelaxedHTTPSValidation();
if (containsIllegals(_route.service_url())) {
response = given().log().all().spec(requestSpecification).when().get(requestParser.endpoint).then().extract().response();
return this;
}
response = given().log().all().spec(requestSpecification).when().get().then().extract().response();
return this;
}
public <E extends Enum<E>> Service get(Enum<E> route) throws Exception {
return this.get(route, null);
}
@SafeVarargs
public final <E extends Enum<E>> Service post(Enum<E> route, Map<String, String> additionalHeaders, Map<Object, Object>... body) throws Exception {
initializeRoute(route);
requestParser = getRequest(additionalHeaders, body);
setRequestSpecifications();
if (containsIllegals(_route.service_url())) {
response = given().log().all().spec(requestSpecification).when().post(requestParser.endpoint).then().extract().response();
return this;
}
response = given().log().all().spec(requestSpecification).when().post().then().extract().response();
return this;
}
@SafeVarargs
public final <E extends Enum<E>> Service put(Enum<E> route, Map<String, String> additionalHeaders, Map<Object, Object>... body) throws Exception {
initializeRoute(route);
requestParser = getRequest(additionalHeaders, body);
setRequestSpecifications();
if (containsIllegals(_route.service_url())) {
response = given().log().all().spec(requestSpecification).when().put(requestParser.endpoint).then().extract().response();
return this;
}
response = given().log().all().spec(requestSpecification).when().put().then().extract().response();
return this;
}
@SafeVarargs
public final <E extends Enum<E>> Service patch(Enum<E> route, Map<String, String> additionalHeaders, Map<Object, Object>... body) throws Exception {
initializeRoute(route);
requestParser = getRequest(additionalHeaders, body);
setRequestSpecifications();
if (containsIllegals(_route.service_url())) {
response = given().log().all().spec(requestSpecification).when().patch(requestParser.endpoint).then().extract().response();
return this;
}
response = given().log().all().spec(requestSpecification).when().patch().then().extract().response();
return this;
}
public <E extends Enum<E>> Service delete(Enum<E> route, Map<String, String> additionalHeaders) throws Exception {
initializeRoute(route);
requestParser = getRequest(additionalHeaders);
setRequestSpecifications();
if (containsIllegals(_route.service_url())) {
response = given().log().all().spec(requestSpecification).when().delete(requestParser.endpoint).then().extract().response();
return this;
}
response = given().log().all().spec(requestSpecification).when().delete().then().extract().response();
return this;
}
public static <T> T responseToPojo(Class<T> type) throws Exception {
try {
return new ObjectMapper().enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY).readValue(response.asString(), type);
} catch(IOException ioException) {
throw new Exception("Response Received did not match the expected Response Format POJO: " + type.getName() + ioException);
}
}
public static <T> T responseToPojo(Class<T> type,Response response) throws Exception {
try {
return new ObjectMapper().enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY).readValue(response.asString(), type);
} catch(IOException ioException) {
throw new Exception("Response Received did not match the expected Response Format POJO: " + type.getName() + ioException);
}
}
@SafeVarargs
private static RequestParser getRequest(Map<String, String> additionalHeaders, Map<Object, Object>... body) throws Exception {
return new RequestParser(additionalHeaders, body);
}
public static IRoutes getRoute() {
return _route;
}
public static Response getResponse() {
return response;
}
private boolean containsIllegals(String toExamine) {
Pattern pattern = Pattern.compile("[~@*?<>^]");
Matcher matcher = pattern.matcher(toExamine);
return matcher.find();
}
private void setRequestSpecifications() {
if (requestParser.headers != null) requestSpecBuilder.addHeaders(requestParser.headers);
if (requestParser.body != null && !requestParser.body.trim().isEmpty()) requestSpecBuilder.setBody(requestParser.body);
requestSpecification = requestSpecBuilder.build();
}
}