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
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.actors;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import akka.actor.ActorRef;
14 import akka.testkit.JavaTestKit;
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.util.Optional;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
20 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
21 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
22 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
23 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
24 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
27
28 public class ShardSnapshotActorTest extends AbstractActorTest {
29     private static final NormalizedNode<?, ?> DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
30
31     private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot,
32             final boolean withInstallSnapshot) throws Exception {
33         new JavaTestKit(getSystem()) {
34             {
35                 final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
36                 watch(snapshotActor);
37
38                 final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
39
40                 ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
41                 ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot,
42                         Optional.ofNullable(installSnapshotStream), getRef());
43
44                 final CaptureSnapshotReply reply = expectMsgClass(duration("3 seconds"), CaptureSnapshotReply.class);
45                 assertNotNull("getSnapshotState is null", reply.getSnapshotState());
46                 assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
47                 assertEquals("Snapshot", snapshot, ((ShardSnapshotState)reply.getSnapshotState()).getSnapshot());
48
49                 if (installSnapshotStream != null) {
50                     final ShardDataTreeSnapshot deserialized = ShardDataTreeSnapshot.deserialize(
51                             new ByteArrayInputStream(installSnapshotStream.toByteArray()));
52                     assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
53
54                     final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
55                     assertEquals("isPresent", true, maybeNode.isPresent());
56                     assertEquals("Root node", expectedRoot, maybeNode.get());
57                 }
58             }
59         };
60     }
61
62     @Test
63     public void testSerializeBoronSnapshot() throws Exception {
64         testSerializeSnapshot("testSerializeBoronSnapshotWithInstallSnapshot",
65                 new MetadataShardDataTreeSnapshot(DATA), true);
66         testSerializeSnapshot("testSerializeBoronSnapshotWithoutInstallSnapshot",
67                 new MetadataShardDataTreeSnapshot(DATA), false);
68     }
69 }