Move ShardManager into its own package
[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.datastore.AbstractActorTest;
23 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
24 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
25 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot;
26 import org.opendaylight.controller.cluster.raft.TestActorFactory;
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 final TestActorFactory actorFactory = new TestActorFactory(getSystem());
38
39     @After
40     public void tearDown() {
41         actorFactory.close();
42     }
43
44     @Test
45     public void testSuccess() {
46         JavaTestKit kit = new JavaTestKit(getSystem());
47
48         byte[] shardManagerSnapshot = new byte[]{0,5,9};
49         ActorRef replyActor = actorFactory.createActor(ShardManagerGetSnapshotReplyActor.props(
50                 Arrays.asList("shard1", "shard2", "shard3"), "config",
51                 shardManagerSnapshot, kit.getRef(), "shard-manager", Duration.create(100, TimeUnit.SECONDS)),
52                     actorFactory.generateActorId("actor"));
53
54         kit.watch(replyActor);
55
56         byte[] shard1Snapshot = new byte[]{1,2,3};
57         replyActor.tell(new GetSnapshotReply(ShardIdentifier.builder().memberName("member-1").type("config").
58                 shardName("shard1").build().toString(), shard1Snapshot), ActorRef.noSender());
59
60         byte[] shard2Snapshot = new byte[]{4,5,6};
61         replyActor.tell(new GetSnapshotReply(ShardIdentifier.builder().memberName("member-1").type("config").
62                 shardName("shard2").build().toString(), shard2Snapshot), ActorRef.noSender());
63
64         kit.expectNoMsg(FiniteDuration.create(500, TimeUnit.MILLISECONDS));
65
66         byte[] shard3Snapshot = new byte[]{7,8,9};
67         replyActor.tell(new GetSnapshotReply(ShardIdentifier.builder().memberName("member-1").type("config").
68                 shardName("shard3").build().toString(), shard3Snapshot), ActorRef.noSender());
69
70         DatastoreSnapshot datastoreSnapshot = kit.expectMsgClass(DatastoreSnapshot.class);
71
72         assertEquals("getType", "config", datastoreSnapshot.getType());
73         assertArrayEquals("getShardManagerSnapshot", shardManagerSnapshot, datastoreSnapshot.getShardManagerSnapshot());
74         List<ShardSnapshot> shardSnapshots = datastoreSnapshot.getShardSnapshots();
75         assertEquals("ShardSnapshot size", 3, shardSnapshots.size());
76         assertEquals("ShardSnapshot 1 getName", "shard1", shardSnapshots.get(0).getName());
77         assertArrayEquals("ShardSnapshot 1 getSnapshot", shard1Snapshot, shardSnapshots.get(0).getSnapshot());
78         assertEquals("ShardSnapshot 2 getName", "shard2", shardSnapshots.get(1).getName());
79         assertArrayEquals("ShardSnapshot 2 getSnapshot", shard2Snapshot, shardSnapshots.get(1).getSnapshot());
80         assertEquals("ShardSnapshot 3 getName", "shard3", shardSnapshots.get(2).getName());
81         assertArrayEquals("ShardSnapshot 3 getSnapshot", shard3Snapshot, shardSnapshots.get(2).getSnapshot());
82
83         kit.expectMsgClass(Terminated.class);
84     }
85
86     @Test
87     public void testGetSnapshotFailureReply() {
88         JavaTestKit kit = new JavaTestKit(getSystem());
89
90         byte[] shardManagerSnapshot = new byte[]{0,5,9};
91         ActorRef replyActor = actorFactory.createActor(ShardManagerGetSnapshotReplyActor.props(
92                 Arrays.asList("shard1", "shard2"), "config",
93                 shardManagerSnapshot, kit.getRef(), "shard-manager", Duration.create(100, TimeUnit.SECONDS)),
94                     actorFactory.generateActorId("actor"));
95
96         kit.watch(replyActor);
97
98         replyActor.tell(new GetSnapshotReply(ShardIdentifier.builder().memberName("member-1").type("config").
99                 shardName("shard1").build().toString(), new byte[]{1,2,3}), ActorRef.noSender());
100
101         replyActor.tell(new Failure(new RuntimeException()), ActorRef.noSender());
102
103         kit.expectMsgClass(Failure.class);
104         kit.expectTerminated(replyActor);
105     }
106
107     @Test
108     public void testGetSnapshotTimeout() {
109         JavaTestKit kit = new JavaTestKit(getSystem());
110
111         byte[] shardManagerSnapshot = new byte[]{0,5,9};
112         ActorRef replyActor = actorFactory.createActor(ShardManagerGetSnapshotReplyActor.props(
113                 Arrays.asList("shard1"), "config",
114                 shardManagerSnapshot, kit.getRef(), "shard-manager", Duration.create(100, TimeUnit.MILLISECONDS)),
115                     actorFactory.generateActorId("actor"));
116
117         kit.watch(replyActor);
118
119         Failure failure = kit.expectMsgClass(Failure.class);
120         assertEquals("Failure cause type", TimeoutException.class, failure.cause().getClass());
121         kit.expectTerminated(replyActor);
122     }
123 }

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