|
| 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