Remove Helium Tx modify operation messages
[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.datastore.identifiers.ShardIdentifier;
17 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
18 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
19 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
20 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import scala.concurrent.Await;
26 import scala.concurrent.Future;
27 import scala.concurrent.duration.Duration;
28
29 /**
30  * Covers negative test cases
31  *
32  * @author Basheeruddin Ahmed <syedbahm@cisco.com>
33  */
34 public class ShardTransactionFailureTest extends AbstractActorTest {
35     private static final SchemaContext testSchemaContext =
36             TestModel.createTestContext();
37     private static final TransactionType RO = TransactionType.READ_ONLY;
38     private static final TransactionType RW = TransactionType.READ_WRITE;
39     private static final TransactionType WO = TransactionType.WRITE_ONLY;
40
41     private static final ShardDataTree store = new ShardDataTree(testSchemaContext, TreeType.OPERATIONAL);
42
43     private static final ShardIdentifier SHARD_IDENTIFIER =
44         ShardIdentifier.builder().memberName("member-1")
45             .shardName("inventory").type("operational").build();
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", DataStoreVersions.CURRENT_VERSION);
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), 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), 3000);
75         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
76     }
77
78
79     @Test(expected = ReadFailedException.class)
80     public void testNegativeReadWithReadWriteTransactionClosed() throws Throwable {
81
82         final ActorRef shard = createShard();
83         final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard,
84                 datastoreContext, shardStats, "txn", DataStoreVersions.CURRENT_VERSION);
85
86         final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
87                 "testNegativeReadWithReadWriteTransactionClosed");
88
89         Future<Object> future = akka.pattern.Patterns.ask(subject,
90                 new ReadData(YangInstanceIdentifier.EMPTY), 3000);
91         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
92
93         subject.underlyingActor().getDOMStoreTransaction().abort();
94
95         future = akka.pattern.Patterns.ask(subject, new ReadData(YangInstanceIdentifier.EMPTY), 3000);
96         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
97     }
98
99     @Test(expected = ReadFailedException.class)
100     public void testNegativeExistsWithReadWriteTransactionClosed() throws Throwable {
101
102         final ActorRef shard = createShard();
103         final Props props = ShardTransaction.props(RW, store.newReadWriteTransaction("test-txn", null), shard,
104                 datastoreContext, shardStats, "txn", DataStoreVersions.CURRENT_VERSION);
105
106         final TestActorRef<ShardTransaction> subject = TestActorRef.create(getSystem(), props,
107                 "testNegativeExistsWithReadWriteTransactionClosed");
108
109         Future<Object> future = akka.pattern.Patterns.ask(subject,
110                 new DataExists(YangInstanceIdentifier.EMPTY), 3000);
111         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
112
113         subject.underlyingActor().getDOMStoreTransaction().abort();
114
115         future = akka.pattern.Patterns.ask(subject, new DataExists(YangInstanceIdentifier.EMPTY), 3000);
116         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
117     }
118 }