Add AbstractTransactionCommitCohort unit tests
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / actors / dds / AbstractClientHandleTest.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.Matchers.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.HISTORY_ID;
16 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.TRANSACTION_ID;
17
18 import akka.actor.ActorRef;
19 import akka.actor.ActorSelection;
20 import akka.actor.ActorSystem;
21 import akka.testkit.JavaTestKit;
22 import akka.testkit.TestProbe;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.function.Function;
26 import org.junit.After;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.opendaylight.controller.cluster.access.client.AbstractClientConnection;
33 import org.opendaylight.controller.cluster.access.client.AccessClientUtil;
34 import org.opendaylight.controller.cluster.access.client.ClientActorContext;
35 import org.opendaylight.controller.cluster.access.client.InternalCommand;
36 import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest;
37 import org.opendaylight.controller.cluster.access.commands.ConnectClientRequest;
38 import org.opendaylight.controller.cluster.access.commands.ConnectClientSuccess;
39 import org.opendaylight.controller.cluster.access.concepts.Envelope;
40 import org.opendaylight.controller.cluster.access.concepts.FailureEnvelope;
41 import org.opendaylight.controller.cluster.access.concepts.Request;
42 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
43 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
44 import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
45 import org.opendaylight.controller.cluster.access.concepts.Response;
46 import org.opendaylight.controller.cluster.access.concepts.SuccessEnvelope;
47 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
48 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
51 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
52 import scala.concurrent.Promise;
53
54 public abstract class AbstractClientHandleTest<T extends AbstractClientHandle<AbstractProxyTransaction>> {
55
56     private static final String PERSISTENCE_ID = "per-1";
57     private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.EMPTY;
58
59     @Mock
60     private DataTree dataTree;
61     @Mock
62     private DataTreeSnapshot dataTreeSnapshot;
63     private ActorSystem system;
64     private TestProbe backendProbe;
65     private AbstractClientHistory parent;
66     private AbstractDataStoreClientBehavior client;
67     private T handle;
68
69     @Before
70     public void setUp() throws Exception {
71         MockitoAnnotations.initMocks(this);
72         system = ActorSystem.apply();
73         final TestProbe contextProbe = new TestProbe(system, "context");
74         final TestProbe clientContextProbe = new TestProbe(system, "client-context");
75         backendProbe = new TestProbe(system, "backend");
76         //create handle dependencies
77         final ActorContext actorContext = createActorContextMock(system, contextProbe.ref());
78         final ClientActorContext clientContext =
79                 AccessClientUtil.createClientActorContext(system, clientContextProbe.ref(), CLIENT_ID, PERSISTENCE_ID);
80         client = new SimpleDataStoreClientBehavior(clientContext, actorContext, "shard");
81         client.createLocalHistory();
82         parent = new SingleClientHistory(client, HISTORY_ID);
83         //connect client
84         client.getConnection(0L);
85         contextProbe.expectMsgClass(ConnectClientRequest.class);
86         final long sequence = 0L;
87         contextProbe.reply(new ConnectClientSuccess(CLIENT_ID, sequence, backendProbe.ref(),
88                 Collections.emptyList(), dataTree, 3));
89         final InternalCommand command = clientContextProbe.expectMsgClass(InternalCommand.class);
90         command.execute(client);
91         //data tree mock
92         when(dataTree.takeSnapshot()).thenReturn(dataTreeSnapshot);
93
94         handle = createHandle(parent);
95     }
96
97     protected abstract T createHandle(AbstractClientHistory parent);
98
99     /**
100      * Do a operation with handle.
101      * Used for testing, whether closed handle throws exception when the operation is performed.
102      *
103      * @param handle handle
104      */
105     protected abstract void doHandleOperation(T handle);
106
107     @After
108     public void tearDown() throws Exception {
109         JavaTestKit.shutdownActorSystem(system);
110     }
111
112     @Test
113     public void testGetIdentifier() throws Exception {
114         Assert.assertEquals(TRANSACTION_ID, handle.getIdentifier());
115     }
116
117     @Test
118     public void testAbort() throws Exception {
119         doHandleOperation(handle);
120         handle.abort();
121         final Envelope envelope = backendProbe.expectMsgClass(Envelope.class);
122         final AbortLocalTransactionRequest request = (AbortLocalTransactionRequest) envelope.getMessage();
123         Assert.assertEquals(TRANSACTION_ID, request.getTarget());
124         checkClosed();
125     }
126
127     @Test
128     public void testLocalAbort() throws Exception {
129         doHandleOperation(handle);
130         handle.localAbort(new RuntimeException("fail"));
131         final Envelope envelope = backendProbe.expectMsgClass(Envelope.class);
132         final AbortLocalTransactionRequest request = (AbortLocalTransactionRequest) envelope.getMessage();
133         Assert.assertEquals(TRANSACTION_ID, request.getTarget());
134         checkClosed();
135     }
136
137     @Test
138     public void testEnsureClosed() throws Exception {
139         doHandleOperation(handle);
140         final Collection<AbstractProxyTransaction> transactions = handle.ensureClosed();
141         Assert.assertNotNull(transactions);
142         Assert.assertEquals(1, transactions.size());
143     }
144
145     @Test
146     public void testEnsureProxy() throws Exception {
147         final Function<Long, AbstractProxyTransaction> function = mock(Function.class);
148         final AbstractProxyTransaction expected = mock(AbstractProxyTransaction.class);
149         when(function.apply(0L)).thenReturn(expected);
150         final AbstractProxyTransaction proxy = handle.ensureProxy(PATH, function);
151         verify(function).apply(0L);
152         Assert.assertEquals(expected, proxy);
153     }
154
155     @Test
156     public void testParent() throws Exception {
157         Assert.assertEquals(parent, handle.parent());
158     }
159
160     protected void checkClosed() throws Exception {
161         TestUtils.assertOperationThrowsException(() -> doHandleOperation(handle), IllegalStateException.class);
162     }
163
164     /**
165      * Checks, whether backend actor has received request of expected class wrapped in RequestEnvelope.
166      * Then given response wrapped in ResponseEnvelope is sent.
167      *
168      * @param expectedRequestClass expected request class
169      * @param response             response
170      * @param <R>                  expected request type
171      * @return request message
172      */
173     protected <R extends Request> R backendRespondToRequest(final Class<R> expectedRequestClass,
174                                                             final Response response) {
175         final RequestEnvelope envelope = backendProbe.expectMsgClass(RequestEnvelope.class);
176         Assert.assertEquals(expectedRequestClass, envelope.getMessage().getClass());
177         final AbstractClientConnection<ShardBackendInfo> connection = client.getConnection(0L);
178         final long sessionId = envelope.getSessionId();
179         final long txSequence = envelope.getTxSequence();
180         final long executionTime = 0L;
181         if (response instanceof RequestSuccess) {
182             final RequestSuccess<?, ?> success = (RequestSuccess<?, ?>) response;
183             final SuccessEnvelope responseEnvelope = new SuccessEnvelope(success, sessionId, txSequence, executionTime);
184             AccessClientUtil.completeRequest(connection, responseEnvelope);
185         } else if (response instanceof RequestFailure) {
186             final RequestFailure<?, ?> fail = (RequestFailure<?, ?>) response;
187             final FailureEnvelope responseEnvelope = new FailureEnvelope(fail, sessionId, txSequence, executionTime);
188             AccessClientUtil.completeRequest(connection, responseEnvelope);
189         }
190         return (R) envelope.getMessage();
191     }
192
193     protected T getHandle() {
194         return handle;
195     }
196
197     protected DataTreeSnapshot getDataTreeSnapshot() {
198         return dataTreeSnapshot;
199     }
200
201     private static ActorContext createActorContextMock(final ActorSystem system, final ActorRef actor) {
202         final ActorContext mock = mock(ActorContext.class);
203         final Promise<PrimaryShardInfo> promise = new scala.concurrent.impl.Promise.DefaultPromise<>();
204         final ActorSelection selection = system.actorSelection(actor.path());
205         final PrimaryShardInfo shardInfo = new PrimaryShardInfo(selection, (short) 0);
206         promise.success(shardInfo);
207         when(mock.findPrimaryShardAsync(any())).thenReturn(promise.future());
208         return mock;
209     }
210
211 }

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