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;
13 import akka.actor.ActorRef;
14 import akka.testkit.JavaTestKit;
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;
29 public class ShardSnapshotActorTest extends AbstractActorTest {
30 private static final NormalizedNode<?, ?> DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
32 private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot,
33 final boolean withInstallSnapshot) throws Exception {
34 new JavaTestKit(getSystem()) {
36 final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
39 final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
41 ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
42 ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot,
43 Optional.ofNullable(installSnapshotStream), getRef());
45 final CaptureSnapshotReply reply = expectMsgClass(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 assertEquals("isPresent", true, maybeNode.isPresent());
61 assertEquals("Root node", expectedRoot, maybeNode.get());
68 public void testSerializeBoronSnapshot() throws Exception {
69 testSerializeSnapshot("testSerializeBoronSnapshotWithInstallSnapshot",
70 new MetadataShardDataTreeSnapshot(DATA), true);
71 testSerializeSnapshot("testSerializeBoronSnapshotWithoutInstallSnapshot",
72 new MetadataShardDataTreeSnapshot(DATA), false);