Skip to content

Commit

Permalink
bug fix (#113)
Browse files Browse the repository at this point in the history
fixed ability to resolve variable values from json body files
  • Loading branch information
sergeyuttsel authored and GannaChernyshova committed Nov 22, 2018
1 parent 7d21fd8 commit 471d7d1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,6 @@ jacocoTestReport {
license {
header rootProject.file('HEADER')
exclude ("body.json")
exclude ("bodyWithParams.json")
strictCheck false
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ public static String resolveJsonVars(String inputJsonAsString) {
while (m.find()) {
String varName = m.group(1);
String value = loadProperty(varName, (String) AkitaScenario.getInstance().tryGetVar(varName));
if (value == null)
throw new IllegalArgumentException(
"Значение " + varName +
" не было найдено ни в application.properties, ни в environment переменной");
if (value == null) {
AkitaScenario.getInstance().write(
"Значение " + varName +
" не было найдено ни в application.properties, ни в environment переменной");
}
newString = m.replaceFirst(value);
if (isJSONValid(newString)) return newString;
m = p.matcher(newString);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/ru/alfabank/steps/DefaultApiSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ private RequestSender createRequest(List<RequestParam> paramsTable) {
String body = null;
RequestSpecification request = given();
for (RequestParam requestParam : paramsTable) {
String value = resolveJsonVars(requestParam.getValue());
String name = requestParam.getName();
String value = requestParam.getValue();
switch (requestParam.getType()) {
case PARAMETER:
request.param(name, value);
Expand All @@ -174,7 +174,9 @@ private RequestSender createRequest(List<RequestParam> paramsTable) {
request.header(name, value);
break;
case BODY:
request.body(loadValueFromFileOrPropertyOrVariableOrDefault(value));
value = loadValueFromFileOrPropertyOrVariableOrDefault(value);
body = resolveJsonVars(value);
request.body(body);
break;
default:
throw new IllegalArgumentException(String.format("Некорректно задан тип %s для параметра запроса %s ", requestParam.getType(), name));
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/ru/alfabank/steps/ApiStepsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ public void sendHttpRequestWithVarsPost() throws java.lang.Exception {
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"))
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ bodyValue={"property":"body"}
var2="2"
var3=3
titleFromProperty=Title
bodyWithParams1=Jack
bodyWithParams2=35

strJson = {"object1" : { "array" : ["stringInArray", 0.003, true, false, null], "innerObject": {"str": "qwer"} }, "object2": {"number" : 0.003, "string" : "stringValue", "boolean" : true, "nullName" : null}, "object3": {"number": -3579.09}}
strTemplate = {"name": "_name_", "age": _age_}
1 change: 1 addition & 0 deletions src/test/resources/bodyWithParams.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"person": {"name": "{bodyWithParams1}", "age": {bodyWithParams2}}, "object": {"var1": 1}}

0 comments on commit 471d7d1

Please sign in to comment.