Add AbstractTransactionCommitCohort unit tests
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / actors / dds / DirectTransactionCommitCohortTest.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.mockito.Mockito.verify;
11 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.CLIENT_ID;
12 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.HISTORY_ID;
13 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.TRANSACTION_ID;
14 import static org.opendaylight.controller.cluster.databroker.actors.dds.TestUtils.getWithTimeout;
15
16 import akka.actor.ActorSystem;
17 import akka.testkit.JavaTestKit;
18 import akka.testkit.TestProbe;
19 import com.google.common.primitives.UnsignedLong;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import java.util.Optional;
22 import org.junit.After;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.cluster.access.ABIVersion;
29 import org.opendaylight.controller.cluster.access.client.AbstractClientConnection;
30 import org.opendaylight.controller.cluster.access.client.AccessClientUtil;
31 import org.opendaylight.controller.cluster.access.client.ClientActorContext;
32 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
33 import org.opendaylight.controller.cluster.access.commands.PersistenceProtocol;
34 import org.opendaylight.controller.cluster.access.commands.TransactionCommitSuccess;
35 import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
36
37 public class DirectTransactionCommitCohortTest {
38
39     private static final String PERSISTENCE_ID = "per-1";
40
41     @Mock
42     private AbstractClientHistory history;
43     private ActorSystem system;
44     private TranasactionTester transaction;
45     private DirectTransactionCommitCohort cohort;
46
47     @Before
48     public void setUp() throws Exception {
49         MockitoAnnotations.initMocks(this);
50         system = ActorSystem.apply();
51         final TestProbe clientContextProbe = new TestProbe(system, "clientContext");
52         final ClientActorContext context =
53                 AccessClientUtil.createClientActorContext(system, clientContextProbe.ref(), CLIENT_ID, PERSISTENCE_ID);
54         transaction = createTransactionTester(new TestProbe(system, "backend"), context, history);
55         final AbstractProxyTransaction proxy = transaction.getTransaction();
56         proxy.seal();
57         cohort = new DirectTransactionCommitCohort(history, TRANSACTION_ID, proxy);
58     }
59
60     @After
61     public void tearDown() throws Exception {
62         JavaTestKit.shutdownActorSystem(system);
63     }
64
65     @Test
66     public void testCanCommit() throws Exception {
67         final ListenableFuture<Boolean> canCommit = cohort.canCommit();
68         final ModifyTransactionRequest request = transaction.expectTransactionRequest(ModifyTransactionRequest.class);
69         Assert.assertTrue(request.getPersistenceProtocol().isPresent());
70         Assert.assertEquals(PersistenceProtocol.SIMPLE, request.getPersistenceProtocol().get());
71         final RequestSuccess<?, ?> success = new TransactionCommitSuccess(transaction.getTransaction().getIdentifier(),
72                 transaction.getLastReceivedMessage().getSequence());
73         transaction.replySuccess(success);
74         Assert.assertTrue(getWithTimeout(canCommit));
75     }
76
77     @Test
78     public void testPreCommit() throws Exception {
79         final ListenableFuture<Void> preCommit = cohort.preCommit();
80         Assert.assertNull(getWithTimeout(preCommit));
81     }
82
83     @Test
84     public void testAbort() throws Exception {
85         final ListenableFuture<Void> abort = cohort.abort();
86         verify(history).onTransactionComplete(transaction.getTransaction().getIdentifier());
87         Assert.assertNull(getWithTimeout(abort));
88     }
89
90     @Test
91     public void testCommit() throws Exception {
92         final ListenableFuture<Void> commit = cohort.commit();
93         verify(history).onTransactionComplete(transaction.getTransaction().getIdentifier());
94         Assert.assertNull(getWithTimeout(commit));
95     }
96
97     private static TranasactionTester createTransactionTester(final TestProbe backendProbe,
98                                                               final ClientActorContext context,
99                                                               final AbstractClientHistory history) {
100         final ShardBackendInfo backend = new ShardBackendInfo(backendProbe.ref(), 0L, ABIVersion.BORON,
101                 "default", UnsignedLong.ZERO, Optional.empty(), 3);
102         final AbstractClientConnection<ShardBackendInfo> connection =
103                 AccessClientUtil.createConnectedConnection(context, 0L, backend);
104         final ProxyHistory proxyHistory = ProxyHistory.createClient(history, connection, HISTORY_ID);
105         final RemoteProxyTransaction transaction =
106                 new RemoteProxyTransaction(proxyHistory, TRANSACTION_ID, false, false);
107         return new TranasactionTester(transaction, connection, backendProbe);
108     }
109
110 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.