Fix checkstyle issues in module sal-dom-broker
[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,
52                                                                      txChainlistener);
53     }
54
55     @Test
56     public void testClose() {
57         doNothing().when(delegateTxChain).close();
58         txChain.close();
59         verify(delegateTxChain).close();
60     }
61
62     @Test
63     public void testNewWriteTransaction() {
64         DOMDataTreeWriteTransaction delegateWriteTx = mock(DOMDataTreeWriteTransaction.class);
65         doReturn(delegateWriteTx).when(delegateTxChain).newWriteOnlyTransaction();
66         doReturn("TEST-WRITE-TX-DELEGATE").when(delegateWriteTx).getIdentifier();
67         txChain.newWriteOnlyTransaction();
68         verify(delegateTxChain).newWriteOnlyTransaction();
69     }
70
71     @Test
72     public void testNewReadOnlyTransaction() {
73         DOMDataTreeReadTransaction delegateReadTx = mock(DOMDataTreeReadTransaction.class);
74         doReturn("TEST-READ-TX-DELEGATE").when(delegateReadTx).getIdentifier();
75         doReturn(delegateReadTx).when(delegateTxChain).newReadOnlyTransaction();
76         txChain.newReadOnlyTransaction();
77         verify(delegateTxChain).newReadOnlyTransaction();
78     }
79
80
81     @Test
82     public void testNewReadWriteTransaction() {
83         DOMDataTreeReadTransaction delegateReadTx = mock(DOMDataTreeReadTransaction.class);
84         doReturn("TEST-READ-TX-DELEGATE").when(delegateReadTx).getIdentifier();
85         doReturn(delegateReadTx).when(delegateTxChain).newReadOnlyTransaction();
86
87         DOMDataTreeWriteTransaction delegateWriteTx = mock(DOMDataTreeWriteTransaction.class);
88         doReturn(delegateWriteTx).when(delegateTxChain).newWriteOnlyTransaction();
89         doReturn("TEST-WRITE-TX-DELEGATE").when(delegateWriteTx).getIdentifier();
90
91         txChain.newReadWriteTransaction();
92         verify(delegateTxChain).newReadOnlyTransaction();
93         verify(delegateTxChain).newWriteOnlyTransaction();
94     }
95
96     @Test
97     public void testTransactionChainFailed() {
98         final DOMDataTreeWriteTransaction writeTxDelegate = mock(DOMDataTreeWriteTransaction.class);
99         doReturn("DELEGATE-WRITE-TX-1").when(writeTxDelegate).getIdentifier();
100         doReturn(writeTxDelegate).when(delegateTxChain).newWriteOnlyTransaction();
101         doNothing().when(txChainlistener).onTransactionChainFailed(any(), any(), any());
102
103         // verify writetx fail
104         txChain.newWriteOnlyTransaction();
105         txChain.onTransactionChainFailed(delegateTxChain, writeTxDelegate, new Throwable("Fail"));
106
107         final ArgumentCaptor<AsyncTransaction> txCaptor = ArgumentCaptor.forClass(AsyncTransaction.class);
108         final ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
109         verify(txChainlistener).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 }