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.util.Optional;
20 import org.junit.Test;
21 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
22 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
23 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
24 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
25 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
26 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
30 public class ShardSnapshotActorTest extends AbstractActorTest {
31 private static final NormalizedNode<?, ?> DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
33 private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot,
34 final boolean withInstallSnapshot) throws Exception {
35 final TestKit kit = new TestKit(getSystem());
36 final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
37 kit.watch(snapshotActor);
39 final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
41 ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
42 ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot,
43 Optional.ofNullable(installSnapshotStream), kit.getRef());
45 final CaptureSnapshotReply reply = kit.expectMsgClass(kit.duration("3 seconds"), CaptureSnapshotReply.class);
46 assertNotNull("getSnapshotState is null", reply.getSnapshotState());
47 assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
48 assertEquals("Snapshot", snapshot, ((ShardSnapshotState)reply.getSnapshotState()).getSnapshot());
50 if (installSnapshotStream != null) {
51 final ShardDataTreeSnapshot deserialized;
52 try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
53 installSnapshotStream.toByteArray()))) {
54 deserialized = ShardDataTreeSnapshot.deserialize(in);
57 assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
59 final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
60 assertTrue("isPresent", maybeNode.isPresent());
61 assertEquals("Root node", expectedRoot, maybeNode.get());
66 public void testSerializeBoronSnapshot() throws Exception {
67 testSerializeSnapshot("testSerializeBoronSnapshotWithInstallSnapshot",
68 new MetadataShardDataTreeSnapshot(DATA), true);
69 testSerializeSnapshot("testSerializeBoronSnapshotWithoutInstallSnapshot",
70 new MetadataShardDataTreeSnapshot(DATA), false);