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.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.CoreMatchers.endsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertThrows;
15 import static org.junit.Assert.assertTrue;
17 import akka.actor.ActorSystem;
18 import akka.testkit.TestProbe;
19 import akka.testkit.javadsl.TestKit;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnitRunner;
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.concepts.TransactionIdentifier;
30 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
32 @RunWith(MockitoJUnitRunner.StrictStubs.class)
33 public class ClientLocalHistoryTest extends AbstractClientHistoryTest<ClientLocalHistory> {
34 private ActorSystem system;
35 private AbstractDataStoreClientBehavior behavior;
36 private ClientActorContext clientActorContext;
37 private ClientLocalHistory object;
40 private AbstractTransactionCommitCohort cohort;
42 private ClientTransaction transaction;
46 system = ActorSystem.apply();
48 final TestProbe clientContextProbe = new TestProbe(system, "client");
49 final TestProbe actorContextProbe = new TestProbe(system, "actor-context");
50 clientActorContext = AccessClientUtil.createClientActorContext(
51 system, clientContextProbe.ref(), TestUtils.CLIENT_ID, PERSISTENCE_ID);
52 final ActorUtils actorUtilsMock = createActorUtilsMock(system, actorContextProbe.ref());
53 behavior = new SimpleDataStoreClientBehavior(clientActorContext, actorUtilsMock, SHARD_NAME);
55 object = new ClientLocalHistory(behavior, HISTORY_ID);
59 public void tearDown() {
60 TestKit.shutdownActorSystem(system);
64 protected ClientLocalHistory object() {
69 protected ClientActorContext clientActorContext() {
70 return clientActorContext;
74 public void testClose() {
76 assertEquals(AbstractClientHistory.State.CLOSED, object().state());
81 public void testDoCreateTransaction() {
82 final ClientTransaction clientTransaction = object().doCreateTransaction();
83 assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId());
88 public void testOnTransactionAbort() {
89 final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
90 assertTrue(clientSnapshot.abort());
95 public void testCreateHistoryProxy() {
96 final AbstractClientConnection<ShardBackendInfo> clientConnection = behavior.getConnection(0L);
97 final ProxyHistory historyProxy = object().createHistoryProxy(HISTORY_ID, clientConnection);
98 assertEquals(object().getIdentifier(), historyProxy.getIdentifier());
103 public void testDoCreateSnapshot() {
104 final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
105 assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(),
106 clientSnapshot.getIdentifier().getHistoryId());
111 public void testOnTransactionComplete() {
112 final ClientTransaction tx = object().createTransaction();
114 // make transaction ready
115 object().onTransactionReady(tx, cohort);
116 // state should be set to IDLE
117 assertEquals(AbstractClientHistory.State.IDLE, object.state());
119 // complete transaction
120 object().onTransactionComplete(tx.getIdentifier());
121 // state is still IDLE
122 assertEquals(AbstractClientHistory.State.IDLE, object.state());
127 public void testOnTransactionReady() {
128 final AbstractTransactionCommitCohort result = object().onTransactionReady(object().createTransaction(),
130 assertEquals(result, cohort);
135 public void testOnTransactionReadyDuplicate() {
136 final ClientTransaction tx = object().createTransaction();
137 object().onTransactionReady(tx, cohort);
138 final IllegalStateException ise = assertThrows(IllegalStateException.class,
139 () -> object().onTransactionReady(tx, cohort));
140 assertThat(ise.getMessage(), containsString(" is idle when readying transaction "));
144 public void testOnTransactionReadyAndComplete() {
145 object().updateState(AbstractClientHistory.State.IDLE, AbstractClientHistory.State.TX_OPEN);
146 final AbstractTransactionCommitCohort transactionCommitCohort =
147 object().onTransactionReady(transaction, cohort);
148 assertEquals(cohort, transactionCommitCohort);
152 public void testOnTransactionReadyAndCompleteStateClosed() {
153 object().updateState(AbstractClientHistory.State.IDLE, AbstractClientHistory.State.CLOSED);
154 final AbstractTransactionCommitCohort transactionCommitCohort =
155 object().onTransactionReady(transaction, cohort);
156 assertEquals(cohort, transactionCommitCohort);
160 public void testOnTransactionReadyAndCompleteIdleFail() {
161 final IllegalStateException ise = assertThrows(IllegalStateException.class,
162 () -> object().onTransactionReady(transaction, cohort));
163 assertThat(ise.getMessage(), endsWith(" is idle when readying transaction null"));