Fixup checkstyle
[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.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;
16
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;
31
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;
38
39     @Mock
40     private AbstractTransactionCommitCohort cohort;
41     @Mock
42     private ClientTransaction transaction;
43
44     @Before
45     public void setUp() {
46         system = ActorSystem.apply();
47
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);
54
55         object = new ClientLocalHistory(behavior, HISTORY_ID);
56     }
57
58     @After
59     public void tearDown() {
60         TestKit.shutdownActorSystem(system);
61     }
62
63     @Override
64     protected ClientLocalHistory object() {
65         return object;
66     }
67
68     @Override
69     protected ClientActorContext clientActorContext() {
70         return clientActorContext;
71     }
72
73     @Test
74     public void testClose() {
75         object().close();
76         assertEquals(AbstractClientHistory.State.CLOSED, object().state());
77     }
78
79     @Override
80     @Test
81     public void testDoCreateTransaction() {
82         final ClientTransaction clientTransaction = object().doCreateTransaction();
83         assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId());
84     }
85
86     @Override
87     @Test
88     public void testOnTransactionAbort() {
89         final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
90         assertTrue(clientSnapshot.abort());
91     }
92
93     @Override
94     @Test
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());
99     }
100
101     @Override
102     @Test
103     public void testDoCreateSnapshot() {
104         final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
105         assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(),
106                 clientSnapshot.getIdentifier().getHistoryId());
107     }
108
109     @Override
110     @Test
111     public void testOnTransactionComplete() {
112         final ClientTransaction tx = object().createTransaction();
113
114         // make transaction ready
115         object().onTransactionReady(tx, cohort);
116         // state should be set to IDLE
117         assertEquals(AbstractClientHistory.State.IDLE, object.state());
118
119         // complete transaction
120         object().onTransactionComplete(tx.getIdentifier());
121         // state is still IDLE
122         assertEquals(AbstractClientHistory.State.IDLE, object.state());
123     }
124
125     @Override
126     @Test
127     public void testOnTransactionReady() {
128         final AbstractTransactionCommitCohort result = object().onTransactionReady(object().createTransaction(),
129             cohort);
130         assertEquals(result, cohort);
131     }
132
133     @Override
134     @Test
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 "));
141     }
142
143     @Test
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);
149     }
150
151     @Test
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);
157     }
158
159     @Test
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"));
164     }
165 }