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 command = clientContextProbe.expectMsgClass(InternalCommand.class);
90 command.execute(client);
92 when(dataTree.takeSnapshot()).thenReturn(dataTreeSnapshot);
94 handle = createHandle(parent);
97 protected abstract T createHandle(AbstractClientHistory parent);
100 * Do a operation with handle.
101 * Used for testing, whether closed handle throws exception when the operation is performed.
103 * @param handle handle
105 protected abstract void doHandleOperation(T handle);
108 public void tearDown() throws Exception {
109 JavaTestKit.shutdownActorSystem(system);
113 public void testGetIdentifier() throws Exception {
114 Assert.assertEquals(TRANSACTION_ID, handle.getIdentifier());
118 public void testAbort() throws Exception {
119 doHandleOperation(handle);
121 final Envelope envelope = backendProbe.expectMsgClass(Envelope.class);
122 final AbortLocalTransactionRequest request = (AbortLocalTransactionRequest) envelope.getMessage();
123 Assert.assertEquals(TRANSACTION_ID, request.getTarget());
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());
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());
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);
156 public void testParent() throws Exception {
157 Assert.assertEquals(parent, handle.parent());
160 protected void checkClosed() throws Exception {
161 TestUtils.assertOperationThrowsException(() -> doHandleOperation(handle), IllegalStateException.class);
165 * Checks, whether backend actor has received request of expected class wrapped in RequestEnvelope.
166 * Then given response wrapped in ResponseEnvelope is sent.
168 * @param expectedRequestClass expected request class
169 * @param response response
170 * @param <R> expected request type
171 * @return request message
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);
190 return (R) envelope.getMessage();
193 protected T getHandle() {
197 protected DataTreeSnapshot getDataTreeSnapshot() {
198 return dataTreeSnapshot;
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());