Convert DatastoreSnapshotRestore to OSGi DS
[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.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;
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.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
27
28 public final class ShardDataTreeMocking {
29
30     private ShardDataTreeMocking() {
31         throw new UnsupportedOperationException();
32     }
33
34     @SuppressWarnings("unchecked")
35     private static <T> FutureCallback<T> mockCallback() {
36         return mock(FutureCallback.class);
37     }
38
39     public static ShardDataTreeCohort immediateCanCommit(final ShardDataTreeCohort cohort) {
40         final FutureCallback<Void> callback = mockCallback();
41         doNothing().when(callback).onSuccess(null);
42         cohort.canCommit(callback);
43
44         verify(callback).onSuccess(null);
45         verifyNoMoreInteractions(callback);
46         return cohort;
47     }
48
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);
54         return callback;
55     }
56
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);
61
62         verify(callback).onSuccess(any(DataTreeCandidate.class));
63         verifyNoMoreInteractions(callback);
64         return cohort;
65     }
66
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);
72         return callback;
73     }
74
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);
79
80         verify(callback, timeout(5000)).onSuccess(any(UnsignedLong.class));
81         verifyNoMoreInteractions(callback);
82         return cohort;
83     }
84
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);
90         return callback;
91     }
92
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));
97
98         final FutureCallback<DataTreeCandidate> preCommitCallback = mockCallback();
99         doAnswer(invocation -> {
100             cohort.commit(commitCallback);
101             return null;
102         }).when(preCommitCallback).onSuccess(any(DataTreeCandidate.class));
103         doNothing().when(preCommitCallback).onFailure(any(Throwable.class));
104
105         final FutureCallback<Void> canCommit = mockCallback();
106         doAnswer(invocation -> {
107             cohort.preCommit(preCommitCallback);
108             return null;
109         }).when(canCommit).onSuccess(null);
110         doNothing().when(canCommit).onFailure(any(Throwable.class));
111
112         cohort.canCommit(canCommit);
113         return commitCallback;
114     }
115
116     private static <T> Object invokeSuccess(final InvocationOnMock invocation, final T value) {
117         invocation.<FutureCallback<T>>getArgument(0).onSuccess(value);
118         return null;
119     }
120
121     private static Object invokeFailure(final InvocationOnMock invocation) {
122         invocation.<FutureCallback<?>>getArgument(0).onFailure(mock(Exception.class));
123         return null;
124     }
125
126     @SuppressWarnings("unchecked")
127     public static ShardDataTreeCohort failedCanCommit(final ShardDataTreeCohort mock) {
128         doAnswer(ShardDataTreeMocking::invokeFailure).when(mock).canCommit(any(FutureCallback.class));
129         return mock;
130     }
131
132     @SuppressWarnings("unchecked")
133     public static ShardDataTreeCohort failedPreCommit(final ShardDataTreeCohort mock) {
134         doAnswer(ShardDataTreeMocking::invokeFailure).when(mock).preCommit(any(FutureCallback.class));
135         return mock;
136     }
137
138     @SuppressWarnings("unchecked")
139     public static ShardDataTreeCohort failedCommit(final ShardDataTreeCohort mock) {
140         doAnswer(ShardDataTreeMocking::invokeFailure).when(mock).commit(any(FutureCallback.class));
141         return mock;
142     }
143
144     @SuppressWarnings("unchecked")
145     public static ShardDataTreeCohort successfulCanCommit(final ShardDataTreeCohort mock) {
146         doAnswer(invocation -> invokeSuccess(invocation, null)).when(mock).canCommit(any(FutureCallback.class));
147
148         return mock;
149     }
150
151     public static ShardDataTreeCohort successfulPreCommit(final ShardDataTreeCohort mock) {
152         return successfulPreCommit(mock, mock(DataTreeCandidate.class));
153     }
154
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));
159
160         return mock;
161     }
162
163     public static ShardDataTreeCohort successfulCommit(final ShardDataTreeCohort mock) {
164         return successfulCommit(mock, UnsignedLong.ZERO);
165     }
166
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));
170
171         return mock;
172     }
173
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));
180     }
181
182     public static void immediatePayloadReplication(final ShardDataTree shardDataTree, final Shard mockShard) {
183         doAnswer(invocation -> {
184             shardDataTree.applyReplicatedPayload(invocation.getArgument(0), invocation.getArgument(1));
185             return null;
186         }).when(mockShard).persistPayload(any(TransactionIdentifier.class), any(CommitTransactionPayload.class),
187                 anyBoolean());
188     }
189 }