Add AbstractTransactionCommitCohort unit tests
[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     protected abstract AbstractDataStoreClientBehavior createBehavior(ClientActorContext clientContext,
63                                                                       ActorContext context);
64
65     @After
66     public void tearDown() throws Exception {
67         JavaTestKit.shutdownActorSystem(system);
68     }
69
70     @Test
71     public void testResolveShardForPath() throws Exception {
72         Assert.assertEquals(0L, behavior.resolveShardForPath(YangInstanceIdentifier.EMPTY).longValue());
73     }
74
75     @Test
76     public void testHaltClient() throws Exception {
77         behavior.haltClient(new RuntimeException());
78     }
79
80     @Test
81     public void testOnCommand() throws Exception {
82         final TestProbe probe = new TestProbe(system);
83         final GetClientRequest request = new GetClientRequest(probe.ref());
84         final AbstractDataStoreClientBehavior nextBehavior = behavior.onCommand(request);
85         final Status.Success success = probe.expectMsgClass(Status.Success.class);
86         Assert.assertEquals(behavior, success.status());
87         Assert.assertSame(behavior, nextBehavior);
88     }
89
90     @Test
91     public void testOnCommandUnhandled() throws Exception {
92         final AbstractDataStoreClientBehavior nextBehavior = behavior.onCommand("unhandled");
93         Assert.assertSame(behavior, nextBehavior);
94     }
95
96     @Test
97     public void testCreateLocalHistory() throws Exception {
98         final ClientLocalHistory history = behavior.createLocalHistory();
99         Assert.assertEquals(behavior.getIdentifier(), history.getIdentifier().getClientId());
100     }
101
102     @Test
103     public void testCreateTransaction() throws Exception {
104         final ClientTransaction transaction = behavior.createTransaction();
105         Assert.assertEquals(behavior.getIdentifier(), transaction.getIdentifier().getHistoryId().getClientId());
106     }
107
108     @Test
109     public void testCreateSnapshot() throws Exception {
110         final ClientSnapshot snapshot = behavior.createSnapshot();
111         Assert.assertEquals(behavior.getIdentifier(), snapshot.getIdentifier().getHistoryId().getClientId());
112     }
113
114     @Test
115     public void testClose() throws Exception {
116         behavior.close();
117         final InternalCommand<ShardBackendInfo> internalCommand =
118                 clientActorProbe.expectMsgClass(InternalCommand.class);
119         internalCommand.execute(behavior);
120         try {
121             behavior.createLocalHistory();
122             Assert.fail("Behavior is closed and shouldn't allow to create new history.");
123         } catch (final IllegalStateException e) {
124             //ok
125         }
126     }
127
128     @Test
129     public void testGetIdentifier() throws Exception {
130         Assert.assertEquals(CLIENT_ID, behavior.getIdentifier());
131     }
132
133     @Test
134     public void testGetConnection() throws Exception {
135         //set up data tree mock
136         final CursorAwareDataTreeModification modification = mock(CursorAwareDataTreeModification.class);
137         when(modification.readNode(YangInstanceIdentifier.EMPTY)).thenReturn(com.google.common.base.Optional.absent());
138         final DataTreeSnapshot snapshot = mock(DataTreeSnapshot.class);
139         when(snapshot.newModification()).thenReturn(modification);
140         final DataTree dataTree = mock(DataTree.class);
141         when(dataTree.takeSnapshot()).thenReturn(snapshot);
142
143         final TestProbe backendProbe = new TestProbe(system, "backend");
144         final long shard = 0L;
145         behavior.createTransaction().read(YangInstanceIdentifier.EMPTY);
146         final AbstractClientConnection<ShardBackendInfo> connection = behavior.getConnection(shard);
147         //check cached connection for same shard
148         Assert.assertSame(connection, behavior.getConnection(shard));
149
150         final ConnectClientRequest connectClientRequest = actorContextProbe.expectMsgClass(ConnectClientRequest.class);
151         Assert.assertEquals(CLIENT_ID, connectClientRequest.getTarget());
152         final long sequence = 0L;
153         Assert.assertEquals(sequence, connectClientRequest.getSequence());
154         actorContextProbe.reply(new ConnectClientSuccess(CLIENT_ID, sequence, backendProbe.ref(),
155                 Collections.emptyList(), dataTree, 3));
156         Assert.assertEquals(clientActorProbe.ref(), connection.localActor());
157         //capture and execute command passed to client context
158         final InternalCommand<ShardBackendInfo> command = clientActorProbe.expectMsgClass(InternalCommand.class);
159         command.execute(behavior);
160         //check, whether command was reaplayed
161         verify(modification).readNode(YangInstanceIdentifier.EMPTY);
162     }
163
164     private static ActorContext createActorContextMock(final ActorSystem system, final ActorRef actor) {
165         final ActorContext mock = mock(ActorContext.class);
166         final Promise<PrimaryShardInfo> promise = new scala.concurrent.impl.Promise.DefaultPromise<>();
167         final ActorSelection selection = system.actorSelection(actor.path());
168         final PrimaryShardInfo shardInfo = new PrimaryShardInfo(selection, (short) 0);
169         promise.success(shardInfo);
170         when(mock.findPrimaryShardAsync(SHARD)).thenReturn(promise.future());
171         return mock;
172     }
173
174 }