use of org.infinispan.protostream.domain.User 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.User 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);
}
use of org.infinispan.protostream.domain.User in project protostream by infinispan.
the class ProtobufUtilTest method testWrappedMessageTypeIdMapper.
@Test
public void testWrappedMessageTypeIdMapper() throws Exception {
WrappedMessageTypeIdMapper mapper = new WrappedMessageTypeIdMapper() {
@Override
public int mapTypeIdOut(int typeId, ImmutableSerializationContext ctx) {
if (typeId == 100042) {
// change typeId ouf User
return 100021;
}
return typeId;
}
};
Configuration cfg = Configuration.builder().wrappingConfig().wrappedMessageTypeIdMapper(mapper).build();
ImmutableSerializationContext ctx = createContext(cfg);
// this has TypeId 100042
User user = new User();
user.setId(1);
user.setName("John");
user.setSurname("Batman");
user.setGender(User.Gender.MALE);
byte[] bytes = ProtobufUtil.toWrappedByteArray(ctx, user);
int[] seenTypeId = new int[] { -1 };
TagHandler tagHandler = new TagHandler() {
@Override
public void onTag(int fieldNumber, FieldDescriptor fieldDescriptor, Object tagValue) {
if (fieldNumber == WrappedMessage.WRAPPED_TYPE_ID) {
seenTypeId[0] = (Integer) tagValue;
}
}
};
Descriptor wrappedMessageDescriptor = ctx.getMessageDescriptor(WrappedMessage.PROTOBUF_TYPE_NAME);
ProtobufParser.INSTANCE.parse(tagHandler, wrappedMessageDescriptor, bytes);
assertEquals(100021, seenTypeId[0]);
}
use of org.infinispan.protostream.domain.User in project protostream by infinispan.
the class ProtobufUtilTest method testJsonLong.
@Test
public void testJsonLong() throws IOException {
ImmutableSerializationContext ctx = createContext();
User user = new User();
user.setName("");
user.setId(1);
user.setQrCode(12345667L);
byte[] marshalled = ProtobufUtil.toWrappedByteArray(ctx, user);
String json = ProtobufUtil.toCanonicalJSON(ctx, marshalled, true);
assertTrue(json.contains("\"qrCode\": 12345667"));
}
use of org.infinispan.protostream.domain.User in project protostream by infinispan.
the class WrappingTest method testMarshallUserList.
@Test
public void testMarshallUserList() throws Exception {
MessageMarshaller<UserList> m = new MessageMarshaller<UserList>() {
@Override
public String getTypeName() {
return "sample_bank_account.user_list";
}
@Override
public Class<UserList> getJavaClass() {
return UserList.class;
}
@Override
public UserList readFrom(ProtoStreamReader reader) throws IOException {
return reader.readCollection("theList", new UserList(), User.class);
}
@Override
public void writeTo(ProtoStreamWriter writer, UserList list) throws IOException {
writer.writeCollection("theList", list, User.class);
}
};
List<User> users = new UserList();
users.add(createUser(1, "X1", "Y1"));
users.add(createUser(2, "X2", "Y2"));
users.add(createUser(3, "X3", "Y3"));
List list = (List) roundtrip(users, m);
assertTrue(list.get(0) instanceof User);
assertTrue(list.get(1) instanceof User);
assertTrue(list.get(2) instanceof User);
assertEquals(1, ((User) list.get(0)).getId());
assertEquals(2, ((User) list.get(1)).getId());
assertEquals(3, ((User) list.get(2)).getId());
}
Aggregations