Bump versions 9.0.4-SNAPSHOT
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / actors / ShardSnapshotActorTest.java
index e77ab0357b6b419ade59d6d96e3562259b02d90b..0a5c40d29d3ae6b96f5765bfd549e10aff177a70 100644 (file)
@@ -12,56 +12,54 @@ import static org.junit.Assert.assertNotNull;
 
 import akka.actor.ActorRef;
 import akka.testkit.javadsl.TestKit;
-import java.io.ByteArrayInputStream;
+import com.google.common.io.ByteSource;
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
+import java.time.Duration;
 import java.util.Optional;
 import org.junit.Test;
 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
+import org.opendaylight.controller.cluster.io.InputOutputStreamFactory;
 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 
 public class ShardSnapshotActorTest extends AbstractActorTest {
-    private static final NormalizedNode<?, ?> DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
+    private static final InputOutputStreamFactory STREAM_FACTORY = InputOutputStreamFactory.simple();
+
+    private static final NormalizedNode DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
 
     private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot,
             final boolean withInstallSnapshot) throws Exception {
-        new TestKit(getSystem()) {
-            {
-                final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
-                watch(snapshotActor);
-
-                final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
+        final TestKit kit = new TestKit(getSystem());
+        final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(STREAM_FACTORY), testName);
+        kit.watch(snapshotActor);
 
-                ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
-                ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot,
-                        Optional.ofNullable(installSnapshotStream), getRef());
+        final NormalizedNode expectedRoot = snapshot.getRootNode().orElseThrow();
 
-                final CaptureSnapshotReply reply = expectMsgClass(duration("3 seconds"), CaptureSnapshotReply.class);
-                assertNotNull("getSnapshotState is null", reply.getSnapshotState());
-                assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
-                assertEquals("Snapshot", snapshot, ((ShardSnapshotState)reply.getSnapshotState()).getSnapshot());
+        ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
+        ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot,
+            Optional.ofNullable(installSnapshotStream), kit.getRef());
 
-                if (installSnapshotStream != null) {
-                    final ShardDataTreeSnapshot deserialized;
-                    try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
-                            installSnapshotStream.toByteArray()))) {
-                        deserialized = ShardDataTreeSnapshot.deserialize(in);
-                    }
+        final CaptureSnapshotReply reply = kit.expectMsgClass(Duration.ofSeconds(3), CaptureSnapshotReply.class);
+        assertNotNull("getSnapshotState is null", reply.getSnapshotState());
+        assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
+        assertEquals("Snapshot", snapshot, ((ShardSnapshotState)reply.getSnapshotState()).getSnapshot());
 
-                    assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
-
-                    final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
-                    assertEquals("isPresent", true, maybeNode.isPresent());
-                    assertEquals("Root node", expectedRoot, maybeNode.get());
-                }
+        if (installSnapshotStream != null) {
+            final ShardDataTreeSnapshot deserialized;
+            try (ObjectInputStream in = new ObjectInputStream(STREAM_FACTORY.createInputStream(
+                    ByteSource.wrap(installSnapshotStream.toByteArray())))) {
+                deserialized = ShardDataTreeSnapshot.deserialize(in).getSnapshot();
             }
-        };
+
+            assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
+            assertEquals("Root node", Optional.of(expectedRoot), deserialized.getRootNode());
+        }
     }
 
     @Test