Bug 3195: Cleanup on error paths and error handling
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / TransactionProxyTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Matchers.any;
7 import static org.mockito.Matchers.anyString;
8 import static org.mockito.Matchers.eq;
9 import static org.mockito.Matchers.isA;
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.verify;
14 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_ONLY;
15 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_WRITE;
16 import static org.opendaylight.controller.cluster.datastore.TransactionType.WRITE_ONLY;
17 import akka.actor.ActorRef;
18 import akka.actor.ActorSelection;
19 import akka.actor.ActorSystem;
20 import akka.actor.Props;
21 import akka.dispatch.Futures;
22 import com.google.common.base.Optional;
23 import com.google.common.collect.Sets;
24 import com.google.common.util.concurrent.CheckedFuture;
25 import com.google.common.util.concurrent.FutureCallback;
26 import com.google.common.util.concurrent.Uninterruptibles;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.atomic.AtomicReference;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.mockito.InOrder;
36 import org.mockito.Mockito;
37 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
38 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
39 import org.opendaylight.controller.cluster.datastore.exceptions.PrimaryNotFoundException;
40 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
41 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
42 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
43 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
44 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
45 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
46 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
47 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
48 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
49 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
50 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
51 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest;
52 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
53 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
54 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
55 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
56 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
57 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
59 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
62 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
63 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
64 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
65 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
66 import scala.concurrent.Promise;
67
68 @SuppressWarnings("resource")
69 public class TransactionProxyTest extends AbstractTransactionProxyTest {
70
71     @SuppressWarnings("serial")
72     static class TestException extends RuntimeException {
73     }
74
75     static interface Invoker {
76         CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception;
77     }
78
79     @Test
80     public void testRead() throws Exception {
81         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
82
83         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
84
85         doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
86                 eq(actorSelection(actorRef)), eqSerializedReadData());
87
88         Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
89                 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
90
91         assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
92
93         NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
94
95         doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
96                 eq(actorSelection(actorRef)), eqSerializedReadData());
97
98         readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
99
100         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
101
102         assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
103     }
104
105     @Test(expected = ReadFailedException.class)
106     public void testReadWithInvalidReplyMessageType() throws Exception {
107         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
108
109         doReturn(Futures.successful(new Object())).when(mockActorContext).
110                 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
111
112         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
113
114         transactionProxy.read(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
115     }
116
117     @Test(expected = TestException.class)
118     public void testReadWithAsyncRemoteOperatonFailure() throws Throwable {
119         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
120
121         doReturn(Futures.failed(new TestException())).when(mockActorContext).
122                 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedReadData());
123
124         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
125
126         propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
127     }
128
129     private void testExceptionOnInitialCreateTransaction(Exception exToThrow, Invoker invoker)
130             throws Throwable {
131         ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
132
133         if (exToThrow instanceof PrimaryNotFoundException) {
134             doReturn(Futures.failed(exToThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
135         } else {
136             doReturn(primaryShardInfoReply(getSystem(), actorRef)).
137                     when(mockActorContext).findPrimaryShardAsync(anyString());
138         }
139
140         doReturn(Futures.failed(exToThrow)).when(mockActorContext).executeOperationAsync(
141                 any(ActorSelection.class), any());
142
143         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
144
145         propagateReadFailedExceptionCause(invoker.invoke(transactionProxy));
146     }
147
148     private void testReadWithExceptionOnInitialCreateTransaction(Exception exToThrow) throws Throwable {
149         testExceptionOnInitialCreateTransaction(exToThrow, new Invoker() {
150             @Override
151             public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
152                 return proxy.read(TestModel.TEST_PATH);
153             }
154         });
155     }
156
157     @Test(expected = PrimaryNotFoundException.class)
158     public void testReadWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
159         testReadWithExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"));
160     }
161
162     @Test(expected = TimeoutException.class)
163     public void testReadWhenATimeoutExceptionIsThrown() throws Throwable {
164         testReadWithExceptionOnInitialCreateTransaction(new TimeoutException("test",
165                 new Exception("reason")));
166     }
167
168     @Test(expected = TestException.class)
169     public void testReadWhenAnyOtherExceptionIsThrown() throws Throwable {
170         testReadWithExceptionOnInitialCreateTransaction(new TestException());
171     }
172
173     @Test
174     public void testReadWithPriorRecordingOperationSuccessful() throws Throwable {
175         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
176
177         NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
178
179         expectBatchedModifications(actorRef, 1);
180
181         doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
182                 eq(actorSelection(actorRef)), eqSerializedReadData());
183
184         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
185
186         transactionProxy.write(TestModel.TEST_PATH, expectedNode);
187
188         Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
189                 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
190
191         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
192         assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
193
194         InOrder inOrder = Mockito.inOrder(mockActorContext);
195         inOrder.verify(mockActorContext).executeOperationAsync(
196                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
197
198         inOrder.verify(mockActorContext).executeOperationAsync(
199                 eq(actorSelection(actorRef)), eqSerializedReadData());
200     }
201
202     @Test(expected=IllegalStateException.class)
203     public void testReadPreConditionCheck() {
204         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
205         transactionProxy.read(TestModel.TEST_PATH);
206     }
207
208     @Test(expected=IllegalArgumentException.class)
209     public void testInvalidCreateTransactionReply() throws Throwable {
210         ActorRef actorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
211
212         doReturn(getSystem().actorSelection(actorRef.path())).when(mockActorContext).
213             actorSelection(actorRef.path().toString());
214
215         doReturn(primaryShardInfoReply(getSystem(), actorRef)).
216             when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
217
218         doReturn(Futures.successful(new Object())).when(mockActorContext).executeOperationAsync(
219             eq(getSystem().actorSelection(actorRef.path())), eqCreateTransaction(memberName, READ_ONLY));
220
221         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
222
223         propagateReadFailedExceptionCause(transactionProxy.read(TestModel.TEST_PATH));
224     }
225
226     @Test
227     public void testExists() throws Exception {
228         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
229
230         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
231
232         doReturn(dataExistsSerializedReply(false)).when(mockActorContext).executeOperationAsync(
233                 eq(actorSelection(actorRef)), eqSerializedDataExists());
234
235         Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
236
237         assertEquals("Exists response", false, exists);
238
239         doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
240                 eq(actorSelection(actorRef)), eqSerializedDataExists());
241
242         exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
243
244         assertEquals("Exists response", true, exists);
245     }
246
247     @Test(expected = PrimaryNotFoundException.class)
248     public void testExistsWhenAPrimaryNotFoundExceptionIsThrown() throws Throwable {
249         testExceptionOnInitialCreateTransaction(new PrimaryNotFoundException("test"), new Invoker() {
250             @Override
251             public CheckedFuture<?, ReadFailedException> invoke(TransactionProxy proxy) throws Exception {
252                 return proxy.exists(TestModel.TEST_PATH);
253             }
254         });
255     }
256
257     @Test(expected = ReadFailedException.class)
258     public void testExistsWithInvalidReplyMessageType() throws Exception {
259         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
260
261         doReturn(Futures.successful(new Object())).when(mockActorContext).
262                 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
263
264         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
265
266         transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
267     }
268
269     @Test(expected = TestException.class)
270     public void testExistsWithAsyncRemoteOperatonFailure() throws Throwable {
271         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
272
273         doReturn(Futures.failed(new TestException())).when(mockActorContext).
274                 executeOperationAsync(eq(actorSelection(actorRef)), eqSerializedDataExists());
275
276         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
277
278         propagateReadFailedExceptionCause(transactionProxy.exists(TestModel.TEST_PATH));
279     }
280
281     @Test
282     public void testExistsWithPriorRecordingOperationSuccessful() throws Throwable {
283         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
284
285         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
286
287         expectBatchedModifications(actorRef, 1);
288
289         doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
290                 eq(actorSelection(actorRef)), eqSerializedDataExists());
291
292         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
293
294         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
295
296         Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
297
298         assertEquals("Exists response", true, exists);
299
300         InOrder inOrder = Mockito.inOrder(mockActorContext);
301         inOrder.verify(mockActorContext).executeOperationAsync(
302                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
303
304         inOrder.verify(mockActorContext).executeOperationAsync(
305                 eq(actorSelection(actorRef)), eqSerializedDataExists());
306     }
307
308     @Test(expected=IllegalStateException.class)
309     public void testExistsPreConditionCheck() {
310         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
311         transactionProxy.exists(TestModel.TEST_PATH);
312     }
313
314     @Test
315     public void testWrite() throws Exception {
316         dataStoreContextBuilder.shardBatchedModificationCount(1);
317         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
318
319         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
320
321         expectBatchedModifications(actorRef, 1);
322
323         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
324
325         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
326
327         verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), false);
328     }
329
330     @Test
331     public void testWriteAfterAsyncRead() throws Throwable {
332         ActorRef actorRef = setupActorContextWithoutInitialCreateTransaction(getSystem(), DefaultShardStrategy.DEFAULT_SHARD);
333
334         Promise<Object> createTxPromise = akka.dispatch.Futures.promise();
335         doReturn(createTxPromise).when(mockActorContext).executeOperationAsync(
336                 eq(getSystem().actorSelection(actorRef.path())),
337                 eqCreateTransaction(memberName, READ_WRITE));
338
339         doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
340                 eq(actorSelection(actorRef)), eqSerializedReadData());
341
342         expectBatchedModificationsReady(actorRef);
343
344         final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
345
346         final TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
347
348         final CountDownLatch readComplete = new CountDownLatch(1);
349         final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
350         com.google.common.util.concurrent.Futures.addCallback(transactionProxy.read(TestModel.TEST_PATH),
351                 new  FutureCallback<Optional<NormalizedNode<?, ?>>>() {
352                     @Override
353                     public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
354                         try {
355                             transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
356                         } catch (Exception e) {
357                             caughtEx.set(e);
358                         } finally {
359                             readComplete.countDown();
360                         }
361                     }
362
363                     @Override
364                     public void onFailure(Throwable t) {
365                         caughtEx.set(t);
366                         readComplete.countDown();
367                     }
368                 });
369
370         createTxPromise.success(createTransactionReply(actorRef, DataStoreVersions.CURRENT_VERSION));
371
372         Uninterruptibles.awaitUninterruptibly(readComplete, 5, TimeUnit.SECONDS);
373
374         if(caughtEx.get() != null) {
375             throw caughtEx.get();
376         }
377
378         // This sends the batched modification.
379         transactionProxy.ready();
380
381         verifyOneBatchedModification(actorRef, new WriteModification(TestModel.TEST_PATH, nodeToWrite), true);
382     }
383
384     @Test(expected=IllegalStateException.class)
385     public void testWritePreConditionCheck() {
386         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
387         transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
388     }
389
390     @Test(expected=IllegalStateException.class)
391     public void testWriteAfterReadyPreConditionCheck() {
392         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
393
394         transactionProxy.ready();
395
396         transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
397     }
398
399     @Test
400     public void testMerge() throws Exception {
401         dataStoreContextBuilder.shardBatchedModificationCount(1);
402         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
403
404         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
405
406         expectBatchedModifications(actorRef, 1);
407
408         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
409
410         transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
411
412         verifyOneBatchedModification(actorRef, new MergeModification(TestModel.TEST_PATH, nodeToWrite), false);
413     }
414
415     @Test
416     public void testDelete() throws Exception {
417         dataStoreContextBuilder.shardBatchedModificationCount(1);
418         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
419
420         expectBatchedModifications(actorRef, 1);
421
422         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
423
424         transactionProxy.delete(TestModel.TEST_PATH);
425
426         verifyOneBatchedModification(actorRef, new DeleteModification(TestModel.TEST_PATH), false);
427     }
428
429     @Test
430     public void testReadWrite() throws Exception {
431         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
432
433         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
434
435         doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
436                 eq(actorSelection(actorRef)), eqSerializedReadData());
437
438         expectBatchedModifications(actorRef, 1);
439
440         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
441
442         transactionProxy.read(TestModel.TEST_PATH);
443
444         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
445
446         transactionProxy.read(TestModel.TEST_PATH);
447
448         transactionProxy.read(TestModel.TEST_PATH);
449
450         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
451         assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
452
453         verifyBatchedModifications(batchedModifications.get(0), false,
454                 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
455     }
456
457     @Test
458     public void testReadyWithReadWrite() throws Exception {
459         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
460
461         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
462
463         doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
464                 eq(actorSelection(actorRef)), eqSerializedReadData());
465
466         expectBatchedModificationsReady(actorRef, true);
467
468         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
469
470         transactionProxy.read(TestModel.TEST_PATH);
471
472         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
473
474         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
475
476         assertTrue(ready instanceof SingleCommitCohortProxy);
477
478         verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
479
480         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
481         assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
482
483         verifyBatchedModifications(batchedModifications.get(0), true, true,
484                 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
485
486         assertEquals("getTotalMessageCount", 1, batchedModifications.get(0).getTotalMessagesSent());
487     }
488
489     @Test
490     public void testReadyWithNoModifications() throws Exception {
491         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
492
493         doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
494                 eq(actorSelection(actorRef)), eqSerializedReadData());
495
496         expectBatchedModificationsReady(actorRef, true);
497
498         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
499
500         transactionProxy.read(TestModel.TEST_PATH);
501
502         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
503
504         assertTrue(ready instanceof SingleCommitCohortProxy);
505
506         verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
507
508         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
509         assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
510
511         verifyBatchedModifications(batchedModifications.get(0), true, true);
512     }
513
514     @Test
515     public void testReadyWithMultipleShardWrites() throws Exception {
516         ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
517
518         ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
519
520         expectBatchedModificationsReady(actorRef1);
521         expectBatchedModificationsReady(actorRef2);
522
523         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
524
525         transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
526         transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
527
528         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
529
530         assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
531
532         verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef1),
533                 actorSelection(actorRef2));
534     }
535
536     @Test
537     public void testReadyWithWriteOnlyAndLastBatchPending() throws Exception {
538         dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
539
540         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
541
542         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
543
544         expectBatchedModificationsReady(actorRef, true);
545
546         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
547
548         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
549
550         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
551
552         assertTrue(ready instanceof SingleCommitCohortProxy);
553
554         verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
555
556         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
557         assertEquals("Captured BatchedModifications count", 1, batchedModifications.size());
558
559         verifyBatchedModifications(batchedModifications.get(0), true, true,
560                 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
561
562         verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
563                 isA(ReadyTransaction.SERIALIZABLE_CLASS));
564     }
565
566     @Test
567     public void testReadyWithWriteOnlyAndLastBatchEmpty() throws Exception {
568         dataStoreContextBuilder.shardBatchedModificationCount(1).writeOnlyTransactionOptimizationsEnabled(true);
569         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
570
571         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
572
573         expectBatchedModificationsReady(actorRef, true);
574
575         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
576
577         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
578
579         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
580
581         assertTrue(ready instanceof SingleCommitCohortProxy);
582
583         verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
584
585         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
586         assertEquals("Captured BatchedModifications count", 2, batchedModifications.size());
587
588         verifyBatchedModifications(batchedModifications.get(0), false,
589                 new WriteModification(TestModel.TEST_PATH, nodeToWrite));
590
591         verifyBatchedModifications(batchedModifications.get(1), true, true);
592
593         verify(mockActorContext, never()).executeOperationAsync(eq(actorSelection(actorRef)),
594                 isA(ReadyTransaction.SERIALIZABLE_CLASS));
595     }
596
597     @Test
598     public void testReadyWithReplyFailure() throws Exception {
599         dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
600
601         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
602
603         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
604
605         expectFailedBatchedModifications(actorRef);
606
607         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
608
609         transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
610
611         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
612
613         assertTrue(ready instanceof SingleCommitCohortProxy);
614
615         verifyCohortFutures((SingleCommitCohortProxy)ready, TestException.class);
616     }
617
618     @Test
619     public void testReadyWithDebugContextEnabled() throws Exception {
620         dataStoreContextBuilder.transactionDebugContextEnabled(true);
621
622         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
623
624         expectBatchedModificationsReady(actorRef, true);
625
626         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
627
628         transactionProxy.merge(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
629
630         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
631
632         assertTrue(ready instanceof DebugThreePhaseCommitCohort);
633
634         verifyCohortFutures((DebugThreePhaseCommitCohort)ready, new CommitTransactionReply().toSerializable());
635     }
636
637     private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception {
638         doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString());
639
640         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
641
642         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
643
644         transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
645
646         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
647
648         transactionProxy.delete(TestModel.TEST_PATH);
649
650         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
651
652         assertTrue(ready instanceof SingleCommitCohortProxy);
653
654         verifyCohortFutures((SingleCommitCohortProxy)ready, toThrow.getClass());
655     }
656
657     @Test
658     public void testWriteOnlyTxWithPrimaryNotFoundException() throws Exception {
659         testWriteOnlyTxWithFindPrimaryShardFailure(new PrimaryNotFoundException("mock"));
660     }
661
662     @Test
663     public void testWriteOnlyTxWithNotInitializedException() throws Exception {
664         testWriteOnlyTxWithFindPrimaryShardFailure(new NotInitializedException("mock"));
665     }
666
667     @Test
668     public void testWriteOnlyTxWithNoShardLeaderException() throws Exception {
669         testWriteOnlyTxWithFindPrimaryShardFailure(new NoShardLeaderException("mock"));
670     }
671
672     @Test
673     public void testReadyWithInvalidReplyMessageType() throws Exception {
674         dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
675         ActorRef actorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
676
677         ActorRef actorRef2 = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY, "junk");
678
679         doReturn(Futures.successful(new Object())).when(mockActorContext).
680                 executeOperationAsync(eq(actorSelection(actorRef1)), isA(BatchedModifications.class));
681
682         expectBatchedModificationsReady(actorRef2);
683
684         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY);
685
686         transactionProxy.write(TestModel.JUNK_PATH, ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
687         transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
688
689         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
690
691         assertTrue(ready instanceof ThreePhaseCommitCohortProxy);
692
693         verifyCohortFutures((ThreePhaseCommitCohortProxy)ready, actorSelection(actorRef2),
694                 IllegalArgumentException.class);
695     }
696
697     @Test
698     public void testGetIdentifier() {
699         setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
700         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
701
702         Object id = transactionProxy.getIdentifier();
703         assertNotNull("getIdentifier returned null", id);
704         assertTrue("Invalid identifier: " + id, id.toString().startsWith(memberName));
705     }
706
707     @Test
708     public void testClose() throws Exception{
709         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
710
711         doReturn(readSerializedDataReply(null)).when(mockActorContext).executeOperationAsync(
712                 eq(actorSelection(actorRef)), eqSerializedReadData());
713
714         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
715
716         transactionProxy.read(TestModel.TEST_PATH);
717
718         transactionProxy.close();
719
720         verify(mockActorContext).sendOperationAsync(
721                 eq(actorSelection(actorRef)), isA(CloseTransaction.SERIALIZABLE_CLASS));
722     }
723
724
725     /**
726      * Method to test a local Tx actor. The Tx paths are matched to decide if the
727      * Tx actor is local or not. This is done by mocking the Tx actor path
728      * and the caller paths and ensuring that the paths have the remote-address format
729      *
730      * Note: Since the default akka provider for test is not a RemoteActorRefProvider,
731      * the paths returned for the actors for all the tests are not qualified remote paths.
732      * Hence are treated as non-local/remote actors. In short, all tests except
733      * few below run for remote actors
734      *
735      * @throws Exception
736      */
737     @Test
738     public void testLocalTxActorRead() throws Exception {
739         setupActorContextWithInitialCreateTransaction(getSystem(), READ_ONLY);
740         doReturn(true).when(mockActorContext).isPathLocal(anyString());
741
742         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
743
744         // negative test case with null as the reply
745         doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
746             any(ActorSelection.class), eqReadData());
747
748         Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
749             TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
750
751         assertEquals("NormalizedNode isPresent", false, readOptional.isPresent());
752
753         // test case with node as read data reply
754         NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
755
756         doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
757             any(ActorSelection.class), eqReadData());
758
759         readOptional = transactionProxy.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
760
761         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
762
763         assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
764
765         // test for local data exists
766         doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
767             any(ActorSelection.class), eqDataExists());
768
769         boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
770
771         assertEquals("Exists response", true, exists);
772     }
773
774     @Test
775     public void testLocalTxActorReady() throws Exception {
776         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
777         doReturn(true).when(mockActorContext).isPathLocal(anyString());
778
779         expectBatchedModificationsReady(actorRef, true);
780
781         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
782
783         NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
784         transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
785
786         DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready();
787
788         assertTrue(ready instanceof SingleCommitCohortProxy);
789
790         verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable());
791     }
792
793     private static interface TransactionProxyOperation {
794         void run(TransactionProxy transactionProxy);
795     }
796
797     private void throttleOperation(TransactionProxyOperation operation) {
798         throttleOperation(operation, 1, true);
799     }
800
801     private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound){
802         throttleOperation(operation, outstandingOpsLimit, shardFound, TimeUnit.MILLISECONDS.toNanos(
803                 mockActorContext.getDatastoreContext().getOperationTimeoutInMillis()));
804     }
805
806     private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef){
807         return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
808                 Optional.<DataTree>absent());
809     }
810
811     private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef, Optional<DataTree> dataTreeOptional){
812         return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
813                 dataTreeOptional);
814     }
815
816
817     private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound, long expectedCompletionTime){
818         ActorSystem actorSystem = getSystem();
819         ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
820
821         // Note that we setting batchedModificationCount to one less than what we need because in TransactionProxy
822         // we now allow one extra permit to be allowed for ready
823         doReturn(dataStoreContextBuilder.operationTimeoutInSeconds(2).
824                 shardBatchedModificationCount(outstandingOpsLimit-1).build()).when(mockActorContext).getDatastoreContext();
825
826         doReturn(actorSystem.actorSelection(shardActorRef.path())).
827                 when(mockActorContext).actorSelection(shardActorRef.path().toString());
828
829         if(shardFound) {
830             doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
831                     when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
832             doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
833                     when(mockActorContext).findPrimaryShardAsync(eq("cars"));
834
835         } else {
836             doReturn(Futures.failed(new Exception("not found")))
837                     .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
838         }
839
840         String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
841         CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
842                 setTransactionId("txn-1").setTransactionActorPath(actorPath).
843                 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
844
845         doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
846                 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
847                         eqCreateTransaction(memberName, READ_WRITE));
848
849         doReturn(true).when(mockActorContext).isPathLocal(actorPath);
850
851         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
852
853         long start = System.nanoTime();
854
855         operation.run(transactionProxy);
856
857         long end = System.nanoTime();
858
859         Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
860                 expectedCompletionTime, (end-start)),
861                 ((end - start) > expectedCompletionTime) && ((end - start) < expectedCompletionTime*2));
862
863     }
864
865     private void completeOperation(TransactionProxyOperation operation){
866         completeOperation(operation, true);
867     }
868
869     private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
870         ActorSystem actorSystem = getSystem();
871         ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
872
873         doReturn(actorSystem.actorSelection(shardActorRef.path())).
874                 when(mockActorContext).actorSelection(shardActorRef.path().toString());
875
876         if(shardFound) {
877             doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
878                     when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
879         } else {
880             doReturn(Futures.failed(new PrimaryNotFoundException("test")))
881                     .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
882         }
883
884         ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
885         String actorPath = txActorRef.path().toString();
886         CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
887                 setTransactionId("txn-1").setTransactionActorPath(actorPath).
888                 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
889
890         doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
891
892         doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
893                 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
894                         eqCreateTransaction(memberName, READ_WRITE));
895
896         doReturn(true).when(mockActorContext).isPathLocal(anyString());
897
898         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
899
900         long start = System.nanoTime();
901
902         operation.run(transactionProxy);
903
904         long end = System.nanoTime();
905
906         long expected = TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis());
907         Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
908                 expected, (end-start)), (end - start) <= expected);
909     }
910
911     private void completeOperationLocal(TransactionProxyOperation operation, Optional<DataTree> dataTreeOptional){
912         ActorSystem actorSystem = getSystem();
913         ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
914
915         doReturn(actorSystem.actorSelection(shardActorRef.path())).
916                 when(mockActorContext).actorSelection(shardActorRef.path().toString());
917
918         doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, dataTreeOptional))).
919                 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
920
921         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
922
923         long start = System.nanoTime();
924
925         operation.run(transactionProxy);
926
927         long end = System.nanoTime();
928
929         long expected = TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis());
930         Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
931                 expected, (end-start)), (end - start) <= expected);
932     }
933
934     private Optional<DataTree> createDataTree(){
935         DataTree dataTree = mock(DataTree.class);
936         Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
937         DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
938         DataTreeModification dataTreeModification = mock(DataTreeModification.class);
939
940         doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
941         doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
942
943         return dataTreeOptional;
944     }
945
946     private Optional<DataTree> createDataTree(NormalizedNode readResponse){
947         DataTree dataTree = mock(DataTree.class);
948         Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
949         DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
950         DataTreeModification dataTreeModification = mock(DataTreeModification.class);
951
952         doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
953         doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
954         doReturn(Optional.of(readResponse)).when(dataTreeModification).readNode(any(YangInstanceIdentifier.class));
955
956         return dataTreeOptional;
957     }
958
959
960     @Test
961     public void testWriteCompletionForLocalShard(){
962         completeOperationLocal(new TransactionProxyOperation() {
963             @Override
964             public void run(TransactionProxy transactionProxy) {
965                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
966
967                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
968
969                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
970
971             }
972         }, createDataTree());
973     }
974
975     @Test
976     public void testWriteThrottlingWhenShardFound(){
977         throttleOperation(new TransactionProxyOperation() {
978             @Override
979             public void run(TransactionProxy transactionProxy) {
980                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
981
982                 expectIncompleteBatchedModifications();
983
984                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
985
986                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
987             }
988         });
989     }
990
991     @Test
992     public void testWriteThrottlingWhenShardNotFound(){
993         // Confirm that there is no throttling when the Shard is not found
994         completeOperation(new TransactionProxyOperation() {
995             @Override
996             public void run(TransactionProxy transactionProxy) {
997                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
998
999                 expectBatchedModifications(2);
1000
1001                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1002
1003                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1004             }
1005         }, false);
1006
1007     }
1008
1009
1010     @Test
1011     public void testWriteCompletion(){
1012         completeOperation(new TransactionProxyOperation() {
1013             @Override
1014             public void run(TransactionProxy transactionProxy) {
1015                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1016
1017                 expectBatchedModifications(2);
1018
1019                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1020
1021                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1022             }
1023         });
1024     }
1025
1026     @Test
1027     public void testMergeThrottlingWhenShardFound(){
1028         throttleOperation(new TransactionProxyOperation() {
1029             @Override
1030             public void run(TransactionProxy transactionProxy) {
1031                 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1032
1033                 expectIncompleteBatchedModifications();
1034
1035                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1036
1037                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1038             }
1039         });
1040     }
1041
1042     @Test
1043     public void testMergeThrottlingWhenShardNotFound(){
1044         completeOperation(new TransactionProxyOperation() {
1045             @Override
1046             public void run(TransactionProxy transactionProxy) {
1047                 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1048
1049                 expectBatchedModifications(2);
1050
1051                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1052
1053                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1054             }
1055         }, false);
1056     }
1057
1058     @Test
1059     public void testMergeCompletion(){
1060         completeOperation(new TransactionProxyOperation() {
1061             @Override
1062             public void run(TransactionProxy transactionProxy) {
1063                 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1064
1065                 expectBatchedModifications(2);
1066
1067                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1068
1069                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1070             }
1071         });
1072
1073     }
1074
1075     @Test
1076     public void testMergeCompletionForLocalShard(){
1077         completeOperationLocal(new TransactionProxyOperation() {
1078             @Override
1079             public void run(TransactionProxy transactionProxy) {
1080                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1081
1082                 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1083
1084                 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1085
1086             }
1087         }, createDataTree());
1088     }
1089
1090
1091     @Test
1092     public void testDeleteThrottlingWhenShardFound(){
1093
1094         throttleOperation(new TransactionProxyOperation() {
1095             @Override
1096             public void run(TransactionProxy transactionProxy) {
1097                 expectIncompleteBatchedModifications();
1098
1099                 transactionProxy.delete(TestModel.TEST_PATH);
1100
1101                 transactionProxy.delete(TestModel.TEST_PATH);
1102             }
1103         });
1104     }
1105
1106
1107     @Test
1108     public void testDeleteThrottlingWhenShardNotFound(){
1109
1110         completeOperation(new TransactionProxyOperation() {
1111             @Override
1112             public void run(TransactionProxy transactionProxy) {
1113                 expectBatchedModifications(2);
1114
1115                 transactionProxy.delete(TestModel.TEST_PATH);
1116
1117                 transactionProxy.delete(TestModel.TEST_PATH);
1118             }
1119         }, false);
1120     }
1121
1122     @Test
1123     public void testDeleteCompletionForLocalShard(){
1124         completeOperationLocal(new TransactionProxyOperation() {
1125             @Override
1126             public void run(TransactionProxy transactionProxy) {
1127
1128                 transactionProxy.delete(TestModel.TEST_PATH);
1129
1130                 transactionProxy.delete(TestModel.TEST_PATH);
1131             }
1132         }, createDataTree());
1133
1134     }
1135
1136     @Test
1137     public void testDeleteCompletion(){
1138         completeOperation(new TransactionProxyOperation() {
1139             @Override
1140             public void run(TransactionProxy transactionProxy) {
1141                 expectBatchedModifications(2);
1142
1143                 transactionProxy.delete(TestModel.TEST_PATH);
1144
1145                 transactionProxy.delete(TestModel.TEST_PATH);
1146             }
1147         });
1148
1149     }
1150
1151     @Test
1152     public void testReadThrottlingWhenShardFound(){
1153
1154         throttleOperation(new TransactionProxyOperation() {
1155             @Override
1156             public void run(TransactionProxy transactionProxy) {
1157                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1158                         any(ActorSelection.class), eqReadData());
1159
1160                 transactionProxy.read(TestModel.TEST_PATH);
1161
1162                 transactionProxy.read(TestModel.TEST_PATH);
1163             }
1164         });
1165     }
1166
1167     @Test
1168     public void testReadThrottlingWhenShardNotFound(){
1169
1170         completeOperation(new TransactionProxyOperation() {
1171             @Override
1172             public void run(TransactionProxy transactionProxy) {
1173                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1174                         any(ActorSelection.class), eqReadData());
1175
1176                 transactionProxy.read(TestModel.TEST_PATH);
1177
1178                 transactionProxy.read(TestModel.TEST_PATH);
1179             }
1180         }, false);
1181     }
1182
1183
1184     @Test
1185     public void testReadCompletion(){
1186         completeOperation(new TransactionProxyOperation() {
1187             @Override
1188             public void run(TransactionProxy transactionProxy) {
1189                 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1190
1191                 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1192                         any(ActorSelection.class), eqReadData());
1193
1194                 transactionProxy.read(TestModel.TEST_PATH);
1195
1196                 transactionProxy.read(TestModel.TEST_PATH);
1197             }
1198         });
1199
1200     }
1201
1202     @Test
1203     public void testReadCompletionForLocalShard(){
1204         final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1205         completeOperationLocal(new TransactionProxyOperation() {
1206             @Override
1207             public void run(TransactionProxy transactionProxy) {
1208                 transactionProxy.read(TestModel.TEST_PATH);
1209
1210                 transactionProxy.read(TestModel.TEST_PATH);
1211             }
1212         }, createDataTree(nodeToRead));
1213
1214     }
1215
1216     @Test
1217     public void testReadCompletionForLocalShardWhenExceptionOccurs(){
1218         completeOperationLocal(new TransactionProxyOperation() {
1219             @Override
1220             public void run(TransactionProxy transactionProxy) {
1221                 transactionProxy.read(TestModel.TEST_PATH);
1222
1223                 transactionProxy.read(TestModel.TEST_PATH);
1224             }
1225         }, createDataTree());
1226
1227     }
1228
1229     @Test
1230     public void testExistsThrottlingWhenShardFound(){
1231
1232         throttleOperation(new TransactionProxyOperation() {
1233             @Override
1234             public void run(TransactionProxy transactionProxy) {
1235                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1236                         any(ActorSelection.class), eqDataExists());
1237
1238                 transactionProxy.exists(TestModel.TEST_PATH);
1239
1240                 transactionProxy.exists(TestModel.TEST_PATH);
1241             }
1242         });
1243     }
1244
1245     @Test
1246     public void testExistsThrottlingWhenShardNotFound(){
1247
1248         completeOperation(new TransactionProxyOperation() {
1249             @Override
1250             public void run(TransactionProxy transactionProxy) {
1251                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1252                         any(ActorSelection.class), eqDataExists());
1253
1254                 transactionProxy.exists(TestModel.TEST_PATH);
1255
1256                 transactionProxy.exists(TestModel.TEST_PATH);
1257             }
1258         }, false);
1259     }
1260
1261
1262     @Test
1263     public void testExistsCompletion(){
1264         completeOperation(new TransactionProxyOperation() {
1265             @Override
1266             public void run(TransactionProxy transactionProxy) {
1267                 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1268                         any(ActorSelection.class), eqDataExists());
1269
1270                 transactionProxy.exists(TestModel.TEST_PATH);
1271
1272                 transactionProxy.exists(TestModel.TEST_PATH);
1273             }
1274         });
1275
1276     }
1277
1278     @Test
1279     public void testExistsCompletionForLocalShard(){
1280         final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1281         completeOperationLocal(new TransactionProxyOperation() {
1282             @Override
1283             public void run(TransactionProxy transactionProxy) {
1284                 transactionProxy.exists(TestModel.TEST_PATH);
1285
1286                 transactionProxy.exists(TestModel.TEST_PATH);
1287             }
1288         }, createDataTree(nodeToRead));
1289
1290     }
1291
1292     @Test
1293     public void testExistsCompletionForLocalShardWhenExceptionOccurs(){
1294         completeOperationLocal(new TransactionProxyOperation() {
1295             @Override
1296             public void run(TransactionProxy transactionProxy) {
1297                 transactionProxy.exists(TestModel.TEST_PATH);
1298
1299                 transactionProxy.exists(TestModel.TEST_PATH);
1300             }
1301         }, createDataTree());
1302
1303     }
1304     @Test
1305     public void testReadyThrottling(){
1306
1307         throttleOperation(new TransactionProxyOperation() {
1308             @Override
1309             public void run(TransactionProxy transactionProxy) {
1310                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1311
1312                 expectBatchedModifications(1);
1313
1314                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1315                         any(ActorSelection.class), any(ReadyTransaction.class));
1316
1317                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1318
1319                 transactionProxy.ready();
1320             }
1321         });
1322     }
1323
1324     @Test
1325     public void testReadyThrottlingWithTwoTransactionContexts(){
1326         throttleOperation(new TransactionProxyOperation() {
1327             @Override
1328             public void run(TransactionProxy transactionProxy) {
1329                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1330                 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1331
1332                 expectBatchedModifications(2);
1333
1334                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1335                         any(ActorSelection.class), any(ReadyTransaction.class));
1336
1337                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1338
1339                 // Trying to write to Cars will cause another transaction context to get created
1340                 transactionProxy.write(CarsModel.BASE_PATH, carsNode);
1341
1342                 // Now ready should block for both transaction contexts
1343                 transactionProxy.ready();
1344             }
1345         }, 1, true, TimeUnit.MILLISECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInMillis()) * 2);
1346     }
1347
1348     private void testModificationOperationBatching(TransactionType type) throws Exception {
1349         int shardBatchedModificationCount = 3;
1350         dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1351
1352         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1353
1354         expectBatchedModifications(actorRef, shardBatchedModificationCount);
1355
1356         YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1357         NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1358
1359         YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1360         NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1361
1362         YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1363         NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1364
1365         YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1366         NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1367
1368         YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1369         NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1370
1371         YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1372         NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1373
1374         YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1375         YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1376
1377         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, type);
1378
1379         transactionProxy.write(writePath1, writeNode1);
1380         transactionProxy.write(writePath2, writeNode2);
1381         transactionProxy.delete(deletePath1);
1382         transactionProxy.merge(mergePath1, mergeNode1);
1383         transactionProxy.merge(mergePath2, mergeNode2);
1384         transactionProxy.write(writePath3, writeNode3);
1385         transactionProxy.merge(mergePath3, mergeNode3);
1386         transactionProxy.delete(deletePath2);
1387
1388         // This sends the last batch.
1389         transactionProxy.ready();
1390
1391         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1392         assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1393
1394         verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1395                 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1396
1397         verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1398                 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1399
1400         verifyBatchedModifications(batchedModifications.get(2), true, true,
1401                 new MergeModification(mergePath3, mergeNode3), new DeleteModification(deletePath2));
1402
1403         assertEquals("getTotalMessageCount", 3, batchedModifications.get(2).getTotalMessagesSent());
1404     }
1405
1406     @Test
1407     public void testReadWriteModificationOperationBatching() throws Throwable {
1408         testModificationOperationBatching(READ_WRITE);
1409     }
1410
1411     @Test
1412     public void testWriteOnlyModificationOperationBatching() throws Throwable {
1413         testModificationOperationBatching(WRITE_ONLY);
1414     }
1415
1416     @Test
1417     public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1418         dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1419         testModificationOperationBatching(WRITE_ONLY);
1420     }
1421
1422     @Test
1423     public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1424
1425         int shardBatchedModificationCount = 10;
1426         dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1427
1428         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1429
1430         expectBatchedModifications(actorRef, shardBatchedModificationCount);
1431
1432         YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1433         NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1434
1435         YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1436         NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1437
1438         YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1439         NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1440
1441         YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1442         NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1443
1444         YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1445
1446         doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1447                 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1448
1449         doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1450                 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1451
1452         doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1453                 eq(actorSelection(actorRef)), eqSerializedDataExists());
1454
1455         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
1456
1457         transactionProxy.write(writePath1, writeNode1);
1458         transactionProxy.write(writePath2, writeNode2);
1459
1460         Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1461                 get(5, TimeUnit.SECONDS);
1462
1463         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1464         assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1465
1466         transactionProxy.merge(mergePath1, mergeNode1);
1467         transactionProxy.merge(mergePath2, mergeNode2);
1468
1469         readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1470
1471         transactionProxy.delete(deletePath);
1472
1473         Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1474         assertEquals("Exists response", true, exists);
1475
1476         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1477         assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1478
1479         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1480         assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1481
1482         verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1483                 new WriteModification(writePath2, writeNode2));
1484
1485         verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1486                 new MergeModification(mergePath2, mergeNode2));
1487
1488         verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1489
1490         InOrder inOrder = Mockito.inOrder(mockActorContext);
1491         inOrder.verify(mockActorContext).executeOperationAsync(
1492                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1493
1494         inOrder.verify(mockActorContext).executeOperationAsync(
1495                 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1496
1497         inOrder.verify(mockActorContext).executeOperationAsync(
1498                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1499
1500         inOrder.verify(mockActorContext).executeOperationAsync(
1501                 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1502
1503         inOrder.verify(mockActorContext).executeOperationAsync(
1504                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1505
1506         inOrder.verify(mockActorContext).executeOperationAsync(
1507                 eq(actorSelection(actorRef)), eqSerializedDataExists());
1508     }
1509
1510     @Test
1511     public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1512
1513         SchemaContext schemaContext = SchemaContextHelper.full();
1514         Configuration configuration = mock(Configuration.class);
1515         doReturn(configuration).when(mockActorContext).getConfiguration();
1516         doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1517         doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1518
1519         NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1520         NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1521
1522         setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1523         setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1524
1525         doReturn(memberName).when(mockActorContext).getCurrentMemberName();
1526
1527         doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1528
1529         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
1530
1531         Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1532                 YangInstanceIdentifier.builder().build()).get(5, TimeUnit.SECONDS);
1533
1534         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1535
1536         NormalizedNode<?, ?> normalizedNode = readOptional.get();
1537
1538         assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1539
1540         Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1541
1542         for(NormalizedNode<?,?> node : collection){
1543             assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1544         }
1545
1546         assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1547                 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1548
1549         assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1550
1551         assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1552                 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1553
1554         assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1555     }
1556
1557
1558     private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1559         ActorSystem actorSystem = getSystem();
1560         ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1561
1562         doReturn(getSystem().actorSelection(shardActorRef.path())).
1563                 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1564
1565         doReturn(primaryShardInfoReply(getSystem(), shardActorRef)).
1566                 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1567
1568         doReturn(true).when(mockActorContext).isPathLocal(shardActorRef.path().toString());
1569
1570         ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1571
1572         doReturn(actorSystem.actorSelection(txActorRef.path())).
1573                 when(mockActorContext).actorSelection(txActorRef.path().toString());
1574
1575         doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1576                 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1577                         eqCreateTransaction(memberName, TransactionType.READ_ONLY));
1578
1579         doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1580                 eq(actorSelection(txActorRef)), eqSerializedReadData(YangInstanceIdentifier.builder().build()));
1581     }
1582 }