Merge "Add exists method on DOMStoreReadTransaction and DOMDataReadTransaction"
[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 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
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.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
23 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
24 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
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 import java.util.Collections;
31
32 import static org.junit.Assert.assertTrue;
33
34 /**
35  * Covers negative test cases
36  * @author Basheeruddin Ahmed <syedbahm@cisco.com>
37  */
38 public class ShardTransactionFailureTest extends AbstractActorTest {
39     private static ListeningExecutorService storeExecutor =
40         MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
41
42     private static final InMemoryDOMDataStore store =
43         new InMemoryDOMDataStore("OPER", storeExecutor,
44             MoreExecutors.sameThreadExecutor());
45
46     private static final SchemaContext testSchemaContext =
47         TestModel.createTestContext();
48
49     private static final ShardIdentifier SHARD_IDENTIFIER =
50         ShardIdentifier.builder().memberName("member-1")
51             .shardName("inventory").type("config").build();
52
53     static {
54         store.onGlobalContextUpdated(testSchemaContext);
55     }
56
57
58     @Test(expected = ReadFailedException.class)
59     public void testNegativeReadWithReadOnlyTransactionClosed()
60         throws Throwable {
61
62         final ActorRef shard =
63             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
64         final Props props =
65             ShardTransaction.props(store.newReadOnlyTransaction(), shard,
66                 TestModel.createTestContext());
67
68         final TestActorRef<ShardTransaction> subject = TestActorRef
69             .create(getSystem(), props,
70                 "testNegativeReadWithReadOnlyTransactionClosed");
71
72         ShardTransactionMessages.ReadData readData =
73             ShardTransactionMessages.ReadData.newBuilder()
74                 .setInstanceIdentifierPathArguments(
75                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
76                         .build()
77                 ).build();
78         Future<Object> future =
79             akka.pattern.Patterns.ask(subject, readData, 3000);
80         assertTrue(future.isCompleted());
81         Await.result(future, Duration.Zero());
82
83         ((ShardReadTransaction) subject.underlyingActor())
84             .forUnitTestOnlyExplicitTransactionClose();
85
86         future = akka.pattern.Patterns.ask(subject, readData, 3000);
87         Await.result(future, Duration.Zero());
88
89
90     }
91
92
93     @Test(expected = ReadFailedException.class)
94     public void testNegativeReadWithReadWriteOnlyTransactionClosed()
95         throws Throwable {
96
97         final ActorRef shard =
98             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
99         final Props props =
100             ShardTransaction.props(store.newReadWriteTransaction(), shard,
101                 TestModel.createTestContext());
102
103         final TestActorRef<ShardTransaction> subject = TestActorRef
104             .create(getSystem(), props,
105                 "testNegativeReadWithReadWriteOnlyTransactionClosed");
106
107         ShardTransactionMessages.ReadData readData =
108             ShardTransactionMessages.ReadData.newBuilder()
109                 .setInstanceIdentifierPathArguments(
110                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
111                         .build()
112                 ).build();
113         Future<Object> future =
114             akka.pattern.Patterns.ask(subject, readData, 3000);
115         assertTrue(future.isCompleted());
116         Await.result(future, Duration.Zero());
117
118         ((ShardReadWriteTransaction) subject.underlyingActor())
119             .forUnitTestOnlyExplicitTransactionClose();
120
121         future = akka.pattern.Patterns.ask(subject, readData, 3000);
122         Await.result(future, Duration.Zero());
123
124
125     }
126
127     @Test(expected = ReadFailedException.class)
128     public void testNegativeExistsWithReadWriteOnlyTransactionClosed()
129         throws Throwable {
130
131         final ActorRef shard =
132             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
133         final Props props =
134             ShardTransaction.props(store.newReadWriteTransaction(), shard,
135                 TestModel.createTestContext());
136
137         final TestActorRef<ShardTransaction> subject = TestActorRef
138             .create(getSystem(), props,
139                 "testNegativeExistsWithReadWriteOnlyTransactionClosed");
140
141         ShardTransactionMessages.DataExists dataExists =
142             ShardTransactionMessages.DataExists.newBuilder()
143                 .setInstanceIdentifierPathArguments(
144                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
145                         .build()
146                 ).build();
147
148         Future<Object> future =
149             akka.pattern.Patterns.ask(subject, dataExists, 3000);
150         assertTrue(future.isCompleted());
151         Await.result(future, Duration.Zero());
152
153         ((ShardReadWriteTransaction) subject.underlyingActor())
154             .forUnitTestOnlyExplicitTransactionClose();
155
156         future = akka.pattern.Patterns.ask(subject, dataExists, 3000);
157         Await.result(future, Duration.Zero());
158
159
160     }
161
162     @Test(expected = IllegalStateException.class)
163     public void testNegativeWriteWithTransactionReady() throws Exception {
164
165
166         final ActorRef shard =
167             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
168         final Props props =
169             ShardTransaction.props(store.newWriteOnlyTransaction(), shard,
170                 TestModel.createTestContext());
171
172         final TestActorRef<ShardTransaction> subject = TestActorRef
173             .create(getSystem(), props,
174                 "testNegativeWriteWithTransactionReady");
175
176         ShardTransactionMessages.ReadyTransaction readyTransaction =
177             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
178
179         Future<Object> future =
180             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
181         assertTrue(future.isCompleted());
182         Await.result(future, Duration.Zero());
183
184         ShardTransactionMessages.WriteData writeData =
185             ShardTransactionMessages.WriteData.newBuilder()
186                 .setInstanceIdentifierPathArguments(
187                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
188                         .build()).setNormalizedNode(
189                 NormalizedNodeMessages.Node.newBuilder().build()
190
191             ).build();
192
193         future = akka.pattern.Patterns.ask(subject, writeData, 3000);
194         assertTrue(future.isCompleted());
195         Await.result(future, Duration.Zero());
196
197
198     }
199
200
201     @Test(expected = IllegalStateException.class)
202     public void testNegativeReadWriteWithTransactionReady() throws Exception {
203
204
205         final ActorRef shard =
206             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
207         final Props props =
208             ShardTransaction.props(store.newReadWriteTransaction(), shard,
209                 TestModel.createTestContext());
210
211         final TestActorRef<ShardTransaction> subject = TestActorRef
212             .create(getSystem(), props,
213                 "testNegativeReadWriteWithTransactionReady");
214
215         ShardTransactionMessages.ReadyTransaction readyTransaction =
216             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
217
218         Future<Object> future =
219             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
220         assertTrue(future.isCompleted());
221         Await.result(future, Duration.Zero());
222
223         ShardTransactionMessages.WriteData writeData =
224             ShardTransactionMessages.WriteData.newBuilder()
225                 .setInstanceIdentifierPathArguments(
226                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
227                         .build()).setNormalizedNode(
228                 NormalizedNodeMessages.Node.newBuilder().build()
229
230             ).build();
231
232         future = akka.pattern.Patterns.ask(subject, writeData, 3000);
233         assertTrue(future.isCompleted());
234         Await.result(future, Duration.Zero());
235
236
237     }
238
239     @Test(expected = IllegalStateException.class)
240     public void testNegativeMergeTransactionReady() throws Exception {
241
242
243         final ActorRef shard =
244             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
245         final Props props =
246             ShardTransaction.props(store.newReadWriteTransaction(), shard,
247                 TestModel.createTestContext());
248
249         final TestActorRef<ShardTransaction> subject = TestActorRef
250             .create(getSystem(), props, "testNegativeMergeTransactionReady");
251
252         ShardTransactionMessages.ReadyTransaction readyTransaction =
253             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
254
255         Future<Object> future =
256             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
257         assertTrue(future.isCompleted());
258         Await.result(future, Duration.Zero());
259
260         ShardTransactionMessages.MergeData mergeData =
261             ShardTransactionMessages.MergeData.newBuilder()
262                 .setInstanceIdentifierPathArguments(
263                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
264                         .build()).setNormalizedNode(
265                 NormalizedNodeMessages.Node.newBuilder().build()
266
267             ).build();
268
269         future = akka.pattern.Patterns.ask(subject, mergeData, 3000);
270         assertTrue(future.isCompleted());
271         Await.result(future, Duration.Zero());
272
273
274     }
275
276
277     @Test(expected = IllegalStateException.class)
278     public void testNegativeDeleteDataWhenTransactionReady() throws Exception {
279
280
281         final ActorRef shard =
282             getSystem().actorOf(Shard.props(SHARD_IDENTIFIER, Collections.EMPTY_MAP));
283         final Props props =
284             ShardTransaction.props(store.newReadWriteTransaction(), shard,
285                 TestModel.createTestContext());
286
287         final TestActorRef<ShardTransaction> subject = TestActorRef
288             .create(getSystem(), props,
289                 "testNegativeDeleteDataWhenTransactionReady");
290
291         ShardTransactionMessages.ReadyTransaction readyTransaction =
292             ShardTransactionMessages.ReadyTransaction.newBuilder().build();
293
294         Future<Object> future =
295             akka.pattern.Patterns.ask(subject, readyTransaction, 3000);
296         assertTrue(future.isCompleted());
297         Await.result(future, Duration.Zero());
298
299         ShardTransactionMessages.DeleteData deleteData =
300             ShardTransactionMessages.DeleteData.newBuilder()
301                 .setInstanceIdentifierPathArguments(
302                     NormalizedNodeMessages.InstanceIdentifier.newBuilder()
303                         .build()).build();
304
305         future = akka.pattern.Patterns.ask(subject, deleteData, 3000);
306         assertTrue(future.isCompleted());
307         Await.result(future, Duration.Zero());
308
309
310     }
311
312
313 }