5878675fdf84fe1e122cd858d0469ad604ce1769
[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 import static org.junit.Assert.assertTrue;
13
14 import akka.actor.ActorRef;
15 import akka.testkit.javadsl.TestKit;
16 import java.io.ByteArrayInputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.ObjectInputStream;
19 import java.time.Duration;
20 import java.util.Optional;
21 import org.junit.Test;
22 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
23 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
24 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
25 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
26 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
27 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
30
31 public class ShardSnapshotActorTest extends AbstractActorTest {
32     private static final NormalizedNode<?, ?> DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
33
34     private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot,
35             final boolean withInstallSnapshot) throws Exception {
36         final TestKit kit = new TestKit(getSystem());
37         final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
38         kit.watch(snapshotActor);
39
40         final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
41
42         ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
43         ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot,
44             Optional.ofNullable(installSnapshotStream), kit.getRef());
45
46         final CaptureSnapshotReply reply = kit.expectMsgClass(Duration.ofSeconds(3), CaptureSnapshotReply.class);
47         assertNotNull("getSnapshotState is null", reply.getSnapshotState());
48         assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
49         assertEquals("Snapshot", snapshot, ((ShardSnapshotState)reply.getSnapshotState()).getSnapshot());
50
51         if (installSnapshotStream != null) {
52             final ShardDataTreeSnapshot deserialized;
53             try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
54                 installSnapshotStream.toByteArray()))) {
55                 deserialized = ShardDataTreeSnapshot.deserialize(in);
56             }
57
58             assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
59
60             final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
61             assertTrue("isPresent", maybeNode.isPresent());
62             assertEquals("Root node", expectedRoot, maybeNode.get());
63         }
64     }
65
66     @Test
67     public void testSerializeBoronSnapshot() throws Exception {
68         testSerializeSnapshot("testSerializeBoronSnapshotWithInstallSnapshot",
69                 new MetadataShardDataTreeSnapshot(DATA), true);
70         testSerializeSnapshot("testSerializeBoronSnapshotWithoutInstallSnapshot",
71                 new MetadataShardDataTreeSnapshot(DATA), false);
72     }
73 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.