AbstractClientHistory derived classes tests
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / actors / dds / ClientLocalHistoryTest.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.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID;
11
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;
26
27 public class ClientLocalHistoryTest extends AbstractClientHistoryTest<ClientLocalHistory> {
28     private ActorSystem system;
29     private AbstractDataStoreClientBehavior behavior;
30     private ClientActorContext clientActorContext;
31     private ClientLocalHistory object;
32
33     @Mock
34     private AbstractTransactionCommitCohort cohort;
35     @Mock
36     private ClientTransaction transaction;
37
38     @Before
39     public void setUp() throws Exception {
40         MockitoAnnotations.initMocks(this);
41
42         system = ActorSystem.apply();
43
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);
50
51         object = new ClientLocalHistory(behavior, HISTORY_ID);
52     }
53
54     @After
55     public void tearDown() throws Exception {
56         JavaTestKit.shutdownActorSystem(system);
57     }
58
59     @Override
60     protected ClientLocalHistory object() {
61         return object;
62     }
63
64     @Override
65     protected ClientActorContext clientActorContext() {
66         return clientActorContext;
67     }
68
69     @Test
70     public void testClose() throws Exception {
71         object().close();
72         Assert.assertEquals(AbstractClientHistory.State.CLOSED, object().state());
73     }
74
75     @Override
76     @Test
77     public void testDoCreateTransaction() throws Exception {
78         final ClientTransaction clientTransaction = object().doCreateTransaction();
79         Assert.assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId());
80     }
81
82     @Override
83     @Test
84     public void testOnTransactionAbort() throws Exception {
85         final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
86         Assert.assertTrue(clientSnapshot.abort());
87     }
88
89     @Override
90     @Test
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());
95     }
96
97     @Override
98     @Test
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());
103     }
104
105     @Override
106     @Test
107     public void testOnTransactionComplete() throws Exception {
108         final ClientTransaction transaction = object().createTransaction();
109
110         // make transaction ready
111         object().onTransactionReady(transaction, cohort);
112         // state should be set to IDLE
113         Assert.assertEquals(AbstractClientHistory.State.IDLE, object.state());
114
115         // complete transaction
116         object().onTransactionComplete(transaction.getIdentifier());
117         // state is still IDLE
118         Assert.assertEquals(AbstractClientHistory.State.IDLE, object.state());
119     }
120
121     @Override
122     @Test
123     public void testOnTransactionReady() throws Exception {
124         final AbstractTransactionCommitCohort result = object().onTransactionReady(
125                 object().createTransaction(), cohort);
126         Assert.assertEquals(result, cohort);
127     }
128
129     @Override
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);
135     }
136
137     @Test
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);
143     }
144
145     @Test
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);
151     }
152
153     @Test(expected = IllegalStateException.class)
154     public void testOnTransactionReadyAndCompleteIdleFail() throws Exception {
155         object().onTransactionReady(transaction, cohort);
156     }
157 }