Search in sources :

Example 21 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project druid by druid-io.

the class SchemaRegistryBasedProtobufBytesDecoderTest method testParse.

@Test
public void testParse() throws Exception {
    Mockito.when(registry.getSchemaById(ArgumentMatchers.eq(1234))).thenReturn(parseProtobufSchema());
    ProtoTestEventWrapper.ProtoTestEvent event = getTestEvent();
    byte[] bytes = event.toByteArray();
    ByteBuffer bb = ByteBuffer.allocate(bytes.length + 6).put((byte) 0).putInt(1234).put((byte) 0).put(bytes);
    bb.rewind();
    // When
    DynamicMessage actual = new SchemaRegistryBasedProtobufBytesDecoder(registry).parse(bb);
    // Then
    Assert.assertEquals(actual.getField(actual.getDescriptorForType().findFieldByName("id")), event.getId());
}
Also used : DynamicMessage(com.google.protobuf.DynamicMessage) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 22 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project beam by apache.

the class TableRowToStorageApiProtoTest method testMessageWithFFromTableRow.

@Test
public void testMessageWithFFromTableRow() throws Exception {
    Descriptor descriptor = TableRowToStorageApiProto.getDescriptorFromTableSchema(BASE_TABLE_SCHEMA);
    DynamicMessage msg = TableRowToStorageApiProto.messageFromTableRow(descriptor, BASE_TABLE_ROW);
    assertBaseRecord(msg, true);
}
Also used : Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) DynamicMessage(com.google.protobuf.DynamicMessage) Test(org.junit.Test)

Example 23 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project beam by apache.

the class TableRowToStorageApiProtoTest method testRepeatedDescriptorFromTableSchema.

@Test
public void testRepeatedDescriptorFromTableSchema() throws Exception {
    TableRow repeatedRow = new TableRow().set("repeated1", ImmutableList.of(BASE_TABLE_ROW, BASE_TABLE_ROW)).set("repeated2", ImmutableList.of(BASE_TABLE_ROW, BASE_TABLE_ROW)).set("repeatednof1", ImmutableList.of(BASE_TABLE_ROW_NO_F, BASE_TABLE_ROW_NO_F)).set("repeatednof2", ImmutableList.of(BASE_TABLE_ROW_NO_F, BASE_TABLE_ROW_NO_F));
    Descriptor descriptor = TableRowToStorageApiProto.getDescriptorFromTableSchema(REPEATED_MESSAGE_SCHEMA);
    DynamicMessage msg = TableRowToStorageApiProto.messageFromTableRow(descriptor, repeatedRow);
    assertEquals(4, msg.getAllFields().size());
    Map<String, FieldDescriptor> fieldDescriptors = descriptor.getFields().stream().collect(Collectors.toMap(FieldDescriptor::getName, Functions.identity()));
    List<DynamicMessage> repeated1 = (List<DynamicMessage>) msg.getField(fieldDescriptors.get("repeated1"));
    assertEquals(2, repeated1.size());
    assertBaseRecord(repeated1.get(0), true);
    assertBaseRecord(repeated1.get(1), true);
    List<DynamicMessage> repeated2 = (List<DynamicMessage>) msg.getField(fieldDescriptors.get("repeated2"));
    assertEquals(2, repeated2.size());
    assertBaseRecord(repeated2.get(0), true);
    assertBaseRecord(repeated2.get(1), true);
    List<DynamicMessage> repeatednof1 = (List<DynamicMessage>) msg.getField(fieldDescriptors.get("repeatednof1"));
    assertEquals(2, repeatednof1.size());
    assertBaseRecord(repeatednof1.get(0), false);
    assertBaseRecord(repeatednof1.get(1), false);
    List<DynamicMessage> repeatednof2 = (List<DynamicMessage>) msg.getField(fieldDescriptors.get("repeatednof2"));
    assertEquals(2, repeatednof2.size());
    assertBaseRecord(repeatednof2.get(0), false);
    assertBaseRecord(repeatednof2.get(1), false);
}
Also used : TableRow(com.google.api.services.bigquery.model.TableRow) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) List(java.util.List) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) DynamicMessage(com.google.protobuf.DynamicMessage) ByteString(com.google.protobuf.ByteString) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Test(org.junit.Test)

Example 24 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project beam by apache.

the class TableRowToStorageApiProtoTest method testMessageFromTableRow.

@Test
public void testMessageFromTableRow() throws Exception {
    TableRow tableRow = new TableRow().set("nestedValue1", BASE_TABLE_ROW).set("nestedValue2", BASE_TABLE_ROW).set("nestedvalueNoF1", BASE_TABLE_ROW_NO_F).set("nestedvalueNoF2", BASE_TABLE_ROW_NO_F);
    Descriptor descriptor = TableRowToStorageApiProto.getDescriptorFromTableSchema(NESTED_TABLE_SCHEMA);
    DynamicMessage msg = TableRowToStorageApiProto.messageFromTableRow(descriptor, tableRow);
    assertEquals(4, msg.getAllFields().size());
    Map<String, FieldDescriptor> fieldDescriptors = descriptor.getFields().stream().collect(Collectors.toMap(FieldDescriptor::getName, Functions.identity()));
    assertBaseRecord((DynamicMessage) msg.getField(fieldDescriptors.get("nestedvalue1")), true);
    assertBaseRecord((DynamicMessage) msg.getField(fieldDescriptors.get("nestedvalue2")), true);
    assertBaseRecord((DynamicMessage) msg.getField(fieldDescriptors.get("nestedvaluenof1")), false);
    assertBaseRecord((DynamicMessage) msg.getField(fieldDescriptors.get("nestedvaluenof2")), false);
}
Also used : TableRow(com.google.api.services.bigquery.model.TableRow) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) DynamicMessage(com.google.protobuf.DynamicMessage) ByteString(com.google.protobuf.ByteString) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Test(org.junit.Test)

Example 25 with DynamicMessage

use of com.google.protobuf.DynamicMessage in project beam by apache.

the class PubsubIOTest method testProtoDynamicMessages.

@Test
public void testProtoDynamicMessages() {
    ProtoCoder<Primitive> coder = ProtoCoder.of(Primitive.class);
    ImmutableList<Primitive> inputs = ImmutableList.of(Primitive.newBuilder().setPrimitiveInt32(42).build(), Primitive.newBuilder().setPrimitiveBool(true).build(), Primitive.newBuilder().setPrimitiveString("Hello, World!").build());
    setupTestClient(inputs, coder);
    ProtoDomain domain = ProtoDomain.buildFrom(Primitive.getDescriptor());
    String name = Primitive.getDescriptor().getFullName();
    PCollection<Primitive> read = readPipeline.apply(PubsubIO.readProtoDynamicMessages(domain, name).fromSubscription(SUBSCRIPTION.getPath()).withClock(CLOCK).withClientFactory(clientFactory)).apply("Return To Primitive", MapElements.into(TypeDescriptor.of(Primitive.class)).via((DynamicMessage message) -> {
        try {
            return Primitive.parseFrom(message.toByteArray());
        } catch (InvalidProtocolBufferException e) {
            throw new RuntimeException("Could not return to Primitive", e);
        }
    }));
    PAssert.that(read).containsInAnyOrder(inputs);
    readPipeline.run();
}
Also used : ProtoDomain(org.apache.beam.sdk.extensions.protobuf.ProtoDomain) Primitive(org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.Primitive) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteString(com.google.protobuf.ByteString) DynamicMessage(com.google.protobuf.DynamicMessage) Test(org.junit.Test)

Aggregations

DynamicMessage (com.google.protobuf.DynamicMessage)29 Test (org.junit.Test)12 ByteString (com.google.protobuf.ByteString)11 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)11 Descriptor (com.google.protobuf.Descriptors.Descriptor)9 IOException (java.io.IOException)6 List (java.util.List)6 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)5 Message (com.google.protobuf.Message)5 Map (java.util.Map)4 TableRow (com.google.api.services.bigquery.model.TableRow)3 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)3 Builder (com.google.protobuf.DynamicMessage.Builder)3 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 DataHandler (javax.activation.DataHandler)3 Nullable (javax.annotation.Nullable)3 Descriptors (com.google.protobuf.Descriptors)2 JsonFormat (com.google.protobuf.util.JsonFormat)2