Search in sources :

Example 1 with SimplePOJO

use of org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO in project beam by apache.

the class JavaFieldSchemaTest method testFromRowWithGetters.

@Test
public void testFromRowWithGetters() throws NoSuchSchemaException {
    SchemaRegistry registry = SchemaRegistry.createDefault();
    SimplePOJO pojo = createSimple("string");
    Row row = registry.getToRowFunction(SimplePOJO.class).apply(pojo);
    // Test that the fromRowFunction simply returns the original object back.
    SimplePOJO extracted = registry.getFromRowFunction(SimplePOJO.class).apply(row);
    assertSame(pojo, extracted);
}
Also used : Row(org.apache.beam.sdk.values.Row) SimplePOJO(org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO) Test(org.junit.Test)

Example 2 with SimplePOJO

use of org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO in project beam by apache.

the class JavaFieldSchemaTest method testToRow.

@Test
public void testToRow() throws NoSuchSchemaException {
    SchemaRegistry registry = SchemaRegistry.createDefault();
    SimplePOJO pojo = createSimple("string");
    Row row = registry.getToRowFunction(SimplePOJO.class).apply(pojo);
    assertEquals(12, row.getFieldCount());
    assertEquals("string", row.getString("str"));
    assertEquals((byte) 1, (Object) row.getByte("aByte"));
    assertEquals((short) 2, (Object) row.getInt16("aShort"));
    assertEquals((int) 3, (Object) row.getInt32("anInt"));
    assertEquals((long) 4, (Object) row.getInt64("aLong"));
    assertTrue(row.getBoolean("aBoolean"));
    assertEquals(DATE.toInstant(), row.getDateTime("dateTime"));
    assertEquals(INSTANT, row.getDateTime("instant").toInstant());
    assertArrayEquals(BYTE_ARRAY, row.getBytes("bytes"));
    assertArrayEquals(BYTE_BUFFER.array(), row.getBytes("byteBuffer"));
    assertEquals(BigDecimal.ONE, row.getDecimal("bigDecimal"));
    assertEquals("stringbuilder", row.getString("stringBuilder"));
}
Also used : Row(org.apache.beam.sdk.values.Row) SimplePOJO(org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO) Test(org.junit.Test)

Example 3 with SimplePOJO

use of org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO in project beam by apache.

the class JavaFieldSchemaTest method testNestedArraysToRow.

@Test
public void testNestedArraysToRow() throws NoSuchSchemaException {
    SchemaRegistry registry = SchemaRegistry.createDefault();
    Schema schema = registry.getSchema(PojoWithNestedArray.class);
    SchemaTestUtils.assertSchemaEquivalent(POJO_WITH_NESTED_ARRAY_SCHEMA, schema);
    Row simpleRow = createSimpleRow("string");
    List<Row> list = ImmutableList.of(simpleRow, simpleRow);
    List<List<Row>> listOfList = ImmutableList.of(list, list);
    Row nestedRow = Row.withSchema(POJO_WITH_NESTED_ARRAY_SCHEMA).addValue(listOfList).build();
    SimplePOJO simplePojo = createSimple("string");
    List<SimplePOJO> simplePojoList = ImmutableList.of(simplePojo, simplePojo);
    List<List<SimplePOJO>> simplePojoListOfList = ImmutableList.of(simplePojoList, simplePojoList);
    Row converted = registry.getToRowFunction(PojoWithNestedArray.class).apply(new PojoWithNestedArray(simplePojoListOfList));
    assertEquals(nestedRow, converted);
}
Also used : PojoWithNestedArray(org.apache.beam.sdk.schemas.utils.TestPOJOs.PojoWithNestedArray) List(java.util.List) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Row(org.apache.beam.sdk.values.Row) SimplePOJO(org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO) Test(org.junit.Test)

Example 4 with SimplePOJO

use of org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO in project beam by apache.

the class JavaFieldSchemaTest method testRecursiveArrayGetters.

@Test
public void testRecursiveArrayGetters() throws NoSuchSchemaException {
    SchemaRegistry registry = SchemaRegistry.createDefault();
    SchemaTestUtils.assertSchemaEquivalent(NESTED_ARRAY_POJO_SCHEMA, registry.getSchema(NestedArrayPOJO.class));
    SimplePOJO simple1 = createSimple("string1");
    SimplePOJO simple2 = createSimple("string2");
    SimplePOJO simple3 = createSimple("string3");
    NestedArrayPOJO pojo = new NestedArrayPOJO(simple1, simple2, simple3);
    Row row = registry.getToRowFunction(NestedArrayPOJO.class).apply(pojo);
    List<Row> rows = (List) row.getArray("pojos");
    assertSame(simple1, registry.getFromRowFunction(SimplePOJO.class).apply(rows.get(0)));
    assertSame(simple2, registry.getFromRowFunction(SimplePOJO.class).apply(rows.get(1)));
    assertSame(simple3, registry.getFromRowFunction(SimplePOJO.class).apply(rows.get(2)));
}
Also used : NestedArrayPOJO(org.apache.beam.sdk.schemas.utils.TestPOJOs.NestedArrayPOJO) List(java.util.List) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Row(org.apache.beam.sdk.values.Row) SimplePOJO(org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO) Test(org.junit.Test)

Example 5 with SimplePOJO

use of org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO in project beam by apache.

the class POJOUtilsTest method testGeneratedSimpleSetters.

@Test
public void testGeneratedSimpleSetters() {
    SimplePOJO simplePojo = new SimplePOJO();
    List<FieldValueSetter> setters = POJOUtils.getSetters(SimplePOJO.class, SIMPLE_POJO_SCHEMA, JavaFieldTypeSupplier.INSTANCE, new DefaultTypeConversionsFactory());
    assertEquals(12, setters.size());
    setters.get(0).set(simplePojo, "field1");
    setters.get(1).set(simplePojo, (byte) 41);
    setters.get(2).set(simplePojo, (short) 42);
    setters.get(3).set(simplePojo, (int) 43);
    setters.get(4).set(simplePojo, (long) 44);
    setters.get(5).set(simplePojo, true);
    setters.get(6).set(simplePojo, DATE.toInstant());
    setters.get(7).set(simplePojo, INSTANT);
    setters.get(8).set(simplePojo, BYTE_ARRAY);
    setters.get(9).set(simplePojo, BYTE_BUFFER.array());
    setters.get(10).set(simplePojo, new BigDecimal(42));
    setters.get(11).set(simplePojo, "stringBuilder");
    assertEquals("field1", simplePojo.str);
    assertEquals((byte) 41, simplePojo.aByte);
    assertEquals((short) 42, simplePojo.aShort);
    assertEquals((int) 43, simplePojo.anInt);
    assertEquals((long) 44, simplePojo.aLong);
    assertTrue(simplePojo.aBoolean);
    assertEquals(DATE, simplePojo.dateTime);
    assertEquals(INSTANT, simplePojo.instant);
    assertArrayEquals("Unexpected bytes", BYTE_ARRAY, simplePojo.bytes);
    assertEquals(BYTE_BUFFER, simplePojo.byteBuffer);
    assertEquals(new BigDecimal(42), simplePojo.bigDecimal);
    assertEquals("stringBuilder", simplePojo.stringBuilder.toString());
}
Also used : DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) FieldValueSetter(org.apache.beam.sdk.schemas.FieldValueSetter) SimplePOJO(org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

SimplePOJO (org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO)9 Test (org.junit.Test)9 Row (org.apache.beam.sdk.values.Row)7 List (java.util.List)3 ImmutableList (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList)3 BigDecimal (java.math.BigDecimal)2 DefaultTypeConversionsFactory (org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory)2 PojoWithNestedArray (org.apache.beam.sdk.schemas.utils.TestPOJOs.PojoWithNestedArray)2 FieldValueGetter (org.apache.beam.sdk.schemas.FieldValueGetter)1 FieldValueSetter (org.apache.beam.sdk.schemas.FieldValueSetter)1 NestedArrayPOJO (org.apache.beam.sdk.schemas.utils.TestPOJOs.NestedArrayPOJO)1 NestedMapPOJO (org.apache.beam.sdk.schemas.utils.TestPOJOs.NestedMapPOJO)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1