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());
}
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);
}
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);
}
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);
}
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();
}
Aggregations