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(invocation -> {
131 return invokeFailure(invocation);
132 }).when(mock).canCommit(any(FutureCallback.class));
136 @SuppressWarnings("unchecked")
137 public static ShardDataTreeCohort failedPreCommit(final ShardDataTreeCohort mock) {
138 doAnswer(invocation -> {
139 return invokeFailure(invocation);
140 }).when(mock).preCommit(any(FutureCallback.class));
144 @SuppressWarnings("unchecked")
145 public static ShardDataTreeCohort failedCommit(final ShardDataTreeCohort mock) {
146 doAnswer(invocation -> {
147 return invokeFailure(invocation);
148 }).when(mock).commit(any(FutureCallback.class));
152 @SuppressWarnings("unchecked")
153 public static ShardDataTreeCohort successfulCanCommit(final ShardDataTreeCohort mock) {
154 doAnswer(invocation -> {
155 return invokeSuccess(invocation, null);
156 }).when(mock).canCommit(any(FutureCallback.class));
161 public static ShardDataTreeCohort successfulPreCommit(final ShardDataTreeCohort mock) {
162 return successfulPreCommit(mock, mock(DataTreeCandidate.class));
165 @SuppressWarnings("unchecked")
166 public static ShardDataTreeCohort successfulPreCommit(final ShardDataTreeCohort mock,
167 final DataTreeCandidate candidate) {
168 doAnswer(invocation -> {
169 return invokeSuccess(invocation, candidate);
170 }).when(mock).preCommit(any(FutureCallback.class));
175 public static ShardDataTreeCohort successfulCommit(final ShardDataTreeCohort mock) {
176 return successfulCommit(mock, UnsignedLong.ZERO);
179 @SuppressWarnings("unchecked")
180 public static ShardDataTreeCohort successfulCommit(final ShardDataTreeCohort mock, final UnsignedLong index) {
181 doAnswer(invocation -> {
182 return invokeSuccess(invocation, index);
183 }).when(mock).commit(any(FutureCallback.class));
188 @SuppressWarnings("unchecked")
189 public static void assertSequencedCommit(final ShardDataTreeCohort mock) {
190 final InOrder inOrder = inOrder(mock);
191 inOrder.verify(mock).canCommit(any(FutureCallback.class));
192 inOrder.verify(mock).preCommit(any(FutureCallback.class));
193 inOrder.verify(mock).commit(any(FutureCallback.class));
196 public static void immediatePayloadReplication(final ShardDataTree shardDataTree, final Shard mockShard) {
197 doAnswer(invocation -> {
198 shardDataTree.applyReplicatedPayload(invocation.getArgumentAt(0, TransactionIdentifier.class),
199 invocation.getArgumentAt(1, Payload.class));
201 }).when(mockShard).persistPayload(any(TransactionIdentifier.class), any(CommitTransactionPayload.class),