BUG-7033: Implement batchHint in ShardDataTree
[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.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;
18
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;
24
25 public final class ShardDataTreeMocking {
26
27     private ShardDataTreeMocking() {
28         throw new UnsupportedOperationException();
29     }
30
31     @SuppressWarnings("unchecked")
32     private static <T> FutureCallback<T> mockCallback() {
33         return mock(FutureCallback.class);
34     }
35
36     public static ShardDataTreeCohort immediateCanCommit(final ShardDataTreeCohort cohort) {
37         final FutureCallback<Void> callback = mockCallback();
38         doNothing().when(callback).onSuccess(null);
39         cohort.canCommit(callback);
40
41         verify(callback).onSuccess(null);
42         verifyNoMoreInteractions(callback);
43         return cohort;
44     }
45
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);
51         return callback;
52     }
53
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);
58
59         verify(callback).onSuccess(any(DataTreeCandidate.class));
60         verifyNoMoreInteractions(callback);
61         return cohort;
62     }
63
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);
69         return callback;
70     }
71
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);
76
77         verify(callback, timeout(5000)).onSuccess(any(UnsignedLong.class));
78         verifyNoMoreInteractions(callback);
79         return cohort;
80     }
81
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);
87         return callback;
88     }
89
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));
94
95         final FutureCallback<DataTreeCandidate> preCommitCallback = mockCallback();
96         doAnswer(invocation -> {
97             cohort.commit(commitCallback);
98             return null;
99         }).when(preCommitCallback).onSuccess(any(DataTreeCandidate.class));
100         doNothing().when(preCommitCallback).onFailure(any(Throwable.class));
101
102         final FutureCallback<Void> canCommit = mockCallback();
103         doAnswer(invocation -> {
104             cohort.preCommit(preCommitCallback);
105             return null;
106         }).when(canCommit).onSuccess(null);
107         doNothing().when(canCommit).onFailure(any(Throwable.class));
108
109         cohort.canCommit(canCommit);
110         return commitCallback;
111     }
112
113     @SuppressWarnings("unchecked")
114     private static <T> Object invokeSuccess(final InvocationOnMock invocation, final T value) {
115         invocation.getArgumentAt(0, FutureCallback.class).onSuccess(value);
116         return null;
117     }
118
119     private static Object invokeFailure(final InvocationOnMock invocation) {
120         invocation.getArgumentAt(0, FutureCallback.class).onFailure(mock(Exception.class));
121         return null;
122     }
123
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));
129         return mock;
130     }
131
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));
137         return mock;
138     }
139
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));
145         return mock;
146     }
147
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));
153
154         return mock;
155     }
156
157     public static ShardDataTreeCohort successfulPreCommit(final ShardDataTreeCohort mock) {
158         return successfulPreCommit(mock, mock(DataTreeCandidate.class));
159     }
160
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));
167
168         return mock;
169     }
170
171     public static ShardDataTreeCohort successfulCommit(final ShardDataTreeCohort mock) {
172         return successfulCommit(mock, UnsignedLong.ZERO);
173     }
174
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));
180
181         return mock;
182     }
183
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));
190     }
191 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.