127f9f55f89f0bedf78e2f748cd116de3819066a
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTransactionFailureTest.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore;
12
13 import akka.actor.ActorRef;
14 import akka.actor.Props;
15 import akka.testkit.TestActorRef;
16
17 import com.google.common.util.concurrent.ListeningExecutorService;
18 import com.google.common.util.concurrent.MoreExecutors;
19
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
23 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
26 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
27 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29
30 import scala.concurrent.Await;
31 import scala.concurrent.Future;
32 import scala.concurrent.duration.Duration;
33
34 import java.util.Collections;
35 import java.util.concurrent.TimeUnit;
36
37 /**
38  * Covers negative test cases
39  *
40  * @author Basheeruddin Ahmed <syedbahm@cisco.com>
41  */
42 public class ShardTransactionFailureTest extends AbstractActorTest {
43     private static ListeningExecutorService storeExecutor =
44         MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
45
46     private static final InMemoryDOMDataStore store =
47         new InMemoryDOMDataStore("OPER", storeExecutor,
48             MoreExecutors.sameThreadExecutor());
49
50     private static final SchemaContext testSchemaContext =
51         TestModel.createTestContext();
52
53     private static final ShardIdentifier SHARD_IDENTIFIER =
54         ShardIdentifier.builder().memberName("member-1")
55             .shardName("inventory").type("operational").build();
56
57     private final ShardContext shardContext = new ShardContext();
58
59     @BeforeClass
60     public static void staticSetup() {
61         store.onGlobalContextUpdated(testSchemaContext);
62     }
63
64     @Test(expected = ReadFailedException.class)
65     public void testNegativeReadWithReadOnlyTransactionClosed()
66         throws Throwable {
67
68         final ActorRef shard =
69             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
70         final Props props = ShardTransaction.props(store.newReadOnlyTransaction(), shard,
71                 testSchemaContext, shardContext);
72
73         final TestActorRef<ShardTransaction> subject = TestActorRef
74             .create(getSystem(), props,
75                 "testNegativeReadWithReadOnlyTransactionClosed");
76
77         ShardTransactionMessages.ReadData readData =
78             ShardTransactionMessages.ReadData.newBuilder()
79                 .setInstanceIdentifierPathArguments(
80                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
81                         .build()
82                 ).build();
83         Future<Object> future =
84             akka.pattern.Patterns.ask(subject, readData, 3000);
85         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
86
87         subject.underlyingActor().getDOMStoreTransaction().close();
88
89         future = akka.pattern.Patterns.ask(subject, readData, 3000);
90         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
91     }
92
93
94     @Test(expected = ReadFailedException.class)
95     public void testNegativeReadWithReadWriteTransactionClosed()
96         throws Throwable {
97
98         final ActorRef shard =
99             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
100         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
101                 testSchemaContext, shardContext);
102
103         final TestActorRef<ShardTransaction> subject = TestActorRef
104             .create(getSystem(), props,
105                 "testNegativeReadWithReadWriteTransactionClosed");
106
107         ShardTransactionMessages.ReadData readData =
108             ShardTransactionMessages.ReadData.newBuilder()
109                 .setInstanceIdentifierPathArguments(
110                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
111                         .build()
112                 ).build();
113
114         Future<Object> future =
115             akka.pattern.Patterns.ask(subject, readData, 3000);
116         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
117
118         subject.underlyingActor().getDOMStoreTransaction().close();
119
120         future = akka.pattern.Patterns.ask(subject, readData, 3000);
121         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
122     }
123
124     @Test(expected = ReadFailedException.class)
125     public void testNegativeExistsWithReadWriteTransactionClosed()
126         throws Throwable {
127
128         final ActorRef shard =
129             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
130         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
131                 testSchemaContext, shardContext);
132
133         final TestActorRef<ShardTransaction> subject = TestActorRef
134             .create(getSystem(), props,
135                 "testNegativeExistsWithReadWriteTransactionClosed");
136
137         ShardTransactionMessages.DataExists dataExists =
138             ShardTransactionMessages.DataExists.newBuilder()
139                 .setInstanceIdentifierPathArguments(
140                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
141                         .build()
142                 ).build();
143
144         Future<Object> future =
145             akka.pattern.Patterns.ask(subject, dataExists, 3000);
146         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
147
148         subject.underlyingActor().getDOMStoreTransaction().close();
149
150         future = akka.pattern.Patterns.ask(subject, dataExists, 3000);
151         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
152     }
153
154     @Test(expected = IllegalStateException.class)
155     public void testNegativeWriteWithTransactionReady() throws Exception {
156
157
158         final ActorRef shard =
159             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
160         final Props props = ShardTransaction.props(store.newWriteOnlyTransaction(), shard,
161                 testSchemaContext, shardContext);
162
163         final TestActorRef<ShardTransaction> subject = TestActorRef
164             .create(getSystem(), props,
165                 "testNegativeWriteWithTransactionReady");
166
167         ShardTransactionMessages.ReadyTransaction readyTransaction =
168             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
169
170         Future<Object> future =
171             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
172         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
173
174         ShardTransactionMessages.WriteData writeData =
175             ShardTransactionMessages.WriteData.newBuilder()
176                 .setInstanceIdentifierPathArguments(
177                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
178                         .build()).setNormalizedNode(
179                 NormalizedNodeMessages.Node.newBuilder().build()
180
181             ).build();
182
183         future = akka.pattern.Patterns.ask(subject, writeData, 3000);
184         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
185     }
186
187     @Test(expected = IllegalStateException.class)
188     public void testNegativeReadWriteWithTransactionReady() throws Exception {
189
190
191         final ActorRef shard =
192             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
193         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
194                 testSchemaContext, shardContext);
195
196         final TestActorRef<ShardTransaction> subject = TestActorRef
197             .create(getSystem(), props,
198                 "testNegativeReadWriteWithTransactionReady");
199
200         ShardTransactionMessages.ReadyTransaction readyTransaction =
201             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
202
203         Future<Object> future =
204             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
205         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
206
207         ShardTransactionMessages.WriteData writeData =
208             ShardTransactionMessages.WriteData.newBuilder()
209                 .setInstanceIdentifierPathArguments(
210                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
211                         .build()).setNormalizedNode(
212                 NormalizedNodeMessages.Node.newBuilder().build()
213
214             ).build();
215
216         future = akka.pattern.Patterns.ask(subject, writeData, 3000);
217         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
218     }
219
220     @Test(expected = IllegalStateException.class)
221     public void testNegativeMergeTransactionReady() throws Exception {
222
223
224         final ActorRef shard =
225             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
226         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
227                 testSchemaContext, shardContext);
228
229         final TestActorRef<ShardTransaction> subject = TestActorRef
230             .create(getSystem(), props, "testNegativeMergeTransactionReady");
231
232         ShardTransactionMessages.ReadyTransaction readyTransaction =
233             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
234
235         Future<Object> future =
236             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
237         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
238
239         ShardTransactionMessages.MergeData mergeData =
240             ShardTransactionMessages.MergeData.newBuilder()
241                 .setInstanceIdentifierPathArguments(
242                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
243                         .build()).setNormalizedNode(
244                 NormalizedNodeMessages.Node.newBuilder().build()
245
246             ).build();
247
248         future = akka.pattern.Patterns.ask(subject, mergeData, 3000);
249         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
250     }
251
252
253     @Test(expected = IllegalStateException.class)
254     public void testNegativeDeleteDataWhenTransactionReady() throws Exception {
255
256
257         final ActorRef shard =
258             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP, new ShardContext()));
259         final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard,
260                 testSchemaContext, shardContext);
261
262         final TestActorRef<ShardTransaction> subject = TestActorRef
263             .create(getSystem(), props,
264                 "testNegativeDeleteDataWhenTransactionReady");
265
266         ShardTransactionMessages.ReadyTransaction readyTransaction =
267             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
268
269         Future<Object> future =
270             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
271         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
272
273         ShardTransactionMessages.DeleteData deleteData =
274             ShardTransactionMessages.DeleteData.newBuilder()
275                 .setInstanceIdentifierPathArguments(
276                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
277                         .build()).build();
278
279         future = akka.pattern.Patterns.ask(subject, deleteData, 3000);
280         Await.result(future, Duration.create(3, TimeUnit.SECONDS));
281     }
282 }