Improve segmented journal actor metrics
[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
13 import akka.actor.ActorRef;
14 import akka.testkit.javadsl.TestKit;
15 import com.google.common.io.ByteSource;
16 import java.io.ByteArrayOutputStream;
17 import java.io.ObjectInputStream;
18 import java.time.Duration;
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.io.InputOutputStreamFactory;
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 InputOutputStreamFactory STREAM_FACTORY = InputOutputStreamFactory.simple();
33
34     private static final NormalizedNode DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
35
36     private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot,
37             final boolean withInstallSnapshot) throws Exception {
38         final TestKit kit = new TestKit(getSystem());
39         final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(STREAM_FACTORY), testName);
40         kit.watch(snapshotActor);
41
42         final NormalizedNode expectedRoot = snapshot.getRootNode().orElseThrow();
43
44         ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
45         ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot,
46             Optional.ofNullable(installSnapshotStream), kit.getRef());
47
48         final CaptureSnapshotReply reply = kit.expectMsgClass(Duration.ofSeconds(3), CaptureSnapshotReply.class);
49         assertNotNull("getSnapshotState is null", reply.getSnapshotState());
50         assertEquals("SnapshotState type", ShardSnapshotState.class, reply.getSnapshotState().getClass());
51         assertEquals("Snapshot", snapshot, ((ShardSnapshotState)reply.getSnapshotState()).getSnapshot());
52
53         if (installSnapshotStream != null) {
54             final ShardDataTreeSnapshot deserialized;
55             try (ObjectInputStream in = new ObjectInputStream(STREAM_FACTORY.createInputStream(
56                     ByteSource.wrap(installSnapshotStream.toByteArray())))) {
57                 deserialized = ShardDataTreeSnapshot.deserialize(in).getSnapshot();
58             }
59
60             assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
61             assertEquals("Root node", Optional.of(expectedRoot), deserialized.getRootNode());
62         }
63     }
64
65     @Test
66     public void testSerializeBoronSnapshot() throws Exception {
67         testSerializeSnapshot("testSerializeBoronSnapshotWithInstallSnapshot",
68                 new MetadataShardDataTreeSnapshot(DATA), true);
69         testSerializeSnapshot("testSerializeBoronSnapshotWithoutInstallSnapshot",
70                 new MetadataShardDataTreeSnapshot(DATA), false);
71     }
72 }