AbstractClientHistory derived classes tests
[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.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 SingleClientHistoryTest extends AbstractClientHistoryTest<SingleClientHistory> {
28     private ActorSystem system;
29     private AbstractDataStoreClientBehavior behavior;
30     private ClientActorContext clientActorContext;
31     private SingleClientHistory object;
32
33     @Mock
34     private AbstractTransactionCommitCohort cohort;
35
36     @Before
37     public void setUp() throws Exception {
38         MockitoAnnotations.initMocks(this);
39
40         system = ActorSystem.apply();
41
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 ActorContext actorContextMock = createActorContextMock(system, actorContextProbe.ref());
47         behavior = new SimpleDataStoreClientBehavior(clientActorContext, actorContextMock, SHARD_NAME);
48
49         object = new SingleClientHistory(behavior, HISTORY_ID);
50     }
51
52     @After
53     public void tearDown() throws Exception {
54         JavaTestKit.shutdownActorSystem(system);
55     }
56
57     @Override
58     protected SingleClientHistory object() {
59         return object;
60     }
61
62     @Override
63     protected ClientActorContext clientActorContext() {
64         return clientActorContext;
65     }
66
67     @Override
68     @Test
69     public void testDoCreateTransaction() throws Exception {
70         final ClientTransaction clientTransaction = object().doCreateTransaction();
71         Assert.assertEquals(object().getIdentifier(), clientTransaction.getIdentifier().getHistoryId());
72     }
73
74     @Override
75     @Test
76     public void testCreateHistoryProxy() throws Exception {
77         final AbstractClientConnection<ShardBackendInfo> clientConnection = behavior.getConnection(0L);
78         final ProxyHistory historyProxy = object().createHistoryProxy(HISTORY_ID, clientConnection);
79         Assert.assertEquals(object().getIdentifier(), historyProxy.getIdentifier());
80     }
81
82     @Override
83     @Test
84     public void testDoCreateSnapshot() throws Exception {
85         final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
86         Assert.assertEquals(new TransactionIdentifier(object().getIdentifier(), object().nextTx()).getHistoryId(),
87                 clientSnapshot.getIdentifier().getHistoryId());
88     }
89
90     @Override
91     @Test
92     public void testOnTransactionComplete() throws Exception {
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);
101     }
102
103     @Override
104     @Test
105     public void testOnTransactionAbort() throws Exception {
106         final ClientSnapshot clientSnapshot = object().doCreateSnapshot();
107         Assert.assertTrue(clientSnapshot.abort());
108     }
109
110     @Override
111     @Test
112     public void testOnTransactionReady() throws Exception {
113         final AbstractTransactionCommitCohort result = object().onTransactionReady(
114                 object().createTransaction(), cohort);
115         Assert.assertEquals(result, cohort);
116     }
117
118     @Override
119     @Test(expected = IllegalStateException.class)
120     public void testOnTransactionReadyDuplicate() throws Exception {
121         final ClientTransaction transaction = object().createTransaction();
122         object().onTransactionReady(transaction, cohort);
123         object().onTransactionReady(transaction, cohort);
124     }
125 }