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