NonNullableUnionValidatingVisitor implements Visitor<Void> { @Override public void onVisit(Schema schema, Collection<String> breadcrumb) { if (Type.UNION == schema.getType()) { List<Schema> types = schema.getTypes(); if (types.size() != 2 || (types.get(0).getType() != NULL && types.get(1).getType() != NULL)) { String path = breadcrumb.stream().collect(joining("/", "/", "")); throw new IllegalArgumentException(String .format("Only union[any, null] or union[null, any] are supported. At path %s got %s", path, schema)); } } }  @Override void onVisit(Schema schema, Collection<String> breadcrumb); @Override void onVisit(Field field, Collection<String> breadcrumb); @Override Void getResult();  }
@Test public void notAUnion() throws Exception { Schema schema = SchemaBuilder.builder().intType(); underTest.onVisit(schema, breadcrumb); }
@Test(expected = IllegalArgumentException.class) public void unionNotSize2() throws Exception { Schema schema = SchemaBuilder.unionOf().intType().endUnion(); underTest.onVisit(schema, breadcrumb); }
@Test public void nullableUnion() throws Exception { Schema schema = SchemaBuilder.builder().nullable().intType(); underTest.onVisit(schema, breadcrumb); }
@Test public void optionalUnion() throws Exception { Schema schema = SchemaBuilder.unionOf().nullType().and().intType().endUnion(); underTest.onVisit(schema, breadcrumb); }
@Test(expected = IllegalArgumentException.class) public void nonNullableUnion() throws Exception { Schema schema = SchemaBuilder.unionOf().stringType().and().intType().endUnion(); underTest.onVisit(schema, breadcrumb); }
@Test public void field() { try { underTest.onVisit((Field) null, breadcrumb); } catch (Exception e) { fail(); } }