use of org.infinispan.protostream.domain.Address in project protostream by infinispan.
the class UserMarshaller method readFrom.
@Override
public User readFrom(ProtoStreamReader reader) throws IOException {
int id = reader.readInt("id");
Set<Integer> accountIds = reader.readCollection("accountIds", new HashSet<>(), Integer.class);
// Read them out of order. It still works but logs a warning!
String surname = reader.readString("surname");
String name = reader.readString("name");
String salutation = reader.readString("salutation");
List<Address> addresses = reader.readCollection("addresses", new ArrayList<>(), Address.class);
Integer age = reader.readInt("age");
User.Gender gender = reader.readEnum("gender", User.Gender.class);
String notes = reader.readString("notes");
Instant creationDate = reader.readInstant("creationDate");
Instant passwordExpirationDate = reader.readInstant("passwordExpirationDate");
Long qrCode = reader.readLong("qrCode");
User user = new User();
user.setId(id);
user.setAccountIds(accountIds);
user.setName(name);
user.setSurname(surname);
user.setSalutation(salutation);
user.setAge(age);
user.setGender(gender);
user.setAddresses(addresses);
user.setNotes(notes);
user.setCreationDate(creationDate);
user.setPasswordExpirationDate(passwordExpirationDate);
user.setQrCode(qrCode);
return user;
}
use of org.infinispan.protostream.domain.Address in project protostream by infinispan.
the class ProtobufParserTest method testTagHandler.
@Test
public void testTagHandler() throws Exception {
ImmutableSerializationContext ctx = createContext();
User user = new User();
user.setId(1);
user.setName("John");
user.setSurname("Batman");
user.setGender(User.Gender.MALE);
user.setAccountIds(new HashSet<>(Arrays.asList(1, 3)));
user.setAddresses(Arrays.asList(new Address("Old Street", "XYZ42", -12), new Address("Bond Street", "W23", 2)));
byte[] userBytes = ProtobufUtil.toWrappedByteArray(ctx, user);
Descriptor wrapperDescriptor = ctx.getMessageDescriptor(WrappedMessage.PROTOBUF_TYPE_NAME);
TagHandler messageHandler = new TagHandler() {
@Override
public void onStart(GenericDescriptor descriptor) {
log.debugf("\tonStart %s", descriptor);
}
@Override
public void onTag(int fieldNumber, FieldDescriptor fieldDescriptor, Object tagValue) {
log.debugf("\tonTag %d %s %s", fieldNumber, fieldDescriptor != null ? fieldDescriptor.getFullName() : null, tagValue);
}
@Override
public void onStartNested(int fieldNumber, FieldDescriptor fieldDescriptor) {
log.debugf("\tonStartNested %d %s", fieldNumber, fieldDescriptor != null ? fieldDescriptor.getFullName() : null);
}
@Override
public void onEndNested(int fieldNumber, FieldDescriptor fieldDescriptor) {
log.debugf("\tonEndNested %d %s", fieldNumber, fieldDescriptor != null ? fieldDescriptor.getFullName() : null);
}
@Override
public void onEnd() {
log.debug("\tonEnd");
}
};
TagHandler wrapperHandler = new TagHandler() {
private Integer typeId;
private String typeName;
private byte[] wrappedMessage;
private Integer wrappedEnum;
private GenericDescriptor getDescriptor() {
return typeId != null ? ctx.getDescriptorByTypeId(typeId) : ctx.getDescriptorByName(typeName);
}
@Override
public void onStart(GenericDescriptor descriptor) {
log.debugf("onStart %s", descriptor);
}
@Override
public void onTag(int fieldNumber, FieldDescriptor fieldDescriptor, Object tagValue) {
log.debugf("onTag %d %s %s", fieldNumber, fieldDescriptor != null ? fieldDescriptor.getFullName() : null, tagValue);
if (fieldDescriptor == null) {
// ignore unknown fields
return;
}
switch(fieldNumber) {
case WrappedMessage.WRAPPED_TYPE_ID:
typeId = (Integer) tagValue;
break;
case WrappedMessage.WRAPPED_TYPE_NAME:
typeName = (String) tagValue;
break;
case WrappedMessage.WRAPPED_MESSAGE:
wrappedMessage = (byte[]) tagValue;
break;
case WrappedMessage.WRAPPED_ENUM:
wrappedEnum = (Integer) tagValue;
break;
case WrappedMessage.WRAPPED_DOUBLE:
case WrappedMessage.WRAPPED_FLOAT:
case WrappedMessage.WRAPPED_INT64:
case WrappedMessage.WRAPPED_UINT64:
case WrappedMessage.WRAPPED_INT32:
case WrappedMessage.WRAPPED_FIXED64:
case WrappedMessage.WRAPPED_FIXED32:
case WrappedMessage.WRAPPED_BOOL:
case WrappedMessage.WRAPPED_STRING:
case WrappedMessage.WRAPPED_BYTES:
case WrappedMessage.WRAPPED_UINT32:
case WrappedMessage.WRAPPED_SFIXED32:
case WrappedMessage.WRAPPED_SFIXED64:
case WrappedMessage.WRAPPED_SINT32:
case WrappedMessage.WRAPPED_SINT64:
messageHandler.onStart(null);
messageHandler.onTag(fieldNumber, fieldDescriptor, tagValue);
messageHandler.onEnd();
break;
}
}
@Override
public void onStartNested(int fieldNumber, FieldDescriptor fieldDescriptor) {
log.debugf("onStartNested %d %s", fieldNumber, fieldDescriptor != null ? fieldDescriptor.getFullName() : null);
}
@Override
public void onEndNested(int fieldNumber, FieldDescriptor fieldDescriptor) {
log.debugf("onEndNested %d %s", fieldNumber, fieldDescriptor != null ? fieldDescriptor.getFullName() : null);
}
@Override
public void onEnd() {
if (wrappedEnum != null) {
EnumDescriptor enumDescriptor = (EnumDescriptor) getDescriptor();
String enumConstantName = enumDescriptor.findValueByNumber(wrappedEnum).getName();
FieldDescriptor fd = wrapperDescriptor.findFieldByNumber(WrappedMessage.WRAPPED_ENUM);
messageHandler.onStart(enumDescriptor);
messageHandler.onTag(WrappedMessage.WRAPPED_ENUM, fd, enumConstantName);
messageHandler.onEnd();
} else if (wrappedMessage != null) {
try {
Descriptor messageDescriptor = (Descriptor) getDescriptor();
ProtobufParser.INSTANCE.parse(messageHandler, messageDescriptor, wrappedMessage);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
log.debug("onEnd");
}
};
ProtobufParser.INSTANCE.parse(wrapperHandler, wrapperDescriptor, userBytes);
}
use of org.infinispan.protostream.domain.Address in project protostream by infinispan.
the class ProtobufUtilTest method testMessageWrapping.
@Test
public void testMessageWrapping() throws Exception {
ImmutableSerializationContext ctx = createContext();
User user = new User();
user.setId(1);
user.setName("John");
user.setSurname("Batman");
user.setGender(User.Gender.MALE);
user.setAccountIds(new HashSet<>(Arrays.asList(1, 3)));
user.setAddresses(Arrays.asList(new Address("Old Street", "XYZ42", -12), new Address("Bond Street", "W23", 2)));
byte[] userBytes1 = ProtobufUtil.toWrappedByteArray(ctx, user);
byte[] userBytes2 = ProtobufUtil.toByteArray(ctx, new WrappedMessage(user));
// assert that toWrappedByteArray works correctly as a shorthand for toByteArray on a WrappedMessage
assertArrayEquals(userBytes1, userBytes2);
}
use of org.infinispan.protostream.domain.Address in project protostream by infinispan.
the class ProtobufUtilTest method testFromWrappedByteArrayWithExtraPadding.
@Test(expected = IllegalStateException.class)
public void testFromWrappedByteArrayWithExtraPadding() throws Exception {
ImmutableSerializationContext ctx = createContext();
User user = new User();
user.setId(1);
user.setName("John");
user.setSurname("Batman");
user.setGender(User.Gender.MALE);
user.setAccountIds(new HashSet<>(Arrays.asList(1, 3)));
user.setAddresses(Arrays.asList(new Address("Old Street", "XYZ42", -12), new Address("Bond Street", "W23", 2)));
byte[] userBytes = ProtobufUtil.toWrappedByteArray(ctx, user);
byte[] userBytesWithPadding = new byte[userBytes.length + 20];
System.arraycopy(userBytes, 0, userBytesWithPadding, 0, userBytes.length);
Arrays.fill(userBytesWithPadding, userBytes.length, userBytes.length + 20, (byte) 42);
// this must fail
ProtobufUtil.fromWrappedByteArray(ctx, userBytesWithPadding);
}
use of org.infinispan.protostream.domain.Address in project protostream by infinispan.
the class ProtobufUtilTest method testComputeMessageSize.
@Test
public void testComputeMessageSize() throws Exception {
ImmutableSerializationContext ctx = createContext();
User user = new User();
user.setId(1);
user.setName("John");
user.setSurname("Batman");
user.setGender(User.Gender.MALE);
user.setAccountIds(new HashSet<>(Arrays.asList(1, 3)));
user.setAddresses(Arrays.asList(new Address("Old Street", "XYZ42", -12), new Address("Bond Street", "W23", 2)));
int expectedMessageSize = ProtobufUtil.toByteArray(ctx, user).length;
int messageSize = ProtobufUtil.computeMessageSize(ctx, user);
assertEquals(expectedMessageSize, messageSize);
expectedMessageSize = ProtobufUtil.toWrappedByteArray(ctx, user).length;
messageSize = ProtobufUtil.computeWrappedMessageSize(ctx, user);
assertEquals(expectedMessageSize, messageSize);
}
Aggregations