5085cdd305bdb5a5e5636cab31485b9cdd692ca5
[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.Collections;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
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.persisted.DatastoreSnapshot;
27 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot;
28 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
29 import org.opendaylight.controller.cluster.raft.client.messages.GetSnapshotReply;
30 import org.opendaylight.controller.cluster.raft.persisted.ByteState;
31 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
32 import scala.concurrent.duration.Duration;
33 import scala.concurrent.duration.FiniteDuration;
34
35 /**
36  * Unit tests for ShardManagerGetSnapshotReplyActor.
37  *
38  * @author Thomas Pantelis
39  */
40 public class ShardManagerGetSnapshotReplyActorTest extends AbstractActorTest {
41     private static final MemberName MEMBER_1 = MemberName.forName("member-1");
42
43     @Test
44     public void testSuccess() {
45         JavaTestKit kit = new JavaTestKit(getSystem());
46
47         byte[] shardManagerSnapshot = new byte[]{0,5,9};
48         ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
49                 Arrays.asList("shard1", "shard2", "shard3"), "config", shardManagerSnapshot, kit.getRef(),
50                 "shard-manager", Duration.create(100, TimeUnit.SECONDS)), "testSuccess");
51
52         kit.watch(replyActor);
53
54         ByteState shard1SnapshotState = ByteState.of(new byte[]{1,2,3});
55         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
56                 Snapshot.create(shard1SnapshotState, Collections.<ReplicatedLogEntry>emptyList(),
57                         2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
58
59         ByteState shard2SnapshotState = ByteState.of(new byte[]{4,5,6});
60         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard2", MEMBER_1, "config").toString(),
61                 Snapshot.create(shard2SnapshotState, Collections.<ReplicatedLogEntry>emptyList(),
62                         2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
63
64         kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
65
66         ByteState shard3SnapshotState = ByteState.of(new byte[]{7,8,9});
67         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard3", MEMBER_1, "config").toString(),
68                 Snapshot.create(shard3SnapshotState, Collections.<ReplicatedLogEntry>emptyList(),
69                         2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
70
71         DatastoreSnapshot datastoreSnapshot = kit.expectMsgClass(DatastoreSnapshot.class);
72
73         assertEquals("getType", "config", datastoreSnapshot.getType());
74         assertArrayEquals("getShardManagerSnapshot", shardManagerSnapshot, datastoreSnapshot.getShardManagerSnapshot());
75         List<ShardSnapshot> shardSnapshots = datastoreSnapshot.getShardSnapshots();
76         assertEquals("ShardSnapshot size", 3, shardSnapshots.size());
77         assertEquals("ShardSnapshot 1 getName", "shard1", shardSnapshots.get(0).getName());
78         assertEquals("ShardSnapshot 1 getSnapshot", shard1SnapshotState,
79                 shardSnapshots.get(0).getSnapshot().getState());
80         assertEquals("ShardSnapshot 2 getName", "shard2", shardSnapshots.get(1).getName());
81         assertEquals("ShardSnapshot 2 getSnapshot", shard2SnapshotState,
82                 shardSnapshots.get(1).getSnapshot().getState());
83         assertEquals("ShardSnapshot 3 getName", "shard3", shardSnapshots.get(2).getName());
84         assertEquals("ShardSnapshot 3 getSnapshot", shard3SnapshotState,
85                 shardSnapshots.get(2).getSnapshot().getState());
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 = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
96                 Arrays.asList("shard1", "shard2"), "config", shardManagerSnapshot, kit.getRef(), "shard-manager",
97                 Duration.create(100, TimeUnit.SECONDS)), "testGetSnapshotFailureReply");
98
99         kit.watch(replyActor);
100
101         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
102                 Snapshot.create(ByteState.of(new byte[]{1,2,3}), Collections.<ReplicatedLogEntry>emptyList(),
103                         2, 1, 2, 1, 1, "member-1", null)), 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 = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
117                 Arrays.asList("shard1"), "config", shardManagerSnapshot, kit.getRef(), "shard-manager",
118                 Duration.create(100, TimeUnit.MILLISECONDS)), "testGetSnapshotTimeout");
119
120         kit.watch(replyActor);
121
122         Failure failure = kit.expectMsgClass(Failure.class);
123         assertEquals("Failure cause type", TimeoutException.class, failure.cause().getClass());
124         kit.expectTerminated(replyActor);
125     }
126 }

©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.