Skip to content

Commit

Permalink
New test to see if relative paths from schemas addressed with absolut…
Browse files Browse the repository at this point in the history
…e URIs are followed correctly.
  • Loading branch information
gitgrimbo committed Sep 1, 2012
1 parent 9900080 commit 986e0f8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/test/java/grimbo/NonAbsoluteSchemaUris.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,8 @@ public InputStream fetch(URI source) throws IOException {
public void testEmptyObject() throws Exception {
ValidationReport r = doValidate("/grimbo/child1/child.json", "/grimbo/empty-object.json");
printReport(r);
JsonNode errors = r.asJsonNode().get("");
assertEquals(errors.size(), 2);
Set<String> expected = new HashSet<String>(Arrays.asList("header", "body"));
for (int i = errors.size() - 1; i >= 0; i--) {
JsonNode error = errors.get(i);
String missing = error.get("missing").get(0).asText();
assertTrue(expected.remove(missing));
}
assertTrue(expected.isEmpty());
Set<String> expectedMissing = new HashSet<String>(Arrays.asList("header", "body"));
assertPropertiesMissing(r, "", expectedMissing);
}

@Test
Expand All @@ -78,9 +71,31 @@ public void testTestObject() throws Exception {
public void testTestObjectNoBodyItem() throws Exception {
ValidationReport r = doValidate("/grimbo/child1/child.json", "/grimbo/test-object-no-bodyItem.json");
printReport(r);
Set<String> expectedMissing = new HashSet<String>(Arrays.asList("bodyItem"));
assertPropertiesMissing(r, "/body", expectedMissing);
}

@Test
public void testAbsoluteUriForParent() throws Exception {
// Test that a schema can refer to an absolute schema.
// And that any relative refs from the absolute schema, are interpreted
// relative to that schema.
ValidationReport r = doValidate("/grimbo/fake/absolute/location/child.json", "/grimbo/test-object.json");
printReport(r);
assertTrue(r.isSuccess());
}

private void assertPropertiesMissing(ValidationReport report, String path, Set<String> missing) {
JsonNode errors = report.asJsonNode().get(path);
assertEquals(errors.size(), missing.size(), "Number of errors same");
for (int i = errors.size() - 1; i >= 0; i--) {
JsonNode error = errors.get(i);
String prop = error.get("missing").get(0).asText();
assertTrue(missing.remove(prop));
}
assertTrue(missing.isEmpty());
}

private ValidationReport doValidate(String schemaResource, String dataResource) throws Exception {
// will get "file://" URIs for the schema and the data file.
return doValidate(getClass().getResource(schemaResource), getClass().getResource(dataResource));
Expand Down
14 changes: 14 additions & 0 deletions src/test/resources/grimbo/fake/absolute/location/child.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"type": "object",
"extends": {
"$ref": "file:/C:/Users/grimbo/git/json-schema-validator/target/test-classes/grimbo/common/parent.json"
},
"$schema": "http://json-schema.org/draft-03/schema",
"id": "child",
"properties": {
"body": {
"$ref": "common/child-body.json",
"required": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "object",
"$schema": "http://json-schema.org/draft-03/schema",
"id": "child-body",
"properties": {
"bodyItem": {
"type": "string",
"required": true
}
}
}

0 comments on commit 986e0f8

Please sign in to comment.