Eliminate use of deprecated mockito methods
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / actors / dds / AbstractClientHistoryTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.databroker.actors.dds;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID;
15 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.TRANSACTION_ID;
16
17 import akka.actor.ActorRef;
18 import akka.actor.ActorSelection;
19 import akka.actor.ActorSystem;
20 import com.google.common.primitives.UnsignedLong;
21 import java.util.Optional;
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.opendaylight.controller.cluster.access.ABIVersion;
27 import org.opendaylight.controller.cluster.access.client.AccessClientUtil;
28 import org.opendaylight.controller.cluster.access.client.ClientActorContext;
29 import org.opendaylight.controller.cluster.access.client.ConnectedClientConnection;
30 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
31 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
32 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
35 import scala.concurrent.Promise;
36 import scala.concurrent.impl.Promise.DefaultPromise;
37
38 public abstract class AbstractClientHistoryTest<T extends AbstractClientHistory> {
39     protected static final String SHARD_NAME = "default";
40     protected static final String PERSISTENCE_ID = "per-1";
41     protected static final LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_ID, 1L);
42
43     @Mock
44     private DataTree tree;
45
46     protected abstract T object();
47
48     protected abstract ClientActorContext clientActorContext();
49
50     @Test
51     public abstract void testDoCreateSnapshot();
52
53     @Test
54     public abstract void testDoCreateTransaction();
55
56     @Test
57     public abstract void testCreateHistoryProxy();
58
59     @Test
60     public abstract void testOnTransactionComplete();
61
62     @Test
63     public abstract void testOnTransactionAbort();
64
65     @Test
66     public abstract void testOnTransactionReady();
67
68     @Test
69     public abstract void testOnTransactionReadyDuplicate();
70
71     @Test
72     public void testCreateSnapshotProxy() {
73         final AbstractProxyTransaction snapshotProxy = object().createSnapshotProxy(TRANSACTION_ID, 0L);
74         Assert.assertNotNull(snapshotProxy);
75         Assert.assertNotEquals(TRANSACTION_ID, snapshotProxy.getIdentifier());
76     }
77
78     @Test
79     public void testCreateTransactionProxy() {
80         AbstractProxyTransaction transactionProxy = object().createTransactionProxy(TRANSACTION_ID, 0L);
81         Assert.assertNotNull(transactionProxy);
82         Assert.assertNotEquals(TRANSACTION_ID, transactionProxy.getIdentifier());
83     }
84
85     @Test
86     public void testState() {
87         Assert.assertEquals(AbstractClientHistory.State.IDLE, object().state());
88     }
89
90     @Test
91     public void testUpdateState() {
92         object().updateState(AbstractClientHistory.State.IDLE, AbstractClientHistory.State.CLOSED);
93         Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state());
94     }
95
96     @Test
97     public void testDoClose() {
98         object().createTransactionProxy(TRANSACTION_ID, 0L);
99         object().doClose();
100         Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state());
101     }
102
103     @Test
104     public void testGetIdentifier() {
105         Assert.assertEquals(HISTORY_ID, object().getIdentifier());
106     }
107
108     @Test
109     public void testNextTx() {
110         Assert.assertTrue(object().nextTx() + 1 == object().nextTx());
111     }
112
113     @Test
114     public void testResolveShardForPath() {
115         final Long shardForPath = object().resolveShardForPath(YangInstanceIdentifier.EMPTY);
116         Assert.assertEquals(0L, shardForPath.longValue());
117     }
118
119     @Test
120     public void testLocalAbort() {
121         object().localAbort(new Throwable());
122         Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state());
123     }
124
125     @Test
126     public void testOnProxyDestroyed() {
127         final ProxyHistory proxyHistory = Mockito.mock(ProxyHistory.class);
128         when(proxyHistory.getIdentifier()).thenReturn(HISTORY_ID);
129
130         object().onProxyDestroyed(proxyHistory);
131         verify(proxyHistory).getIdentifier();
132     }
133
134     @Test
135     public void testCreateTransaction() {
136         final ClientTransaction transaction = object().createTransaction();
137         Assert.assertNotNull(transaction);
138     }
139
140     @Test
141     public void testTakeSnapshot() {
142         final ClientSnapshot clientSnapshot = object().takeSnapshot();
143         Assert.assertEquals(object().getIdentifier(), clientSnapshot.getIdentifier().getHistoryId());
144     }
145
146     @Test
147     public void testStartReconnect() {
148         // cookie and shard are the same
149         final Long cookie = 0L;
150         final Long shard = cookie;
151
152         final ShardBackendInfo info = new ShardBackendInfo(clientActorContext().self(), 0L, ABIVersion.current(),
153                 SHARD_NAME, UnsignedLong.ZERO, Optional.of(tree), 10);
154         final ConnectedClientConnection<ShardBackendInfo> newConn = AccessClientUtil.createConnectedConnection(
155                 clientActorContext(), cookie, info);
156         object().createSnapshotProxy(TRANSACTION_ID, shard);
157
158         final HistoryReconnectCohort reconnectCohort = object().startReconnect(newConn);
159         Assert.assertNotNull(reconnectCohort);
160     }
161
162     @Test
163     public void testStartReconnectMissingOldProxy() {
164         // cookie and shard are different
165         final Long cookie = 1L;
166         final Long shard = 0L;
167
168         final ShardBackendInfo info = new ShardBackendInfo(clientActorContext().self(), 0L, ABIVersion.current(),
169                 SHARD_NAME, UnsignedLong.ZERO, Optional.of(tree), 10);
170         final ConnectedClientConnection<ShardBackendInfo> newConn = AccessClientUtil.createConnectedConnection(
171                 clientActorContext(), cookie, info);
172         object().createSnapshotProxy(TRANSACTION_ID, shard);
173
174         final HistoryReconnectCohort reconnectCohort = object().startReconnect(newConn);
175         Assert.assertNull(reconnectCohort);
176     }
177
178     protected static ActorContext createActorContextMock(final ActorSystem system, final ActorRef actor) {
179         final ActorContext mock = mock(ActorContext.class);
180         final Promise<PrimaryShardInfo> promise = new DefaultPromise<>();
181         final ActorSelection selection = system.actorSelection(actor.path());
182         final PrimaryShardInfo shardInfo = new PrimaryShardInfo(selection, (short) 0);
183         promise.success(shardInfo);
184         when(mock.findPrimaryShardAsync(any())).thenReturn(promise.future());
185         return mock;
186     }
187 }

©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.