30b8bab1b7bafe9887e639fe20639a931b90c016
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / actors / ShardSnapshotActorTest.java
1 /*
2  * Copyright (c) 2016, 2017 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.javadsl.TestKit;
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.ObjectInputStream;
18 import java.util.Optional;
19 import org.junit.Test;
20 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
21 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
22 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
23 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
24 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
25 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
28
29 public class ShardSnapshotActorTest extends AbstractActorTest {
30     private static final NormalizedNode<?, ?> DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
31
32     private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot,
33             final boolean withInstallSnapshot) throws Exception {
34         final TestKit kit = new TestKit(getSystem());
35         final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
36         kit.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), kit.getRef());
43
44         final CaptureSnapshotReply reply = kit.expectMsgClass(kit.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;
51             try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
52                 installSnapshotStream.toByteArray()))) {
53                 deserialized = ShardDataTreeSnapshot.deserialize(in);
54             }
55
56             assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
57
58             final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
59             assertEquals("isPresent", true, maybeNode.isPresent());
60             assertEquals("Root node", expectedRoot, maybeNode.get());
61         }
62     }
63
64     @Test
65     public void testSerializeBoronSnapshot() throws Exception {
66         testSerializeSnapshot("testSerializeBoronSnapshotWithInstallSnapshot",
67                 new MetadataShardDataTreeSnapshot(DATA), true);
68         testSerializeSnapshot("testSerializeBoronSnapshotWithoutInstallSnapshot",
69                 new MetadataShardDataTreeSnapshot(DATA), false);
70     }
71 }