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.Mockito.doAnswer;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.inOrder;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.timeout;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.verifyNoMoreInteractions;
19 import com.google.common.primitives.UnsignedLong;
20 import com.google.common.util.concurrent.FutureCallback;
21 import org.mockito.InOrder;
22 import org.mockito.invocation.InvocationOnMock;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
25 public final class ShardDataTreeMocking {
27 private ShardDataTreeMocking() {
28 throw new UnsupportedOperationException();
31 @SuppressWarnings("unchecked")
32 private static <T> FutureCallback<T> mockCallback() {
33 return mock(FutureCallback.class);
36 public static ShardDataTreeCohort immediateCanCommit(final ShardDataTreeCohort cohort) {
37 final FutureCallback<Void> callback = mockCallback();
38 doNothing().when(callback).onSuccess(null);
39 cohort.canCommit(callback);
41 verify(callback).onSuccess(null);
42 verifyNoMoreInteractions(callback);
46 public static FutureCallback<Void> coordinatedCanCommit(final ShardDataTreeCohort cohort) {
47 final FutureCallback<Void> callback = mockCallback();
48 doNothing().when(callback).onSuccess(null);
49 doNothing().when(callback).onFailure(any(Throwable.class));
50 cohort.canCommit(callback);
54 public static ShardDataTreeCohort immediatePreCommit(final ShardDataTreeCohort cohort) {
55 final FutureCallback<DataTreeCandidate> callback = mockCallback();
56 doNothing().when(callback).onSuccess(any(DataTreeCandidate.class));
57 cohort.preCommit(callback);
59 verify(callback).onSuccess(any(DataTreeCandidate.class));
60 verifyNoMoreInteractions(callback);
64 public static FutureCallback<DataTreeCandidate> coordinatedPreCommit(final ShardDataTreeCohort cohort) {
65 final FutureCallback<DataTreeCandidate> callback = mockCallback();
66 doNothing().when(callback).onSuccess(any(DataTreeCandidate.class));
67 doNothing().when(callback).onFailure(any(Throwable.class));
68 cohort.preCommit(callback);
72 public static ShardDataTreeCohort immediateCommit(final ShardDataTreeCohort cohort) {
73 final FutureCallback<UnsignedLong> callback = mockCallback();
74 doNothing().when(callback).onSuccess(any(UnsignedLong.class));
75 cohort.commit(callback);
77 verify(callback, timeout(5000)).onSuccess(any(UnsignedLong.class));
78 verifyNoMoreInteractions(callback);
82 public static FutureCallback<UnsignedLong> coordinatedCommit(final ShardDataTreeCohort cohort) {
83 final FutureCallback<UnsignedLong> callback = mockCallback();
84 doNothing().when(callback).onSuccess(any(UnsignedLong.class));
85 doNothing().when(callback).onFailure(any(Throwable.class));
86 cohort.commit(callback);
90 public static FutureCallback<UnsignedLong> immediate3PhaseCommit(final ShardDataTreeCohort cohort) {
91 final FutureCallback<UnsignedLong> commitCallback = mockCallback();
92 doNothing().when(commitCallback).onSuccess(any(UnsignedLong.class));
93 doNothing().when(commitCallback).onFailure(any(Throwable.class));
95 final FutureCallback<DataTreeCandidate> preCommitCallback = mockCallback();
96 doAnswer(invocation -> {
97 cohort.commit(commitCallback);
99 }).when(preCommitCallback).onSuccess(any(DataTreeCandidate.class));
100 doNothing().when(preCommitCallback).onFailure(any(Throwable.class));
102 final FutureCallback<Void> canCommit = mockCallback();
103 doAnswer(invocation -> {
104 cohort.preCommit(preCommitCallback);
106 }).when(canCommit).onSuccess(null);
107 doNothing().when(canCommit).onFailure(any(Throwable.class));
109 cohort.canCommit(canCommit);
110 return commitCallback;
113 @SuppressWarnings("unchecked")
114 private static <T> Object invokeSuccess(final InvocationOnMock invocation, final T value) {
115 invocation.getArgumentAt(0, FutureCallback.class).onSuccess(value);
119 private static Object invokeFailure(final InvocationOnMock invocation) {
120 invocation.getArgumentAt(0, FutureCallback.class).onFailure(mock(Exception.class));
124 @SuppressWarnings("unchecked")
125 public static ShardDataTreeCohort failedCanCommit(final ShardDataTreeCohort mock) {
126 doAnswer(invocation -> {
127 return invokeFailure(invocation);
128 }).when(mock).canCommit(any(FutureCallback.class));
132 @SuppressWarnings("unchecked")
133 public static ShardDataTreeCohort failedPreCommit(final ShardDataTreeCohort mock) {
134 doAnswer(invocation -> {
135 return invokeFailure(invocation);
136 }).when(mock).preCommit(any(FutureCallback.class));
140 @SuppressWarnings("unchecked")
141 public static ShardDataTreeCohort failedCommit(final ShardDataTreeCohort mock) {
142 doAnswer(invocation -> {
143 return invokeFailure(invocation);
144 }).when(mock).commit(any(FutureCallback.class));
148 @SuppressWarnings("unchecked")
149 public static ShardDataTreeCohort successfulCanCommit(final ShardDataTreeCohort mock) {
150 doAnswer(invocation -> {
151 return invokeSuccess(invocation, null);
152 }).when(mock).canCommit(any(FutureCallback.class));
157 public static ShardDataTreeCohort successfulPreCommit(final ShardDataTreeCohort mock) {
158 return successfulPreCommit(mock, mock(DataTreeCandidate.class));
161 @SuppressWarnings("unchecked")
162 public static ShardDataTreeCohort successfulPreCommit(final ShardDataTreeCohort mock,
163 final DataTreeCandidate candidate) {
164 doAnswer(invocation -> {
165 return invokeSuccess(invocation, candidate);
166 }).when(mock).preCommit(any(FutureCallback.class));
171 public static ShardDataTreeCohort successfulCommit(final ShardDataTreeCohort mock) {
172 return successfulCommit(mock, UnsignedLong.ZERO);
175 @SuppressWarnings("unchecked")
176 public static ShardDataTreeCohort successfulCommit(final ShardDataTreeCohort mock, final UnsignedLong index) {
177 doAnswer(invocation -> {
178 return invokeSuccess(invocation, index);
179 }).when(mock).commit(any(FutureCallback.class));
184 @SuppressWarnings("unchecked")
185 public static void assertSequencedCommit(final ShardDataTreeCohort mock) {
186 final InOrder inOrder = inOrder(mock);
187 inOrder.verify(mock).canCommit(any(FutureCallback.class));
188 inOrder.verify(mock).preCommit(any(FutureCallback.class));
189 inOrder.verify(mock).commit(any(FutureCallback.class));