Skip to content

Commit

Permalink
test: add cucumber steps for checking JSON reports
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rdeltour committed Apr 21, 2023
1 parent c075143 commit 13a11b9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Expand Up @@ -160,6 +160,11 @@
<version>1.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
Expand Down Expand Up @@ -190,6 +195,11 @@
<version>2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>

<build>
Expand Down
54 changes: 54 additions & 0 deletions 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())));
}
}

0 comments on commit 13a11b9

Please sign in to comment.