Update sal-distributed-datastore tests a bit
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / actors / dds / SingleClientHistoryTest.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.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;
15
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;
30
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;
37
38     @Mock
39     private AbstractTransactionCommitCohort cohort;
40
41     @Before
42     public void setUp() {
43         system = ActorSystem.apply();
44
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);
51
52         object = new SingleClientHistory(behavior, HISTORY_ID);
53     }
54
55     @After
56     public void tearDown() {
57         TestKit.shutdownActorSystem(system);
58     }
59
60     @Override
61     protected SingleClientHistory object() {
62         return object;
63     }
64
65     @Override
66     protected ClientActorContext clientActorContext() {
67         return clientActorContext;
68     }
69
70     @Override
71     @Test
72     public void testDoCreateTransaction() {
73         final ClientTransaction clientTransaction = object().doCreateTransaction();
74         assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId());
75     }
76
77     @Override
78     @Test
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());
83     }
84
85     @Override
86     @Test
87     public void testDoCreateSnapshot() {
88         final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
89         assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(),
90                 clientSnapshot.getIdentifier().getHistoryId());
91     }
92
93     @Override
94     @Test
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);
104     }
105
106     @Override
107     @Test
108     public void testOnTransactionAbort() {
109         final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
110         assertTrue(clientSnapshot.abort());
111     }
112
113     @Override
114     @Test
115     public void testOnTransactionReady() {
116         final AbstractTransactionCommitCohort result = object().onTransactionReady(
117                 object().createTransaction(), cohort);
118         assertEquals(result, cohort);
119     }
120
121     @Override
122     @Test
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 "));
129     }
130 }