ReadyLocalTransactionSerializer extends JSerializer { @Override public byte[] toBinary(final Object obj) { checkArgument(obj instanceof ReadyLocalTransaction, "Unsupported object type %s", obj.getClass()); final ReadyLocalTransaction readyLocal = (ReadyLocalTransaction) obj; final BatchedModifications batched = new BatchedModifications(readyLocal.getTransactionId(), readyLocal.getRemoteVersion()); batched.setDoCommitOnReady(readyLocal.isDoCommitOnReady()); batched.setTotalMessagesSent(1); batched.setReady(readyLocal.getParticipatingShardNames()); readyLocal.getModification().applyToCursor(new BatchedCursor(batched)); return SerializationUtils.serialize(batched); } ReadyLocalTransactionSerializer(final ExtendedActorSystem system); @Override int identifier(); @Override boolean includeManifest(); @Override byte[] toBinary(final Object obj); @Override Object fromBinaryJava(final byte[] bytes, final Class<?> clazz);  }
@Test public void testToAndFromBinary() throws NotSerializableException { DataTree dataTree = new InMemoryDataTreeFactory().create( DataTreeConfiguration.DEFAULT_OPERATIONAL, TestModel.createTestContext()); DataTreeModification modification = dataTree.takeSnapshot().newModification(); ContainerNode writeData = ImmutableNodes.containerNode(TestModel.TEST_QNAME); new WriteModification(TestModel.TEST_PATH, writeData).apply(modification); MapNode mergeData = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build(); new MergeModification(TestModel.OUTER_LIST_PATH, mergeData).apply(modification); final SortedSet<String> shardNames = ImmutableSortedSet.of("one", "two"); TransactionIdentifier txId = nextTransactionId(); ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, true, Optional.of(shardNames)); final ExtendedActorSystem system = (ExtendedActorSystem) ExtendedActorSystem.create("test"); final Object deserialized; try { final ReadyLocalTransactionSerializer serializer = new ReadyLocalTransactionSerializer(system); final byte[] bytes = serializer.toBinary(readyMessage); deserialized = serializer.fromBinary(bytes, ReadyLocalTransaction.class); } finally { TestKit.shutdownActorSystem(system); } assertNotNull("fromBinary returned null", deserialized); assertEquals("fromBinary return type", BatchedModifications.class, deserialized.getClass()); BatchedModifications batched = (BatchedModifications)deserialized; assertEquals("getTransactionID", txId, batched.getTransactionId()); assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, batched.getVersion()); assertTrue("isReady", batched.isReady()); assertTrue("isDoCommitOnReady", batched.isDoCommitOnReady()); assertTrue("participatingShardNames present", batched.getParticipatingShardNames().isPresent()); assertEquals("participatingShardNames", shardNames, batched.getParticipatingShardNames().get()); List<Modification> batchedMods = batched.getModifications(); assertEquals("getModifications size", 2, batchedMods.size()); Modification mod = batchedMods.get(0); assertEquals("Modification type", WriteModification.class, mod.getClass()); assertEquals("Modification getPath", TestModel.TEST_PATH, ((WriteModification)mod).getPath()); assertEquals("Modification getData", writeData, ((WriteModification)mod).getData()); mod = batchedMods.get(1); assertEquals("Modification type", MergeModification.class, mod.getClass()); assertEquals("Modification getPath", TestModel.OUTER_LIST_PATH, ((MergeModification)mod).getPath()); assertEquals("Modification getData", mergeData, ((MergeModification)mod).getData()); }