Search in sources :

Example 1 with SimpleBean

use of org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean in project beam by apache.

the class JavaBeanUtilsTest method testGeneratedSimpleGetters.

@Test
public void testGeneratedSimpleGetters() {
    SimpleBean simpleBean = new SimpleBean();
    simpleBean.setStr("field1");
    simpleBean.setaByte((byte) 41);
    simpleBean.setaShort((short) 42);
    simpleBean.setAnInt(43);
    simpleBean.setaLong(44);
    simpleBean.setaBoolean(true);
    simpleBean.setDateTime(DateTime.parse("1979-03-14"));
    simpleBean.setInstant(DateTime.parse("1979-03-15").toInstant());
    simpleBean.setBytes("bytes1".getBytes(StandardCharsets.UTF_8));
    simpleBean.setByteBuffer(ByteBuffer.wrap("bytes2".getBytes(StandardCharsets.UTF_8)));
    simpleBean.setBigDecimal(new BigDecimal(42));
    simpleBean.setStringBuilder(new StringBuilder("stringBuilder"));
    List<FieldValueGetter> getters = JavaBeanUtils.getGetters(SimpleBean.class, SIMPLE_BEAN_SCHEMA, new JavaBeanSchema.GetterTypeSupplier(), new DefaultTypeConversionsFactory());
    assertEquals(12, getters.size());
    assertEquals("str", getters.get(0).name());
    assertEquals("field1", getters.get(0).get(simpleBean));
    assertEquals((byte) 41, getters.get(1).get(simpleBean));
    assertEquals((short) 42, getters.get(2).get(simpleBean));
    assertEquals((int) 43, getters.get(3).get(simpleBean));
    assertEquals((long) 44, getters.get(4).get(simpleBean));
    assertTrue((Boolean) getters.get(5).get(simpleBean));
    assertEquals(DateTime.parse("1979-03-14").toInstant(), getters.get(6).get(simpleBean));
    assertEquals(DateTime.parse("1979-03-15").toInstant(), getters.get(7).get(simpleBean));
    assertArrayEquals("Unexpected bytes", "bytes1".getBytes(StandardCharsets.UTF_8), (byte[]) getters.get(8).get(simpleBean));
    assertArrayEquals("Unexpected bytes", "bytes2".getBytes(StandardCharsets.UTF_8), (byte[]) getters.get(9).get(simpleBean));
    assertEquals(new BigDecimal(42), getters.get(10).get(simpleBean));
    assertEquals("stringBuilder", getters.get(11).get(simpleBean).toString());
}
Also used : FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) JavaBeanSchema(org.apache.beam.sdk.schemas.JavaBeanSchema) SimpleBean(org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean) DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) GetterTypeSupplier(org.apache.beam.sdk.schemas.JavaBeanSchema.GetterTypeSupplier) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 2 with SimpleBean

use of org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean in project beam by apache.

the class JavaBeanUtilsTest method testGeneratedSimpleSetters.

@Test
public void testGeneratedSimpleSetters() {
    SimpleBean simpleBean = new SimpleBean();
    List<FieldValueSetter> setters = JavaBeanUtils.getSetters(SimpleBean.class, SIMPLE_BEAN_SCHEMA, new SetterTypeSupplier(), new DefaultTypeConversionsFactory());
    assertEquals(12, setters.size());
    setters.get(0).set(simpleBean, "field1");
    setters.get(1).set(simpleBean, (byte) 41);
    setters.get(2).set(simpleBean, (short) 42);
    setters.get(3).set(simpleBean, (int) 43);
    setters.get(4).set(simpleBean, (long) 44);
    setters.get(5).set(simpleBean, true);
    setters.get(6).set(simpleBean, DateTime.parse("1979-03-14").toInstant());
    setters.get(7).set(simpleBean, DateTime.parse("1979-03-15").toInstant());
    setters.get(8).set(simpleBean, "bytes1".getBytes(StandardCharsets.UTF_8));
    setters.get(9).set(simpleBean, "bytes2".getBytes(StandardCharsets.UTF_8));
    setters.get(10).set(simpleBean, new BigDecimal(42));
    setters.get(11).set(simpleBean, "stringBuilder");
    assertEquals("field1", simpleBean.getStr());
    assertEquals((byte) 41, simpleBean.getaByte());
    assertEquals((short) 42, simpleBean.getaShort());
    assertEquals((int) 43, simpleBean.getAnInt());
    assertEquals((long) 44, simpleBean.getaLong());
    assertTrue(simpleBean.isaBoolean());
    assertEquals(DateTime.parse("1979-03-14"), simpleBean.getDateTime());
    assertEquals(DateTime.parse("1979-03-15").toInstant(), simpleBean.getInstant());
    assertArrayEquals("Unexpected bytes", "bytes1".getBytes(StandardCharsets.UTF_8), simpleBean.getBytes());
    assertEquals(ByteBuffer.wrap("bytes2".getBytes(StandardCharsets.UTF_8)), simpleBean.getByteBuffer());
    assertEquals(new BigDecimal(42), simpleBean.getBigDecimal());
    assertEquals("stringBuilder", simpleBean.getStringBuilder().toString());
}
Also used : SimpleBean(org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean) DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) FieldValueSetter(org.apache.beam.sdk.schemas.FieldValueSetter) SetterTypeSupplier(org.apache.beam.sdk.schemas.JavaBeanSchema.SetterTypeSupplier) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 3 with SimpleBean

use of org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean in project beam by apache.

the class JavaBeanSchemaTest method testMapFieldGetters.

@Test
public void testMapFieldGetters() throws NoSuchSchemaException {
    SchemaRegistry registry = SchemaRegistry.createDefault();
    SchemaTestUtils.assertSchemaEquivalent(NESTED_MAP_BEAN_SCHEMA, registry.getSchema(NestedMapBean.class));
    SimpleBean simple1 = createSimple("string1");
    SimpleBean simple2 = createSimple("string2");
    SimpleBean simple3 = createSimple("string3");
    NestedMapBean bean = new NestedMapBean(ImmutableMap.of("simple1", simple1, "simple2", simple2, "simple3", simple3));
    Row row = registry.getToRowFunction(NestedMapBean.class).apply(bean);
    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 : SimpleBean(org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean) Row(org.apache.beam.sdk.values.Row) Matchers.containsString(org.hamcrest.Matchers.containsString) NestedMapBean(org.apache.beam.sdk.schemas.utils.TestJavaBeans.NestedMapBean) Test(org.junit.Test)

Example 4 with SimpleBean

use of org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean in project beam by apache.

the class JavaBeanSchemaTest method testRecursiveArrayGetters.

@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 = (List) row.getArray("beans");
    assertSame(simple1, registry.getFromRowFunction(SimpleBean.class).apply(rows.get(0)));
    assertSame(simple2, registry.getFromRowFunction(SimpleBean.class).apply(rows.get(1)));
    assertSame(simple3, registry.getFromRowFunction(SimpleBean.class).apply(rows.get(2)));
}
Also used : SimpleBean(org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean) NestedArrayBean(org.apache.beam.sdk.schemas.utils.TestJavaBeans.NestedArrayBean) 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) Test(org.junit.Test)

Example 5 with SimpleBean

use of org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean in project beam by apache.

the class JavaBeanSchemaTest method testToRow.

@Test
public void testToRow() throws NoSuchSchemaException {
    SchemaRegistry registry = SchemaRegistry.createDefault();
    SimpleBean bean = createSimple("string");
    Row row = registry.getToRowFunction(SimpleBean.class).apply(bean);
    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(DATE.toInstant(), row.getDateTime("instant"));
    assertArrayEquals(BYTE_ARRAY, row.getBytes("bytes"));
    assertArrayEquals(BYTE_ARRAY, row.getBytes("byteBuffer"));
    assertEquals(BigDecimal.ONE, row.getDecimal("bigDecimal"));
    assertEquals("stringbuilder", row.getString("stringBuilder"));
}
Also used : SimpleBean(org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Aggregations

SimpleBean (org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean)8 Test (org.junit.Test)8 Row (org.apache.beam.sdk.values.Row)6 BigDecimal (java.math.BigDecimal)2 DefaultTypeConversionsFactory (org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory)2 List (java.util.List)1 FieldValueGetter (org.apache.beam.sdk.schemas.FieldValueGetter)1 FieldValueSetter (org.apache.beam.sdk.schemas.FieldValueSetter)1 JavaBeanSchema (org.apache.beam.sdk.schemas.JavaBeanSchema)1 GetterTypeSupplier (org.apache.beam.sdk.schemas.JavaBeanSchema.GetterTypeSupplier)1 SetterTypeSupplier (org.apache.beam.sdk.schemas.JavaBeanSchema.SetterTypeSupplier)1 DefaultSchema (org.apache.beam.sdk.schemas.annotations.DefaultSchema)1 NestedArrayBean (org.apache.beam.sdk.schemas.utils.TestJavaBeans.NestedArrayBean)1 NestedMapBean (org.apache.beam.sdk.schemas.utils.TestJavaBeans.NestedMapBean)1 ImmutableList (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1