Changes for akka 2.4.11
[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.Test;
22 import org.opendaylight.controller.cluster.access.concepts.MemberName;
23 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
24 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
25 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
26 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot;
27 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
28 import scala.concurrent.duration.Duration;
29 import scala.concurrent.duration.FiniteDuration;
30
31 /**
32  * Unit tests for ShardManagerGetSnapshotReplyActor.
33  *
34  * @author Thomas Pantelis
35  */
36 public class ShardManagerGetSnapshotReplyActorTest extends AbstractActorTest {
37     private static final MemberName MEMBER_1 = MemberName.forName("member-1");
38
39     @Test
40     public void testSuccess() {
41         JavaTestKit kit = new JavaTestKit(getSystem());
42
43         byte[] shardManagerSnapshot = new byte[]{0,5,9};
44         ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
45                 Arrays.asList("shard1", "shard2", "shard3"), "config", shardManagerSnapshot, kit.getRef(),
46                 "shard-manager", Duration.create(100, TimeUnit.SECONDS)), "testSuccess");
47
48         kit.watch(replyActor);
49
50         byte[] shard1Snapshot = new byte[]{1,2,3};
51         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
52             shard1Snapshot), ActorRef.noSender());
53
54         byte[] shard2Snapshot = new byte[]{4,5,6};
55         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard2", MEMBER_1, "config").toString(),
56             shard2Snapshot), ActorRef.noSender());
57
58         kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
59
60         byte[] shard3Snapshot = new byte[]{7,8,9};
61         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard3", MEMBER_1, "config").toString(),
62             shard3Snapshot), ActorRef.noSender());
63
64         DatastoreSnapshot datastoreSnapshot = kit.expectMsgClass(DatastoreSnapshot.class);
65
66         assertEquals("getType", "config", datastoreSnapshot.getType());
67         assertArrayEquals("getShardManagerSnapshot", shardManagerSnapshot, datastoreSnapshot.getShardManagerSnapshot());
68         List<ShardSnapshot> shardSnapshots = datastoreSnapshot.getShardSnapshots();
69         assertEquals("ShardSnapshot size", 3, shardSnapshots.size());
70         assertEquals("ShardSnapshot 1 getName", "shard1", shardSnapshots.get(0).getName());
71         assertArrayEquals("ShardSnapshot 1 getSnapshot", shard1Snapshot, shardSnapshots.get(0).getSnapshot());
72         assertEquals("ShardSnapshot 2 getName", "shard2", shardSnapshots.get(1).getName());
73         assertArrayEquals("ShardSnapshot 2 getSnapshot", shard2Snapshot, shardSnapshots.get(1).getSnapshot());
74         assertEquals("ShardSnapshot 3 getName", "shard3", shardSnapshots.get(2).getName());
75         assertArrayEquals("ShardSnapshot 3 getSnapshot", shard3Snapshot, shardSnapshots.get(2).getSnapshot());
76
77         kit.expectMsgClass(Terminated.class);
78     }
79
80     @Test
81     public void testGetSnapshotFailureReply() {
82         JavaTestKit kit = new JavaTestKit(getSystem());
83
84         byte[] shardManagerSnapshot = new byte[]{0,5,9};
85         ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
86                 Arrays.asList("shard1", "shard2"), "config", shardManagerSnapshot, kit.getRef(), "shard-manager",
87                 Duration.create(100, TimeUnit.SECONDS)), "testGetSnapshotFailureReply");
88
89         kit.watch(replyActor);
90
91         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
92             new byte[]{1,2,3}), ActorRef.noSender());
93
94         replyActor.tell(new Failure(new RuntimeException()), ActorRef.noSender());
95
96         kit.expectMsgClass(Failure.class);
97         kit.expectTerminated(replyActor);
98     }
99
100     @Test
101     public void testGetSnapshotTimeout() {
102         JavaTestKit kit = new JavaTestKit(getSystem());
103
104         byte[] shardManagerSnapshot = new byte[]{0,5,9};
105         ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
106                 Arrays.asList("shard1"), "config", shardManagerSnapshot, kit.getRef(), "shard-manager",
107                 Duration.create(100, TimeUnit.MILLISECONDS)), "testGetSnapshotTimeout");
108
109         kit.watch(replyActor);
110
111         Failure failure = kit.expectMsgClass(Failure.class);
112         assertEquals("Failure cause type", TimeoutException.class, failure.cause().getClass());
113         kit.expectTerminated(replyActor);
114     }
115 }