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.Mockito.mock;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
13 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID;
15 import akka.actor.ActorRef;
16 import akka.actor.ActorSelection;
17 import akka.actor.ActorSystem;
18 import akka.actor.Status;
19 import akka.testkit.JavaTestKit;
20 import akka.testkit.TestProbe;
21 import java.util.Collections;
22 import org.junit.After;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.cluster.access.client.AbstractClientConnection;
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.InternalCommand;
30 import org.opendaylight.controller.cluster.access.commands.ConnectClientRequest;
31 import org.opendaylight.controller.cluster.access.commands.ConnectClientSuccess;
32 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
33 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeModification;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
38 import scala.concurrent.Promise;
40 public abstract class AbstractDataStoreClientBehaviorTest {
42 protected static final String SHARD = "default";
43 private static final String PERSISTENCE_ID = "per-1";
45 private ActorSystem system;
46 private ClientActorContext clientContext;
47 private TestProbe clientActorProbe;
48 private TestProbe actorContextProbe;
49 private AbstractDataStoreClientBehavior behavior;
52 public void setUp() throws Exception {
53 system = ActorSystem.apply();
54 clientActorProbe = new TestProbe(system, "client");
55 actorContextProbe = new TestProbe(system, "actor-context");
56 final ActorContext context = createActorContextMock(system, actorContextProbe.ref());
58 AccessClientUtil.createClientActorContext(system, clientActorProbe.ref(), CLIENT_ID, PERSISTENCE_ID);
59 behavior = createBehavior(clientContext, context);
62 @SuppressWarnings("checkstyle:hiddenField")
63 protected abstract AbstractDataStoreClientBehavior createBehavior(ClientActorContext clientContext,
64 ActorContext context);
67 public void tearDown() throws Exception {
68 JavaTestKit.shutdownActorSystem(system);
72 public void testResolveShardForPath() throws Exception {
73 Assert.assertEquals(0L, behavior.resolveShardForPath(YangInstanceIdentifier.EMPTY).longValue());
77 public void testHaltClient() throws Exception {
78 behavior.haltClient(new RuntimeException());
82 public void testOnCommand() throws Exception {
83 final TestProbe probe = new TestProbe(system);
84 final GetClientRequest request = new GetClientRequest(probe.ref());
85 final AbstractDataStoreClientBehavior nextBehavior = behavior.onCommand(request);
86 final Status.Success success = probe.expectMsgClass(Status.Success.class);
87 Assert.assertEquals(behavior, success.status());
88 Assert.assertSame(behavior, nextBehavior);
92 public void testOnCommandUnhandled() throws Exception {
93 final AbstractDataStoreClientBehavior nextBehavior = behavior.onCommand("unhandled");
94 Assert.assertSame(behavior, nextBehavior);
98 public void testCreateLocalHistory() throws Exception {
99 final ClientLocalHistory history = behavior.createLocalHistory();
100 Assert.assertEquals(behavior.getIdentifier(), history.getIdentifier().getClientId());
104 public void testCreateTransaction() throws Exception {
105 final ClientTransaction transaction = behavior.createTransaction();
106 Assert.assertEquals(behavior.getIdentifier(), transaction.getIdentifier().getHistoryId().getClientId());
110 public void testCreateSnapshot() throws Exception {
111 final ClientSnapshot snapshot = behavior.createSnapshot();
112 Assert.assertEquals(behavior.getIdentifier(), snapshot.getIdentifier().getHistoryId().getClientId());
116 public void testClose() throws Exception {
118 final InternalCommand<ShardBackendInfo> internalCommand =
119 clientActorProbe.expectMsgClass(InternalCommand.class);
120 internalCommand.execute(behavior);
122 behavior.createLocalHistory();
123 Assert.fail("Behavior is closed and shouldn't allow to create new history.");
124 } catch (final IllegalStateException e) {
130 public void testGetIdentifier() throws Exception {
131 Assert.assertEquals(CLIENT_ID, behavior.getIdentifier());
135 public void testGetConnection() throws Exception {
136 //set up data tree mock
137 final CursorAwareDataTreeModification modification = mock(CursorAwareDataTreeModification.class);
138 when(modification.readNode(YangInstanceIdentifier.EMPTY)).thenReturn(com.google.common.base.Optional.absent());
139 final DataTreeSnapshot snapshot = mock(DataTreeSnapshot.class);
140 when(snapshot.newModification()).thenReturn(modification);
141 final DataTree dataTree = mock(DataTree.class);
142 when(dataTree.takeSnapshot()).thenReturn(snapshot);
144 final TestProbe backendProbe = new TestProbe(system, "backend");
145 final long shard = 0L;
146 behavior.createTransaction().read(YangInstanceIdentifier.EMPTY);
147 final AbstractClientConnection<ShardBackendInfo> connection = behavior.getConnection(shard);
148 //check cached connection for same shard
149 Assert.assertSame(connection, behavior.getConnection(shard));
151 final ConnectClientRequest connectClientRequest = actorContextProbe.expectMsgClass(ConnectClientRequest.class);
152 Assert.assertEquals(CLIENT_ID, connectClientRequest.getTarget());
153 final long sequence = 0L;
154 Assert.assertEquals(sequence, connectClientRequest.getSequence());
155 actorContextProbe.reply(new ConnectClientSuccess(CLIENT_ID, sequence, backendProbe.ref(),
156 Collections.emptyList(), dataTree, 3));
157 Assert.assertEquals(clientActorProbe.ref(), connection.localActor());
158 //capture and execute command passed to client context
159 final InternalCommand<ShardBackendInfo> command = clientActorProbe.expectMsgClass(InternalCommand.class);
160 command.execute(behavior);
161 //check, whether command was reaplayed
162 verify(modification).readNode(YangInstanceIdentifier.EMPTY);
165 private static ActorContext createActorContextMock(final ActorSystem system, final ActorRef actor) {
166 final ActorContext mock = mock(ActorContext.class);
167 final Promise<PrimaryShardInfo> promise = new scala.concurrent.impl.Promise.DefaultPromise<>();
168 final ActorSelection selection = system.actorSelection(actor.path());
169 final PrimaryShardInfo shardInfo = new PrimaryShardInfo(selection, (short) 0);
170 promise.success(shardInfo);
171 when(mock.findPrimaryShardAsync(SHARD)).thenReturn(promise.future());