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.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThrows;
14 import static org.junit.Assert.assertTrue;
16 import akka.actor.ActorSystem;
17 import akka.testkit.TestProbe;
18 import akka.testkit.javadsl.TestKit;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.controller.cluster.access.client.AbstractClientConnection;
26 import org.opendaylight.controller.cluster.access.client.AccessClientUtil;
27 import org.opendaylight.controller.cluster.access.client.ClientActorContext;
28 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
29 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
31 @RunWith(MockitoJUnitRunner.StrictStubs.class)
32 public class SingleClientHistoryTest extends AbstractClientHistoryTest<SingleClientHistory> {
33 private ActorSystem system;
34 private AbstractDataStoreClientBehavior behavior;
35 private ClientActorContext clientActorContext;
36 private SingleClientHistory object;
39 private AbstractTransactionCommitCohort cohort;
43 system = ActorSystem.apply();
45 final TestProbe clientContextProbe = new TestProbe(system, "client");
46 final TestProbe actorContextProbe = new TestProbe(system, "actor-context");
47 clientActorContext = AccessClientUtil.createClientActorContext(
48 system, clientContextProbe.ref(), TestUtils.CLIENT_ID, PERSISTENCE_ID);
49 final ActorUtils actorUtilsMock = createActorUtilsMock(system, actorContextProbe.ref());
50 behavior = new SimpleDataStoreClientBehavior(clientActorContext, actorUtilsMock, SHARD_NAME);
52 object = new SingleClientHistory(behavior, HISTORY_ID);
56 public void tearDown() {
57 TestKit.shutdownActorSystem(system);
61 protected SingleClientHistory object() {
66 protected ClientActorContext clientActorContext() {
67 return clientActorContext;
72 public void testDoCreateTransaction() {
73 final ClientTransaction clientTransaction = object().doCreateTransaction();
74 assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId());
79 public void testCreateHistoryProxy() {
80 final AbstractClientConnection<ShardBackendInfo> clientConnection = behavior.getConnection(0L);
81 final ProxyHistory historyProxy = object().createHistoryProxy(HISTORY_ID, clientConnection);
82 assertEquals(object().getIdentifier(), historyProxy.getIdentifier());
87 public void testDoCreateSnapshot() {
88 final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
89 assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(),
90 clientSnapshot.getIdentifier().getHistoryId());
95 public void testOnTransactionComplete() {
96 final ClientTransaction transaction = object().createTransaction();
97 // make transaction ready
98 object().onTransactionReady(transaction, cohort);
99 // complete transaction
100 object().onTransactionComplete(transaction.getIdentifier());
101 // it is possible to make transaction ready again
102 final AbstractTransactionCommitCohort result = object().onTransactionReady(transaction, cohort);
103 assertEquals(result, cohort);
108 public void testOnTransactionAbort() {
109 final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
110 assertTrue(clientSnapshot.abort());
115 public void testOnTransactionReady() {
116 final AbstractTransactionCommitCohort result = object().onTransactionReady(
117 object().createTransaction(), cohort);
118 assertEquals(result, cohort);
123 public void testOnTransactionReadyDuplicate() {
124 final ClientTransaction transaction = object().createTransaction();
125 object().onTransactionReady(transaction, cohort);
126 final IllegalStateException ise = assertThrows(IllegalStateException.class,
127 () -> object().onTransactionReady(transaction, cohort));
128 assertThat(ise.getMessage(), startsWith("Duplicate cohort "));