2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.databroker.actors.dds;
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;
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;
54 public abstract class AbstractClientHandleTest<T extends AbstractClientHandle<AbstractProxyTransaction>> {
56 private static final String PERSISTENCE_ID = "per-1";
57 private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.EMPTY;
60 private DataTree dataTree;
62 private DataTreeSnapshot dataTreeSnapshot;
63 private ActorSystem system;
64 private TestProbe backendProbe;
65 private AbstractClientHistory parent;
66 private AbstractDataStoreClientBehavior client;
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);
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);
92 when(dataTree.takeSnapshot()).thenReturn(dataTreeSnapshot);
94 handle = createHandle(parent);
97 @SuppressWarnings("checkstyle:hiddenField")
98 protected abstract T createHandle(AbstractClientHistory parent);
101 * Do a operation with handle.
102 * Used for testing, whether closed handle throws exception when the operation is performed.
104 * @param handle handle
106 @SuppressWarnings("checkstyle:hiddenField")
107 protected abstract void doHandleOperation(T handle);
110 public void tearDown() throws Exception {
111 JavaTestKit.shutdownActorSystem(system);
115 public void testGetIdentifier() throws Exception {
116 Assert.assertEquals(TRANSACTION_ID, handle.getIdentifier());
120 public void testAbort() throws Exception {
121 doHandleOperation(handle);
123 final Envelope<?> envelope = backendProbe.expectMsgClass(Envelope.class);
124 final AbortLocalTransactionRequest request = (AbortLocalTransactionRequest) envelope.getMessage();
125 Assert.assertEquals(TRANSACTION_ID, request.getTarget());
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());
140 public void testEnsureClosed() throws Exception {
141 doHandleOperation(handle);
142 final Collection<AbstractProxyTransaction> transactions = handle.ensureClosed();
143 Assert.assertNotNull(transactions);
144 Assert.assertEquals(1, transactions.size());
148 public void testEnsureProxy() throws Exception {
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);
158 public void testParent() throws Exception {
159 Assert.assertEquals(parent, handle.parent());
162 protected void checkClosed() throws Exception {
163 TestUtils.assertOperationThrowsException(() -> doHandleOperation(handle), IllegalStateException.class);
167 * Checks, whether backend actor has received request of expected class wrapped in RequestEnvelope.
168 * Then given response wrapped in ResponseEnvelope is sent.
170 * @param expectedRequestClass expected request class
171 * @param response response
172 * @param <R> expected request type
173 * @return request message
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);
192 return expectedRequestClass.cast(envelope.getMessage());
195 protected T getHandle() {
199 protected DataTreeSnapshot getDataTreeSnapshot() {
200 return dataTreeSnapshot;
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());