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