use of example.serialization.EmployerDTO in project hazelcast by hazelcast.
the class CompactStreamSerializerTest method testDefaultsReflection_insideCollection.
@Test
public void testDefaultsReflection_insideCollection() {
SerializationService serializationService = createSerializationService();
NodeDTO node = new NodeDTO(new NodeDTO(new NodeDTO(2), 1), 0);
EmployeeDTO employeeDTO = new EmployeeDTO(30, 102310312);
long[] ids = new long[] { 22, 44 };
EmployeeDTO[] employeeDTOS = new EmployeeDTO[5];
for (int j = 0; j < employeeDTOS.length; j++) {
employeeDTOS[j] = new EmployeeDTO(20 + j, j * 100);
}
EmployerDTO employerDTO = new EmployerDTO("nbss", 40, HIRING, ids, employeeDTO, employeeDTOS);
ArrayList<Object> expected = new ArrayList<>();
expected.add(node);
expected.add(employeeDTO);
expected.add(employerDTO);
Data data = serializationService.toData(expected);
ArrayList<Object> arrayList = serializationService.toObject(data);
assertEquals(node, arrayList.get(0));
assertEquals(employeeDTO, arrayList.get(1));
assertEquals(employerDTO, arrayList.get(2));
}
use of example.serialization.EmployerDTO in project hazelcast by hazelcast.
the class CompactStreamSerializerTest method testWithExplicitSerializer_nested.
@Test
public void testWithExplicitSerializer_nested() {
SerializationConfig serializationConfig = new SerializationConfig();
CompactSerializationConfig compactSerializationConfig = serializationConfig.getCompactSerializationConfig();
compactSerializationConfig.setEnabled(true);
compactSerializationConfig.register(EmployeeDTO.class, "employee", new CompactSerializer<EmployeeDTO>() {
@Nonnull
@Override
public EmployeeDTO read(@Nonnull CompactReader in) {
return new EmployeeDTO(in.readInt32("a"), in.readInt64("i"));
}
@Override
public void write(@Nonnull CompactWriter out, @Nonnull EmployeeDTO object) {
out.writeInt32("a", object.getAge());
out.writeInt64("i", object.getId());
}
});
compactSerializationConfig.register(EmployerDTO.class, "employer", new CompactSerializer<EmployerDTO>() {
@Nonnull
@Override
public EmployerDTO read(@Nonnull CompactReader in) {
String name = in.readString("n");
String status = in.readString("hs");
int age = in.readInt32("a");
long[] ids = in.readArrayOfInt64("ids");
EmployeeDTO s = in.readCompact("s");
EmployeeDTO[] ss = in.readArrayOfCompact("ss", EmployeeDTO.class);
return new EmployerDTO(name, age, status == null ? null : HiringStatus.valueOf(status), ids, s, ss);
}
@Override
public void write(@Nonnull CompactWriter out, @Nonnull EmployerDTO object) {
out.writeString("n", object.getName());
out.writeString("hs", object.getHiringStatus() == null ? null : object.getHiringStatus().name());
out.writeInt32("a", object.getZcode());
out.writeArrayOfInt64("ids", object.getIds());
out.writeCompact("s", object.getSingleEmployee());
out.writeArrayOfCompact("ss", object.getOtherEmployees());
}
});
SerializationService serializationService = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).setSchemaService(schemaService).build();
EmployeeDTO employeeDTO = new EmployeeDTO(30, 102310312);
long[] ids = new long[2];
ids[0] = 22;
ids[1] = 44;
EmployeeDTO[] employeeDTOS = new EmployeeDTO[5];
for (int j = 0; j < employeeDTOS.length; j++) {
employeeDTOS[j] = new EmployeeDTO(20 + j, j * 100);
}
EmployerDTO employerDTO = new EmployerDTO("nbss", 40, HIRING, ids, employeeDTO, employeeDTOS);
Data data = serializationService.toData(employerDTO);
Object object = serializationService.toObject(data);
EmployerDTO o = (EmployerDTO) object;
assertEquals(employerDTO, o);
}
use of example.serialization.EmployerDTO in project hazelcast by hazelcast.
the class CompactStreamSerializerTest method testFieldOrder.
@Test
public void testFieldOrder() throws IOException {
EmployeeDTO employeeDTO = new EmployeeDTO(30, 102310312);
long[] ids = new long[2];
ids[0] = 22;
ids[1] = 44;
EmployeeDTO[] employeeDTOS = new EmployeeDTO[5];
for (int j = 0; j < employeeDTOS.length; j++) {
employeeDTOS[j] = new EmployeeDTO(20 + j, j * 100);
}
SchemaWriter writer = new SchemaWriter("typeName");
ReflectiveCompactSerializer reflectiveCompactSerializer = new ReflectiveCompactSerializer();
EmployerDTO employerDTO = new EmployerDTO("nbss", 40, HIRING, ids, employeeDTO, employeeDTOS);
reflectiveCompactSerializer.write(writer, employerDTO);
Schema schema = writer.build();
assertEquals(0, schema.getField("zcode").getOffset());
assertEquals(-1, schema.getField("zcode").getIndex());
assertEquals(-1, schema.getField("hiringStatus").getOffset());
assertEquals(0, schema.getField("hiringStatus").getIndex());
assertEquals(-1, schema.getField("ids").getOffset());
assertEquals(1, schema.getField("ids").getIndex());
assertEquals(-1, schema.getField("name").getOffset());
assertEquals(2, schema.getField("name").getIndex());
assertEquals(-1, schema.getField("otherEmployees").getOffset());
assertEquals(3, schema.getField("otherEmployees").getIndex());
assertEquals(-1, schema.getField("singleEmployee").getOffset());
assertEquals(4, schema.getField("singleEmployee").getIndex());
}
use of example.serialization.EmployerDTO in project hazelcast by hazelcast.
the class CompactStreamSerializerTest method testDefaultsReflection_nested.
@Test
public void testDefaultsReflection_nested() {
SerializationService serializationService = createSerializationService();
EmployeeDTO employeeDTO = new EmployeeDTO(30, 102310312);
long[] ids = new long[2];
ids[0] = 22;
ids[1] = 44;
EmployeeDTO[] employeeDTOS = new EmployeeDTO[5];
for (int j = 0; j < employeeDTOS.length; j++) {
employeeDTOS[j] = new EmployeeDTO(20 + j, j * 100);
}
EmployerDTO employerDTO = new EmployerDTO("nbss", 40, HIRING, ids, employeeDTO, employeeDTOS);
Data data = serializationService.toData(employerDTO);
Object object = serializationService.toObject(data);
EmployerDTO o = (EmployerDTO) object;
assertEquals(employerDTO, o);
}
Aggregations