Update sal-distributed-datastore tests a bit
[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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.verify;
18
19 import akka.actor.ActorRef;
20 import akka.actor.ActorSelection;
21 import akka.actor.ActorSystem;
22 import com.google.common.primitives.UnsignedLong;
23 import java.util.Optional;
24 import org.junit.Test;
25 import org.mockito.Mock;
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.ActorUtils;
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(TestUtils.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(TestUtils.TRANSACTION_ID, 0L);
74         assertNotNull(snapshotProxy);
75         assertNotEquals(TestUtils.TRANSACTION_ID, snapshotProxy.getIdentifier());
76     }
77
78     @Test
79     public void testCreateTransactionProxy() {
80         AbstractProxyTransaction transactionProxy = object().createTransactionProxy(TestUtils.TRANSACTION_ID, 0L);
81         assertNotNull(transactionProxy);
82         assertNotEquals(TestUtils.TRANSACTION_ID, transactionProxy.getIdentifier());
83     }
84
85     @Test
86     public void testState() {
87         assertEquals(AbstractClientHistory.State.IDLE, object().state());
88     }
89
90     @Test
91     public void testUpdateState() {
92         object().updateState(AbstractClientHistory.State.IDLE, AbstractClientHistory.State.CLOSED);
93         assertEquals(AbstractClientHistory.State.CLOSED, object().state());
94     }
95
96     @Test
97     public void testDoClose() {
98         object().createTransactionProxy(TestUtils.TRANSACTION_ID, 0L);
99         object().doClose();
100         assertEquals(AbstractClientHistory.State.CLOSED, object().state());
101     }
102
103     @Test
104     public void testGetIdentifier() {
105         assertEquals(HISTORY_ID, object().getIdentifier());
106     }
107
108     @Test
109     public void testNextTx() {
110         assertEquals(object().nextTx() + 1, object().nextTx());
111     }
112
113     @Test
114     public void testResolveShardForPath() {
115         final Long shardForPath = object().resolveShardForPath(YangInstanceIdentifier.empty());
116         assertNotNull(shardForPath);
117         assertEquals(0L, (long) shardForPath);
118     }
119
120     @Test
121     public void testLocalAbort() {
122         object().localAbort(new Throwable());
123         assertEquals(AbstractClientHistory.State.CLOSED, object().state());
124     }
125
126     @Test
127     public void testOnProxyDestroyed() {
128         final ProxyHistory proxyHistory = mock(ProxyHistory.class);
129         doReturn(HISTORY_ID).when(proxyHistory).getIdentifier();
130
131         object().onProxyDestroyed(proxyHistory);
132         verify(proxyHistory).getIdentifier();
133     }
134
135     @Test
136     public void testCreateTransaction() {
137         final ClientTransaction transaction = object().createTransaction();
138         assertNotNull(transaction);
139     }
140
141     @Test
142     public void testTakeSnapshot() {
143         final ClientSnapshot clientSnapshot = object().takeSnapshot();
144         assertEquals(object().getIdentifier(), clientSnapshot.getIdentifier().getHistoryId());
145     }
146
147     @Test
148     public void testStartReconnect() {
149         // cookie and shard are the same
150         final Long cookie = 0L;
151         final Long shard = cookie;
152
153         final ShardBackendInfo info = new ShardBackendInfo(clientActorContext().self(), 0L, ABIVersion.current(),
154                 SHARD_NAME, UnsignedLong.ZERO, Optional.of(tree), 10);
155         final ConnectedClientConnection<ShardBackendInfo> newConn = AccessClientUtil.createConnectedConnection(
156                 clientActorContext(), cookie, info);
157         object().createSnapshotProxy(TestUtils.TRANSACTION_ID, shard);
158
159         final HistoryReconnectCohort reconnectCohort = object().startReconnect(newConn);
160         assertNotNull(reconnectCohort);
161     }
162
163     @Test
164     public void testStartReconnectMissingOldProxy() {
165         // cookie and shard are different
166         final Long cookie = 1L;
167         final Long shard = 0L;
168
169         final ShardBackendInfo info = new ShardBackendInfo(clientActorContext().self(), 0L, ABIVersion.current(),
170                 SHARD_NAME, UnsignedLong.ZERO, Optional.of(tree), 10);
171         final ConnectedClientConnection<ShardBackendInfo> newConn = AccessClientUtil.createConnectedConnection(
172                 clientActorContext(), cookie, info);
173         object().createSnapshotProxy(TestUtils.TRANSACTION_ID, shard);
174
175         final HistoryReconnectCohort reconnectCohort = object().startReconnect(newConn);
176         assertNull(reconnectCohort);
177     }
178
179     protected static ActorUtils createActorUtilsMock(final ActorSystem system, final ActorRef actor) {
180         final ActorUtils mock = mock(ActorUtils.class);
181         final Promise<PrimaryShardInfo> promise = new DefaultPromise<>();
182         final ActorSelection selection = system.actorSelection(actor.path());
183         final PrimaryShardInfo shardInfo = new PrimaryShardInfo(selection, (short) 0);
184         promise.success(shardInfo);
185         doReturn(promise.future()).when(mock).findPrimaryShardAsync(any());
186         return mock;
187     }
188 }