752c8e169e7385fd756552ba1da77c78c1541cb8
[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.assertEquals;
11
12 import akka.actor.ActorRef;
13 import akka.actor.Status.Failure;
14 import akka.actor.Terminated;
15 import akka.testkit.javadsl.TestKit;
16 import java.util.Arrays;
17 import java.util.Collections;
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.persisted.DatastoreSnapshot;
26 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot;
27 import org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot;
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         TestKit kit = new TestKit(getSystem());
46
47         List<String> shardList = Arrays.asList("shard1", "shard2", "shard3");
48         ShardManagerSnapshot shardManagerSnapshot = new ShardManagerSnapshot(shardList, Collections.emptyMap());
49         ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
50                 shardList, "config", shardManagerSnapshot, kit.getRef(),
51                 "shard-manager", Duration.create(100, TimeUnit.SECONDS)), "testSuccess");
52
53         kit.watch(replyActor);
54
55         ByteState shard1SnapshotState = ByteState.of(new byte[]{1,2,3});
56         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
57                 Snapshot.create(shard1SnapshotState, Collections.<ReplicatedLogEntry>emptyList(),
58                         2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
59
60         ByteState shard2SnapshotState = ByteState.of(new byte[]{4,5,6});
61         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard2", MEMBER_1, "config").toString(),
62                 Snapshot.create(shard2SnapshotState, Collections.<ReplicatedLogEntry>emptyList(),
63                         2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
64
65         kit.expectNoMessage(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
66
67         ByteState shard3SnapshotState = ByteState.of(new byte[]{7,8,9});
68         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard3", MEMBER_1, "config").toString(),
69                 Snapshot.create(shard3SnapshotState, Collections.<ReplicatedLogEntry>emptyList(),
70                         2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
71
72         DatastoreSnapshot datastoreSnapshot = kit.expectMsgClass(DatastoreSnapshot.class);
73
74         assertEquals("getType", "config", datastoreSnapshot.getType());
75         assertEquals("getShardManagerSnapshot", shardManagerSnapshot.getShardList(),
76                 datastoreSnapshot.getShardManagerSnapshot().getShardList());
77         List<ShardSnapshot> shardSnapshots = datastoreSnapshot.getShardSnapshots();
78         assertEquals("ShardSnapshot size", 3, shardSnapshots.size());
79         assertEquals("ShardSnapshot 1 getName", "shard1", shardSnapshots.get(0).getName());
80         assertEquals("ShardSnapshot 1 getSnapshot", shard1SnapshotState,
81                 shardSnapshots.get(0).getSnapshot().getState());
82         assertEquals("ShardSnapshot 2 getName", "shard2", shardSnapshots.get(1).getName());
83         assertEquals("ShardSnapshot 2 getSnapshot", shard2SnapshotState,
84                 shardSnapshots.get(1).getSnapshot().getState());
85         assertEquals("ShardSnapshot 3 getName", "shard3", shardSnapshots.get(2).getName());
86         assertEquals("ShardSnapshot 3 getSnapshot", shard3SnapshotState,
87                 shardSnapshots.get(2).getSnapshot().getState());
88
89         kit.expectMsgClass(Terminated.class);
90     }
91
92     @Test
93     public void testGetSnapshotFailureReply() {
94         TestKit kit = new TestKit(getSystem());
95
96         ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
97                 Arrays.asList("shard1", "shard2"), "config", null, kit.getRef(), "shard-manager",
98                 Duration.create(100, TimeUnit.SECONDS)), "testGetSnapshotFailureReply");
99
100         kit.watch(replyActor);
101
102         replyActor.tell(new GetSnapshotReply(ShardIdentifier.create("shard1", MEMBER_1, "config").toString(),
103                 Snapshot.create(ByteState.of(new byte[]{1,2,3}), Collections.<ReplicatedLogEntry>emptyList(),
104                         2, 1, 2, 1, 1, "member-1", null)), ActorRef.noSender());
105
106         replyActor.tell(new Failure(new RuntimeException()), ActorRef.noSender());
107
108         kit.expectMsgClass(Failure.class);
109         kit.expectTerminated(replyActor);
110     }
111
112     @Test
113     public void testGetSnapshotTimeout() {
114         TestKit kit = new TestKit(getSystem());
115
116         ActorRef replyActor = getSystem().actorOf(ShardManagerGetSnapshotReplyActor.props(
117                 Arrays.asList("shard1"), "config", null, 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 }