Fix checkstyle problems not detected by the current version
[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.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;
28
29 public class ShardSnapshotActorTest extends AbstractActorTest {
30     private static final NormalizedNode<?, ?> DATA = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
31
32     private static void testSerializeSnapshot(final String testName, final ShardDataTreeSnapshot snapshot,
33             final boolean withInstallSnapshot) throws Exception {
34         new JavaTestKit(getSystem()) {
35             {
36                 final ActorRef snapshotActor = getSystem().actorOf(ShardSnapshotActor.props(), testName);
37                 watch(snapshotActor);
38
39                 final NormalizedNode<?, ?> expectedRoot = snapshot.getRootNode().get();
40
41                 ByteArrayOutputStream installSnapshotStream = withInstallSnapshot ? new ByteArrayOutputStream() : null;
42                 ShardSnapshotActor.requestSnapshot(snapshotActor, snapshot,
43                         Optional.ofNullable(installSnapshotStream), getRef());
44
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());
49
50                 if (installSnapshotStream != null) {
51                     final ShardDataTreeSnapshot deserialized;
52                     try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
53                             installSnapshotStream.toByteArray()))) {
54                         deserialized = ShardDataTreeSnapshot.deserialize(in);
55                     }
56
57                     assertEquals("Deserialized snapshot type", snapshot.getClass(), deserialized.getClass());
58
59                     final Optional<NormalizedNode<?, ?>> maybeNode = deserialized.getRootNode();
60                     assertEquals("isPresent", true, maybeNode.isPresent());
61                     assertEquals("Root node", expectedRoot, maybeNode.get());
62                 }
63             }
64         };
65     }
66
67     @Test
68     public void testSerializeBoronSnapshot() throws Exception {
69         testSerializeSnapshot("testSerializeBoronSnapshotWithInstallSnapshot",
70                 new MetadataShardDataTreeSnapshot(DATA), true);
71         testSerializeSnapshot("testSerializeBoronSnapshotWithoutInstallSnapshot",
72                 new MetadataShardDataTreeSnapshot(DATA), false);
73     }
74 }