BUG-5280: centralize ShardSnapshot operations
[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 import static org.junit.Assert.assertTrue;
13 import akka.actor.ActorRef;
14 import akka.testkit.JavaTestKit;
15 import java.util.Optional;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
18 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
19 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
20 import org.opendaylight.controller.cluster.datastore.persisted.PreBoronShardDataTreeSnapshot;
21 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
22 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
25
26 public class ShardSnapshotActorTest extends AbstractActorTest {
27     private static final NormalizedNode<?, ?> DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
28
29     private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot)
30             throws Exception {
31         new JavaTestKit(getSystem()) {{
32
33             final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
34             watch(snapshotActor);
35
36             final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
37
38             ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot, getRef());
39
40             final CaptureSnapshotReply reply = expectMsgClass(duration("3 seconds"), CaptureSnapshotReply.class);
41             assertNotNull("getSnapshot is null", reply.getSnapshot());
42
43             final ShardDataTreeSnapshot actual = ShardDataTreeSnapshot.deserialize(reply.getSnapshot());
44             assertNotNull(actual);
45             assertEquals(snapshot.getClass(), actual.getClass());
46
47             final Optional<NormalizedNode<?, ?>> maybeNode = actual.getRootNode();
48             assertTrue(maybeNode.isPresent());
49
50             assertEquals("Root node", expectedRoot, maybeNode.get());
51         }};
52     }
53
54     @Test
55     public void testSerializeBoronSnapshot() throws Exception {
56         testSerializeSnapshot("testSerializeBoronSnapshot", new MetadataShardDataTreeSnapshot(DATA));
57     }
58
59     @Deprecated
60     @Test
61     public void testSerializeLegacySnapshot() throws Exception {
62         testSerializeSnapshot("testSerializeLegacySnapshot", new PreBoronShardDataTreeSnapshot(DATA));
63     }
64 }