Bug 7521: Convert Snapshot to store a State instance
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / actors / ShardSnapshotActorTest.java
index 65a8ac9ce8605c1c19493fab000c865696ec8725..5cf81834d8d40c4ec2c5ca9cac78cb987492d025 100644 (file)
@@ -9,16 +9,17 @@ package org.opendaylight.controller.cluster.datastore.actors;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
 
 import akka.actor.ActorRef;
 import akka.testkit.JavaTestKit;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 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.PreBoronShardDataTreeSnapshot;
 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
+import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
 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;
@@ -27,41 +28,42 @@ 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 void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot)
-            throws Exception {
+    private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot,
+            final boolean withInstallSnapshot) throws Exception {
         new JavaTestKit(getSystem()) {
             {
-
                 final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
                 watch(snapshotActor);
 
                 final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
 
-                ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot, getRef());
+                ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
+                ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot,
+                        Optional.ofNullable(installSnapshotStream), getRef());
 
                 final CaptureSnapshotReply reply = expectMsgClass(duration("3 seconds"), CaptureSnapshotReply.class);
-                assertNotNull("getSnapshot is null", reply.getSnapshot());
-
-                final ShardDataTreeSnapshot actual = ShardDataTreeSnapshot.deserialize(reply.getSnapshot());
-                assertNotNull(actual);
-                assertEquals(snapshot.getClass(), actual.getClass());
+                assertNotNull("getSnapshotState is null", reply.getSnapshotState());
+                assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
+                assertEquals("Snapshot", snapshot, ((ShardSnapshotState)reply.getSnapshotState()).getSnapshot());
 
-                final Optional<NormalizedNode<?, ?>> maybeNode = actual.getRootNode();
-                assertTrue(maybeNode.isPresent());
+                if (installSnapshotStream != null) {
+                    final ShardDataTreeSnapshot deserialized = ShardDataTreeSnapshot.deserialize(
+                            new ByteArrayInputStream(installSnapshotStream.toByteArray()));
+                    assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
 
-                assertEquals("Root node", expectedRoot, maybeNode.get());
+                    final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
+                    assertEquals("isPresent", true, maybeNode.isPresent());
+                    assertEquals("Root node", expectedRoot, maybeNode.get());
+                }
             }
         };
     }
 
     @Test
     public void testSerializeBoronSnapshot() throws Exception {
-        testSerializeSnapshot("testSerializeBoronSnapshot", new MetadataShardDataTreeSnapshot(DATA));
-    }
-
-    @Deprecated
-    @Test
-    public void testSerializeLegacySnapshot() throws Exception {
-        testSerializeSnapshot("testSerializeLegacySnapshot", new PreBoronShardDataTreeSnapshot(DATA));
+        testSerializeSnapshot("testSerializeBoronSnapshotWithInstallSnapshot",
+                new MetadataShardDataTreeSnapshot(DATA), true);
+        testSerializeSnapshot("testSerializeBoronSnapshotWithoutInstallSnapshot",
+                new MetadataShardDataTreeSnapshot(DATA), false);
     }
 }