From 13a11b9a55b1d870f11814cd59e2bea88740979a Mon Sep 17 00:00:00 2001 From: Romain Deltour Date: Fri, 21 Apr 2023 13:50:24 +0200 Subject: [PATCH] test: add cucumber steps for checking JSON reports Add the following Cucumber steps: - `Then the JSON report is valid` - `Then JSON at {string} contains {int} items` - `Then JSON at {string} has no null values` - `Then JSON at {string} is not null` - `Then JSON at {string} is {string}` JSON values or arrays are inspected using expressions based on a JSON path DSL, and asserted with the [json-path-assert](https://github.com/json-path/JsonPath) library. --- pom.xml | 10 ++++ .../test/JSONReportAssertionSteps.java | 54 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/test/java/org/w3c/epubcheck/test/JSONReportAssertionSteps.java diff --git a/pom.xml b/pom.xml index 129298fdd..f4aa923b0 100644 --- a/pom.xml +++ b/pom.xml @@ -160,6 +160,11 @@ 1.1.1 test + + com.jayway.jsonpath + json-path-assert + 2.8.0 + xmlunit xmlunit @@ -190,6 +195,11 @@ 2.1 test + + org.slf4j + slf4j-nop + 1.7.36 + diff --git a/src/test/java/org/w3c/epubcheck/test/JSONReportAssertionSteps.java b/src/test/java/org/w3c/epubcheck/test/JSONReportAssertionSteps.java new file mode 100644 index 000000000..6064d48ff --- /dev/null +++ b/src/test/java/org/w3c/epubcheck/test/JSONReportAssertionSteps.java @@ -0,0 +1,54 @@ +package org.w3c.epubcheck.test; + +import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath; +import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJson; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.everyItem; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; + +import io.cucumber.java.en.Then; + +public class JSONReportAssertionSteps +{ + + private TestReport report; + + public JSONReportAssertionSteps(TestConfiguration configuration) + { + this.report = configuration.getReport(); + } + + @Then("the JSON report is valid") + public void jsonIsValid() + { + assertThat(report.getOutput(), isJson()); + } + + @Then("JSON at {string} contains {int} items") + public void jsonArrayHasSize(String path, int size) + { + assertThat(report.getOutput(), hasJsonPath(path, hasSize(size))); + } + + @Then("JSON at {string} is {string}") + public void jsonValueIs(String path, String value) + { + assertThat(report.getOutput(), hasJsonPath(path, equalTo(value))); + } + + @Then("JSON at {string} is not null") + public void jsonValueIsNotNull(String path) + { + assertThat(report.getOutput(), hasJsonPath(path, not(nullValue()))); + } + + @Then("JSON at {string} has no null values") + public void jsonValuesAreNotNull(String path) + { + assertThat(report.getOutput(), hasJsonPath(path, everyItem(notNullValue()))); + } +}