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.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID;
12 import akka.actor.ActorSystem;
13 import akka.testkit.JavaTestKit;
14 import akka.testkit.TestProbe;
15 import org.junit.After;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.cluster.access.client.AbstractClientConnection;
22 import org.opendaylight.controller.cluster.access.client.AccessClientUtil;
23 import org.opendaylight.controller.cluster.access.client.ClientActorContext;
24 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
25 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
27 public class ClientLocalHistoryTest extends AbstractClientHistoryTest<ClientLocalHistory> {
28 private ActorSystem system;
29 private AbstractDataStoreClientBehavior behavior;
30 private ClientActorContext clientActorContext;
31 private ClientLocalHistory object;
34 private AbstractTransactionCommitCohort cohort;
36 private ClientTransaction transaction;
39 public void setUp() throws Exception {
40 MockitoAnnotations.initMocks(this);
42 system = ActorSystem.apply();
44 final TestProbe clientContextProbe = new TestProbe(system, "client");
45 final TestProbe actorContextProbe = new TestProbe(system, "actor-context");
46 clientActorContext = AccessClientUtil.createClientActorContext(
47 system, clientContextProbe.ref(), CLIENT_ID, PERSISTENCE_ID);
48 final ActorContext actorContextMock = createActorContextMock(system, actorContextProbe.ref());
49 behavior = new SimpleDataStoreClientBehavior(clientActorContext, actorContextMock, SHARD_NAME);
51 object = new ClientLocalHistory(behavior, HISTORY_ID);
55 public void tearDown() throws Exception {
56 JavaTestKit.shutdownActorSystem(system);
60 protected ClientLocalHistory object() {
65 protected ClientActorContext clientActorContext() {
66 return clientActorContext;
70 public void testClose() throws Exception {
72 Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state());
77 public void testDoCreateTransaction() throws Exception {
78 final ClientTransaction clientTransaction = object().doCreateTransaction();
79 Assert.assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId());
84 public void testOnTransactionAbort() throws Exception {
85 final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
86 Assert.assertTrue(clientSnapshot.abort());
91 public void testCreateHistoryProxy() throws Exception {
92 final AbstractClientConnection<ShardBackendInfo> clientConnection = behavior.getConnection(0L);
93 final ProxyHistory historyProxy = object().createHistoryProxy(HISTORY_ID, clientConnection);
94 Assert.assertEquals(object().getIdentifier(), historyProxy.getIdentifier());
99 public void testDoCreateSnapshot() throws Exception {
100 final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
101 Assert.assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(),
102 clientSnapshot.getIdentifier().getHistoryId());
107 public void testOnTransactionComplete() throws Exception {
108 final ClientTransaction transaction = object().createTransaction();
110 // make transaction ready
111 object().onTransactionReady(transaction, cohort);
112 // state should be set to IDLE
113 Assert.assertEquals(AbstractClientHistory.State.IDLE, object.state());
115 // complete transaction
116 object().onTransactionComplete(transaction.getIdentifier());
117 // state is still IDLE
118 Assert.assertEquals(AbstractClientHistory.State.IDLE, object.state());
123 public void testOnTransactionReady() throws Exception {
124 final AbstractTransactionCommitCohort result = object().onTransactionReady(
125 object().createTransaction(), cohort);
126 Assert.assertEquals(result, cohort);
130 @Test(expected = IllegalStateException.class)
131 public void testOnTransactionReadyDuplicate() throws Exception {
132 final ClientTransaction transaction = object().createTransaction();
133 object().onTransactionReady(transaction, cohort);
134 object().onTransactionReady(transaction, cohort);
138 public void testOnTransactionReadyAndComplete() throws Exception {
139 object().updateState(AbstractClientHistory.State.IDLE, AbstractClientHistory.State.TX_OPEN);
140 final AbstractTransactionCommitCohort transactionCommitCohort =
141 object().onTransactionReady(transaction, cohort);
142 Assert.assertEquals(cohort, transactionCommitCohort);
146 public void testOnTransactionReadyAndCompleteStateClosed() throws Exception {
147 object().updateState(AbstractClientHistory.State.IDLE, AbstractClientHistory.State.CLOSED);
148 final AbstractTransactionCommitCohort transactionCommitCohort =
149 object().onTransactionReady(transaction, cohort);
150 Assert.assertEquals(cohort, transactionCommitCohort);
153 @Test(expected = IllegalStateException.class)
154 public void testOnTransactionReadyAndCompleteIdleFail() throws Exception {
155 object().onTransactionReady(transaction, cohort);