@Test
public void registerCustomKryoSerializer() {

    try {
        ExampleServiceState st = new ExampleServiceState();
        st.id = UUID.randomUUID().toString();
        st.counter = Utils.getNowMicrosUtc();
        st.documentSelfLink = st.id;
        st.keyValues = new HashMap<>();
        st.keyValues.put(st.id, st.id);
        // we need to prove that the default serializer, for both object and document is
        // used and produces a different result, compared to the custom serializer. we first
        // serialize with defaults, then with custom, and compare sizes
        int byteCountToObjectDefault = Utils.toBytes(st, Utils.getBuffer(1024), 0);
        Output outDocumentImplicitDefault = KryoSerializers.serializeAsDocument(st,
                1024);
        int pDefaultImplicit = outDocumentImplicitDefault.position();
        Output outDocumentDefault = KryoSerializers.serializeDocument(st, 1024);
        int pDefault = outDocumentDefault.position();
        Utils.registerCustomKryoSerializer(new CustomKryoForObjectThreadLocal(), false);
        Utils.registerCustomKryoSerializer(new CustomKryoForDocumentThreadLocal(),
                true);
        byte[] objectData = new byte[1024];
        int byteCountToObjectCustom = Utils.toBytes(st, objectData, 0);
        Output outDocumentImplicitCustom = KryoSerializers.serializeAsDocument(st,
                1024);
        int p = outDocumentImplicitCustom.position();
        Output outDocumentCustom = KryoSerializers.serializeDocument(st, 1024);
        assertTrue(byteCountToObjectCustom != byteCountToObjectDefault);
        assertTrue(p != pDefaultImplicit);
        assertTrue(outDocumentCustom.position() != pDefault);
        ExampleServiceState stDeserializedFromObject = (ExampleServiceState) Utils.fromBytes(
                objectData);
        ExampleServiceState stDeserializedImplicit =
                (ExampleServiceState) KryoSerializers.deserializeDocument(
                        outDocumentImplicitDefault.getBuffer(), 0,
                        outDocumentImplicitDefault.position());
        ExampleServiceState stDeserialized = (ExampleServiceState) KryoSerializers
                .deserializeDocument(
                        outDocumentDefault.getBuffer(), 0, outDocumentDefault.position());
        assertEquals(st.id, stDeserializedFromObject.id);
        assertEquals(st.id, stDeserializedImplicit.id);
        assertEquals(st.id, stDeserialized.id);
        assertEquals(st.id, stDeserializedFromObject.keyValues.get(st.id));
        assertEquals(st.id, stDeserializedImplicit.keyValues.get(st.id));
        assertEquals(st.id, stDeserialized.keyValues.get(st.id));
    } finally {
        Utils.registerCustomKryoSerializer(null, false);
        Utils.registerCustomKryoSerializer(null, true);
    }
}