Fix unit test CS warnings in sal-distributed-datastore
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / shardmanager / ShardManagerGetSnapshotReplyActorTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.shardmanager;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import akka.actor.ActorRef;
14 import akka.actor.Status.Failure;
15 import akka.actor.Terminated;
16 import akka.testkit.JavaTestKit;
17 import java.util.Arrays;
18 import java.util.List;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
21 import org.junit.After;
22 import org.junit.Test;
23 import org.opendaylight.controller.cluster.access.concepts.MemberName;
24 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
25 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
26 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
27 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot;
28 import org.opendaylight.controller.cluster.raft.TestActorFactory;
29 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
30 import scala.concurrent.duration.Duration;
31 import scala.concurrent.duration.FiniteDuration;
32
33 /**
34  * Unit tests for ShardManagerGetSnapshotReplyActor.
35  *
36  * @author Thomas Pantelis
37  */
38 public class ShardManagerGetSnapshotReplyActorTest extends AbstractActorTest {
39     private static final MemberName MEMBER_1 = MemberName.forName("member-1");
40
41     private final TestActorFactory actorFactory = new TestActorFactory(getSystem());
42
43     @After
44     public void tearDown() {
45         actorFactory.close();
46     }
47
48     @Test
49     public void testSuccess() {
50         JavaTestKit kit = new JavaTestKit(getSystem());
51
52         byte[] shardManagerSnapshot = new byte[]{0,5,9};
53         ActorRef replyActor = actorFactory.createActor(ShardManagerGetSnapshotReplyActor.props(
54                 Arrays.asList("shard1", "shard2", "shard3"), "config",
55                 shardManagerSnapshot, kit.getRef(), "shard-manager", Duration.create(100, TimeUnit.SECONDS)),
56                     actorFactory.generateActorId("actor"));
57
58         kit.watch(replyActor);
59
60         byte[] shard1Snapshot = new byte[]{1,2,3};
61         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
62             shard1Snapshot), ActorRef.noSender());
63
64         byte[] shard2Snapshot = new byte[]{4,5,6};
65         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard2", MEMBER_1, "config").toString(),
66             shard2Snapshot), ActorRef.noSender());
67
68         kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
69
70         byte[] shard3Snapshot = new byte[]{7,8,9};
71         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard3", MEMBER_1, "config").toString(),
72             shard3Snapshot), ActorRef.noSender());
73
74         DatastoreSnapshot datastoreSnapshot = kit.expectMsgClass(DatastoreSnapshot.class);
75
76         assertEquals("getType", "config", datastoreSnapshot.getType());
77         assertArrayEquals("getShardManagerSnapshot", shardManagerSnapshot, datastoreSnapshot.getShardManagerSnapshot());
78         List<ShardSnapshot> shardSnapshots = datastoreSnapshot.getShardSnapshots();
79         assertEquals("ShardSnapshot size", 3, shardSnapshots.size());
80         assertEquals("ShardSnapshot 1 getName", "shard1", shardSnapshots.get(0).getName());
81         assertArrayEquals("ShardSnapshot 1 getSnapshot", shard1Snapshot, shardSnapshots.get(0).getSnapshot());
82         assertEquals("ShardSnapshot 2 getName", "shard2", shardSnapshots.get(1).getName());
83         assertArrayEquals("ShardSnapshot 2 getSnapshot", shard2Snapshot, shardSnapshots.get(1).getSnapshot());
84         assertEquals("ShardSnapshot 3 getName", "shard3", shardSnapshots.get(2).getName());
85         assertArrayEquals("ShardSnapshot 3 getSnapshot", shard3Snapshot, shardSnapshots.get(2).getSnapshot());
86
87         kit.expectMsgClass(Terminated.class);
88     }
89
90     @Test
91     public void testGetSnapshotFailureReply() {
92         JavaTestKit kit = new JavaTestKit(getSystem());
93
94         byte[] shardManagerSnapshot = new byte[]{0,5,9};
95         ActorRef replyActor = actorFactory.createActor(ShardManagerGetSnapshotReplyActor.props(
96                 Arrays.asList("shard1", "shard2"), "config",
97                 shardManagerSnapshot, kit.getRef(), "shard-manager", Duration.create(100, TimeUnit.SECONDS)),
98                     actorFactory.generateActorId("actor"));
99
100         kit.watch(replyActor);
101
102         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
103             new byte[]{1,2,3}), ActorRef.noSender());
104
105         replyActor.tell(new Failure(new RuntimeException()), ActorRef.noSender());
106
107         kit.expectMsgClass(Failure.class);
108         kit.expectTerminated(replyActor);
109     }
110
111     @Test
112     public void testGetSnapshotTimeout() {
113         JavaTestKit kit = new JavaTestKit(getSystem());
114
115         byte[] shardManagerSnapshot = new byte[]{0,5,9};
116         ActorRef replyActor = actorFactory.createActor(ShardManagerGetSnapshotReplyActor.props(
117                 Arrays.asList("shard1"), "config",
118                 shardManagerSnapshot, kit.getRef(), "shard-manager", Duration.create(100, TimeUnit.MILLISECONDS)),
119                     actorFactory.generateActorId("actor"));
120
121         kit.watch(replyActor);
122
123         Failure failure = kit.expectMsgClass(Failure.class);
124         assertEquals("Failure cause type", TimeoutException.class, failure.cause().getClass());
125         kit.expectTerminated(replyActor);
126     }
127 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.