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.TestProbe;
14 import akka.testkit.javadsl.TestKit;
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.ActorUtils;
27 public class SingleClientHistoryTest extends AbstractClientHistoryTest<SingleClientHistory> {
28 private ActorSystem system;
29 private AbstractDataStoreClientBehavior behavior;
30 private ClientActorContext clientActorContext;
31 private SingleClientHistory object;
34 private AbstractTransactionCommitCohort cohort;
38 MockitoAnnotations.initMocks(this);
40 system = ActorSystem.apply();
42 final TestProbe clientContextProbe = new TestProbe(system, "client");
43 final TestProbe actorContextProbe = new TestProbe(system, "actor-context");
44 clientActorContext = AccessClientUtil.createClientActorContext(
45 system, clientContextProbe.ref(), CLIENT_ID, PERSISTENCE_ID);
46 final ActorUtils actorUtilsMock = createActorUtilsMock(system, actorContextProbe.ref());
47 behavior = new SimpleDataStoreClientBehavior(clientActorContext, actorUtilsMock, SHARD_NAME);
49 object = new SingleClientHistory(behavior, HISTORY_ID);
53 public void tearDown() {
54 TestKit.shutdownActorSystem(system);
58 protected SingleClientHistory object() {
63 protected ClientActorContext clientActorContext() {
64 return clientActorContext;
69 public void testDoCreateTransaction() {
70 final ClientTransaction clientTransaction = object().doCreateTransaction();
71 Assert.assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId());
76 public void testCreateHistoryProxy() {
77 final AbstractClientConnection<ShardBackendInfo> clientConnection = behavior.getConnection(0L);
78 final ProxyHistory historyProxy = object().createHistoryProxy(HISTORY_ID, clientConnection);
79 Assert.assertEquals(object().getIdentifier(), historyProxy.getIdentifier());
84 public void testDoCreateSnapshot() {
85 final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
86 Assert.assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(),
87 clientSnapshot.getIdentifier().getHistoryId());
92 public void testOnTransactionComplete() {
93 final ClientTransaction transaction = object().createTransaction();
94 // make transaction ready
95 object().onTransactionReady(transaction, cohort);
96 // complete transaction
97 object().onTransactionComplete(transaction.getIdentifier());
98 // it is possible to make transaction ready again
99 final AbstractTransactionCommitCohort result = object().onTransactionReady(transaction, cohort);
100 Assert.assertEquals(result, cohort);
105 public void testOnTransactionAbort() {
106 final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
107 Assert.assertTrue(clientSnapshot.abort());
112 public void testOnTransactionReady() {
113 final AbstractTransactionCommitCohort result = object().onTransactionReady(
114 object().createTransaction(), cohort);
115 Assert.assertEquals(result, cohort);
119 @Test(expected = IllegalStateException.class)
120 public void testOnTransactionReadyDuplicate() {
121 final ClientTransaction transaction = object().createTransaction();
122 object().onTransactionReady(transaction, cohort);
123 object().onTransactionReady(transaction, cohort);