-
Notifications
You must be signed in to change notification settings - Fork 30
/
ApiStepsImpl.java
253 lines (230 loc) · 9.1 KB
/
ApiStepsImpl.java
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
package ru.sbtqa.tag.api.junit;
import java.util.Map;
import ru.sbtqa.tag.api.annotation.ParameterType;
import ru.sbtqa.tag.api.annotation.Validation;
import ru.sbtqa.tag.api.context.EndpointContext;
import ru.sbtqa.tag.api.environment.ApiEnvironment;
import ru.sbtqa.tag.api.exception.RestPluginException;
import ru.sbtqa.tag.api.manager.EndpointManager;
import ru.sbtqa.tag.api.storage.BlankStorage;
import ru.sbtqa.tag.api.storage.EndpointBlank;
import ru.sbtqa.tag.api.utils.FromResponseUtils;
/**
* Basic step definitions, that should be available on every project
*
* <p>
* To pass a Cucumber {@link cucumber.api.DataTable} as a parameter to method,
* supply a table in the following format after a step ini feature:
* <p>
* | name 1| name 2 | | value 1 | value 2 |
* <p>
* This table will be converted to a {@link cucumber.api.DataTable} object.
* First line is not enforced to be a name.
* <p>
* To pass a list as parameter, use flattened table as follows: | value 1 | }
* value 2 |
*
* @see <a href="https://cucumber.io/docs/reference#step-definitions">Cucumber
* documentation</a>
*/
public class ApiStepsImpl<T extends ApiStepsImpl<T>> {
public ApiStepsImpl() {
ApiSetupSteps.initApi();
}
/**
* Execute the last endpoint (request) in {@link BlankStorage} with no
* parameters.
*/
public T send() {
String endpoint = ApiEnvironment.getBlankStorage().getLast().getTitle();
EndpointManager.getEndpoint(endpoint).send();
return (T) this;
}
/**
* Execute endpoint (request) with no parameters
*
* @param endpoint class of the endpoint annotation to execute
*/
public T send(Class endpoint) {
EndpointManager.getEndpoint(endpoint).send();
return (T) this;
}
/**
* Execute endpoint (request) with no parameters
*
* @param endpoint name value of the endpoint annotation to execute
*/
public T send(String endpoint) {
EndpointManager.getEndpoint(endpoint).send();
return (T) this;
}
/**
* Execute endpoint endpoint (request) with parameters from given
* {@link Map}
*
* @param endpoint name value of the endpoint annotation to execute
* @param data table of parameters
*/
public T send(String endpoint, Map<String, String> data) {
EndpointManager.getEndpoint(endpoint).send(data);
return (T) this;
}
/**
* Execute endpoint endpoint (request) with parameters from given
* {@link Map}
*
* @param endpoint class of the endpoint annotation to execute
* @param data table of parameters
*/
public T send(Class endpoint, Map<String, String> data) {
EndpointManager.getEndpoint(endpoint).send(data);
return (T) this;
}
/**
* Execute a validation rule annotated by {@link Validation} on current
* endpoint. Works if endpoint contains only one validation rule
*/
public void validate() {
EndpointContext.getCurrentEndpoint().validate();
}
/**
* Execute a validation rule annotated by {@link Validation} on current
* endpoint
*
* @param rule name of the validation rule (name value of the
* {@link Validation} annotation)
*/
public void validate(String rule) {
EndpointContext.getCurrentEndpoint().validate(rule);
}
/**
* Execute a validation rule annotated by {@link Validation} on current
* endpoint with parameters from given {@link Map}
*
* @param rule name of the validation rule (name value of the
* {@link Validation} annotation)
* @param data map of parameters
* @throws RestPluginException if there is an error while validation rule
* executing
*/
public void validate(String rule, Map<String, String> data) {
EndpointContext.getCurrentEndpoint().validate(rule, data);
}
/**
* Start filling parameters in endpoint
*
* @param title endpoint title
*/
public T fill(String title) {
ApiEnvironment.getBlankStorage().add(new EndpointBlank(title));
return (T) this;
}
public T fill(Class endpoint) {
ApiEnvironment.getBlankStorage().add(new EndpointBlank(endpoint));
return (T) this;
}
/**
* Add parameter to the last endpoint blank in {@link BlankStorage}
*
* @param parameterType {@link ParameterType} of parameter
* @param name with this name the parameter will be added to endpoint blank
* @param value with this value the parameter will be added to endpoint
* blank
*/
public T add(String parameterType, String name, String value) {
ParameterType type = ParameterType.valueOf(parameterType.toUpperCase());
return add(type, name, value);
}
/**
* Add parameter to the last endpoint blank in {@link BlankStorage}
*
* @param type {@link ParameterType} of parameter
* @param name with this name the parameter will be added to endpoint blank
* @param value with this value the parameter will be added to endpoint
* blank
*/
public T add(ParameterType type, String name, String value) {
ApiEnvironment.getBlankStorage().getLast().addParameter(type, name, value);
return (T) this;
}
/**
* Add parameters to the last endpoint blank in {@link BlankStorage}
*
* @param type {@link ParameterType} of parameters
* @param data parameters name-value pairs
*/
public T add(ParameterType type, Map<String, String> data) {
for (Map.Entry<String, String> dataTableRow : data.entrySet()) {
ApiEnvironment.getBlankStorage().getLast().addParameter(type, dataTableRow.getKey(), dataTableRow.getValue());
}
return (T) this;
}
/**
* Add parameter with {@link ParameterType} to the last endpoint blank in
* {@link BlankStorage}. Get value from body of one of the previous
* responses
*
* @param parameterType {@link ParameterType} of parameter
* @param parameterName with this name the parameter will be added to
* endpoint blank
* @param fromEndpointTitle get response with this title
* @param path get value from body by this path
*/
public T addToBody(String parameterType, String parameterName, String fromEndpointTitle, String path) {
addToBody(parameterType, parameterName, fromEndpointTitle, path, "");
return (T) this;
}
/**
* Add parameter with {@link ParameterType} to the last endpoint blank in
* {@link BlankStorage}. Get value from body of one of the previous
* responses
*
* @param parameterType {@link ParameterType} of parameter
* @param parameterName with this name the parameter will be added to
* endpoint blank
* @param fromEndpointTitle get response with this title
* @param path get value from body by this path
* @param mask apply mask on this value
*/
public T addToBody(String parameterType, String parameterName, String fromEndpointTitle, String path, String mask) {
Class fromEndpoint = ApiEnvironment.getRepository().get(fromEndpointTitle);
String value = (String) FromResponseUtils.getValueFromResponse(fromEndpoint, "", path, mask, true);
ParameterType type = ParameterType.valueOf(parameterType.toUpperCase());
ApiEnvironment.getBlankStorage().getLast().addParameter(type, parameterName, value);
return (T) this;
}
/**
* Add parameter with {@link ParameterType} to the last endpoint blank in
* {@link BlankStorage}. Get value from header of one of the previous
* responses
*
* @param parameterType {@link ParameterType} of parameter to add
* @param parameterName with this name the parameter will be added to
* endpoint blank
* @param fromEndpointTitle get response with this title
* @param headerName get value from header with this name
*/
public T addToHeader(String parameterType, String parameterName, String fromEndpointTitle, String headerName) {
addToBody(parameterType, parameterName, fromEndpointTitle, headerName, "");
return (T) this;
}
/**
* Add parameter with {@link ParameterType} to the last endpoint blank in
* {@link BlankStorage}. Get value from header of one of the previous
* responses
*
* @param parameterType {@link ParameterType} of parameter to add
* @param parameterName with this name the parameter will be added to
* endpoint blank
* @param fromEndpointTitle get response with this title
* @param headerName get value from header with this name
* @param mask apply mask on this value
*/
public T addToHeader(String parameterType, String parameterName, String fromEndpointTitle, String headerName, String mask) {
Class fromEndpoint = ApiEnvironment.getRepository().get(fromEndpointTitle);
String value = (String) FromResponseUtils.getValueFromResponse(fromEndpoint, headerName, "", mask, true);
ParameterType type = ParameterType.valueOf(parameterType.toUpperCase());
ApiEnvironment.getBlankStorage().getLast().addParameter(type, parameterName, value);
return (T) this;
}
}