8469d027957005d7522b52d5861a6c72837c90ab
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / AbstractShardTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.doAnswer;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.opendaylight.controller.cluster.datastore.DataStoreVersions.CURRENT_VERSION;
19 import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.immediateCanCommit;
20 import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.immediateCommit;
21 import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.immediatePreCommit;
22 import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.successfulCanCommit;
23 import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.successfulCommit;
24 import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.successfulPreCommit;
25 import akka.actor.ActorRef;
26 import akka.actor.PoisonPill;
27 import akka.actor.Props;
28 import akka.dispatch.Dispatchers;
29 import akka.japi.Creator;
30 import akka.pattern.Patterns;
31 import akka.testkit.TestActorRef;
32 import akka.util.Timeout;
33 import com.google.common.base.Optional;
34 import com.google.common.primitives.UnsignedLong;
35 import com.google.common.util.concurrent.FutureCallback;
36 import com.google.common.util.concurrent.ListenableFuture;
37 import com.google.common.util.concurrent.Uninterruptibles;
38 import java.io.IOException;
39 import java.util.Collections;
40 import java.util.HashMap;
41 import java.util.Map;
42 import java.util.Set;
43 import java.util.concurrent.CountDownLatch;
44 import java.util.concurrent.ExecutionException;
45 import java.util.concurrent.TimeUnit;
46 import java.util.concurrent.TimeoutException;
47 import java.util.concurrent.atomic.AtomicInteger;
48 import org.junit.After;
49 import org.junit.Assert;
50 import org.junit.Before;
51 import org.opendaylight.controller.cluster.access.concepts.MemberName;
52 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
53 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
54 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
55 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
56 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
57 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
58 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
59 import org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload;
60 import org.opendaylight.controller.cluster.datastore.persisted.PreBoronShardDataTreeSnapshot;
61 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
62 import org.opendaylight.controller.cluster.raft.Snapshot;
63 import org.opendaylight.controller.cluster.raft.TestActorFactory;
64 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
65 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
66 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
67 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
68 import org.opendaylight.yangtools.concepts.Identifier;
69 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
71 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
72 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
73 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
74 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
75 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
76 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
77 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
78 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
79 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
80 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
81 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
82 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
83 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
84 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
85 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
86 import scala.concurrent.Await;
87 import scala.concurrent.Future;
88 import scala.concurrent.duration.Duration;
89
90 /**
91  * Abstract base for shard unit tests.
92  *
93  * @author Thomas Pantelis
94  */
95 public abstract class AbstractShardTest extends AbstractActorTest{
96     protected static final SchemaContext SCHEMA_CONTEXT = TestModel.createTestContext();
97
98     private static final AtomicInteger NEXT_SHARD_NUM = new AtomicInteger();
99
100     protected final ShardIdentifier shardID = ShardIdentifier.create("inventory", MemberName.forName("member-1"),
101         "config" + NEXT_SHARD_NUM.getAndIncrement());
102
103     protected final Builder dataStoreContextBuilder = DatastoreContext.newBuilder().
104             shardJournalRecoveryLogBatchSize(3).shardSnapshotBatchCount(5000).
105             shardHeartbeatIntervalInMillis(100);
106
107     protected final TestActorFactory actorFactory = new TestActorFactory(getSystem());
108
109     @Before
110     public void setUp() {
111         InMemorySnapshotStore.clear();
112         InMemoryJournal.clear();
113     }
114
115     @After
116     public void tearDown() {
117         InMemorySnapshotStore.clear();
118         InMemoryJournal.clear();
119         actorFactory.close();
120     }
121
122     protected DatastoreContext newDatastoreContext() {
123         return dataStoreContextBuilder.build();
124     }
125
126     protected Props newShardProps() {
127         return newShardBuilder().props();
128     }
129
130     protected Shard.Builder newShardBuilder() {
131         return Shard.builder().id(shardID).datastoreContext(newDatastoreContext()).schemaContext(SCHEMA_CONTEXT);
132     }
133
134     protected void testRecovery(final Set<Integer> listEntryKeys) throws Exception {
135         // Create the actor and wait for recovery complete.
136
137         final int nListEntries = listEntryKeys.size();
138
139         final CountDownLatch recoveryComplete = new CountDownLatch(1);
140
141         @SuppressWarnings("serial")
142         final Creator<Shard> creator = new Creator<Shard>() {
143             @Override
144             public Shard create() throws Exception {
145                 return new Shard(newShardBuilder()) {
146                     @Override
147                     protected void onRecoveryComplete() {
148                         try {
149                             super.onRecoveryComplete();
150                         } finally {
151                             recoveryComplete.countDown();
152                         }
153                     }
154                 };
155             }
156         };
157
158         final TestActorRef<Shard> shard = TestActorRef.create(getSystem(),
159                 Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()), "testRecovery");
160
161         assertEquals("Recovery complete", true, recoveryComplete.await(5, TimeUnit.SECONDS));
162
163         // Verify data in the data store.
164
165         final NormalizedNode<?, ?> outerList = readStore(shard, TestModel.OUTER_LIST_PATH);
166         assertNotNull(TestModel.OUTER_LIST_QNAME.getLocalName() + " not found", outerList);
167         assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " value is not Iterable",
168                 outerList.getValue() instanceof Iterable);
169         for(final Object entry: (Iterable<?>) outerList.getValue()) {
170             assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " entry is not MapEntryNode",
171                     entry instanceof MapEntryNode);
172             final MapEntryNode mapEntry = (MapEntryNode)entry;
173             final Optional<DataContainerChild<? extends PathArgument, ?>> idLeaf =
174                     mapEntry.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.ID_QNAME));
175             assertTrue("Missing leaf " + TestModel.ID_QNAME.getLocalName(), idLeaf.isPresent());
176             final Object value = idLeaf.get().getValue();
177             assertTrue("Unexpected value for leaf "+ TestModel.ID_QNAME.getLocalName() + ": " + value,
178                     listEntryKeys.remove(value));
179         }
180
181         if(!listEntryKeys.isEmpty()) {
182             fail("Missing " + TestModel.OUTER_LIST_QNAME.getLocalName() + " entries with keys: " +
183                     listEntryKeys);
184         }
185
186         assertEquals("Last log index", nListEntries,
187                 shard.underlyingActor().getShardMBean().getLastLogIndex());
188         assertEquals("Commit index", nListEntries,
189                 shard.underlyingActor().getShardMBean().getCommitIndex());
190         assertEquals("Last applied", nListEntries,
191                 shard.underlyingActor().getShardMBean().getLastApplied());
192
193         shard.tell(PoisonPill.getInstance(), ActorRef.noSender());
194     }
195
196     protected void verifyLastApplied(final TestActorRef<Shard> shard, final long expectedValue) {
197         long lastApplied = -1;
198         for(int i = 0; i < 20 * 5; i++) {
199             lastApplied = shard.underlyingActor().getShardMBean().getLastApplied();
200             if(lastApplied == expectedValue) {
201                 return;
202             }
203             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
204         }
205
206         Assert.fail(String.format("Expected last applied: %d, Actual: %d", expectedValue, lastApplied));
207     }
208
209     protected TipProducingDataTree createDelegatingMockDataTree() throws Exception {
210         TipProducingDataTree actual = InMemoryDataTreeFactory.getInstance().create(TreeType.CONFIGURATION);
211         final TipProducingDataTree mock = mock(TipProducingDataTree.class);
212
213         doAnswer(invocation -> {
214             actual.validate(invocation.getArgumentAt(0, DataTreeModification.class));
215             return null;
216         }).when(mock).validate(any(DataTreeModification.class));
217
218         doAnswer(invocation -> {
219             return actual.prepare(invocation.getArgumentAt(0, DataTreeModification.class));
220         }).when(mock).prepare(any(DataTreeModification.class));
221
222         doAnswer(invocation -> {
223             actual.commit(invocation.getArgumentAt(0, DataTreeCandidate.class));
224             return null;
225         }).when(mock).commit(any(DataTreeCandidate.class));
226
227         doAnswer(invocation -> {
228             actual.setSchemaContext(invocation.getArgumentAt(0, SchemaContext.class));
229             return null;
230         }).when(mock).setSchemaContext(any(SchemaContext.class));
231
232         doAnswer(invocation -> {
233             return actual.takeSnapshot();
234         }).when(mock).takeSnapshot();
235
236         doAnswer(invocation -> {
237             return actual.getRootPath();
238         }).when(mock).getRootPath();
239
240         return mock;
241     }
242
243     protected ShardDataTreeCohort mockShardDataTreeCohort() {
244         ShardDataTreeCohort cohort = mock(ShardDataTreeCohort.class);
245         DataTreeCandidate candidate = mockCandidate("candidate");
246         successfulCanCommit(cohort);
247         successfulPreCommit(cohort, candidate);
248         successfulCommit(cohort);
249         doReturn(candidate).when(cohort).getCandidate();
250         return cohort;
251     }
252
253     protected Map<TransactionIdentifier, CapturingShardDataTreeCohort> setupCohortDecorator(final Shard shard,
254             final TransactionIdentifier... transactionIDs) {
255         final Map<TransactionIdentifier, CapturingShardDataTreeCohort> cohortMap = new HashMap<>();
256         for(TransactionIdentifier id: transactionIDs) {
257             cohortMap.put(id, new CapturingShardDataTreeCohort());
258         }
259
260         shard.getCommitCoordinator().setCohortDecorator(new ShardCommitCoordinator.CohortDecorator() {
261             @Override
262             public ShardDataTreeCohort decorate(final Identifier transactionID, final ShardDataTreeCohort actual) {
263                 CapturingShardDataTreeCohort cohort = cohortMap.get(transactionID);
264                 cohort.setDelegate(actual);
265                 return cohort;
266             }
267         });
268
269         return cohortMap;
270     }
271
272     protected BatchedModifications prepareBatchedModifications(final TransactionIdentifier transactionID,
273                                                                final MutableCompositeModification modification) {
274         return prepareBatchedModifications(transactionID, modification, false);
275     }
276
277     protected static BatchedModifications prepareBatchedModifications(final TransactionIdentifier transactionID,
278                                                              final MutableCompositeModification modification,
279                                                              final boolean doCommitOnReady) {
280         final BatchedModifications batchedModifications = new BatchedModifications(transactionID, CURRENT_VERSION);
281         batchedModifications.addModification(modification);
282         batchedModifications.setReady(true);
283         batchedModifications.setDoCommitOnReady(doCommitOnReady);
284         batchedModifications.setTotalMessagesSent(1);
285         return batchedModifications;
286     }
287
288     protected static BatchedModifications prepareBatchedModifications(final TransactionIdentifier transactionID,
289             final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final boolean doCommitOnReady) {
290         final MutableCompositeModification modification = new MutableCompositeModification();
291         modification.addModification(new WriteModification(path, data));
292         return prepareBatchedModifications(transactionID, modification, doCommitOnReady);
293     }
294
295     protected static ForwardedReadyTransaction prepareForwardedReadyTransaction(final TestActorRef<Shard> shard,
296             final TransactionIdentifier transactionID, final YangInstanceIdentifier path,
297             final NormalizedNode<?, ?> data, final boolean doCommitOnReady) {
298         ReadWriteShardDataTreeTransaction rwTx = shard.underlyingActor().getDataStore().
299                 newReadWriteTransaction(transactionID);
300         rwTx.getSnapshot().write(path, data);
301         return new ForwardedReadyTransaction(transactionID, CURRENT_VERSION, rwTx, doCommitOnReady);
302     }
303
304     public static NormalizedNode<?,?> readStore(final TestActorRef<? extends Shard> shard, final YangInstanceIdentifier id)
305             throws ExecutionException, InterruptedException {
306         return shard.underlyingActor().getDataStore().readNode(id).orNull();
307     }
308
309     public static NormalizedNode<?,?> readStore(final DataTree store, final YangInstanceIdentifier id) {
310         return store.takeSnapshot().readNode(id).orNull();
311     }
312
313     public void writeToStore(final TestActorRef<Shard> shard, final YangInstanceIdentifier id,
314             final NormalizedNode<?,?> node) throws InterruptedException, ExecutionException {
315         Future<Object> future = Patterns.ask(shard, newBatchedModifications(nextTransactionId(), id, node, true, true, 1),
316                 new Timeout(5, TimeUnit.SECONDS));
317         try {
318             Await.ready(future, Duration.create(5, TimeUnit.SECONDS));
319         } catch(TimeoutException e) {
320             throw new ExecutionException(e);
321         }
322     }
323
324     public static void writeToStore(final ShardDataTree store, final YangInstanceIdentifier id,
325             final NormalizedNode<?,?> node) throws InterruptedException, ExecutionException {
326         final ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction(nextTransactionId());
327
328         transaction.getSnapshot().write(id, node);
329         final ShardDataTreeCohort cohort = transaction.ready();
330         immediateCanCommit(cohort);
331         immediatePreCommit(cohort);
332         immediateCommit(cohort);
333     }
334
335     public void mergeToStore(final ShardDataTree store, final YangInstanceIdentifier id,
336             final NormalizedNode<?,?> node) throws InterruptedException, ExecutionException {
337         final ReadWriteShardDataTreeTransaction transaction = store.newReadWriteTransaction(nextTransactionId());
338
339         transaction.getSnapshot().merge(id, node);
340         final ShardDataTreeCohort cohort = transaction.ready();
341         immediateCanCommit(cohort);
342         immediatePreCommit(cohort);
343         immediateCommit(cohort);
344     }
345
346     public static void writeToStore(final DataTree store, final YangInstanceIdentifier id,
347             final NormalizedNode<?,?> node) throws DataValidationFailedException {
348         final DataTreeModification transaction = store.takeSnapshot().newModification();
349
350         transaction.write(id, node);
351         transaction.ready();
352         store.validate(transaction);
353         final DataTreeCandidate candidate = store.prepare(transaction);
354         store.commit(candidate);
355     }
356
357     DataTree setupInMemorySnapshotStore() throws DataValidationFailedException {
358         final DataTree testStore = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
359         testStore.setSchemaContext(SCHEMA_CONTEXT);
360
361         writeToStore(testStore, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
362
363         final NormalizedNode<?, ?> root = readStore(testStore, YangInstanceIdentifier.EMPTY);
364
365         InMemorySnapshotStore.addSnapshot(shardID.toString(), Snapshot.create(
366                 new PreBoronShardDataTreeSnapshot(root).serialize(),
367                 Collections.<ReplicatedLogEntry>emptyList(), 0, 1, -1, -1));
368         return testStore;
369     }
370
371     static CommitTransactionPayload payloadForModification(final DataTree source, final DataTreeModification mod,
372             final TransactionIdentifier transactionId) throws DataValidationFailedException, IOException {
373         source.validate(mod);
374         final DataTreeCandidate candidate = source.prepare(mod);
375         source.commit(candidate);
376         return CommitTransactionPayload.create(transactionId, candidate);
377     }
378
379     static BatchedModifications newBatchedModifications(final TransactionIdentifier transactionID,
380             final YangInstanceIdentifier path, final NormalizedNode<?, ?> data, final boolean ready, final boolean doCommitOnReady,
381             final int messagesSent) {
382         final BatchedModifications batched = new BatchedModifications(transactionID, CURRENT_VERSION);
383         batched.addModification(new WriteModification(path, data));
384         batched.setReady(ready);
385         batched.setDoCommitOnReady(doCommitOnReady);
386         batched.setTotalMessagesSent(messagesSent);
387         return batched;
388     }
389
390     @SuppressWarnings("unchecked")
391     static void verifyOuterListEntry(final TestActorRef<Shard> shard, final Object expIDValue) throws Exception {
392         final NormalizedNode<?, ?> outerList = readStore(shard, TestModel.OUTER_LIST_PATH);
393         assertNotNull(TestModel.OUTER_LIST_QNAME.getLocalName() + " not found", outerList);
394         assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " value is not Iterable",
395                 outerList.getValue() instanceof Iterable);
396         final Object entry = ((Iterable<Object>)outerList.getValue()).iterator().next();
397         assertTrue(TestModel.OUTER_LIST_QNAME.getLocalName() + " entry is not MapEntryNode",
398                 entry instanceof MapEntryNode);
399         final MapEntryNode mapEntry = (MapEntryNode)entry;
400         final Optional<DataContainerChild<? extends PathArgument, ?>> idLeaf =
401                 mapEntry.getChild(new YangInstanceIdentifier.NodeIdentifier(TestModel.ID_QNAME));
402         assertTrue("Missing leaf " + TestModel.ID_QNAME.getLocalName(), idLeaf.isPresent());
403         assertEquals(TestModel.ID_QNAME.getLocalName() + " value", expIDValue, idLeaf.get().getValue());
404     }
405
406     public static DataTreeCandidateTip mockCandidate(final String name) {
407         final DataTreeCandidateTip mockCandidate = mock(DataTreeCandidateTip.class, name);
408         final DataTreeCandidateNode mockCandidateNode = mock(DataTreeCandidateNode.class, name + "-node");
409         doReturn(ModificationType.WRITE).when(mockCandidateNode).getModificationType();
410         doReturn(Optional.of(ImmutableNodes.containerNode(CarsModel.CARS_QNAME))).when(mockCandidateNode).getDataAfter();
411         doReturn(CarsModel.BASE_PATH).when(mockCandidate).getRootPath();
412         doReturn(mockCandidateNode).when(mockCandidate).getRootNode();
413         return mockCandidate;
414     }
415
416     static DataTreeCandidateTip mockUnmodifiedCandidate(final String name) {
417         final DataTreeCandidateTip mockCandidate = mock(DataTreeCandidateTip.class, name);
418         final DataTreeCandidateNode mockCandidateNode = mock(DataTreeCandidateNode.class, name + "-node");
419         doReturn(ModificationType.UNMODIFIED).when(mockCandidateNode).getModificationType();
420         doReturn(YangInstanceIdentifier.EMPTY).when(mockCandidate).getRootPath();
421         doReturn(mockCandidateNode).when(mockCandidate).getRootNode();
422         return mockCandidate;
423     }
424
425     static void commitTransaction(final DataTree store, final DataTreeModification modification) throws DataValidationFailedException {
426         modification.ready();
427         store.validate(modification);
428         store.commit(store.prepare(modification));
429     }
430
431     @SuppressWarnings("serial")
432     public static final class DelegatingShardCreator implements Creator<Shard> {
433         private final Creator<Shard> delegate;
434
435         DelegatingShardCreator(final Creator<Shard> delegate) {
436             this.delegate = delegate;
437         }
438
439         @Override
440         public Shard create() throws Exception {
441             return delegate.create();
442         }
443     }
444
445     public static class CapturingShardDataTreeCohort extends ShardDataTreeCohort {
446         private volatile ShardDataTreeCohort delegate;
447         private FutureCallback<Void> canCommit;
448         private FutureCallback<DataTreeCandidate> preCommit;
449         private FutureCallback<UnsignedLong> commit;
450
451         public void setDelegate(ShardDataTreeCohort delegate) {
452             this.delegate = delegate;
453         }
454
455         public FutureCallback<Void> getCanCommit() {
456             assertNotNull("canCommit was not invoked", canCommit);
457             return canCommit;
458         }
459
460         public FutureCallback<DataTreeCandidate> getPreCommit() {
461             assertNotNull("preCommit was not invoked", preCommit);
462             return preCommit;
463         }
464
465         public FutureCallback<UnsignedLong> getCommit() {
466             assertNotNull("commit was not invoked", commit);
467             return commit;
468         }
469
470         @Override
471         public TransactionIdentifier getIdentifier() {
472             return delegate.getIdentifier();
473         }
474
475         @Override
476         DataTreeCandidateTip getCandidate() {
477             return delegate.getCandidate();
478         }
479
480         @Override
481         DataTreeModification getDataTreeModification() {
482             return delegate.getDataTreeModification();
483         }
484
485         @Override
486         public void canCommit(FutureCallback<Void> callback) {
487             canCommit = mockFutureCallback(callback);
488             delegate.canCommit(canCommit);
489         }
490
491         @Override
492         public void preCommit(FutureCallback<DataTreeCandidate> callback) {
493             preCommit = mockFutureCallback(callback);
494             delegate.preCommit(preCommit);
495         }
496
497         @Override
498         public void commit(FutureCallback<UnsignedLong> callback) {
499             commit = mockFutureCallback(callback);
500             delegate.commit(commit);
501         }
502
503         @SuppressWarnings("unchecked")
504         private <T> FutureCallback<T> mockFutureCallback(final FutureCallback<T> actual ) {
505             FutureCallback<T> mock = mock(FutureCallback.class);
506             doAnswer(invocation -> {
507                 actual.onFailure(invocation.getArgumentAt(0, Throwable.class));
508                 return null;
509             }).when(mock).onFailure(any(Throwable.class));
510
511             doAnswer(invocation -> {
512                 actual.onSuccess((T) invocation.getArgumentAt(0, Throwable.class));
513                 return null;
514             }).when(mock).onSuccess((T) any(Object.class));
515
516             return mock;
517         }
518
519         @Override
520         public ListenableFuture<Void> abort() {
521             return delegate.abort();
522         }
523
524         @Override
525         public boolean isFailed() {
526             return delegate.isFailed();
527         }
528
529         @Override
530         public State getState() {
531             return delegate.getState();
532         }
533     }
534 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.