Search in sources :

Example 11 with User

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);
}
Also used : User(org.infinispan.protostream.domain.User) Address(org.infinispan.protostream.domain.Address) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 12 with User

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);
}
Also used : User(org.infinispan.protostream.domain.User) Address(org.infinispan.protostream.domain.Address) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 13 with User

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]);
}
Also used : User(org.infinispan.protostream.domain.User) Configuration(org.infinispan.protostream.config.Configuration) Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 14 with User

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"));
}
Also used : User(org.infinispan.protostream.domain.User) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 15 with User

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());
}
Also used : User(org.infinispan.protostream.domain.User) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Aggregations

User (org.infinispan.protostream.domain.User)21 Test (org.junit.Test)15 Address (org.infinispan.protostream.domain.Address)12 AbstractProtoStreamTest (org.infinispan.protostream.test.AbstractProtoStreamTest)11 ArrayList (java.util.ArrayList)3 Date (java.util.Date)2 SerializationContext (org.infinispan.protostream.SerializationContext)2 Descriptor (org.infinispan.protostream.descriptors.Descriptor)2 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)2 Note (org.infinispan.protostream.domain.Note)2 IOException (java.io.IOException)1 Instant (java.time.Instant)1 List (java.util.List)1 ImmutableSerializationContext (org.infinispan.protostream.ImmutableSerializationContext)1 MessageMarshaller (org.infinispan.protostream.MessageMarshaller)1 Configuration (org.infinispan.protostream.config.Configuration)1 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)1 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)1 Account (org.infinispan.protostream.domain.Account)1