Bug 3195: Cleanup on error paths and error handling
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / LocalTransactionContextTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import static org.junit.Assert.assertTrue;
4 import static org.mockito.Mockito.doReturn;
5 import static org.mockito.Mockito.doThrow;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.verify;
8 import akka.actor.ActorSelection;
9 import com.google.common.base.Optional;
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.SettableFuture;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.mockito.Mock;
15 import org.mockito.MockitoAnnotations;
16 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import scala.concurrent.Future;
23
24 public class LocalTransactionContextTest {
25
26     @Mock
27     OperationLimiter limiter;
28
29     @Mock
30     TransactionIdentifier identifier;
31
32     @Mock
33     DOMStoreReadWriteTransaction readWriteTransaction;
34
35     @Mock
36     LocalTransactionReadySupport mockReadySupport;
37
38     LocalTransactionContext localTransactionContext;
39
40     @Before
41     public void setUp() {
42         MockitoAnnotations.initMocks(this);
43         localTransactionContext = new LocalTransactionContext(readWriteTransaction, limiter.getIdentifier(), mockReadySupport) {
44             @Override
45             protected DOMStoreWriteTransaction getWriteDelegate() {
46                 return readWriteTransaction;
47             }
48
49             @Override
50             protected DOMStoreReadTransaction getReadDelegate() {
51                 return readWriteTransaction;
52             }
53         };
54     }
55
56     @Test
57     public void testWrite() {
58         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.builder().build();
59         NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class);
60         localTransactionContext.writeData(yangInstanceIdentifier, normalizedNode);
61         verify(readWriteTransaction).write(yangInstanceIdentifier, normalizedNode);
62     }
63
64     @Test
65     public void testMerge() {
66         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.builder().build();
67         NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class);
68         localTransactionContext.mergeData(yangInstanceIdentifier, normalizedNode);
69         verify(readWriteTransaction).merge(yangInstanceIdentifier, normalizedNode);
70     }
71
72     @Test
73     public void testDelete() {
74         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.builder().build();
75         localTransactionContext.deleteData(yangInstanceIdentifier);
76         verify(readWriteTransaction).delete(yangInstanceIdentifier);
77     }
78
79
80     @Test
81     public void testRead() {
82         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.builder().build();
83         NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class);
84         doReturn(Futures.immediateCheckedFuture(Optional.of(normalizedNode))).when(readWriteTransaction).read(yangInstanceIdentifier);
85         localTransactionContext.readData(yangInstanceIdentifier, SettableFuture.<Optional<NormalizedNode<?,?>>>create());
86         verify(readWriteTransaction).read(yangInstanceIdentifier);
87     }
88
89     @Test
90     public void testExists() {
91         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.builder().build();
92         doReturn(Futures.immediateCheckedFuture(true)).when(readWriteTransaction).exists(yangInstanceIdentifier);
93         localTransactionContext.dataExists(yangInstanceIdentifier, SettableFuture.<Boolean> create());
94         verify(readWriteTransaction).exists(yangInstanceIdentifier);
95     }
96
97     @Test
98     public void testReady() {
99         final LocalThreePhaseCommitCohort mockCohort = mock(LocalThreePhaseCommitCohort.class);
100         doReturn(akka.dispatch.Futures.successful(null)).when(mockCohort).initiateCoordinatedCommit();
101         doReturn(mockCohort).when(mockReadySupport).onTransactionReady(readWriteTransaction);
102
103         Future<ActorSelection> future = localTransactionContext.readyTransaction();
104         assertTrue(future.isCompleted());
105
106         verify(mockReadySupport).onTransactionReady(readWriteTransaction);
107     }
108
109     @Test
110     public void testReadyWithWriteError() {
111         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.builder().build();
112         NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class);
113         RuntimeException error = new RuntimeException("mock");
114         doThrow(error).when(readWriteTransaction).write(yangInstanceIdentifier, normalizedNode);
115
116         localTransactionContext.writeData(yangInstanceIdentifier, normalizedNode);
117         localTransactionContext.writeData(yangInstanceIdentifier, normalizedNode);
118
119         verify(readWriteTransaction).write(yangInstanceIdentifier, normalizedNode);
120
121         doReadyWithExpectedError(error);
122     }
123
124     @Test
125     public void testReadyWithMergeError() {
126         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.builder().build();
127         NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class);
128         RuntimeException error = new RuntimeException("mock");
129         doThrow(error).when(readWriteTransaction).merge(yangInstanceIdentifier, normalizedNode);
130
131         localTransactionContext.mergeData(yangInstanceIdentifier, normalizedNode);
132         localTransactionContext.mergeData(yangInstanceIdentifier, normalizedNode);
133
134         verify(readWriteTransaction).merge(yangInstanceIdentifier, normalizedNode);
135
136         doReadyWithExpectedError(error);
137     }
138
139     @Test
140     public void testReadyWithDeleteError() {
141         YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.builder().build();
142         RuntimeException error = new RuntimeException("mock");
143         doThrow(error).when(readWriteTransaction).delete(yangInstanceIdentifier);
144
145         localTransactionContext.deleteData(yangInstanceIdentifier);
146         localTransactionContext.deleteData(yangInstanceIdentifier);
147
148         verify(readWriteTransaction).delete(yangInstanceIdentifier);
149
150         doReadyWithExpectedError(error);
151     }
152
153     private void doReadyWithExpectedError(RuntimeException expError) {
154         LocalThreePhaseCommitCohort mockCohort = mock(LocalThreePhaseCommitCohort.class);
155         doReturn(akka.dispatch.Futures.successful(null)).when(mockCohort).initiateCoordinatedCommit();
156         doReturn(mockCohort).when(mockReadySupport).onTransactionReady(readWriteTransaction);
157
158         localTransactionContext.readyTransaction();
159
160         verify(mockCohort).setOperationError(expError);
161     }
162 }

©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.