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.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.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.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
28 public final class ShardDataTreeMocking {
30 private ShardDataTreeMocking() {
31 throw new UnsupportedOperationException();
34 @SuppressWarnings("unchecked")
35 private static <T> FutureCallback<T> mockCallback() {
36 return mock(FutureCallback.class);
39 public static ShardDataTreeCohort immediateCanCommit(final ShardDataTreeCohort cohort) {
40 final FutureCallback<Void> callback = mockCallback();
41 doNothing().when(callback).onSuccess(null);
42 cohort.canCommit(callback);
44 verify(callback).onSuccess(null);
45 verifyNoMoreInteractions(callback);
49 public static FutureCallback<Void> coordinatedCanCommit(final ShardDataTreeCohort cohort) {
50 final FutureCallback<Void> callback = mockCallback();
51 doNothing().when(callback).onSuccess(null);
52 doNothing().when(callback).onFailure(any(Throwable.class));
53 cohort.canCommit(callback);
57 public static ShardDataTreeCohort immediatePreCommit(final ShardDataTreeCohort cohort) {
58 final FutureCallback<DataTreeCandidate> callback = mockCallback();
59 doNothing().when(callback).onSuccess(any(DataTreeCandidate.class));
60 cohort.preCommit(callback);
62 verify(callback).onSuccess(any(DataTreeCandidate.class));
63 verifyNoMoreInteractions(callback);
67 public static FutureCallback<DataTreeCandidate> coordinatedPreCommit(final ShardDataTreeCohort cohort) {
68 final FutureCallback<DataTreeCandidate> callback = mockCallback();
69 doNothing().when(callback).onSuccess(any(DataTreeCandidate.class));
70 doNothing().when(callback).onFailure(any(Throwable.class));
71 cohort.preCommit(callback);
75 public static ShardDataTreeCohort immediateCommit(final ShardDataTreeCohort cohort) {
76 final FutureCallback<UnsignedLong> callback = mockCallback();
77 doNothing().when(callback).onSuccess(any(UnsignedLong.class));
78 cohort.commit(callback);
80 verify(callback, timeout(5000)).onSuccess(any(UnsignedLong.class));
81 verifyNoMoreInteractions(callback);
85 public static FutureCallback<UnsignedLong> coordinatedCommit(final ShardDataTreeCohort cohort) {
86 final FutureCallback<UnsignedLong> callback = mockCallback();
87 doNothing().when(callback).onSuccess(any(UnsignedLong.class));
88 doNothing().when(callback).onFailure(any(Throwable.class));
89 cohort.commit(callback);
93 public static FutureCallback<UnsignedLong> immediate3PhaseCommit(final ShardDataTreeCohort cohort) {
94 final FutureCallback<UnsignedLong> commitCallback = mockCallback();
95 doNothing().when(commitCallback).onSuccess(any(UnsignedLong.class));
96 doNothing().when(commitCallback).onFailure(any(Throwable.class));
98 final FutureCallback<DataTreeCandidate> preCommitCallback = mockCallback();
99 doAnswer(invocation -> {
100 cohort.commit(commitCallback);
102 }).when(preCommitCallback).onSuccess(any(DataTreeCandidate.class));
103 doNothing().when(preCommitCallback).onFailure(any(Throwable.class));
105 final FutureCallback<Void> canCommit = mockCallback();
106 doAnswer(invocation -> {
107 cohort.preCommit(preCommitCallback);
109 }).when(canCommit).onSuccess(null);
110 doNothing().when(canCommit).onFailure(any(Throwable.class));
112 cohort.canCommit(canCommit);
113 return commitCallback;
116 private static <T> Object invokeSuccess(final InvocationOnMock invocation, final T value) {
117 invocation.<FutureCallback<T>>getArgument(0).onSuccess(value);
121 private static Object invokeFailure(final InvocationOnMock invocation) {
122 invocation.<FutureCallback<?>>getArgument(0).onFailure(mock(Exception.class));
126 @SuppressWarnings("unchecked")
127 public static ShardDataTreeCohort failedCanCommit(final ShardDataTreeCohort mock) {
128 doAnswer(ShardDataTreeMocking::invokeFailure).when(mock).canCommit(any(FutureCallback.class));
132 @SuppressWarnings("unchecked")
133 public static ShardDataTreeCohort failedPreCommit(final ShardDataTreeCohort mock) {
134 doAnswer(ShardDataTreeMocking::invokeFailure).when(mock).preCommit(any(FutureCallback.class));
138 @SuppressWarnings("unchecked")
139 public static ShardDataTreeCohort failedCommit(final ShardDataTreeCohort mock) {
140 doAnswer(ShardDataTreeMocking::invokeFailure).when(mock).commit(any(FutureCallback.class));
144 @SuppressWarnings("unchecked")
145 public static ShardDataTreeCohort successfulCanCommit(final ShardDataTreeCohort mock) {
146 doAnswer(invocation -> invokeSuccess(invocation, null)).when(mock).canCommit(any(FutureCallback.class));
151 public static ShardDataTreeCohort successfulPreCommit(final ShardDataTreeCohort mock) {
152 return successfulPreCommit(mock, mock(DataTreeCandidate.class));
155 @SuppressWarnings("unchecked")
156 public static ShardDataTreeCohort successfulPreCommit(final ShardDataTreeCohort mock,
157 final DataTreeCandidate candidate) {
158 doAnswer(invocation -> invokeSuccess(invocation, candidate)).when(mock).preCommit(any(FutureCallback.class));
163 public static ShardDataTreeCohort successfulCommit(final ShardDataTreeCohort mock) {
164 return successfulCommit(mock, UnsignedLong.ZERO);
167 @SuppressWarnings("unchecked")
168 public static ShardDataTreeCohort successfulCommit(final ShardDataTreeCohort mock, final UnsignedLong index) {
169 doAnswer(invocation -> invokeSuccess(invocation, index)).when(mock).commit(any(FutureCallback.class));
174 @SuppressWarnings("unchecked")
175 public static void assertSequencedCommit(final ShardDataTreeCohort mock) {
176 final InOrder inOrder = inOrder(mock);
177 inOrder.verify(mock).canCommit(any(FutureCallback.class));
178 inOrder.verify(mock).preCommit(any(FutureCallback.class));
179 inOrder.verify(mock).commit(any(FutureCallback.class));
182 public static void immediatePayloadReplication(final ShardDataTree shardDataTree, final Shard mockShard) {
183 doAnswer(invocation -> {
184 shardDataTree.applyReplicatedPayload(invocation.getArgument(0), invocation.getArgument(1));
186 }).when(mockShard).persistPayload(any(TransactionIdentifier.class), any(CommitTransactionPayload.class),