BUG-2138: DistributedShardListeners support for nested shards
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTransactionFailureTest.java
index 3ecc8c9902d0a65fd02f7dfb144dd4d830322ea3..6ccede2776bc6a58b8857e79c17c747c3e6745e6 100644 (file)
@@ -13,6 +13,8 @@ import akka.actor.Props;
 import akka.testkit.TestActorRef;
 import java.util.concurrent.TimeUnit;
 import org.junit.Test;
+import org.mockito.Mockito;
+import org.opendaylight.controller.cluster.access.concepts.MemberName;
 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
@@ -27,92 +29,94 @@ import scala.concurrent.Future;
 import scala.concurrent.duration.Duration;
 
 /**
- * Covers negative test cases
+ * Covers negative test cases.
  *
- * @author Basheeruddin Ahmed <syedbahm@cisco.com>
+ * @author Basheeruddin Ahmed
  */
 public class ShardTransactionFailureTest extends AbstractActorTest {
-    private static final SchemaContext testSchemaContext =
-            TestModel.createTestContext();
+    private static final SchemaContext TEST_SCHEMA_CONTEXT = TestModel.createTestContext();
     private static final TransactionType RO = TransactionType.READ_ONLY;
     private static final TransactionType RW = TransactionType.READ_WRITE;
-    private static final TransactionType WO = TransactionType.WRITE_ONLY;
 
-    private static final ShardDataTree store = new ShardDataTree(testSchemaContext, TreeType.OPERATIONAL);
+    private static final Shard MOCK_SHARD = Mockito.mock(Shard.class);
+
+    private static final ShardDataTree STORE = new ShardDataTree(MOCK_SHARD, TEST_SCHEMA_CONTEXT, TreeType.OPERATIONAL);
 
     private static final ShardIdentifier SHARD_IDENTIFIER =
-        ShardIdentifier.builder().memberName("member-1")
-            .shardName("inventory").type("operational").build();
+        ShardIdentifier.create("inventory", MemberName.forName("member-1"), "operational");
 
     private final DatastoreContext datastoreContext = DatastoreContext.newBuilder().build();
 
-    private final ShardStats shardStats = new ShardStats(SHARD_IDENTIFIER.toString(), "DataStore");
+    private final ShardStats shardStats = new ShardStats(SHARD_IDENTIFIER.toString(), "DataStore", null);
 
-    private ActorRef createShard(){
-        ActorRef shard = getSystem().actorOf(Shard.builder().id(SHARD_IDENTIFIER).datastoreContext(datastoreContext).
-                schemaContext(TestModel.createTestContext()).props());
+    private ActorRef createShard() {
+        ActorRef shard = getSystem().actorOf(Shard.builder().id(SHARD_IDENTIFIER).datastoreContext(datastoreContext)
+                .schemaContext(TestModel.createTestContext()).props());
         ShardTestKit.waitUntilLeader(shard);
         return shard;
     }
 
     @Test(expected = ReadFailedException.class)
-    public void testNegativeReadWithReadOnlyTransactionClosed() throws Throwable {
+    public void testNegativeReadWithReadOnlyTransactionClosed() throws Exception {
 
         final ActorRef shard = createShard();
-        final Props props = ShardTransaction.props(RO, store.newReadOnlyTransaction("test-txn", null), shard,
-                datastoreContext, shardStats, "txn", DataStoreVersions.CURRENT_VERSION);
+        final Props props = ShardTransaction.props(RO, STORE.newReadOnlyTransaction(nextTransactionId()), shard,
+                datastoreContext, shardStats);
 
         final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
                 "testNegativeReadWithReadOnlyTransactionClosed");
 
         Future<Object> future = akka.pattern.Patterns.ask(subject,
-                new ReadData(YangInstanceIdentifier.EMPTY), 3000);
+                new ReadData(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
 
         subject.underlyingActor().getDOMStoreTransaction().abort();
 
-        future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY), 3000);
+        future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY,
+                DataStoreVersions.CURRENT_VERSION), 3000);
         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
     }
 
 
     @Test(expected = ReadFailedException.class)
-    public void testNegativeReadWithReadWriteTransactionClosed() throws Throwable {
+    public void testNegativeReadWithReadWriteTransactionClosed() throws Exception {
 
         final ActorRef shard = createShard();
-        final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard,
-                datastoreContext, shardStats, "txn", DataStoreVersions.CURRENT_VERSION);
+        final Props props = ShardTransaction.props(RW, STORE.newReadWriteTransaction(nextTransactionId()), shard,
+                datastoreContext, shardStats);
 
         final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
                 "testNegativeReadWithReadWriteTransactionClosed");
 
         Future<Object> future = akka.pattern.Patterns.ask(subject,
-                new ReadData(YangInstanceIdentifier.EMPTY), 3000);
+                new ReadData(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
 
         subject.underlyingActor().getDOMStoreTransaction().abort();
 
-        future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY), 3000);
+        future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY,
+                DataStoreVersions.CURRENT_VERSION), 3000);
         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
     }
 
     @Test(expected = ReadFailedException.class)
-    public void testNegativeExistsWithReadWriteTransactionClosed() throws Throwable {
+    public void testNegativeExistsWithReadWriteTransactionClosed() throws Exception {
 
         final ActorRef shard = createShard();
-        final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard,
-                datastoreContext, shardStats, "txn", DataStoreVersions.CURRENT_VERSION);
+        final Props props = ShardTransaction.props(RW, STORE.newReadWriteTransaction(nextTransactionId()), shard,
+                datastoreContext, shardStats);
 
         final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
                 "testNegativeExistsWithReadWriteTransactionClosed");
 
         Future<Object> future = akka.pattern.Patterns.ask(subject,
-                new DataExists(YangInstanceIdentifier.EMPTY), 3000);
+                new DataExists(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
 
         subject.underlyingActor().getDOMStoreTransaction().abort();
 
-        future = akka.pattern.Patterns.ask(subject, new DataExists(YangInstanceIdentifier.EMPTY), 3000);
+        future = akka.pattern.Patterns.ask(subject,
+                new DataExists(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
     }
 }