KeyPathValidator { public void validate() throws InvalidKeyPathException { List<Element> elements = path.elements(); validateElement(elements, rootSchema, elements.get(0).toString()); } KeyPathValidator(Path path, Schema schema); void validate();  }
@Test public void rootPath() throws InvalidKeyPathException { Path path = KeyPathParser.parse("$"); Schema schema = Schema.create(Type.STRING); new KeyPathValidator(path, schema).validate(); }
@Test public void nested() throws InvalidKeyPathException { Path path = KeyPathParser.parse("$[\"a\"][\"b\"].c"); Schema schema = SchemaBuilder .record("X") .fields() .name("a") .type(SchemaBuilder .record("Y") .fields() .name("b") .type(SchemaBuilder.record("Z").fields().name("c").type(Schema.create(Type.STRING)).noDefault().endRecord()) .noDefault() .endRecord()) .noDefault() .endRecord(); new KeyPathValidator(path, schema).validate(); }
@Test(expected = InvalidKeyPathException.class) public void mapFail() throws InvalidKeyPathException { Path path = KeyPathParser.parse("$.a.b[\"c\"]"); Schema schema = SchemaBuilder .record("X") .fields() .name("a") .type(SchemaBuilder.map().values( SchemaBuilder.record("Z").fields().name("c").type(Schema.create(Type.STRING)).noDefault().endRecord())) .noDefault() .endRecord(); try { new KeyPathValidator(path, schema).validate(); } catch (Exception e) { assertThat(e.getMessage(), is("Element '$.a' not a traversable type (found 'MAP'), in path: $.a.b[\"c\"]")); throw e; } }
@Test(expected = InvalidKeyPathException.class) public void arrayFail() throws InvalidKeyPathException { Path path = KeyPathParser.parse("$.a.b.c"); Schema schema = SchemaBuilder .record("X") .fields() .name("a") .type(SchemaBuilder.array().items( SchemaBuilder.record("Z").fields().name("c").type(Schema.create(Type.STRING)).noDefault().endRecord())) .noDefault() .endRecord(); try { new KeyPathValidator(path, schema).validate(); } catch (Exception e) { assertThat(e.getMessage(), is("Element '$.a' not a traversable type (found 'ARRAY'), in path: $.a.b.c")); throw e; } }
@Test public void unionAllBranchesOk() throws InvalidKeyPathException { Path path = KeyPathParser.parse("$[\"a\"][\"c\"]"); Schema schema = SchemaBuilder .record("X") .fields() .name("a") .type(SchemaBuilder .unionOf() .type(SchemaBuilder.record("Z").fields().name("c").type(Schema.create(Type.STRING)).noDefault().endRecord()) .and() .type(SchemaBuilder.record("K").fields().name("c").type(Schema.create(Type.INT)).noDefault().endRecord()) .endUnion()) .noDefault() .endRecord(); new KeyPathValidator(path, schema).validate(); }
@Test(expected = InvalidKeyPathException.class) public void unionBranchIncompatible() throws InvalidKeyPathException { Path path = KeyPathParser.parse("$.a.c"); Schema schema = SchemaBuilder .record("X") .fields() .name("a") .type(SchemaBuilder .unionOf() .type(SchemaBuilder.record("Z").fields().name("c").type(Schema.create(Type.STRING)).noDefault().endRecord()) .and() .type(Schema.create(Type.INT)) .endUnion()) .noDefault() .endRecord(); try { new KeyPathValidator(path, schema).validate(); } catch (Exception e) { assertThat(e.getMessage(), is("Element '$.a(UnionIndex:1)' not a traversable type (found 'INT'), in path: $.a.c")); throw e; } }
@Test public void smallerNested() throws InvalidKeyPathException { Path path = KeyPathParser.parse("$[\"a\"][\"b\"]"); Schema schema = SchemaBuilder .record("X") .fields() .name("a") .type(SchemaBuilder.record("Y").fields().name("b").type(Schema.create(Type.STRING)).noDefault().endRecord()) .noDefault() .endRecord(); new KeyPathValidator(path, schema).validate(); }
@Test public void smallestNested() throws InvalidKeyPathException { Path path = KeyPathParser.parse("$[\"a\"]"); Schema schema = SchemaBuilder .record("X") .fields() .name("a") .type(Schema.create(Type.STRING)) .noDefault() .endRecord(); new KeyPathValidator(path, schema).validate(); }
@Test(expected = InvalidKeyPathException.class) public void rootNotARecord() throws InvalidKeyPathException { Path path = KeyPathParser.parse("$[\"a\"]"); Schema schema = SchemaBuilder.array().items(Schema.create(Type.STRING)); try { new KeyPathValidator(path, schema).validate(); } catch (Exception e) { assertThat(e.getMessage(), is("Element '$' not a traversable type (found 'ARRAY'), in path: $[\"a\"]")); throw e; } }
@Test(expected = InvalidKeyPathException.class) public void nestedFail() throws InvalidKeyPathException { Path path = KeyPathParser.parse("$[\"a\"].b[\"c\"]"); Schema schema = SchemaBuilder .record("X") .fields() .name("a") .type(SchemaBuilder.record("Y").fields().name("b").type(Schema.create(Type.STRING)).noDefault().endRecord()) .noDefault() .endRecord(); try { new KeyPathValidator(path, schema).validate(); } catch (Exception e) { assertThat(e.getMessage(), is("Element '$[\"a\"].b' not a traversable type (found 'STRING'), in path: $[\"a\"].b[\"c\"]")); throw e; } }