Skip to content

Commit 8a689e7

Browse files
committed
Get All Posts test
1 parent 65c7c88 commit 8a689e7

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Feature: Posts Endpoint
2+
3+
Scenario: Get all posts
4+
When I make a GET request to the Posts endpoint
5+
Then the response has a status code of 200
6+
And the response body follows the "GetAllPosts" JSON schema
7+
And the results array contains 100 elements
8+
And the response body matches the "GetAllPosts" expected response
9+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.typicode.jsonplaceholder.helpers;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.http.HttpClient;
6+
import java.net.http.HttpRequest;
7+
import java.net.http.HttpResponse;
8+
9+
public class RequestHelpers {
10+
11+
private static final HttpClient httpClient = HttpClient.newBuilder().build();
12+
private static final String BASE_URL = "https://jsonplaceholder.typicode.com";
13+
14+
public static HttpResponse<String> sendGetRequestTo(String endpoint) {
15+
HttpRequest request = HttpRequest.newBuilder()
16+
.GET()
17+
.uri(URI.create(BASE_URL + endpoint))
18+
.build();
19+
try {
20+
return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
21+
} catch (IOException | InterruptedException e) {
22+
e.printStackTrace();
23+
return null;
24+
}
25+
}
26+
27+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.typicode.jsonplaceholder.steps;
2+
3+
import com.typicode.jsonplaceholder.helpers.RequestHelpers;
4+
import io.cucumber.datatable.DataTable;
5+
import io.cucumber.java.Before;
6+
import io.cucumber.java.en.Given;
7+
import io.cucumber.java.en.Then;
8+
import io.cucumber.java.en.When;
9+
import org.everit.json.schema.Schema;
10+
import org.everit.json.schema.loader.SchemaLoader;
11+
import org.json.JSONArray;
12+
import org.json.JSONObject;
13+
14+
import static org.junit.Assert.assertEquals;
15+
16+
import java.io.File;
17+
import java.io.IOException;
18+
import java.net.http.HttpResponse;
19+
import java.nio.file.Files;
20+
import java.util.ArrayList;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
public class CommonSteps {
26+
27+
private static final String POSTS_ENDPOINT = "/posts";
28+
private static final String COMMENTS_ENDPOINT = "/comments";
29+
private static final String ALBUMS_ENDPOINT = "/albums";
30+
private static final String PHOTOS_ENDPOINT = "/photos";
31+
private static final String TODOS_ENDPOINT = "/todos";
32+
private static final String USERS_ENDPOINT = "/users";
33+
34+
private static final Map<String, String> endpoints = Map.ofEntries(
35+
Map.entry("Posts", POSTS_ENDPOINT),
36+
Map.entry("Comments", COMMENTS_ENDPOINT),
37+
Map.entry("Albums", ALBUMS_ENDPOINT),
38+
Map.entry("Photos", PHOTOS_ENDPOINT),
39+
Map.entry("ToDos", TODOS_ENDPOINT),
40+
Map.entry("Users", USERS_ENDPOINT)
41+
);
42+
43+
private static final String BASE_RESOURCES_DIR = "src/test/resources/";
44+
private static final String SCHEMAS_DIR = BASE_RESOURCES_DIR + "schemas/";
45+
private static final String EXPECTED_RESPONSES_DIR = BASE_RESOURCES_DIR + "expectedResponses/";
46+
47+
public static HttpResponse<String> response;
48+
public static List<HttpResponse<String>> responses;
49+
50+
@Before
51+
public static void setup() {
52+
responses = new ArrayList<>();
53+
}
54+
55+
@When("^I make a GET request to the (Posts|Comments|Albums|Photos|ToDos|Users) endpoint$")
56+
public static void makeGetRequest(String endpoint) {
57+
response = RequestHelpers.sendGetRequestTo(endpoints.get(endpoint));
58+
responses.add(response);
59+
}
60+
@Then("the response has a status code of {int}")
61+
public static void verifyResponseStatusCode(int code) {
62+
assertEquals(code, response.statusCode());
63+
}
64+
65+
@Then("the response body follows the {string} JSON schema")
66+
public static void verifyResponseBodyAgainstJsonSchema(String type) throws IOException {
67+
String filename = SCHEMAS_DIR + type.replaceAll(" ", "") + "Schema.json";
68+
String json = Files.readString(new File(filename).toPath());
69+
JSONObject schemaObject = new JSONObject(json);
70+
Schema expectedSchema = SchemaLoader.load(schemaObject);
71+
if (response.body().startsWith("[")) {
72+
expectedSchema.validate(new JSONArray(response.body()));
73+
} else {
74+
expectedSchema.validate(new JSONObject(response.body()));
75+
}
76+
}
77+
78+
@Then("the results array contains {int} elements")
79+
public static void verifyNumberOfResultsArrayElements(int numElements) {
80+
JSONArray results = new JSONArray (response.body());
81+
assertEquals(numElements, results.length());
82+
}
83+
84+
@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";
87+
String json = Files.readString(new File(filename).toPath());
88+
assertEquals(json.replace("\r", ""), response.body());
89+
}
90+
91+
92+
}

0 commit comments

Comments
 (0)