24ab054124750659049d55c17bae0dd3b43f3152
[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.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.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.TestProbe;
22 import akka.testkit.javadsl.TestKit;
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<ShardBackendInfo> 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     @SuppressWarnings("checkstyle:hiddenField")
98     protected abstract T createHandle(AbstractClientHistory parent);
99
100     /**
101      * Do a operation with handle.
102      * Used for testing, whether closed handle throws exception when the operation is performed.
103      *
104      * @param handle handle
105      */
106     @SuppressWarnings("checkstyle:hiddenField")
107     protected abstract void doHandleOperation(T handle);
108
109     @After
110     public void tearDown() {
111         TestKit.shutdownActorSystem(system);
112     }
113
114     @Test
115     public void testGetIdentifier() {
116         Assert.assertEquals(TRANSACTION_ID, handle.getIdentifier());
117     }
118
119     @Test
120     public void testAbort() throws Exception {
121         doHandleOperation(handle);
122         handle.abort();
123         final Envelope<?> envelope = backendProbe.expectMsgClass(Envelope.class);
124         final AbortLocalTransactionRequest request = (AbortLocalTransactionRequest) envelope.getMessage();
125         Assert.assertEquals(TRANSACTION_ID, request.getTarget());
126         checkClosed();
127     }
128
129     @Test
130     public void testLocalAbort() throws Exception {
131         doHandleOperation(handle);
132         handle.localAbort(new RuntimeException("fail"));
133         final Envelope<?> envelope = backendProbe.expectMsgClass(Envelope.class);
134         final AbortLocalTransactionRequest request = (AbortLocalTransactionRequest) envelope.getMessage();
135         Assert.assertEquals(TRANSACTION_ID, request.getTarget());
136         checkClosed();
137     }
138
139     @Test
140     public void testEnsureClosed() {
141         doHandleOperation(handle);
142         final Collection<AbstractProxyTransaction> transactions = handle.ensureClosed();
143         Assert.assertNotNull(transactions);
144         Assert.assertEquals(1, transactions.size());
145     }
146
147     @Test
148     public void testEnsureProxy() {
149         final Function<Long, AbstractProxyTransaction> function = mock(Function.class);
150         final AbstractProxyTransaction expected = mock(AbstractProxyTransaction.class);
151         when(function.apply(0L)).thenReturn(expected);
152         final AbstractProxyTransaction proxy = handle.ensureProxy(PATH, function);
153         verify(function).apply(0L);
154         Assert.assertEquals(expected, proxy);
155     }
156
157     @Test
158     public void testParent() {
159         Assert.assertEquals(parent, handle.parent());
160     }
161
162     protected void checkClosed() throws Exception {
163         TestUtils.assertOperationThrowsException(() -> doHandleOperation(handle), IllegalStateException.class);
164     }
165
166     /**
167      * Checks, whether backend actor has received request of expected class wrapped in RequestEnvelope.
168      * Then given response wrapped in ResponseEnvelope is sent.
169      *
170      * @param expectedRequestClass expected request class
171      * @param response             response
172      * @param <R>                  expected request type
173      * @return request message
174      */
175     protected <R extends Request<?, R>> R backendRespondToRequest(final Class<R> expectedRequestClass,
176                                                             final Response<?, ?> response) {
177         final RequestEnvelope envelope = backendProbe.expectMsgClass(RequestEnvelope.class);
178         Assert.assertEquals(expectedRequestClass, envelope.getMessage().getClass());
179         final AbstractClientConnection<ShardBackendInfo> connection = client.getConnection(0L);
180         final long sessionId = envelope.getSessionId();
181         final long txSequence = envelope.getTxSequence();
182         final long executionTime = 0L;
183         if (response instanceof RequestSuccess) {
184             final RequestSuccess<?, ?> success = (RequestSuccess<?, ?>) response;
185             final SuccessEnvelope responseEnvelope = new SuccessEnvelope(success, sessionId, txSequence, executionTime);
186             AccessClientUtil.completeRequest(connection, responseEnvelope);
187         } else if (response instanceof RequestFailure) {
188             final RequestFailure<?, ?> fail = (RequestFailure<?, ?>) response;
189             final FailureEnvelope responseEnvelope = new FailureEnvelope(fail, sessionId, txSequence, executionTime);
190             AccessClientUtil.completeRequest(connection, responseEnvelope);
191         }
192         return expectedRequestClass.cast(envelope.getMessage());
193     }
194
195     protected T getHandle() {
196         return handle;
197     }
198
199     protected DataTreeSnapshot getDataTreeSnapshot() {
200         return dataTreeSnapshot;
201     }
202
203     private static ActorContext createActorContextMock(final ActorSystem system, final ActorRef actor) {
204         final ActorContext mock = mock(ActorContext.class);
205         final Promise<PrimaryShardInfo> promise = new scala.concurrent.impl.Promise.DefaultPromise<>();
206         final ActorSelection selection = system.actorSelection(actor.path());
207         final PrimaryShardInfo shardInfo = new PrimaryShardInfo(selection, (short) 0);
208         promise.success(shardInfo);
209         when(mock.findPrimaryShardAsync(any())).thenReturn(promise.future());
210         return mock;
211     }
212
213 }