Bug 4105: Implement DistributedEntityOwnershipService#registerCandidate
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / entityownership / DistributedEntityOwnershipServiceTest.java
index b7f75c98f3b49276be2ed4dc96bb768792627df3..ee5b1c5a2d3d1f5a896e05e56c20b18ad2a010ba 100644 (file)
@@ -7,19 +7,37 @@
  */
 package org.opendaylight.controller.cluster.datastore.entityownership;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
 import akka.actor.ActorRef;
+import akka.actor.Props;
+import com.google.common.util.concurrent.Uninterruptibles;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
+import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
+import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
+import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
+import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidate;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import scala.concurrent.Await;
 import scala.concurrent.Future;
 import scala.concurrent.duration.Duration;
@@ -30,10 +48,12 @@ import scala.concurrent.duration.Duration;
  * @author Thomas Pantelis
  */
 public class DistributedEntityOwnershipServiceTest extends AbstractActorTest {
-    private static int ID_COUNTER = 1;
+    static String ENTITY_TYPE = "test";
+    static String ENTITY_TYPE2 = "test2";
+    static int ID_COUNTER = 1;
+    static final QName QNAME = QName.create("test", "2015-08-11", "foo");
 
     private final String dataStoreType = "config" + ID_COUNTER++;
-    private DistributedEntityOwnershipService service;
     private DistributedDataStore dataStore;
 
     @Before
@@ -44,25 +64,136 @@ public class DistributedEntityOwnershipServiceTest extends AbstractActorTest {
                 new MockConfiguration(Collections.<String, List<String>>emptyMap()), datastoreContext );
 
         dataStore.onGlobalContextUpdated(TestModel.createTestContext());
-
-        service = new DistributedEntityOwnershipService(dataStore);
     }
 
     @Test
     public void testEntityOwnershipShardCreated() throws Exception {
+        DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore);
         service.start();
 
         Future<ActorRef> future = dataStore.getActorContext().findLocalShardAsync(
                 DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME);
         ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
         assertNotNull(DistributedEntityOwnershipService.ENTITY_OWNERSHIP_SHARD_NAME + " not found", shardActor);
+
+        service.close();
     }
 
     @Test
-    public void testRegisterCandidate() {
+    public void testRegisterCandidate() throws Exception {
+        final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
+        DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
+            @Override
+            protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
+                return shardPropsCreator;
+            }
+        };
+
+        service.start();
+
+        shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
+
+        Entity entity = new Entity(ENTITY_TYPE, YangInstanceIdentifier.of(QNAME));
+        EntityOwnershipCandidate candidate = mock(EntityOwnershipCandidate.class);
+
+        EntityOwnershipCandidateRegistration reg = service.registerCandidate(entity, candidate);
+
+        verifyEntityOwnershipCandidateRegistration(entity, reg);
+        verifyRegisterCandidateLocal(shardPropsCreator, entity, candidate);
+
+        // Test same entity - should throw exception
+
+        EntityOwnershipCandidate candidate2 = mock(EntityOwnershipCandidate.class);
+        try {
+            service.registerCandidate(entity, candidate2);
+            fail("Expected CandidateAlreadyRegisteredException");
+        } catch(CandidateAlreadyRegisteredException e) {
+            // expected
+            assertSame("getCandidate", candidate, e.getRegisteredCandidate());
+            assertEquals("getEntity", entity, e.getEntity());
+        }
+
+        // Test different entity
+
+        Entity entity2 = new Entity(ENTITY_TYPE2, YangInstanceIdentifier.of(QNAME));
+        shardPropsCreator.expectShardMessage(RegisterCandidateLocal.class);
+
+        EntityOwnershipCandidateRegistration reg2 = service.registerCandidate(entity2, candidate);
+
+        verifyEntityOwnershipCandidateRegistration(entity2, reg2);
+        verifyRegisterCandidateLocal(shardPropsCreator, entity2, candidate);
+
+        service.close();
     }
 
     @Test
     public void testRegisterListener() {
     }
+
+    private void verifyRegisterCandidateLocal(final TestShardPropsCreator shardPropsCreator, Entity entity,
+            EntityOwnershipCandidate candidate) {
+        RegisterCandidateLocal regCandidate = shardPropsCreator.waitForShardMessage();
+        assertSame("getCandidate", candidate, regCandidate.getCandidate());
+        assertEquals("getEntity", entity, regCandidate.getEntity());
+    }
+
+    private void verifyEntityOwnershipCandidateRegistration(Entity entity, EntityOwnershipCandidateRegistration reg) {
+        assertNotNull("EntityOwnershipCandidateRegistration null", reg);
+        assertEquals("getEntity", entity, reg.getEntity());
+    }
+
+    static class TestShardPropsCreator extends EntityOwnershipShardPropsCreator {
+        private final AtomicReference<CountDownLatch> messageReceived = new AtomicReference<>();
+        private final AtomicReference<Object> receivedMessage = new AtomicReference<>();
+        private final AtomicReference<Class<?>> messageClass = new AtomicReference<>();
+
+        @Override
+        public Props newProps(ShardIdentifier shardId, Map<String, String> peerAddresses,
+                DatastoreContext datastoreContext, SchemaContext schemaContext) {
+            return Props.create(TestEntityOwnershipShard.class, shardId, peerAddresses, datastoreContext,
+                    schemaContext, messageClass, messageReceived, receivedMessage);
+        }
+
+        @SuppressWarnings("unchecked")
+        <T> T waitForShardMessage() {
+            assertEquals("Message received", true, Uninterruptibles.awaitUninterruptibly(
+                    messageReceived.get(), 5, TimeUnit.SECONDS));
+            assertEquals("Message type", messageClass.get(), receivedMessage.get().getClass());
+            return (T) receivedMessage.get();
+        }
+
+        void expectShardMessage(Class<?> ofType) {
+            messageReceived.set(new CountDownLatch(1));
+            receivedMessage.set(null);
+            messageClass.set(ofType);
+        }
+    }
+
+    static class TestEntityOwnershipShard extends EntityOwnershipShard {
+        private final AtomicReference<CountDownLatch> messageReceived;
+        private final AtomicReference<Object> receivedMessage;
+        private final AtomicReference<Class<?>> messageClass;
+
+        protected TestEntityOwnershipShard(ShardIdentifier name, Map<String, String> peerAddresses,
+                DatastoreContext datastoreContext, SchemaContext schemaContext, AtomicReference<Class<?>> messageClass,
+                AtomicReference<CountDownLatch> messageReceived, AtomicReference<Object> receivedMessage) {
+            super(name, peerAddresses, datastoreContext, schemaContext);
+            this.messageClass = messageClass;
+            this.messageReceived = messageReceived;
+            this.receivedMessage = receivedMessage;
+        }
+
+        @Override
+        public void onReceiveCommand(final Object message) throws Exception {
+            try {
+                super.onReceiveCommand(message);
+            } finally {
+                Class<?> expMsgClass = messageClass.get();
+                if(expMsgClass != null && expMsgClass.equals(message.getClass())) {
+                    receivedMessage.set(message);
+                    messageReceived.get().countDown();
+                }
+            }
+        }
+    }
 }