Keep DataChange registrations and notifications local
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardManagerTest.java
index 87d257a3f217bf0bfac0b3c8ade1cbe8d4555b04..268ed3c27383f3254eafec08e74c8f287456456a 100644 (file)
@@ -1,5 +1,6 @@
 package org.opendaylight.controller.cluster.datastore;
 
+import akka.actor.ActorRef;
 import akka.actor.ActorSystem;
 import akka.actor.Props;
 import akka.testkit.JavaTestKit;
@@ -8,13 +9,19 @@ import junit.framework.Assert;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
+import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
+import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
 import scala.concurrent.duration.Duration;
 
+import static junit.framework.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
 public class ShardManagerTest {
     private static ActorSystem system;
 
@@ -47,7 +54,6 @@ public class ShardManagerTest {
                     expectMsgEquals(Duration.Zero(),
                         new PrimaryNotFound("inventory").toSerializable());
 
-                    // Will wait for the rest of the 3 seconds
                     expectNoMsg();
                 }
             };
@@ -64,7 +70,6 @@ public class ShardManagerTest {
             final TestActorRef<ShardManager> subject =
                 TestActorRef.create(system, props);
 
-            // the run() method needs to finish within 3 seconds
             new Within(duration("1 seconds")) {
                 protected void run() {
 
@@ -78,6 +83,75 @@ public class ShardManagerTest {
         }};
     }
 
+    @Test
+    public void testOnReceiveFindLocalShardForNonExistentShard() throws Exception {
+
+        new JavaTestKit(system) {{
+            final Props props = ShardManager
+                .props("config", new MockClusterWrapper(),
+                    new MockConfiguration());
+            final TestActorRef<ShardManager> subject =
+                TestActorRef.create(system, props);
+
+            new Within(duration("1 seconds")) {
+                protected void run() {
+
+                    subject.tell(new FindLocalShard("inventory"), getRef());
+
+                    final String out = new ExpectMsg<String>(duration("1 seconds"), "find local") {
+                        protected String match(Object in) {
+                            if (in instanceof LocalShardNotFound) {
+                                return ((LocalShardNotFound) in).getShardName();
+                            } else {
+                                throw noMatch();
+                            }
+                        }
+                    }.get(); // this extracts the received message
+
+                    assertEquals("inventory", out);
+
+                    expectNoMsg();
+                }
+            };
+        }};
+    }
+
+    @Test
+    public void testOnReceiveFindLocalShardForExistentShard() throws Exception {
+
+        final MockClusterWrapper mockClusterWrapper = new MockClusterWrapper();
+
+        new JavaTestKit(system) {{
+            final Props props = ShardManager
+                .props("config", mockClusterWrapper,
+                    new MockConfiguration());
+            final TestActorRef<ShardManager> subject =
+                TestActorRef.create(system, props);
+
+            new Within(duration("1 seconds")) {
+                protected void run() {
+
+                    subject.tell(new FindLocalShard(Shard.DEFAULT_NAME), getRef());
+
+                    final ActorRef out = new ExpectMsg<ActorRef>(duration("1 seconds"), "find local") {
+                        protected ActorRef match(Object in) {
+                            if (in instanceof LocalShardFound) {
+                                return ((LocalShardFound) in).getPath();
+                            } else {
+                                throw noMatch();
+                            }
+                        }
+                    }.get(); // this extracts the received message
+
+                    assertTrue(out.path().toString(), out.path().toString().contains("member-1-shard-default-config"));
+
+
+                    expectNoMsg();
+                }
+            };
+        }};
+    }
+
     @Test
     public void testOnReceiveMemberUp() throws Exception {