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 com.google.common.io.ByteSource;
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.io.InputOutputStreamFactory;
27 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
28 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
32 public class ShardSnapshotActorTest extends AbstractActorTest {
33 private static final InputOutputStreamFactory STREAM_FACTORY = InputOutputStreamFactory.simple();
35 private static final NormalizedNode<?, ?> DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
37 private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot,
38 final boolean withInstallSnapshot) throws Exception {
39 final TestKit kit = new TestKit(getSystem());
40 final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(STREAM_FACTORY), testName);
41 kit.watch(snapshotActor);
43 final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
45 ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
46 ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot,
47 Optional.ofNullable(installSnapshotStream), kit.getRef());
49 final CaptureSnapshotReply reply = kit.expectMsgClass(Duration.ofSeconds(3), CaptureSnapshotReply.class);
50 assertNotNull("getSnapshotState is null", reply.getSnapshotState());
51 assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
52 assertEquals("Snapshot", snapshot, ((ShardSnapshotState)reply.getSnapshotState()).getSnapshot());
54 if (installSnapshotStream != null) {
55 final ShardDataTreeSnapshot deserialized;
56 try (ObjectInputStream in = new ObjectInputStream(STREAM_FACTORY.createInputStream(
57 ByteSource.wrap(installSnapshotStream.toByteArray())))) {
58 deserialized = ShardDataTreeSnapshot.deserialize(in).getSnapshot();
61 assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
63 final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
64 assertTrue("isPresent", maybeNode.isPresent());
65 assertEquals("Root node", expectedRoot, maybeNode.get());
70 public void testSerializeBoronSnapshot() throws Exception {
71 testSerializeSnapshot("testSerializeBoronSnapshotWithInstallSnapshot",
72 new MetadataShardDataTreeSnapshot(DATA), true);
73 testSerializeSnapshot("testSerializeBoronSnapshotWithoutInstallSnapshot",
74 new MetadataShardDataTreeSnapshot(DATA), false);