Do not use MoreExecutors.sameThreadExecutor()
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / AbstractTransactionProxyTest.java
index a64a5802b8387102bdfac19e4535a440088ccf55..f7a364b67ec59987fde854865a00bcaec58dd9e9 100644 (file)
@@ -23,6 +23,7 @@ import akka.actor.ActorSystem;
 import akka.actor.Props;
 import akka.dispatch.Futures;
 import akka.testkit.JavaTestKit;
+import akka.util.Timeout;
 import com.codahale.metrics.MetricRegistry;
 import com.codahale.metrics.Timer;
 import com.google.common.base.Objects;
@@ -46,8 +47,8 @@ import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
-import org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType;
 import org.opendaylight.controller.cluster.datastore.TransactionProxyTest.TestException;
+import org.opendaylight.controller.cluster.datastore.config.Configuration;
 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
@@ -67,6 +68,7 @@ import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy
 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
+import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
@@ -91,27 +93,41 @@ public abstract class AbstractTransactionProxyTest {
     private static ActorSystem system;
 
     private final Configuration configuration = new MockConfiguration() {
+        Map<String, ShardStrategy> strategyMap = ImmutableMap.<String, ShardStrategy>builder().put(
+                "junk", new ShardStrategy() {
+                    @Override
+                    public String findShard(YangInstanceIdentifier path) {
+                        return "junk";
+                    }
+                }).put(
+                "cars", new ShardStrategy() {
+                    @Override
+                    public String findShard(YangInstanceIdentifier path) {
+                        return "cars";
+                    }
+                }).build();
+
         @Override
-        public Map<String, ShardStrategy> getModuleNameToShardStrategyMap() {
-            return ImmutableMap.<String, ShardStrategy>builder().put(
-                    "junk", new ShardStrategy() {
-                        @Override
-                        public String findShard(YangInstanceIdentifier path) {
-                            return "junk";
-                        }
-                    }).build();
+        public ShardStrategy getStrategyForModule(String moduleName) {
+            return strategyMap.get(moduleName);
         }
 
         @Override
-        public Optional<String> getModuleNameFromNameSpace(String nameSpace) {
-            return TestModel.JUNK_QNAME.getNamespace().toASCIIString().equals(nameSpace) ?
-                    Optional.of("junk") : Optional.<String>absent();
+        public String getModuleNameFromNameSpace(String nameSpace) {
+            if(TestModel.JUNK_QNAME.getNamespace().toASCIIString().equals(nameSpace)) {
+                return "junk";
+            } else if(CarsModel.BASE_QNAME.getNamespace().toASCIIString().equals(nameSpace)){
+                return "cars";
+            }
+            return null;
         }
     };
 
     @Mock
     protected ActorContext mockActorContext;
 
+    protected TransactionContextFactory mockComponentFactory;
+
     private SchemaContext schemaContext;
 
     @Mock
@@ -119,7 +135,9 @@ public abstract class AbstractTransactionProxyTest {
 
     protected final String memberName = "mock-member";
 
-    protected final Builder dataStoreContextBuilder = DatastoreContext.newBuilder().operationTimeoutInSeconds(2);
+    private final int operationTimeoutInSeconds = 2;
+    protected final Builder dataStoreContextBuilder = DatastoreContext.newBuilder()
+            .operationTimeoutInSeconds(operationTimeoutInSeconds);
 
     @BeforeClass
     public static void setUpClass() throws IOException {
@@ -146,16 +164,17 @@ public abstract class AbstractTransactionProxyTest {
         doReturn(getSystem()).when(mockActorContext).getActorSystem();
         doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
         doReturn(memberName).when(mockActorContext).getCurrentMemberName();
+        doReturn(new ShardStrategyFactory(configuration)).when(mockActorContext).getShardStrategyFactory();
         doReturn(schemaContext).when(mockActorContext).getSchemaContext();
+        doReturn(new Timeout(operationTimeoutInSeconds, TimeUnit.SECONDS)).when(mockActorContext).getOperationTimeout();
         doReturn(mockClusterWrapper).when(mockActorContext).getClusterWrapper();
         doReturn(mockClusterWrapper).when(mockActorContext).getClusterWrapper();
         doReturn(dataStoreContextBuilder.build()).when(mockActorContext).getDatastoreContext();
-        doReturn(10).when(mockActorContext).getTransactionOutstandingOperationLimit();
+
+        mockComponentFactory = TransactionContextFactory.create(mockActorContext);
 
         Timer timer = new MetricRegistry().timer("test");
         doReturn(timer).when(mockActorContext).getOperationTimer(any(String.class));
-
-        ShardStrategyFactory.setConfiguration(configuration);
     }
 
     protected ActorSystem getSystem() {
@@ -261,6 +280,7 @@ public abstract class AbstractTransactionProxyTest {
         return Futures.successful(new BatchedModificationsReply(count));
     }
 
+    @SuppressWarnings("unchecked")
     protected Future<Object> incompleteFuture() {
         return mock(Future.class);
     }
@@ -312,37 +332,46 @@ public abstract class AbstractTransactionProxyTest {
     }
 
     protected Future<PrimaryShardInfo> primaryShardInfoReply(ActorSystem actorSystem, ActorRef actorRef) {
+        return primaryShardInfoReply(actorSystem, actorRef, DataStoreVersions.CURRENT_VERSION);
+    }
+
+    protected Future<PrimaryShardInfo> primaryShardInfoReply(ActorSystem actorSystem, ActorRef actorRef,
+            short transactionVersion) {
         return Futures.successful(new PrimaryShardInfo(actorSystem.actorSelection(actorRef.path()),
-                Optional.<DataTree>absent()));
+                transactionVersion, Optional.<DataTree>absent()));
     }
 
     protected ActorRef setupActorContextWithoutInitialCreateTransaction(ActorSystem actorSystem, String shardName) {
+        return setupActorContextWithoutInitialCreateTransaction(actorSystem, shardName, DataStoreVersions.CURRENT_VERSION);
+    }
+
+    protected ActorRef setupActorContextWithoutInitialCreateTransaction(ActorSystem actorSystem, String shardName,
+            short transactionVersion) {
         ActorRef actorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
         log.info("Created mock shard actor {}", actorRef);
 
         doReturn(actorSystem.actorSelection(actorRef.path())).
                 when(mockActorContext).actorSelection(actorRef.path().toString());
 
-        doReturn(primaryShardInfoReply(actorSystem, actorRef)).
+        doReturn(primaryShardInfoReply(actorSystem, actorRef, transactionVersion)).
                 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
 
         doReturn(false).when(mockActorContext).isPathLocal(actorRef.path().toString());
 
-        doReturn(10).when(mockActorContext).getTransactionOutstandingOperationLimit();
-
         return actorRef;
     }
 
     protected ActorRef setupActorContextWithInitialCreateTransaction(ActorSystem actorSystem,
-            TransactionType type, int transactionVersion, String shardName) {
-        ActorRef shardActorRef = setupActorContextWithoutInitialCreateTransaction(actorSystem, shardName);
+            TransactionType type, short transactionVersion, String shardName) {
+        ActorRef shardActorRef = setupActorContextWithoutInitialCreateTransaction(actorSystem, shardName,
+                transactionVersion);
 
         return setupActorContextWithInitialCreateTransaction(actorSystem, type, transactionVersion,
                 memberName, shardActorRef);
     }
 
     protected ActorRef setupActorContextWithInitialCreateTransaction(ActorSystem actorSystem,
-            TransactionType type, int transactionVersion, String prefix, ActorRef shardActorRef) {
+            TransactionType type, short transactionVersion, String prefix, ActorRef shardActorRef) {
 
         ActorRef txActorRef;
         if(type == TransactionType.WRITE_ONLY && transactionVersion >= DataStoreVersions.LITHIUM_VERSION &&
@@ -353,11 +382,11 @@ public abstract class AbstractTransactionProxyTest {
             log.info("Created mock shard Tx actor {}", txActorRef);
 
             doReturn(actorSystem.actorSelection(txActorRef.path())).
-            when(mockActorContext).actorSelection(txActorRef.path().toString());
+                when(mockActorContext).actorSelection(txActorRef.path().toString());
 
             doReturn(Futures.successful(createTransactionReply(txActorRef, transactionVersion))).when(mockActorContext).
-            executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
-                    eqCreateTransaction(prefix, type));
+                executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
+                        eqCreateTransaction(prefix, type), any(Timeout.class));
         }
 
         return txActorRef;
@@ -381,7 +410,12 @@ public abstract class AbstractTransactionProxyTest {
             future.checkedGet(5, TimeUnit.SECONDS);
             fail("Expected ReadFailedException");
         } catch(ReadFailedException e) {
-            throw e.getCause();
+            assertNotNull("Expected a cause", e.getCause());
+            if(e.getCause().getCause() != null) {
+                throw e.getCause().getCause();
+            } else {
+                throw e.getCause();
+            }
         }
     }