Bug 5700 - Backwards compatibility of sharding api's with old api's
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / md / sal / dom / broker / impl / legacy / sharded / adapter / ShardedDOMDataBrokerDelegatingTransactionChainTest.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
9 package org.opendaylight.controller.md.sal.dom.broker.impl.legacy.sharded.adapter;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
27 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
28 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
31 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
32
33 public class ShardedDOMDataBrokerDelegatingTransactionChainTest {
34
35     @Mock
36     private DOMDataBroker dataBroker;
37
38     @Mock
39     private DOMTransactionChain delegateTxChain;
40
41     @Mock
42     private TransactionChainListener txChainlistener;
43
44     private ShardedDOMDataBrokerDelegatingTransactionChain txChain;
45
46     @Before
47     public void setUp() {
48         MockitoAnnotations.initMocks(this);
49
50         doReturn(delegateTxChain).when(dataBroker).createTransactionChain(any());
51         txChain = new ShardedDOMDataBrokerDelegatingTransactionChain("1", TestModel.createTestContext(), dataBroker, txChainlistener);
52     }
53
54     @Test
55     public void testClose() {
56         doNothing().when(delegateTxChain).close();
57         txChain.close();
58         verify(delegateTxChain).close();
59     }
60
61     @Test
62     public void testNewWriteTransaction() {
63         DOMDataTreeWriteTransaction delegateWriteTx = mock(DOMDataTreeWriteTransaction.class);
64         doReturn(delegateWriteTx).when(delegateTxChain).newWriteOnlyTransaction();
65         doReturn("TEST-WRITE-TX-DELEGATE").when(delegateWriteTx).getIdentifier();
66         txChain.newWriteOnlyTransaction();
67         verify(delegateTxChain).newWriteOnlyTransaction();
68     }
69
70     @Test
71     public void testNewReadOnlyTransaction() {
72         DOMDataTreeReadTransaction delegateReadTx = mock(DOMDataTreeReadTransaction.class);
73         doReturn("TEST-READ-TX-DELEGATE").when(delegateReadTx).getIdentifier();
74         doReturn(delegateReadTx).when(delegateTxChain).newReadOnlyTransaction();
75         txChain.newReadOnlyTransaction();
76         verify(delegateTxChain).newReadOnlyTransaction();
77     }
78
79
80     @Test
81     public void testNewReadWriteTransaction() {
82         DOMDataTreeReadTransaction delegateReadTx = mock(DOMDataTreeReadTransaction.class);
83         doReturn("TEST-READ-TX-DELEGATE").when(delegateReadTx).getIdentifier();
84         doReturn(delegateReadTx).when(delegateTxChain).newReadOnlyTransaction();
85
86         DOMDataTreeWriteTransaction delegateWriteTx = mock(DOMDataTreeWriteTransaction.class);
87         doReturn(delegateWriteTx).when(delegateTxChain).newWriteOnlyTransaction();
88         doReturn("TEST-WRITE-TX-DELEGATE").when(delegateWriteTx).getIdentifier();
89
90         txChain.newReadWriteTransaction();
91         verify(delegateTxChain).newReadOnlyTransaction();
92         verify(delegateTxChain).newWriteOnlyTransaction();
93     }
94
95     @Test
96     public void testTransactionChainFailed() {
97         final DOMDataTreeWriteTransaction writeTxDelegate = mock(DOMDataTreeWriteTransaction.class);
98         doReturn("DELEGATE-WRITE-TX-1").when(writeTxDelegate).getIdentifier();
99         doReturn(writeTxDelegate).when(delegateTxChain).newWriteOnlyTransaction();
100         doNothing().when(txChainlistener).onTransactionChainFailed(any(), any(), any());
101
102         // verify writetx fail
103         txChain.newWriteOnlyTransaction();
104         txChain.onTransactionChainFailed(delegateTxChain, writeTxDelegate, new Throwable("Fail"));
105
106         final ArgumentCaptor<AsyncTransaction> txCaptor = ArgumentCaptor.forClass(AsyncTransaction.class);
107         final ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
108         verify(txChainlistener)
109                 .onTransactionChainFailed(eq(txChain), txCaptor.capture(), throwableCaptor.capture());
110         assertEquals("DOM-CHAIN-1-0", txCaptor.getValue().getIdentifier());
111         assertEquals("Fail", throwableCaptor.getValue().getMessage());
112
113         // verify readtx fail
114         final DOMDataTreeReadTransaction readTxDelegate = mock(DOMDataTreeReadTransaction.class);
115         doReturn("DELEGATE-READ-TX-1").when(readTxDelegate).getIdentifier();
116         doReturn(readTxDelegate).when(delegateTxChain).newReadOnlyTransaction();
117         doNothing().when(txChainlistener).onTransactionChainFailed(any(), any(), any());
118         txChain.newReadOnlyTransaction();
119         txChain.onTransactionChainFailed(delegateTxChain, readTxDelegate, new Throwable("Fail"));
120         verify(txChainlistener, times(2))
121                 .onTransactionChainFailed(eq(txChain), txCaptor.capture(), throwableCaptor.capture());
122         assertEquals("DOM-CHAIN-1-1", txCaptor.getValue().getIdentifier());
123         assertEquals("Fail", throwableCaptor.getValue().getMessage());
124
125
126         // verify readwritetx fail, we must check both read and write failure
127         // translates to returned readwritetx
128
129         // we can reuse write and read tx delegates, just return different
130         // identifiers to avoid conflicts in keys in tx dictionary
131         doReturn("DELEGATE-WRITE-RWTX-1").when(writeTxDelegate).getIdentifier();
132         doReturn("DELEGATE-READ-RWTX-1").when(readTxDelegate).getIdentifier();
133         txChain.newReadWriteTransaction();
134         txChain.onTransactionChainFailed(delegateTxChain, writeTxDelegate, new Throwable("Fail"));
135         verify(txChainlistener, times(3))
136                 .onTransactionChainFailed(eq(txChain), txCaptor.capture(), throwableCaptor.capture());
137         assertEquals("DOM-CHAIN-1-2", txCaptor.getValue().getIdentifier());
138         assertEquals("Fail", throwableCaptor.getValue().getMessage());
139
140         txChain.onTransactionChainFailed(delegateTxChain, readTxDelegate, new Throwable("Fail"));
141         verify(txChainlistener, times(4))
142                 .onTransactionChainFailed(eq(txChain), txCaptor.capture(), throwableCaptor.capture());
143         assertEquals("DOM-CHAIN-1-2", txCaptor.getValue().getIdentifier());
144         assertEquals("Fail", throwableCaptor.getValue().getMessage());
145     }
146
147     @Test
148     public void testTransactionChainSuccessful() {
149         doNothing().when(txChainlistener).onTransactionChainSuccessful(any());
150         txChain.onTransactionChainSuccessful(delegateTxChain);
151         verify(txChainlistener).onTransactionChainSuccessful(eq(txChain));
152     }
153 }