/** * Get a schema for a given {@link Class} type. If no schema exists, throws {@link * NoSuchSchemaException}. */ public <T> Schema getSchema(Class<T> clazz) throws NoSuchSchemaException { return getSchema(TypeDescriptor.of(clazz)); }
@Test(expected = IllegalArgumentException.class) @Category(NeedsRunner.class) public void testWeakedNullableFail() throws Exception { Schema inputSchema = pipeline.getSchemaRegistry().getSchema(Nullable1.class); Schema outputSchema = pipeline.getSchemaRegistry().getSchema(Nullable2.class); Cast.widening(outputSchema).verifyCompatibility(inputSchema); }
@Test public void testSchema() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Schema schema = registry.getSchema(SimpleBean.class); SchemaTestUtils.assertSchemaEquivalent(SIMPLE_BEAN_SCHEMA, schema); }
@Test(expected = IllegalArgumentException.class) @Category(NeedsRunner.class) public void testIgnoreNullableFail() throws Exception { // ignoring nullable is opposite of weakening Schema inputSchema = pipeline.getSchemaRegistry().getSchema(Nullable2.class); Schema outputSchema = pipeline.getSchemaRegistry().getSchema(Nullable1.class); Cast.widening(outputSchema).verifyCompatibility(inputSchema); }
@Test public void testSchema() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); Schema schema = registry.getSchema(SimplePOJO.class); SchemaTestUtils.assertSchemaEquivalent(SIMPLE_POJO_SCHEMA, schema); }
@Test public void testAutoSchemaProvider() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); assertEquals(EMPTY_SCHEMA, registry.getSchema(TestSchemaClass.class)); }
@Test public void testDefaultSchemaProvider() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); assertEquals(EMPTY_SCHEMA, registry.getSchema(TestDefaultSchemaClass.class)); }
@Test(expected = IllegalArgumentException.class) @Category(NeedsRunner.class) public void testTypeNarrowFail() throws Exception { // narrowing is the opposite of widening Schema inputSchema = pipeline.getSchemaRegistry().getSchema(TypeWiden2.class); Schema outputSchema = pipeline.getSchemaRegistry().getSchema(TypeWiden1.class); Cast.narrowing(outputSchema).verifyCompatibility(inputSchema); }
@Test(expected = IllegalArgumentException.class) @Category(NeedsRunner.class) public void testComplexCastFail() throws Exception { Schema inputSchema = pipeline.getSchemaRegistry().getSchema(All1.class); Schema outputSchema = pipeline.getSchemaRegistry().getSchema(All2.class); Cast.widening(outputSchema).verifyCompatibility(inputSchema); }
@Test public void testRegisterPojo() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); registry.registerPOJO(SimplePOJO.class); Schema schema = registry.getSchema(SimplePOJO.class); assertTrue(SIMPLE_POJO_SCHEMA.equivalent(schema)); }
@Test public void testRegisterJavaBean() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); registry.registerJavaBean(SimpleBean.class); Schema schema = registry.getSchema(SimpleBean.class); assertTrue(SIMPLE_BEAN_SCHEMA.equivalent(schema)); } }
@Test public void testRecursiveArrayGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); SchemaTestUtils.assertSchemaEquivalent( NESTED_ARRAY_BEAN_SCHEMA, registry.getSchema(NestedArrayBean.class)); SimpleBean simple1 = createSimple("string1"); SimpleBean simple2 = createSimple("string2"); SimpleBean simple3 = createSimple("string3"); NestedArrayBean bean = new NestedArrayBean(simple1, simple2, simple3); Row row = registry.getToRowFunction(NestedArrayBean.class).apply(bean); List<Row> rows = row.getArray("beans"); assertSame(simple1, registry.getFromRowFunction(NestedArrayBean.class).apply(rows.get(0))); assertSame(simple2, registry.getFromRowFunction(NestedArrayBean.class).apply(rows.get(1))); assertSame(simple3, registry.getFromRowFunction(NestedArrayBean.class).apply(rows.get(2))); }
@Test public void testNestedArraysGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); SchemaTestUtils.assertSchemaEquivalent( NESTED_ARRAYS_POJO_SCHEMA, registry.getSchema(NestedArraysPOJO.class)); List<List<String>> listOfLists = Lists.newArrayList( Lists.newArrayList("a", "b", "c"), Lists.newArrayList("d", "e", "f"), Lists.newArrayList("g", "h", "i")); NestedArraysPOJO pojo = new NestedArraysPOJO(listOfLists); Row row = registry.getToRowFunction(NestedArraysPOJO.class).apply(pojo); assertEquals(listOfLists, row.getArray("lists")); }
@Test public void testNestedArraysGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); SchemaTestUtils.assertSchemaEquivalent( NESTED_ARRAYS_BEAM_SCHEMA, registry.getSchema(NestedArraysBean.class)); List<List<String>> listOfLists = Lists.newArrayList( Lists.newArrayList("a", "b", "c"), Lists.newArrayList("d", "e", "f"), Lists.newArrayList("g", "h", "i")); NestedArraysBean bean = new NestedArraysBean(listOfLists); Row row = registry.getToRowFunction(NestedArraysBean.class).apply(bean); assertEquals(listOfLists, row.getArray("lists")); }
@Test @Category(NeedsRunner.class) public void testTypeWiden() throws Exception { Schema outputSchema = pipeline.getSchemaRegistry().getSchema(TypeWiden2.class); PCollection<TypeWiden2> pojos = pipeline .apply(Create.of(new TypeWiden1())) .apply(Cast.widening(outputSchema)) .apply(Convert.to(TypeWiden2.class)); PAssert.that(pojos).containsInAnyOrder(new TypeWiden2()); pipeline.run(); }
@Test @Category(NeedsRunner.class) public void testWeakedNullable() throws Exception { Schema outputSchema = pipeline.getSchemaRegistry().getSchema(Nullable2.class); PCollection<Nullable2> pojos = pipeline .apply(Create.of(new Nullable1())) .apply(Cast.narrowing(outputSchema)) .apply(Convert.to(Nullable2.class)); PAssert.that(pojos).containsInAnyOrder(new Nullable2()); pipeline.run(); }
@Test @Category(NeedsRunner.class) public void testProjection() throws Exception { Schema outputSchema = pipeline.getSchemaRegistry().getSchema(Projection2.class); PCollection<Projection2> pojos = pipeline .apply(Create.of(new Projection1())) .apply(Cast.widening(outputSchema)) .apply(Convert.to(Projection2.class)); PAssert.that(pojos).containsInAnyOrder(new Projection2()); pipeline.run(); }
@Test @Category(NeedsRunner.class) public void testTypeNarrow() throws Exception { // narrowing is the opposite of widening Schema outputSchema = pipeline.getSchemaRegistry().getSchema(TypeWiden1.class); PCollection<TypeWiden1> pojos = pipeline .apply(Create.of(new TypeWiden2())) .apply(Cast.narrowing(outputSchema)) .apply(Convert.to(TypeWiden1.class)); PAssert.that(pojos).containsInAnyOrder(new TypeWiden1()); pipeline.run(); }
@Test @Category(NeedsRunner.class) public void testComplexCast() throws Exception { Schema outputSchema = pipeline.getSchemaRegistry().getSchema(All2.class); PCollection<All2> pojos = pipeline .apply(Create.of(new All1())) .apply(Cast.narrowing(outputSchema)) .apply(Convert.to(All2.class)); PAssert.that(pojos).containsInAnyOrder(new All2()); pipeline.run(); }
@Test @Category(NeedsRunner.class) public void testIgnoreNullable() throws Exception { // ignoring nullable is opposite of weakening Schema outputSchema = pipeline.getSchemaRegistry().getSchema(Nullable1.class); PCollection<Nullable1> pojos = pipeline .apply(Create.of(new Nullable2())) .apply(Cast.narrowing(outputSchema)) .apply(Convert.to(Nullable1.class)); PAssert.that(pojos).containsInAnyOrder(new Nullable1()); pipeline.run(); }