Search in sources :

Example 6 with SimplePOJO

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

the class JavaFieldSchemaTest method testFromRow.

@Test
public void testFromRow() throws NoSuchSchemaException {
    SchemaRegistry registry = SchemaRegistry.createDefault();
    Row row = createSimpleRow("string");
    SimplePOJO pojo = registry.getFromRowFunction(SimplePOJO.class).apply(row);
    assertEquals("string", pojo.str);
    assertEquals((byte) 1, pojo.aByte);
    assertEquals((short) 2, pojo.aShort);
    assertEquals((int) 3, pojo.anInt);
    assertEquals((long) 4, pojo.aLong);
    assertTrue(pojo.aBoolean);
    assertEquals(DATE, pojo.dateTime);
    assertEquals(INSTANT, pojo.instant);
    assertArrayEquals("not equal", BYTE_ARRAY, pojo.bytes);
    assertEquals(BYTE_BUFFER, pojo.byteBuffer);
    assertEquals(BigDecimal.ONE, pojo.bigDecimal);
    assertEquals("stringbuilder", pojo.stringBuilder.toString());
}
Also used : Row(org.apache.beam.sdk.values.Row) SimplePOJO(org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO) Test(org.junit.Test)

Example 7 with SimplePOJO

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

the class JavaFieldSchemaTest method testNestedArraysFromRow.

@Test
public void testNestedArraysFromRow() 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);
    PojoWithNestedArray converted = registry.getFromRowFunction(PojoWithNestedArray.class).apply(nestedRow);
    assertEquals(simplePojoListOfList, converted.pojos);
}
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 8 with SimplePOJO

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

the class JavaFieldSchemaTest method testMapFieldGetters.

@Test
public void testMapFieldGetters() throws NoSuchSchemaException {
    SchemaRegistry registry = SchemaRegistry.createDefault();
    SchemaTestUtils.assertSchemaEquivalent(NESTED_MAP_POJO_SCHEMA, registry.getSchema(NestedMapPOJO.class));
    SimplePOJO simple1 = createSimple("string1");
    SimplePOJO simple2 = createSimple("string2");
    SimplePOJO simple3 = createSimple("string3");
    NestedMapPOJO pojo = new NestedMapPOJO(ImmutableMap.of("simple1", simple1, "simple2", simple2, "simple3", simple3));
    Row row = registry.getToRowFunction(NestedMapPOJO.class).apply(pojo);
    Map<String, Row> extractedMap = row.getMap("map");
    assertEquals(3, extractedMap.size());
    assertEquals("string1", extractedMap.get("simple1").getString("str"));
    assertEquals("string2", extractedMap.get("simple2").getString("str"));
    assertEquals("string3", extractedMap.get("simple3").getString("str"));
}
Also used : NestedMapPOJO(org.apache.beam.sdk.schemas.utils.TestPOJOs.NestedMapPOJO) Row(org.apache.beam.sdk.values.Row) Matchers.containsString(org.hamcrest.Matchers.containsString) SimplePOJO(org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO) Test(org.junit.Test)

Example 9 with SimplePOJO

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

the class POJOUtilsTest method testGeneratedSimpleGetters.

@Test
public void testGeneratedSimpleGetters() {
    SimplePOJO simplePojo = new SimplePOJO("field1", (byte) 41, (short) 42, 43, 44L, true, DATE, INSTANT, BYTE_ARRAY, BYTE_BUFFER, new BigDecimal(42), new StringBuilder("stringBuilder"));
    List<FieldValueGetter> getters = POJOUtils.getGetters(SimplePOJO.class, SIMPLE_POJO_SCHEMA, JavaFieldTypeSupplier.INSTANCE, new DefaultTypeConversionsFactory());
    assertEquals(12, getters.size());
    assertEquals("str", getters.get(0).name());
    assertEquals("field1", getters.get(0).get(simplePojo));
    assertEquals((byte) 41, getters.get(1).get(simplePojo));
    assertEquals((short) 42, getters.get(2).get(simplePojo));
    assertEquals((int) 43, getters.get(3).get(simplePojo));
    assertEquals((long) 44, getters.get(4).get(simplePojo));
    assertTrue((Boolean) getters.get(5).get(simplePojo));
    assertEquals(DATE.toInstant(), getters.get(6).get(simplePojo));
    assertEquals(INSTANT, getters.get(7).get(simplePojo));
    assertArrayEquals("Unexpected bytes", BYTE_ARRAY, (byte[]) getters.get(8).get(simplePojo));
    assertArrayEquals("Unexpected bytes", BYTE_BUFFER.array(), (byte[]) getters.get(9).get(simplePojo));
    assertEquals(new BigDecimal(42), getters.get(10).get(simplePojo));
    assertEquals("stringBuilder", getters.get(11).get(simplePojo));
}
Also used : FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) 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