Skip to content

Commit 67583c7

Browse files
committed
Verify single post response against file
1 parent 8a689e7 commit 67583c7

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/test/java/com/typicode/jsonplaceholder/features/Posts.feature

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ Feature: Posts Endpoint
77
And the results array contains 100 elements
88
And the response body matches the "GetAllPosts" expected response
99

10+
Scenario: Verify single post against expected response in file
11+
When I make a GET request to the Posts endpoint with a path parameter of 1
12+
Then the response has a status code of 200
13+
And the response body follows the "GetSinglePost" JSON schema
14+
And the response body matches the 1st post in the "GetAllPosts" expected response
15+

src/test/java/com/typicode/jsonplaceholder/steps/CommonSteps.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public static void makeGetRequest(String endpoint) {
5757
response = RequestHelpers.sendGetRequestTo(endpoints.get(endpoint));
5858
responses.add(response);
5959
}
60+
@When("^I make a GET request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint with a path parameter of (\\d+)$")
61+
public static void makeGetRequestWithPathParameter(String endpoint, int pathParam) {
62+
response = RequestHelpers.sendGetRequestTo(endpoints.get(endpoint) + "/" + pathParam);
63+
responses.add(response);
64+
}
6065
@Then("the response has a status code of {int}")
6166
public static void verifyResponseStatusCode(int code) {
6267
assertEquals(code, response.statusCode());
@@ -82,11 +87,20 @@ public static void verifyNumberOfResultsArrayElements(int numElements) {
8287
}
8388

8489
@Then("the response body matches the {string} expected response")
85-
public static void verifyResponseBodyAgainstExpectedResponse(String type) throws IOException {
86-
String filename = EXPECTED_RESPONSES_DIR + type.replaceAll(" ", "") + "Response.json";
90+
public static void verifyResponseBodyAgainstExpectedResponse(String expectedResponse) throws IOException {
91+
String filename = EXPECTED_RESPONSES_DIR + expectedResponse.replaceAll(" ", "") + "Response.json";
8792
String json = Files.readString(new File(filename).toPath());
8893
assertEquals(json.replace("\r", ""), response.body());
8994
}
9095

96+
@Then("^the response body matches the (\\d+).{2} post in the \"(.*)\" expected response$")
97+
public static void verifyResponseBodyAgainstPartOfExpectedResponse(int index, String expectedResponse) throws IOException {
98+
String filename = EXPECTED_RESPONSES_DIR + expectedResponse.replaceAll(" ", "") + "Response.json";
99+
String json = Files.readString(new File(filename).toPath());
100+
JSONObject expected = new JSONArray(json).getJSONObject(index - 1);
101+
JSONObject actual = new JSONObject(response.body());
102+
assertEquals(expected.toString(), actual.toString());
103+
}
104+
91105

92106
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"userId": {
5+
"type": "integer"
6+
},
7+
"id": {
8+
"type": "integer"
9+
},
10+
"title": {
11+
"type": "string"
12+
},
13+
"body": {
14+
"type": "string"
15+
}
16+
},
17+
"required": [
18+
"userId",
19+
"id",
20+
"title",
21+
"body"
22+
],
23+
"additionalProperties": false
24+
}

0 commit comments

Comments
 (0)