BUG 3019 : Fix Operation throttling for modification batching scenarios
[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.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds()));
803     }
804
805     private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef){
806         return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
807                 Optional.<DataTree>absent());
808     }
809
810     private PrimaryShardInfo newPrimaryShardInfo(ActorRef actorRef, Optional<DataTree> dataTreeOptional){
811         return new PrimaryShardInfo(getSystem().actorSelection(actorRef.path()), DataStoreVersions.CURRENT_VERSION,
812                 dataTreeOptional);
813     }
814
815
816     private void throttleOperation(TransactionProxyOperation operation, int outstandingOpsLimit, boolean shardFound, long expectedCompletionTime){
817         ActorSystem actorSystem = getSystem();
818         ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
819
820         // Note that we setting batchedModificationCount to one less than what we need because in TransactionProxy
821         // we now allow one extra permit to be allowed for ready
822         doReturn(dataStoreContextBuilder.operationTimeoutInSeconds(2).
823                 shardBatchedModificationCount(outstandingOpsLimit-1).build()).when(mockActorContext).getDatastoreContext();
824
825         doReturn(actorSystem.actorSelection(shardActorRef.path())).
826                 when(mockActorContext).actorSelection(shardActorRef.path().toString());
827
828         if(shardFound) {
829             doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
830                     when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
831             doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
832                     when(mockActorContext).findPrimaryShardAsync(eq("cars"));
833
834         } else {
835             doReturn(Futures.failed(new Exception("not found")))
836                     .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
837         }
838
839         String actorPath = "akka.tcp://system@127.0.0.1:2550/user/tx-actor";
840         CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
841                 setTransactionId("txn-1").setTransactionActorPath(actorPath).
842                 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
843
844         doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
845                 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
846                         eqCreateTransaction(memberName, READ_WRITE));
847
848         doReturn(true).when(mockActorContext).isPathLocal(actorPath);
849
850         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
851
852         long start = System.nanoTime();
853
854         operation.run(transactionProxy);
855
856         long end = System.nanoTime();
857
858         Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
859                 expectedCompletionTime, (end-start)),
860                 ((end - start) > expectedCompletionTime) && ((end - start) < expectedCompletionTime*2));
861
862     }
863
864     private void completeOperation(TransactionProxyOperation operation){
865         completeOperation(operation, true);
866     }
867
868     private void completeOperation(TransactionProxyOperation operation, boolean shardFound){
869         ActorSystem actorSystem = getSystem();
870         ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
871
872         doReturn(actorSystem.actorSelection(shardActorRef.path())).
873                 when(mockActorContext).actorSelection(shardActorRef.path().toString());
874
875         if(shardFound) {
876             doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef))).
877                     when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
878         } else {
879             doReturn(Futures.failed(new PrimaryNotFoundException("test")))
880                     .when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
881         }
882
883         ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
884         String actorPath = txActorRef.path().toString();
885         CreateTransactionReply createTransactionReply = CreateTransactionReply.newBuilder().
886                 setTransactionId("txn-1").setTransactionActorPath(actorPath).
887                 setMessageVersion(DataStoreVersions.CURRENT_VERSION).build();
888
889         doReturn(actorSystem.actorSelection(actorPath)).when(mockActorContext).actorSelection(actorPath);
890
891         doReturn(Futures.successful(createTransactionReply)).when(mockActorContext).
892                 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
893                         eqCreateTransaction(memberName, READ_WRITE));
894
895         doReturn(true).when(mockActorContext).isPathLocal(anyString());
896
897         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
898
899         long start = System.nanoTime();
900
901         operation.run(transactionProxy);
902
903         long end = System.nanoTime();
904
905         long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
906         Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
907                 expected, (end-start)), (end - start) <= expected);
908     }
909
910     private void completeOperationLocal(TransactionProxyOperation operation, Optional<DataTree> dataTreeOptional){
911         ActorSystem actorSystem = getSystem();
912         ActorRef shardActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
913
914         doReturn(actorSystem.actorSelection(shardActorRef.path())).
915                 when(mockActorContext).actorSelection(shardActorRef.path().toString());
916
917         doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, dataTreeOptional))).
918                 when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD));
919
920         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
921
922         long start = System.nanoTime();
923
924         operation.run(transactionProxy);
925
926         long end = System.nanoTime();
927
928         long expected = TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds());
929         Assert.assertTrue(String.format("Expected elapsed time: %s. Actual: %s",
930                 expected, (end-start)), (end - start) <= expected);
931     }
932
933     private Optional<DataTree> createDataTree(){
934         DataTree dataTree = mock(DataTree.class);
935         Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
936         DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
937         DataTreeModification dataTreeModification = mock(DataTreeModification.class);
938
939         doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
940         doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
941
942         return dataTreeOptional;
943     }
944
945     private Optional<DataTree> createDataTree(NormalizedNode readResponse){
946         DataTree dataTree = mock(DataTree.class);
947         Optional<DataTree> dataTreeOptional = Optional.of(dataTree);
948         DataTreeSnapshot dataTreeSnapshot = mock(DataTreeSnapshot.class);
949         DataTreeModification dataTreeModification = mock(DataTreeModification.class);
950
951         doReturn(dataTreeSnapshot).when(dataTree).takeSnapshot();
952         doReturn(dataTreeModification).when(dataTreeSnapshot).newModification();
953         doReturn(Optional.of(readResponse)).when(dataTreeModification).readNode(any(YangInstanceIdentifier.class));
954
955         return dataTreeOptional;
956     }
957
958
959     @Test
960     public void testWriteCompletionForLocalShard(){
961         completeOperationLocal(new TransactionProxyOperation() {
962             @Override
963             public void run(TransactionProxy transactionProxy) {
964                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
965
966                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
967
968                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
969
970             }
971         }, createDataTree());
972     }
973
974     @Test
975     public void testWriteThrottlingWhenShardFound(){
976         throttleOperation(new TransactionProxyOperation() {
977             @Override
978             public void run(TransactionProxy transactionProxy) {
979                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
980
981                 expectIncompleteBatchedModifications();
982
983                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
984
985                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
986             }
987         });
988     }
989
990     @Test
991     public void testWriteThrottlingWhenShardNotFound(){
992         // Confirm that there is no throttling when the Shard is not found
993         completeOperation(new TransactionProxyOperation() {
994             @Override
995             public void run(TransactionProxy transactionProxy) {
996                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
997
998                 expectBatchedModifications(2);
999
1000                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1001
1002                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1003             }
1004         }, false);
1005
1006     }
1007
1008
1009     @Test
1010     public void testWriteCompletion(){
1011         completeOperation(new TransactionProxyOperation() {
1012             @Override
1013             public void run(TransactionProxy transactionProxy) {
1014                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1015
1016                 expectBatchedModifications(2);
1017
1018                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1019
1020                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1021             }
1022         });
1023     }
1024
1025     @Test
1026     public void testMergeThrottlingWhenShardFound(){
1027         throttleOperation(new TransactionProxyOperation() {
1028             @Override
1029             public void run(TransactionProxy transactionProxy) {
1030                 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1031
1032                 expectIncompleteBatchedModifications();
1033
1034                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1035
1036                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1037             }
1038         });
1039     }
1040
1041     @Test
1042     public void testMergeThrottlingWhenShardNotFound(){
1043         completeOperation(new TransactionProxyOperation() {
1044             @Override
1045             public void run(TransactionProxy transactionProxy) {
1046                 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1047
1048                 expectBatchedModifications(2);
1049
1050                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1051
1052                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1053             }
1054         }, false);
1055     }
1056
1057     @Test
1058     public void testMergeCompletion(){
1059         completeOperation(new TransactionProxyOperation() {
1060             @Override
1061             public void run(TransactionProxy transactionProxy) {
1062                 NormalizedNode<?, ?> nodeToMerge = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1063
1064                 expectBatchedModifications(2);
1065
1066                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1067
1068                 transactionProxy.merge(TestModel.TEST_PATH, nodeToMerge);
1069             }
1070         });
1071
1072     }
1073
1074     @Test
1075     public void testMergeCompletionForLocalShard(){
1076         completeOperationLocal(new TransactionProxyOperation() {
1077             @Override
1078             public void run(TransactionProxy transactionProxy) {
1079                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1080
1081                 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1082
1083                 transactionProxy.merge(TestModel.TEST_PATH, nodeToWrite);
1084
1085             }
1086         }, createDataTree());
1087     }
1088
1089
1090     @Test
1091     public void testDeleteThrottlingWhenShardFound(){
1092
1093         throttleOperation(new TransactionProxyOperation() {
1094             @Override
1095             public void run(TransactionProxy transactionProxy) {
1096                 expectIncompleteBatchedModifications();
1097
1098                 transactionProxy.delete(TestModel.TEST_PATH);
1099
1100                 transactionProxy.delete(TestModel.TEST_PATH);
1101             }
1102         });
1103     }
1104
1105
1106     @Test
1107     public void testDeleteThrottlingWhenShardNotFound(){
1108
1109         completeOperation(new TransactionProxyOperation() {
1110             @Override
1111             public void run(TransactionProxy transactionProxy) {
1112                 expectBatchedModifications(2);
1113
1114                 transactionProxy.delete(TestModel.TEST_PATH);
1115
1116                 transactionProxy.delete(TestModel.TEST_PATH);
1117             }
1118         }, false);
1119     }
1120
1121     @Test
1122     public void testDeleteCompletionForLocalShard(){
1123         completeOperationLocal(new TransactionProxyOperation() {
1124             @Override
1125             public void run(TransactionProxy transactionProxy) {
1126
1127                 transactionProxy.delete(TestModel.TEST_PATH);
1128
1129                 transactionProxy.delete(TestModel.TEST_PATH);
1130             }
1131         }, createDataTree());
1132
1133     }
1134
1135     @Test
1136     public void testDeleteCompletion(){
1137         completeOperation(new TransactionProxyOperation() {
1138             @Override
1139             public void run(TransactionProxy transactionProxy) {
1140                 expectBatchedModifications(2);
1141
1142                 transactionProxy.delete(TestModel.TEST_PATH);
1143
1144                 transactionProxy.delete(TestModel.TEST_PATH);
1145             }
1146         });
1147
1148     }
1149
1150     @Test
1151     public void testReadThrottlingWhenShardFound(){
1152
1153         throttleOperation(new TransactionProxyOperation() {
1154             @Override
1155             public void run(TransactionProxy transactionProxy) {
1156                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1157                         any(ActorSelection.class), eqReadData());
1158
1159                 transactionProxy.read(TestModel.TEST_PATH);
1160
1161                 transactionProxy.read(TestModel.TEST_PATH);
1162             }
1163         });
1164     }
1165
1166     @Test
1167     public void testReadThrottlingWhenShardNotFound(){
1168
1169         completeOperation(new TransactionProxyOperation() {
1170             @Override
1171             public void run(TransactionProxy transactionProxy) {
1172                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1173                         any(ActorSelection.class), eqReadData());
1174
1175                 transactionProxy.read(TestModel.TEST_PATH);
1176
1177                 transactionProxy.read(TestModel.TEST_PATH);
1178             }
1179         }, false);
1180     }
1181
1182
1183     @Test
1184     public void testReadCompletion(){
1185         completeOperation(new TransactionProxyOperation() {
1186             @Override
1187             public void run(TransactionProxy transactionProxy) {
1188                 NormalizedNode<?, ?> nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1189
1190                 doReturn(readDataReply(nodeToRead)).when(mockActorContext).executeOperationAsync(
1191                         any(ActorSelection.class), eqReadData());
1192
1193                 transactionProxy.read(TestModel.TEST_PATH);
1194
1195                 transactionProxy.read(TestModel.TEST_PATH);
1196             }
1197         });
1198
1199     }
1200
1201     @Test
1202     public void testReadCompletionForLocalShard(){
1203         final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1204         completeOperationLocal(new TransactionProxyOperation() {
1205             @Override
1206             public void run(TransactionProxy transactionProxy) {
1207                 transactionProxy.read(TestModel.TEST_PATH);
1208
1209                 transactionProxy.read(TestModel.TEST_PATH);
1210             }
1211         }, createDataTree(nodeToRead));
1212
1213     }
1214
1215     @Test
1216     public void testReadCompletionForLocalShardWhenExceptionOccurs(){
1217         completeOperationLocal(new TransactionProxyOperation() {
1218             @Override
1219             public void run(TransactionProxy transactionProxy) {
1220                 transactionProxy.read(TestModel.TEST_PATH);
1221
1222                 transactionProxy.read(TestModel.TEST_PATH);
1223             }
1224         }, createDataTree());
1225
1226     }
1227
1228     @Test
1229     public void testExistsThrottlingWhenShardFound(){
1230
1231         throttleOperation(new TransactionProxyOperation() {
1232             @Override
1233             public void run(TransactionProxy transactionProxy) {
1234                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1235                         any(ActorSelection.class), eqDataExists());
1236
1237                 transactionProxy.exists(TestModel.TEST_PATH);
1238
1239                 transactionProxy.exists(TestModel.TEST_PATH);
1240             }
1241         });
1242     }
1243
1244     @Test
1245     public void testExistsThrottlingWhenShardNotFound(){
1246
1247         completeOperation(new TransactionProxyOperation() {
1248             @Override
1249             public void run(TransactionProxy transactionProxy) {
1250                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1251                         any(ActorSelection.class), eqDataExists());
1252
1253                 transactionProxy.exists(TestModel.TEST_PATH);
1254
1255                 transactionProxy.exists(TestModel.TEST_PATH);
1256             }
1257         }, false);
1258     }
1259
1260
1261     @Test
1262     public void testExistsCompletion(){
1263         completeOperation(new TransactionProxyOperation() {
1264             @Override
1265             public void run(TransactionProxy transactionProxy) {
1266                 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
1267                         any(ActorSelection.class), eqDataExists());
1268
1269                 transactionProxy.exists(TestModel.TEST_PATH);
1270
1271                 transactionProxy.exists(TestModel.TEST_PATH);
1272             }
1273         });
1274
1275     }
1276
1277     @Test
1278     public void testExistsCompletionForLocalShard(){
1279         final NormalizedNode nodeToRead = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1280         completeOperationLocal(new TransactionProxyOperation() {
1281             @Override
1282             public void run(TransactionProxy transactionProxy) {
1283                 transactionProxy.exists(TestModel.TEST_PATH);
1284
1285                 transactionProxy.exists(TestModel.TEST_PATH);
1286             }
1287         }, createDataTree(nodeToRead));
1288
1289     }
1290
1291     @Test
1292     public void testExistsCompletionForLocalShardWhenExceptionOccurs(){
1293         completeOperationLocal(new TransactionProxyOperation() {
1294             @Override
1295             public void run(TransactionProxy transactionProxy) {
1296                 transactionProxy.exists(TestModel.TEST_PATH);
1297
1298                 transactionProxy.exists(TestModel.TEST_PATH);
1299             }
1300         }, createDataTree());
1301
1302     }
1303     @Test
1304     public void testReadyThrottling(){
1305
1306         throttleOperation(new TransactionProxyOperation() {
1307             @Override
1308             public void run(TransactionProxy transactionProxy) {
1309                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1310
1311                 expectBatchedModifications(1);
1312
1313                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1314                         any(ActorSelection.class), any(ReadyTransaction.class));
1315
1316                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1317
1318                 transactionProxy.ready();
1319             }
1320         });
1321     }
1322
1323     @Test
1324     public void testReadyThrottlingWithTwoTransactionContexts(){
1325         throttleOperation(new TransactionProxyOperation() {
1326             @Override
1327             public void run(TransactionProxy transactionProxy) {
1328                 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1329                 NormalizedNode<?, ?> carsNode = ImmutableNodes.containerNode(CarsModel.BASE_QNAME);
1330
1331                 expectBatchedModifications(2);
1332
1333                 doReturn(incompleteFuture()).when(mockActorContext).executeOperationAsync(
1334                         any(ActorSelection.class), any(ReadyTransaction.class));
1335
1336                 transactionProxy.write(TestModel.TEST_PATH, nodeToWrite);
1337
1338                 // Trying to write to Cars will cause another transaction context to get created
1339                 transactionProxy.write(CarsModel.BASE_PATH, carsNode);
1340
1341                 // Now ready should block for both transaction contexts
1342                 transactionProxy.ready();
1343             }
1344         }, 1, true, TimeUnit.SECONDS.toNanos(mockActorContext.getDatastoreContext().getOperationTimeoutInSeconds()) * 2);
1345     }
1346
1347     private void testModificationOperationBatching(TransactionType type) throws Exception {
1348         int shardBatchedModificationCount = 3;
1349         dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1350
1351         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), type);
1352
1353         expectBatchedModifications(actorRef, shardBatchedModificationCount);
1354
1355         YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1356         NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1357
1358         YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1359         NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1360
1361         YangInstanceIdentifier writePath3 = TestModel.INNER_LIST_PATH;
1362         NormalizedNode<?, ?> writeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1363
1364         YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1365         NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1366
1367         YangInstanceIdentifier mergePath2 = TestModel.OUTER_LIST_PATH;
1368         NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1369
1370         YangInstanceIdentifier mergePath3 = TestModel.INNER_LIST_PATH;
1371         NormalizedNode<?, ?> mergeNode3 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1372
1373         YangInstanceIdentifier deletePath1 = TestModel.TEST_PATH;
1374         YangInstanceIdentifier deletePath2 = TestModel.OUTER_LIST_PATH;
1375
1376         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, type);
1377
1378         transactionProxy.write(writePath1, writeNode1);
1379         transactionProxy.write(writePath2, writeNode2);
1380         transactionProxy.delete(deletePath1);
1381         transactionProxy.merge(mergePath1, mergeNode1);
1382         transactionProxy.merge(mergePath2, mergeNode2);
1383         transactionProxy.write(writePath3, writeNode3);
1384         transactionProxy.merge(mergePath3, mergeNode3);
1385         transactionProxy.delete(deletePath2);
1386
1387         // This sends the last batch.
1388         transactionProxy.ready();
1389
1390         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1391         assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1392
1393         verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1394                 new WriteModification(writePath2, writeNode2), new DeleteModification(deletePath1));
1395
1396         verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1397                 new MergeModification(mergePath2, mergeNode2), new WriteModification(writePath3, writeNode3));
1398
1399         verifyBatchedModifications(batchedModifications.get(2), true, true,
1400                 new MergeModification(mergePath3, mergeNode3), new DeleteModification(deletePath2));
1401
1402         assertEquals("getTotalMessageCount", 3, batchedModifications.get(2).getTotalMessagesSent());
1403     }
1404
1405     @Test
1406     public void testReadWriteModificationOperationBatching() throws Throwable {
1407         testModificationOperationBatching(READ_WRITE);
1408     }
1409
1410     @Test
1411     public void testWriteOnlyModificationOperationBatching() throws Throwable {
1412         testModificationOperationBatching(WRITE_ONLY);
1413     }
1414
1415     @Test
1416     public void testOptimizedWriteOnlyModificationOperationBatching() throws Throwable {
1417         dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
1418         testModificationOperationBatching(WRITE_ONLY);
1419     }
1420
1421     @Test
1422     public void testModificationOperationBatchingWithInterleavedReads() throws Throwable {
1423
1424         int shardBatchedModificationCount = 10;
1425         dataStoreContextBuilder.shardBatchedModificationCount(shardBatchedModificationCount);
1426
1427         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
1428
1429         expectBatchedModifications(actorRef, shardBatchedModificationCount);
1430
1431         YangInstanceIdentifier writePath1 = TestModel.TEST_PATH;
1432         NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1433
1434         YangInstanceIdentifier writePath2 = TestModel.OUTER_LIST_PATH;
1435         NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
1436
1437         YangInstanceIdentifier mergePath1 = TestModel.TEST_PATH;
1438         NormalizedNode<?, ?> mergeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1439
1440         YangInstanceIdentifier mergePath2 = TestModel.INNER_LIST_PATH;
1441         NormalizedNode<?, ?> mergeNode2 = ImmutableNodes.containerNode(TestModel.INNER_LIST_QNAME);
1442
1443         YangInstanceIdentifier deletePath = TestModel.OUTER_LIST_PATH;
1444
1445         doReturn(readSerializedDataReply(writeNode2)).when(mockActorContext).executeOperationAsync(
1446                 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1447
1448         doReturn(readSerializedDataReply(mergeNode2)).when(mockActorContext).executeOperationAsync(
1449                 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1450
1451         doReturn(dataExistsSerializedReply(true)).when(mockActorContext).executeOperationAsync(
1452                 eq(actorSelection(actorRef)), eqSerializedDataExists());
1453
1454         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
1455
1456         transactionProxy.write(writePath1, writeNode1);
1457         transactionProxy.write(writePath2, writeNode2);
1458
1459         Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(writePath2).
1460                 get(5, TimeUnit.SECONDS);
1461
1462         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1463         assertEquals("Response NormalizedNode", writeNode2, readOptional.get());
1464
1465         transactionProxy.merge(mergePath1, mergeNode1);
1466         transactionProxy.merge(mergePath2, mergeNode2);
1467
1468         readOptional = transactionProxy.read(mergePath2).get(5, TimeUnit.SECONDS);
1469
1470         transactionProxy.delete(deletePath);
1471
1472         Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet();
1473         assertEquals("Exists response", true, exists);
1474
1475         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1476         assertEquals("Response NormalizedNode", mergeNode2, readOptional.get());
1477
1478         List<BatchedModifications> batchedModifications = captureBatchedModifications(actorRef);
1479         assertEquals("Captured BatchedModifications count", 3, batchedModifications.size());
1480
1481         verifyBatchedModifications(batchedModifications.get(0), false, new WriteModification(writePath1, writeNode1),
1482                 new WriteModification(writePath2, writeNode2));
1483
1484         verifyBatchedModifications(batchedModifications.get(1), false, new MergeModification(mergePath1, mergeNode1),
1485                 new MergeModification(mergePath2, mergeNode2));
1486
1487         verifyBatchedModifications(batchedModifications.get(2), false, new DeleteModification(deletePath));
1488
1489         InOrder inOrder = Mockito.inOrder(mockActorContext);
1490         inOrder.verify(mockActorContext).executeOperationAsync(
1491                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1492
1493         inOrder.verify(mockActorContext).executeOperationAsync(
1494                 eq(actorSelection(actorRef)), eqSerializedReadData(writePath2));
1495
1496         inOrder.verify(mockActorContext).executeOperationAsync(
1497                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1498
1499         inOrder.verify(mockActorContext).executeOperationAsync(
1500                 eq(actorSelection(actorRef)), eqSerializedReadData(mergePath2));
1501
1502         inOrder.verify(mockActorContext).executeOperationAsync(
1503                 eq(actorSelection(actorRef)), isA(BatchedModifications.class));
1504
1505         inOrder.verify(mockActorContext).executeOperationAsync(
1506                 eq(actorSelection(actorRef)), eqSerializedDataExists());
1507     }
1508
1509     @Test
1510     public void testReadRoot() throws ReadFailedException, InterruptedException, ExecutionException, java.util.concurrent.TimeoutException {
1511
1512         SchemaContext schemaContext = SchemaContextHelper.full();
1513         Configuration configuration = mock(Configuration.class);
1514         doReturn(configuration).when(mockActorContext).getConfiguration();
1515         doReturn(schemaContext).when(mockActorContext).getSchemaContext();
1516         doReturn(Sets.newHashSet("test", "cars")).when(configuration).getAllShardNames();
1517
1518         NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
1519         NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
1520
1521         setUpReadData("test", NormalizedNodeAggregatorTest.getRootNode(expectedNode1, schemaContext));
1522         setUpReadData("cars", NormalizedNodeAggregatorTest.getRootNode(expectedNode2, schemaContext));
1523
1524         doReturn(memberName).when(mockActorContext).getCurrentMemberName();
1525
1526         doReturn(getSystem().dispatchers().defaultGlobalDispatcher()).when(mockActorContext).getClientDispatcher();
1527
1528         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_ONLY);
1529
1530         Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
1531                 YangInstanceIdentifier.builder().build()).get(5, TimeUnit.SECONDS);
1532
1533         assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
1534
1535         NormalizedNode<?, ?> normalizedNode = readOptional.get();
1536
1537         assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
1538
1539         Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
1540
1541         for(NormalizedNode<?,?> node : collection){
1542             assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
1543         }
1544
1545         assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
1546                 NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME) != null);
1547
1548         assertEquals(expectedNode1, NormalizedNodeAggregatorTest.findChildWithQName(collection, TestModel.TEST_QNAME));
1549
1550         assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
1551                 NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
1552
1553         assertEquals(expectedNode2, NormalizedNodeAggregatorTest.findChildWithQName(collection, CarsModel.BASE_QNAME));
1554     }
1555
1556
1557     private void setUpReadData(String shardName, NormalizedNode<?, ?> expectedNode) {
1558         ActorSystem actorSystem = getSystem();
1559         ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class));
1560
1561         doReturn(getSystem().actorSelection(shardActorRef.path())).
1562                 when(mockActorContext).actorSelection(shardActorRef.path().toString());
1563
1564         doReturn(primaryShardInfoReply(getSystem(), shardActorRef)).
1565                 when(mockActorContext).findPrimaryShardAsync(eq(shardName));
1566
1567         doReturn(true).when(mockActorContext).isPathLocal(shardActorRef.path().toString());
1568
1569         ActorRef txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
1570
1571         doReturn(actorSystem.actorSelection(txActorRef.path())).
1572                 when(mockActorContext).actorSelection(txActorRef.path().toString());
1573
1574         doReturn(Futures.successful(createTransactionReply(txActorRef, DataStoreVersions.CURRENT_VERSION))).when(mockActorContext).
1575                 executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
1576                         eqCreateTransaction(memberName, TransactionType.READ_ONLY));
1577
1578         doReturn(readSerializedDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
1579                 eq(actorSelection(txActorRef)), eqSerializedReadData(YangInstanceIdentifier.builder().build()));
1580     }
1581 }