2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.anyBoolean;
12 import static org.mockito.Mockito.doAnswer;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.inOrder;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.timeout;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.verifyNoMoreInteractions;
20 import com.google.common.primitives.UnsignedLong;
21 import com.google.common.util.concurrent.FutureCallback;
22 import org.mockito.InOrder;
23 import org.mockito.invocation.InvocationOnMock;
24 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
25 import org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload;
26 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
29 public final class ShardDataTreeMocking {
31 private ShardDataTreeMocking() {
32 throw new UnsupportedOperationException();
35 @SuppressWarnings("unchecked")
36 private static <T> FutureCallback<T> mockCallback() {
37 return mock(FutureCallback.class);
40 public static ShardDataTreeCohort immediateCanCommit(final ShardDataTreeCohort cohort) {
41 final FutureCallback<Void> callback = mockCallback();
42 doNothing().when(callback).onSuccess(null);
43 cohort.canCommit(callback);
45 verify(callback).onSuccess(null);
46 verifyNoMoreInteractions(callback);
50 public static FutureCallback<Void> coordinatedCanCommit(final ShardDataTreeCohort cohort) {
51 final FutureCallback<Void> callback = mockCallback();
52 doNothing().when(callback).onSuccess(null);
53 doNothing().when(callback).onFailure(any(Throwable.class));
54 cohort.canCommit(callback);
58 public static ShardDataTreeCohort immediatePreCommit(final ShardDataTreeCohort cohort) {
59 final FutureCallback<DataTreeCandidate> callback = mockCallback();
60 doNothing().when(callback).onSuccess(any(DataTreeCandidate.class));
61 cohort.preCommit(callback);
63 verify(callback).onSuccess(any(DataTreeCandidate.class));
64 verifyNoMoreInteractions(callback);
68 public static FutureCallback<DataTreeCandidate> coordinatedPreCommit(final ShardDataTreeCohort cohort) {
69 final FutureCallback<DataTreeCandidate> callback = mockCallback();
70 doNothing().when(callback).onSuccess(any(DataTreeCandidate.class));
71 doNothing().when(callback).onFailure(any(Throwable.class));
72 cohort.preCommit(callback);
76 public static ShardDataTreeCohort immediateCommit(final ShardDataTreeCohort cohort) {
77 final FutureCallback<UnsignedLong> callback = mockCallback();
78 doNothing().when(callback).onSuccess(any(UnsignedLong.class));
79 cohort.commit(callback);
81 verify(callback, timeout(5000)).onSuccess(any(UnsignedLong.class));
82 verifyNoMoreInteractions(callback);
86 public static FutureCallback<UnsignedLong> coordinatedCommit(final ShardDataTreeCohort cohort) {
87 final FutureCallback<UnsignedLong> callback = mockCallback();
88 doNothing().when(callback).onSuccess(any(UnsignedLong.class));
89 doNothing().when(callback).onFailure(any(Throwable.class));
90 cohort.commit(callback);
94 public static FutureCallback<UnsignedLong> immediate3PhaseCommit(final ShardDataTreeCohort cohort) {
95 final FutureCallback<UnsignedLong> commitCallback = mockCallback();
96 doNothing().when(commitCallback).onSuccess(any(UnsignedLong.class));
97 doNothing().when(commitCallback).onFailure(any(Throwable.class));
99 final FutureCallback<DataTreeCandidate> preCommitCallback = mockCallback();
100 doAnswer(invocation -> {
101 cohort.commit(commitCallback);
103 }).when(preCommitCallback).onSuccess(any(DataTreeCandidate.class));
104 doNothing().when(preCommitCallback).onFailure(any(Throwable.class));
106 final FutureCallback<Void> canCommit = mockCallback();
107 doAnswer(invocation -> {
108 cohort.preCommit(preCommitCallback);
110 }).when(canCommit).onSuccess(null);
111 doNothing().when(canCommit).onFailure(any(Throwable.class));
113 cohort.canCommit(canCommit);
114 return commitCallback;
117 @SuppressWarnings("unchecked")
118 private static <T> Object invokeSuccess(final InvocationOnMock invocation, final T value) {
119 invocation.getArgumentAt(0, FutureCallback.class).onSuccess(value);
123 private static Object invokeFailure(final InvocationOnMock invocation) {
124 invocation.getArgumentAt(0, FutureCallback.class).onFailure(mock(Exception.class));
128 @SuppressWarnings("unchecked")
129 public static ShardDataTreeCohort failedCanCommit(final ShardDataTreeCohort mock) {
130 doAnswer(ShardDataTreeMocking::invokeFailure).when(mock).canCommit(any(FutureCallback.class));
134 @SuppressWarnings("unchecked")
135 public static ShardDataTreeCohort failedPreCommit(final ShardDataTreeCohort mock) {
136 doAnswer(ShardDataTreeMocking::invokeFailure).when(mock).preCommit(any(FutureCallback.class));
140 @SuppressWarnings("unchecked")
141 public static ShardDataTreeCohort failedCommit(final ShardDataTreeCohort mock) {
142 doAnswer(ShardDataTreeMocking::invokeFailure).when(mock).commit(any(FutureCallback.class));
146 @SuppressWarnings("unchecked")
147 public static ShardDataTreeCohort successfulCanCommit(final ShardDataTreeCohort mock) {
148 doAnswer(invocation -> invokeSuccess(invocation, null)).when(mock).canCommit(any(FutureCallback.class));
153 public static ShardDataTreeCohort successfulPreCommit(final ShardDataTreeCohort mock) {
154 return successfulPreCommit(mock, mock(DataTreeCandidate.class));
157 @SuppressWarnings("unchecked")
158 public static ShardDataTreeCohort successfulPreCommit(final ShardDataTreeCohort mock,
159 final DataTreeCandidate candidate) {
160 doAnswer(invocation -> invokeSuccess(invocation, candidate)).when(mock).preCommit(any(FutureCallback.class));
165 public static ShardDataTreeCohort successfulCommit(final ShardDataTreeCohort mock) {
166 return successfulCommit(mock, UnsignedLong.ZERO);
169 @SuppressWarnings("unchecked")
170 public static ShardDataTreeCohort successfulCommit(final ShardDataTreeCohort mock, final UnsignedLong index) {
171 doAnswer(invocation -> invokeSuccess(invocation, index)).when(mock).commit(any(FutureCallback.class));
176 @SuppressWarnings("unchecked")
177 public static void assertSequencedCommit(final ShardDataTreeCohort mock) {
178 final InOrder inOrder = inOrder(mock);
179 inOrder.verify(mock).canCommit(any(FutureCallback.class));
180 inOrder.verify(mock).preCommit(any(FutureCallback.class));
181 inOrder.verify(mock).commit(any(FutureCallback.class));
184 public static void immediatePayloadReplication(final ShardDataTree shardDataTree, final Shard mockShard) {
185 doAnswer(invocation -> {
186 shardDataTree.applyReplicatedPayload(invocation.getArgumentAt(0, TransactionIdentifier.class),
187 invocation.getArgumentAt(1, Payload.class));
189 }).when(mockShard).persistPayload(any(TransactionIdentifier.class), any(CommitTransactionPayload.class),