Search in sources :

Example 11 with Address

use of org.infinispan.protostream.domain.Address in project protostream by infinispan.

the class MarshallingTest method doMarshallUserTest.

private void doMarshallUserTest(EncoderMethod<User> method) 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(Collections.singletonList(new Address("Old Street", "XYZ42", -12)));
    User decoded = method.encodeAndDecode(user, ctx);
    assertEquals(1, decoded.getId());
    assertEquals("John", decoded.getName());
    assertEquals("Batman", decoded.getSurname());
    assertEquals(User.Gender.MALE, decoded.getGender());
    assertNotNull(decoded.getAddresses());
    assertEquals(1, decoded.getAddresses().size());
    assertEquals("Old Street", decoded.getAddresses().get(0).getStreet());
    assertEquals("XYZ42", decoded.getAddresses().get(0).getPostCode());
    assertNotNull(decoded.getAccountIds());
    assertEquals(2, decoded.getAccountIds().size());
    assertTrue(decoded.getAccountIds().contains(1));
    assertTrue(decoded.getAccountIds().contains(3));
}
Also used : User(org.infinispan.protostream.domain.User) Address(org.infinispan.protostream.domain.Address)

Example 12 with Address

use of org.infinispan.protostream.domain.Address in project protostream by infinispan.

the class ProtobufUtilTest method testFromByteArrayWithExtraPadding.

@Test(expected = MalformedProtobufException.class)
public void testFromByteArrayWithExtraPadding() 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.toByteArray(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.fromByteArray(ctx, userBytesWithPadding, User.class);
}
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 Address

use of org.infinispan.protostream.domain.Address in project protostream by infinispan.

the class ProtobufUtilTest method testCanonicalJSON.

@Test
public void testCanonicalJSON() 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, false), new Address("Bond Street", "W23", 2, true)));
    Address address = new Address("Abbey Rd", "NW89AY", 3);
    Account account = createAccount();
    testJsonConversion(ctx, address);
    testJsonConversion(ctx, 3.14);
    testJsonConversion(ctx, 777L);
    testJsonConversion(ctx, 3.14f);
    testJsonConversion(ctx, 1);
    testJsonConversion(ctx, null);
    testJsonConversion(ctx, true);
    testJsonConversion(ctx, "Merry Christmas, you filthy animal. And a Happy New Year!");
    testJsonConversion(ctx, User.Gender.FEMALE);
    testJsonConversion(ctx, account);
    testJsonConversion(ctx, user);
}
Also used : Account(org.infinispan.protostream.domain.Account) User(org.infinispan.protostream.domain.User) Address(org.infinispan.protostream.domain.Address) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Example 14 with Address

use of org.infinispan.protostream.domain.Address in project protostream by infinispan.

the class AddressMarshaller method readFrom.

@Override
public Address readFrom(ProtoStreamReader reader) throws IOException {
    String street = reader.readString("street");
    String postCode = reader.readString("postCode");
    int number = reader.readInt("number");
    Boolean isCommercial = reader.readBoolean("isCommercial");
    Address address = new Address();
    address.setStreet(street);
    address.setPostCode(postCode);
    address.setNumber(number);
    address.setCommercial(isCommercial);
    return address;
}
Also used : Address(org.infinispan.protostream.domain.Address)

Aggregations

Address (org.infinispan.protostream.domain.Address)14 User (org.infinispan.protostream.domain.User)12 Test (org.junit.Test)8 AbstractProtoStreamTest (org.infinispan.protostream.test.AbstractProtoStreamTest)7 ArrayList (java.util.ArrayList)2 SerializationContext (org.infinispan.protostream.SerializationContext)2 IOException (java.io.IOException)1 Instant (java.time.Instant)1 Date (java.util.Date)1 UUID (java.util.UUID)1 ImmutableSerializationContext (org.infinispan.protostream.ImmutableSerializationContext)1 ProtoSchemaBuilder (org.infinispan.protostream.annotations.ProtoSchemaBuilder)1 UUIDAdapter (org.infinispan.protostream.annotations.impl.testdomain.UUIDAdapter)1 Descriptor (org.infinispan.protostream.descriptors.Descriptor)1 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)1 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)1 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)1 Account (org.infinispan.protostream.domain.Account)1 Note (org.infinispan.protostream.domain.Note)1