2 * Copyright (c) 2016, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.actors;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
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;
31 public class ShardSnapshotActorTest extends AbstractActorTest {
32 private static final NormalizedNode<?, ?> DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
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);
40 final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
42 ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
43 ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot,
44 Optional.ofNullable(installSnapshotStream), kit.getRef());
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());
51 if (installSnapshotStream != null) {
52 final ShardDataTreeSnapshot deserialized;
53 try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
54 installSnapshotStream.toByteArray()))) {
55 deserialized = ShardDataTreeSnapshot.deserialize(in);
58 assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
60 final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
61 assertTrue("isPresent", maybeNode.isPresent());
62 assertEquals("Root node", expectedRoot, maybeNode.get());
67 public void testSerializeBoronSnapshot() throws Exception {
68 testSerializeSnapshot("testSerializeBoronSnapshotWithInstallSnapshot",
69 new MetadataShardDataTreeSnapshot(DATA), true);
70 testSerializeSnapshot("testSerializeBoronSnapshotWithoutInstallSnapshot",
71 new MetadataShardDataTreeSnapshot(DATA), false);