Skip to content

Commit

Permalink
Update tests to handle yaml test data and not fail on other types
Browse files Browse the repository at this point in the history
Looks like SchemaStore now has test schemas written in yaml and other formats.  I've updated `SchemaStoreTest` to handle the yaml cases, and ignore any others.
  • Loading branch information
big-andy-coates committed Jan 24, 2024
1 parent c7582b8 commit 3253521
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.0'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.0'
testImplementation 'org.json:json:20210307'
testImplementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.16.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
Expand All @@ -22,6 +24,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -46,6 +49,7 @@ Collection<DynamicNode> allNegative() {

private Collection<DynamicNode> test(String dirName, boolean mustFail) {
ObjectMapper objectMapper = new ObjectMapper();
ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory().enable(YAMLGenerator.Feature.MINIMIZE_QUOTES));
DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
ObjectWriter objectWriter = objectMapper.writer(prettyPrinter);
Expand Down Expand Up @@ -76,7 +80,19 @@ private Collection<DynamicNode> test(String dirName, boolean mustFail) {

URI testSourceUri = URI.create(testDataUrl.toString());
tests.add(DynamicTest.dynamicTest(testFileName, testSourceUri, () -> {
Object o = objectMapper.readValue(
ObjectMapper mapper = null;
if (testFile.toString().endsWith(".json")) {
mapper = objectMapper;
}
if (testFile.toString().endsWith(".yml") || testFile.toString().endsWith(".yaml")) {
mapper = yamlMapper;
}

if (mapper == null) {
return; // Unsupported test file format.
}

Object o = mapper.readValue(
getResourceAsStream(SchemaStoreTest.class, testFile.toString()), Object.class);
Path errorsPath =
FILE_SYSTEM.getPath("/schemaStoreErrors").resolve(schemaName).resolve(testFileName);
Expand All @@ -91,7 +107,7 @@ private Collection<DynamicNode> test(String dirName, boolean mustFail) {
Collection<String> extraReported = new LinkedHashSet<>();

SchemaStore schemaStore = new SchemaStore(true);
Collection<String> allErrors = new ArrayList<>();
List<String> allErrors = new ArrayList<>();
Consumer<ValidationError> errorHandler = error -> {
System.out.println(error);
String e = error.getUri().toString();
Expand Down Expand Up @@ -122,6 +138,7 @@ private Collection<DynamicNode> test(String dirName, boolean mustFail) {
} else {
Path schemaStoreErrors = FILE_SYSTEM.getPath("schemaStoreErrors");
Files.createDirectories(schemaStoreErrors.resolve(schemaName));
allErrors.sort(Comparator.naturalOrder());
try (FileWriter w = new FileWriter(
schemaStoreErrors.resolve(schemaName).resolve(testFileName).toString())) {
objectWriter.writeValue(w, allErrors);
Expand Down
5 changes: 4 additions & 1 deletion library/src/test/resources/suites/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Update JSON-Schema-Test-Suite with

git submodule update --remote
First time:
`git submodule update --init`
Subsequent times:
`git submodule update --remote`

0 comments on commit 3253521

Please sign in to comment.