1961792a96961b6279fceae91748a92811639b2c
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / actors / dds / AbstractDataStoreClientBehaviorTest.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.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;
14
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;
39
40 public abstract class AbstractDataStoreClientBehaviorTest {
41
42     protected static final String SHARD = "default";
43     private static final String PERSISTENCE_ID = "per-1";
44
45     private ActorSystem system;
46     private ClientActorContext clientContext;
47     private TestProbe clientActorProbe;
48     private TestProbe actorContextProbe;
49     private AbstractDataStoreClientBehavior behavior;
50
51     @Before
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());
57         clientContext =
58                 AccessClientUtil.createClientActorContext(system, clientActorProbe.ref(), CLIENT_ID, PERSISTENCE_ID);
59         behavior = createBehavior(clientContext, context);
60     }
61
62     @SuppressWarnings("checkstyle:hiddenField")
63     protected abstract AbstractDataStoreClientBehavior createBehavior(ClientActorContext clientContext,
64                                                                       ActorContext context);
65
66     @After
67     public void tearDown() throws Exception {
68         JavaTestKit.shutdownActorSystem(system);
69     }
70
71     @Test
72     public void testResolveShardForPath() throws Exception {
73         Assert.assertEquals(0L, behavior.resolveShardForPath(YangInstanceIdentifier.EMPTY).longValue());
74     }
75
76     @Test
77     public void testHaltClient() throws Exception {
78         behavior.haltClient(new RuntimeException());
79     }
80
81     @Test
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);
89     }
90
91     @Test
92     public void testOnCommandUnhandled() throws Exception {
93         final AbstractDataStoreClientBehavior nextBehavior = behavior.onCommand("unhandled");
94         Assert.assertSame(behavior, nextBehavior);
95     }
96
97     @Test
98     public void testCreateLocalHistory() throws Exception {
99         final ClientLocalHistory history = behavior.createLocalHistory();
100         Assert.assertEquals(behavior.getIdentifier(), history.getIdentifier().getClientId());
101     }
102
103     @Test
104     public void testCreateTransaction() throws Exception {
105         final ClientTransaction transaction = behavior.createTransaction();
106         Assert.assertEquals(behavior.getIdentifier(), transaction.getIdentifier().getHistoryId().getClientId());
107     }
108
109     @Test
110     public void testCreateSnapshot() throws Exception {
111         final ClientSnapshot snapshot = behavior.createSnapshot();
112         Assert.assertEquals(behavior.getIdentifier(), snapshot.getIdentifier().getHistoryId().getClientId());
113     }
114
115     @Test
116     public void testClose() throws Exception {
117         behavior.close();
118         final InternalCommand<ShardBackendInfo> internalCommand =
119                 clientActorProbe.expectMsgClass(InternalCommand.class);
120         internalCommand.execute(behavior);
121         try {
122             behavior.createLocalHistory();
123             Assert.fail("Behavior is closed and shouldn't allow to create new history.");
124         } catch (final IllegalStateException e) {
125             //ok
126         }
127     }
128
129     @Test
130     public void testGetIdentifier() throws Exception {
131         Assert.assertEquals(CLIENT_ID, behavior.getIdentifier());
132     }
133
134     @Test
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);
143
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));
150
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);
163     }
164
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());
172         return mock;
173     }
174
175 }

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