Fix InMemory shard transaction chaining.
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / test / java / org / opendaylight / mdsal / dom / store / inmemory / ShardCommitCoordinationTaskTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.mdsal.dom.store.inmemory;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.doNothing;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.verify;
16 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.COHORTS;
17 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.DOM_DATA_TREE_IDENTIFIER;
18 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.DOM_STORE_THREE_PHASE_COMMIT_COHORT;
19 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.LISTENABLE_FUTURE;
20 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.resetMocks;
21
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
26
27 public class ShardCommitCoordinationTaskTest {
28
29     final InmemoryDOMDataTreeShardWriteTransaction mockTx = mock(InmemoryDOMDataTreeShardWriteTransaction.class);
30
31     @Before
32     public void setUp() throws Exception {
33         doReturn("MockedTx").when(mockTx).toString();
34         doNothing().when(mockTx).transactionCommited(any());
35     }
36
37     @Test
38     public void basicTest() throws Exception {
39         doReturn(Void.TYPE).when(LISTENABLE_FUTURE).get();
40         doReturn(LISTENABLE_FUTURE).when(DOM_STORE_THREE_PHASE_COMMIT_COHORT).commit();
41
42         COHORTS.add(DOM_STORE_THREE_PHASE_COMMIT_COHORT);
43
44         ShardCommitCoordinationTask shardCommitCoordinationTask =
45                 new ShardCommitCoordinationTask(DOM_DATA_TREE_IDENTIFIER, COHORTS, mockTx);
46
47         shardCommitCoordinationTask.call();
48         verify(DOM_STORE_THREE_PHASE_COMMIT_COHORT).commit();
49     }
50
51     @Test(expected = TransactionCommitFailedException.class)
52     public void exceptionCallTest() throws Exception {
53         doThrow(new InterruptedException()).when(LISTENABLE_FUTURE).get();
54         doReturn(LISTENABLE_FUTURE).when(DOM_STORE_THREE_PHASE_COMMIT_COHORT).commit();
55
56         COHORTS.add(DOM_STORE_THREE_PHASE_COMMIT_COHORT);
57         ShardCommitCoordinationTask shardCommitCoordinationTask =
58                 new ShardCommitCoordinationTask(DOM_DATA_TREE_IDENTIFIER, COHORTS, mockTx);
59         shardCommitCoordinationTask.call();
60     }
61
62     @After
63     public void reset() {
64         resetMocks();
65     }
66 }