-
Notifications
You must be signed in to change notification settings - Fork 64
/
ApiStepsTest.java
228 lines (202 loc) · 8.92 KB
/
ApiStepsTest.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
/**
* Copyright 2017 Alfa Laboratory
*
* 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.
*/
package ru.alfabank.steps;
import com.codeborne.selenide.WebDriverRunner;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import ru.alfabank.StubScenario;
import ru.alfabank.alfatest.cucumber.api.AkitaEnvironment;
import ru.alfabank.alfatest.cucumber.api.AkitaScenario;
import ru.alfabank.tests.core.rest.RequestParam;
import ru.alfabank.tests.core.rest.RequestParamType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static ru.alfabank.alfatest.cucumber.ScopedVariables.resolveVars;
import static ru.alfabank.tests.core.helpers.PropertyLoader.loadValueFromFileOrPropertyOrVariableOrDefault;
import static ru.alfabank.tests.core.rest.RequestParamType.PARAMETER;
public class ApiStepsTest {
private static DefaultApiSteps api;
private static AkitaScenario akitaScenario;
@BeforeClass
public static void setup() {
akitaScenario = AkitaScenario.getInstance();
api = new DefaultApiSteps();
akitaScenario.setEnvironment(new AkitaEnvironment(new StubScenario()));
}
@AfterClass
public static void close() {
WebDriverRunner.closeWebDriver();
}
@Rule
public WireMockRule wireMockRule = new WireMockRule();
@Test
public void getURLwithPathParamsCalculatedSimple() {
assertThat(resolveVars("alfabank.ru"),
equalTo("alfabank.ru"));
}
@Test
public void sendHttpRequestGET() throws java.lang.Exception {
stubFor(get(urlEqualTo("/get/resource"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/xml")
.withBody("TEST_BODY")));
api.sendHttpRequestWithoutParams("GET", "/get/resource", "RESPONSE_GET_BODY");
assertThat(akitaScenario.getVar("RESPONSE_GET_BODY"), equalTo("TEST_BODY"));
}
@Test
public void sendHttpRequestPost() throws java.lang.Exception {
stubFor(post(urlEqualTo("/post/resource"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/xml")
.withBody("TEST_BODY")));
api.sendHttpRequestWithoutParams("POST", "/post/resource", "RESPONSE_POST_BODY");
assertThat(akitaScenario.getVar("RESPONSE_POST_BODY"), equalTo("TEST_BODY"));
}
@Test
public void sendHttpRequestWithVarsPost() throws java.lang.Exception {
String body = "testBodyValue";
String bodyVarName = "testBodyName";
akitaScenario.setVar(bodyVarName, body);
stubFor(post(urlEqualTo("/post/resource"))
.withRequestBody(WireMock.equalTo(body))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/xml")
.withBody("TEST_BODY")));
List<RequestParam> params = Collections.singletonList(
RequestParam.builder()
.name("body")
.type(RequestParamType.BODY)
.value("{" + bodyVarName + "}")
.build());
api.sendHttpRequestSaveResponse("POST", "/post/resource", "RESPONSE_POST_BODY", params);
assertThat(akitaScenario.getVar("RESPONSE_POST_BODY"), equalTo("TEST_BODY"));
}
@Test
public void sendHttpRequestFromFileWithVarsPost() throws java.lang.Exception {
String body = "{\"person\": {\"name\": \"Jack\", \"age\": 35}, \"object\": {\"var1\": 1}}";
String bodyFileName = "/src/test/resources/bodyWithParams.json";
stubFor(post(urlEqualTo("/post/resource"))
.withRequestBody(WireMock.equalTo(body))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/xml")
.withBody("TEST_BODY")));
List<RequestParam> params = Collections.singletonList(
RequestParam.builder()
.name("body")
.type(RequestParamType.BODY)
.value(bodyFileName)
.build());
api.sendHttpRequestSaveResponse("POST", "/post/resource", "RESPONSE_POST_BODY", params);
assertThat(akitaScenario.getVar("RESPONSE_POST_BODY"), equalTo("TEST_BODY"));
}
@Test
public void sendHttpRequestSaveResponseTest() throws java.lang.Exception {
stubFor(post(urlEqualTo("/post/saveWithTable"))
.willReturn(aResponse()
.withStatus(201)
.withHeader("Content-Type", "text/xml")
.withBody("TEST_BODY_1")));
List<RequestParam> paramTable = new ArrayList<>();
RequestParam requestParamHeader = RequestParam.builder()
.type(RequestParamType.HEADER)
.name("Accept")
.value("text/plain")
.build();
RequestParam requestParamBody = RequestParam.builder()
.type(RequestParamType.BODY)
.name("TEST_BODY_PARAM")
.value("TEST")
.build();
paramTable.add(requestParamHeader);
paramTable.add(requestParamBody);
api.sendHttpRequestSaveResponse("POST", "/post/saveWithTable",
"TEST_HTTP", paramTable);
assertThat(akitaScenario.getVar("TEST_HTTP"), equalTo("TEST_BODY_1"));
}
@Test
public void checkResponseCodeTest() throws java.lang.Exception {
stubFor(get(urlEqualTo("/get/responseWithTable?param=test"))
.willReturn(aResponse()
.withStatus(404)
.withHeader("Content-Type", "text/xml")));
List<RequestParam> paramTable = new ArrayList<>();
RequestParam requestParam = RequestParam.builder()
.type(PARAMETER)
.name("param")
.value("test")
.build();
paramTable.add(requestParam);
api.checkResponseCode("GET", "/get/responseWithTable",
404, paramTable);
}
@Test
public void shouldCreateRequestForEqualsParamNamesTest() throws Exception {
stubFor(get(urlEqualTo("/get/responseWithTable?param=first¶m=second"))
.willReturn(aResponse()
.withStatus(200)));
List<RequestParam> paramTable = asList(
new RequestParam(PARAMETER, "param", "first"),
new RequestParam(PARAMETER, "param", "second")
);
api.checkResponseCode("GET", "/get/responseWithTable", 200, paramTable);
}
@Test
public void shouldSendPutRequest() throws Exception {
stubFor(put(urlEqualTo("/put/someInfo"))
.willReturn(aResponse()
.withStatus(205)));
api.checkResponseCode("PUT", "/put/someInfo", 205, new ArrayList<>());
}
@Test()
public void shouldNotFindBodyByPath() throws Exception {
String expectedBodyValue = "{\"value\": \"true\"}";
String actualBodyValue = loadValueFromFileOrPropertyOrVariableOrDefault(resolveVars(expectedBodyValue));
assertThat(actualBodyValue, equalTo(expectedBodyValue));
}
@Test()
public void shouldFindBodyByPath() throws Exception {
String expectedBodyValue = "{\"asn\": \"1\"}";
String actualBodyValue = loadValueFromFileOrPropertyOrVariableOrDefault(resolveVars("/src/test/resources/body.json"));
assertThat(actualBodyValue, equalTo(expectedBodyValue));
}
@Test()
public void shouldFindBodyByPropertyKey() throws Exception {
String expectedBodyValue = "{\"property\":\"body\"}";
String actualBodyValue = loadValueFromFileOrPropertyOrVariableOrDefault(resolveVars("bodyValue"));
assertThat(actualBodyValue, equalTo(expectedBodyValue));
}
@Test
public void shouldFindBodyWithVarResolving() throws Exception {
akitaScenario.setVar("var1", "\"1\"");
String defaultBodyValue = "{\"a\":{var1}, \"b\": {\"c\": {var2}}}";
String expectedBodyValue = "{\"a\":\"1\", \"b\": {\"c\": \"2\"}}";
String actualBodyValue = loadValueFromFileOrPropertyOrVariableOrDefault(resolveVars(defaultBodyValue));
assertThat(actualBodyValue, equalTo(expectedBodyValue));
}
}