-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommonAssertions.java
135 lines (112 loc) · 6.64 KB
/
CommonAssertions.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
package testSuit.stepDef;
import com.aventstack.extentreports.Status;
import com.google.inject.Inject;
import io.cucumber.java.en.Then;
import io.restassured.module.jsv.JsonSchemaValidator;
import io.restassured.response.Response;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.comparator.CustomComparator;
import org.testng.Assert;
import testSuit.utils.ReporterFactory;
import testSuit.utils.ScenarioContext;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author Abhishek Kadavil
*/
@Slf4j
public class CommonAssertions {
@Inject
ScenarioContext scenarioContext;
@SneakyThrows
@Then("should have response schema as {string}")
public void should_have_response_schema(String expectedResSchema) {
Response response = scenarioContext.getResponseContext().get(scenarioContext.getReqId());
String responseSchema = new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir") + "/src/test/resources/testData" + expectedResSchema)));
response.then().assertThat().body(JsonSchemaValidator.matchesJsonSchema(responseSchema));
ReporterFactory.getInstance().getExtentTest().log(Status.PASS, "Response schema validation passed");
}
@Then("response code should be {string}")
public void should_have_response_code(String expectedResCode) {
String actualValueResponseStatusCode =
String.valueOf(scenarioContext.getResponseContext().get(scenarioContext.getReqId()).getStatusCode());
Assert.assertEquals(actualValueResponseStatusCode, expectedResCode);
ReporterFactory.getInstance().getExtentTest().log(Status.PASS, "Response code validation passed");
}
@SneakyThrows
@Then("response body should be {string} ignoring all extra fields")
public void should_have_response_body_with_ignoring_all_extra_fields(String expectedValue) {
String expectedRes =
new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir") + "/src/test/resources/testData" + expectedValue)));
String actualRes = scenarioContext.getResponseContext().get(scenarioContext.getReqId()).body().asString();
JSONAssert.assertEquals(expectedRes, actualRes, false);
ReporterFactory.getInstance().getExtentTest().log(Status.PASS, "Response body validation passed");
}
@SneakyThrows
@Then("response body should be non extensible {string}")
public void should_have_response_body_NON_EXTENSIBLE(String expectedValue) {
String expectedRes =
new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir") + "/src/test/resources/testData" + expectedValue)));
String actualRes = scenarioContext.getResponseContext().get(scenarioContext.getReqId()).body().asString();
JSONAssert.assertEquals(expectedRes, actualRes, JSONCompareMode.NON_EXTENSIBLE);
ReporterFactory.getInstance().getExtentTest().log(Status.PASS, "Response body should be same as in " + expectedValue);
}
@SneakyThrows
@Then("response body should be {string}")
public void should_have_response_body(String expectedValue) {
String expectedRes =
new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir") + "/src/test/resources/testData" + expectedValue)));
String actualRes = scenarioContext.getResponseContext().get(scenarioContext.getReqId()).body().asString();
JSONAssert.assertEquals(expectedRes, actualRes, true);
ReporterFactory.getInstance().getExtentTest().log(Status.PASS, "Response body validation passed");
}
@SneakyThrows
@Then("response body should be {string} ignoring specified fields")
public void should_have_response_body_with_ignoring_specified_fields(String expectedValue, List<List<String>> cols) {
String expectedRes =
new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir") + "/src/test/resources/testData" + expectedValue)));
String actualRes = scenarioContext.getResponseContext().get(scenarioContext.getReqId()).body().asString();
List<String> row = cols.get(0);
Customization[] customizationArray = new Customization[row.size()];
int i = 0;
for (String element : row) {
customizationArray[i] = new Customization(element, (o1, o2) -> true);
i++;
}
JSONAssert.assertEquals(expectedRes, actualRes,
new CustomComparator(JSONCompareMode.LENIENT, customizationArray));
ReporterFactory.getInstance().getExtentTest().log(Status.PASS, "Response body validation passed");
}
@Then("response should have {string} as {string}")
public void should_have_value_in_path(String jsonpath, String expectedValue) {
String actualValue = scenarioContext.getResponseContext().get(scenarioContext.getReqId()).then().extract().path(jsonpath).toString();
Assert.assertEquals(actualValue, expectedValue);
ReporterFactory.getInstance().getExtentTest().log(Status.PASS, "Response field validation passed");
}
@Then("response should have scenario context value {string} in path {string}")
public void response_should_have_scenario_context_value_in_path(String contextKey, String jsonpath) {
String extractedValue = scenarioContext.getResponseContext().get(scenarioContext.getReqId()).then().extract().path(jsonpath).toString();
String valueToBeCompared = scenarioContext.getContextValues().get(contextKey);
Assert.assertEquals(extractedValue, valueToBeCompared);
ReporterFactory.getInstance().getExtentTest().log(Status.PASS, "Response field validation passed");
}
@Then("response should have scenario context value {string} in array path {string}")
public void response_should_have_scenario_context_value_in_array_path(String contextKey, String jsonpath) {
String valueToBeCompared = scenarioContext.getContextValues().get(contextKey);
List<?> list =
scenarioContext.getResponseContext().get(scenarioContext.getReqId()).then().extract().jsonPath().getList(jsonpath);
List<String> stringsList = list.stream()
.map(object -> Objects.toString(object, null))
.collect(Collectors.toList());
Assert.assertTrue(stringsList.contains(valueToBeCompared));
ReporterFactory.getInstance().getExtentTest().log(Status.PASS,
"Response have " + valueToBeCompared + " in json path " + jsonpath);
}
}