BUG-5626: Eliminate ShardIdentifier.Builder
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTransactionFailureTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco 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
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import akka.testkit.TestActorRef;
14 import java.util.concurrent.TimeUnit;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.access.concepts.MemberName;
17 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
18 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
19 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
20 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
21 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
22 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import scala.concurrent.Await;
27 import scala.concurrent.Future;
28 import scala.concurrent.duration.Duration;
29
30 /**
31  * Covers negative test cases
32  *
33  * @author Basheeruddin Ahmed <syedbahm@cisco.com>
34  */
35 public class ShardTransactionFailureTest extends AbstractActorTest {
36     private static final SchemaContext testSchemaContext =
37             TestModel.createTestContext();
38     private static final TransactionType RO = TransactionType.READ_ONLY;
39     private static final TransactionType RW = TransactionType.READ_WRITE;
40     private static final TransactionType WO = TransactionType.WRITE_ONLY;
41
42     private static final ShardDataTree store = new ShardDataTree(testSchemaContext, TreeType.OPERATIONAL);
43
44     private static final ShardIdentifier SHARD_IDENTIFIER =
45         ShardIdentifier.create("inventory", MemberName.forName("member-1"), "operational");
46
47     private final DatastoreContext datastoreContext = DatastoreContext.newBuilder().build();
48
49     private final ShardStats shardStats = new ShardStats(SHARD_IDENTIFIER.toString(), "DataStore");
50
51     private ActorRef createShard(){
52         ActorRef shard = getSystem().actorOf(Shard.builder().id(SHARD_IDENTIFIER).datastoreContext(datastoreContext).
53                 schemaContext(TestModel.createTestContext()).props());
54         ShardTestKit.waitUntilLeader(shard);
55         return shard;
56     }
57
58     @Test(expected = ReadFailedException.class)
59     public void testNegativeReadWithReadOnlyTransactionClosed() throws Throwable {
60
61         final ActorRef shard = createShard();
62         final Props props = ShardTransaction.props(RO, store.newReadOnlyTransaction("test-txn", null), shard,
63                 datastoreContext, shardStats, "txn");
64
65         final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
66                 "testNegativeReadWithReadOnlyTransactionClosed");
67
68         Future<Object> future = akka.pattern.Patterns.ask(subject,
69                 new ReadData(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
70         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
71
72         subject.underlyingActor().getDOMStoreTransaction().abort();
73
74         future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY,
75                 DataStoreVersions.CURRENT_VERSION), 3000);
76         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
77     }
78
79
80     @Test(expected = ReadFailedException.class)
81     public void testNegativeReadWithReadWriteTransactionClosed() throws Throwable {
82
83         final ActorRef shard = createShard();
84         final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard,
85                 datastoreContext, shardStats, "txn");
86
87         final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
88                 "testNegativeReadWithReadWriteTransactionClosed");
89
90         Future<Object> future = akka.pattern.Patterns.ask(subject,
91                 new ReadData(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
92         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
93
94         subject.underlyingActor().getDOMStoreTransaction().abort();
95
96         future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY,
97                 DataStoreVersions.CURRENT_VERSION), 3000);
98         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
99     }
100
101     @Test(expected = ReadFailedException.class)
102     public void testNegativeExistsWithReadWriteTransactionClosed() throws Throwable {
103
104         final ActorRef shard = createShard();
105         final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard,
106                 datastoreContext, shardStats, "txn");
107
108         final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
109                 "testNegativeExistsWithReadWriteTransactionClosed");
110
111         Future<Object> future = akka.pattern.Patterns.ask(subject,
112                 new DataExists(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
113         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
114
115         subject.underlyingActor().getDOMStoreTransaction().abort();
116
117         future = akka.pattern.Patterns.ask(subject,
118                 new DataExists(YangInstanceIdentifier.EMPTY, DataStoreVersions.CURRENT_VERSION), 3000);
119         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
120     }
121 }