BUG-7033: Remove payload replication short-circuits
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardDataTreeMocking.java
1 /*
2  * Copyright (c) 2016 Cisco 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.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;
19
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;
28
29 public final class ShardDataTreeMocking {
30
31     private ShardDataTreeMocking() {
32         throw new UnsupportedOperationException();
33     }
34
35     @SuppressWarnings("unchecked")
36     private static <T> FutureCallback<T> mockCallback() {
37         return mock(FutureCallback.class);
38     }
39
40     public static ShardDataTreeCohort immediateCanCommit(final ShardDataTreeCohort cohort) {
41         final FutureCallback<Void> callback = mockCallback();
42         doNothing().when(callback).onSuccess(null);
43         cohort.canCommit(callback);
44
45         verify(callback).onSuccess(null);
46         verifyNoMoreInteractions(callback);
47         return cohort;
48     }
49
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);
55         return callback;
56     }
57
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);
62
63         verify(callback).onSuccess(any(DataTreeCandidate.class));
64         verifyNoMoreInteractions(callback);
65         return cohort;
66     }
67
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);
73         return callback;
74     }
75
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);
80
81         verify(callback, timeout(5000)).onSuccess(any(UnsignedLong.class));
82         verifyNoMoreInteractions(callback);
83         return cohort;
84     }
85
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);
91         return callback;
92     }
93
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));
98
99         final FutureCallback<DataTreeCandidate> preCommitCallback = mockCallback();
100         doAnswer(invocation -> {
101             cohort.commit(commitCallback);
102             return null;
103         }).when(preCommitCallback).onSuccess(any(DataTreeCandidate.class));
104         doNothing().when(preCommitCallback).onFailure(any(Throwable.class));
105
106         final FutureCallback<Void> canCommit = mockCallback();
107         doAnswer(invocation -> {
108             cohort.preCommit(preCommitCallback);
109             return null;
110         }).when(canCommit).onSuccess(null);
111         doNothing().when(canCommit).onFailure(any(Throwable.class));
112
113         cohort.canCommit(canCommit);
114         return commitCallback;
115     }
116
117     @SuppressWarnings("unchecked")
118     private static <T> Object invokeSuccess(final InvocationOnMock invocation, final T value) {
119         invocation.getArgumentAt(0, FutureCallback.class).onSuccess(value);
120         return null;
121     }
122
123     private static Object invokeFailure(final InvocationOnMock invocation) {
124         invocation.getArgumentAt(0, FutureCallback.class).onFailure(mock(Exception.class));
125         return null;
126     }
127
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));
133         return mock;
134     }
135
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));
141         return mock;
142     }
143
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));
149         return mock;
150     }
151
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));
157
158         return mock;
159     }
160
161     public static ShardDataTreeCohort successfulPreCommit(final ShardDataTreeCohort mock) {
162         return successfulPreCommit(mock, mock(DataTreeCandidate.class));
163     }
164
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));
171
172         return mock;
173     }
174
175     public static ShardDataTreeCohort successfulCommit(final ShardDataTreeCohort mock) {
176         return successfulCommit(mock, UnsignedLong.ZERO);
177     }
178
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));
184
185         return mock;
186     }
187
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));
194     }
195
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));
200             return null;
201         }).when(mockShard).persistPayload(any(TransactionIdentifier.class), any(CommitTransactionPayload.class),
202                 anyBoolean());
203     }
204 }