Use Assert.assertThrows()
[genius.git] / mdsalutil / mdsalutil-testutils / src / test / java / org / opendaylight / genius / infra / tests / ManagedNewTransactionRunnerImplTest.java
1 /*
2  * Copyright (c) 2017 Red Hat, 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.genius.infra.tests;
9
10 import static com.google.common.truth.Truth.assertThat;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
16 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
17 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList;
18 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
19
20 import java.io.IOException;
21 import java.util.concurrent.ExecutionException;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
27 import org.opendaylight.genius.datastoreutils.testutils.DataBrokerFailuresImpl;
28 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
30 import org.opendaylight.infrautils.testutils.LogCaptureRule;
31 import org.opendaylight.infrautils.testutils.LogRule;
32 import org.opendaylight.mdsal.binding.api.DataBroker;
33 import org.opendaylight.mdsal.binding.api.WriteTransaction;
34 import org.opendaylight.mdsal.binding.testutils.DataBrokerTestModule;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
37 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugmentBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.complex.from.grouping.ContainerWithUsesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43
44 /**
45  * Test for {@link ManagedNewTransactionRunnerImpl}.
46  *
47  * @author Michael Vorburger.ch
48  * @author Stephen Kitt
49  */
50 public class ManagedNewTransactionRunnerImplTest {
51
52     static final InstanceIdentifier<TopLevelList> TEST_PATH = path(TOP_FOO_KEY);
53
54     public @Rule LogRule logRule = new LogRule();
55     public @Rule LogCaptureRule logCaptureRule = new LogCaptureRule();
56
57     DataBrokerFailuresImpl testableDataBroker;
58     SingleTransactionDataBroker singleTransactionDataBroker;
59     ManagedNewTransactionRunner managedNewTransactionRunner;
60
61     protected ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(DataBroker dataBroker) {
62         return new ManagedNewTransactionRunnerImpl(dataBroker);
63     }
64
65     @Before
66     public void beforeTest() {
67         testableDataBroker = new DataBrokerFailuresImpl(new DataBrokerTestModule(true).getDataBroker());
68         managedNewTransactionRunner = createManagedNewTransactionRunnerToTest(testableDataBroker);
69         singleTransactionDataBroker = new SingleTransactionDataBroker(testableDataBroker);
70     }
71
72     @Test
73     public void testApplyWithNewReadTransactionAndCloseEmptySuccessfully() throws Exception {
74         Assert.assertEquals(Long.valueOf(1),
75             managedNewTransactionRunner.applyWithNewReadOnlyTransactionAndClose(OPERATIONAL, tx -> 1L));
76     }
77
78     @Test
79     public void testCallWithNewReadTransactionAndCloseEmptySuccessfully() throws Exception {
80         managedNewTransactionRunner.callWithNewReadOnlyTransactionAndClose(OPERATIONAL, tx -> { });
81     }
82
83     @Test
84     public void testCallWithNewWriteOnlyTransactionAndSubmitEmptySuccessfully() throws Exception {
85         managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(writeTx -> { }).get();
86     }
87
88     @Test
89     public void testCallWithNewTypedWriteOnlyTransactionAndSubmitEmptySuccessfully() throws Exception {
90         managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeTx -> {
91         }).get();
92     }
93
94     @Test
95     public void testCallWithNewReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
96         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(tx -> { }).get();
97     }
98
99     @Test
100     public void testCallWithNewTypedReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
101         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
102         }).get();
103     }
104
105     @Test
106     public void testApplyWithNewReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
107         assertEquals(1,
108             (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
109                 tx -> 1).get());
110     }
111
112     @Test
113     public void testCallWithNewWriteOnlyTransactionAndSubmitPutSuccessfully() throws Exception {
114         TopLevelList data = newTestDataObject();
115         managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
116             writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data)).get();
117         assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
118     }
119
120     @Test
121     public void testCallWithNewTypedWriteOnlyTransactionAndSubmitPutSuccessfully() throws Exception {
122         TopLevelList data = newTestDataObject();
123         managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
124             writeTx -> writeTx.put(TEST_PATH, data)).get();
125         assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
126     }
127
128     @Test
129     public void testCallWithNewReadWriteTransactionAndSubmitPutSuccessfully() throws Exception {
130         TopLevelList data = newTestDataObject();
131         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
132             tx -> tx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data)).get();
133         assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
134     }
135
136     @Test
137     public void testCallWithNewTypedReadWriteTransactionAndSubmitPutSuccessfully() throws Exception {
138         TopLevelList data = newTestDataObject();
139         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
140             tx -> tx.put(TEST_PATH, data)).get();
141         assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
142     }
143
144     @Test
145     public void testApplyWithNewReadWriteTransactionAndSubmitPutSuccessfully() throws Exception {
146         TopLevelList data = newTestDataObject();
147         assertEquals(1, (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(
148             OPERATIONAL,
149             tx -> {
150                 tx.put(TEST_PATH, data);
151                 return 1;
152             }).get());
153         assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
154     }
155
156     @Test
157     public void testCallWithNewReadTransactionAndCloseReadSuccessfully() throws Exception {
158         TopLevelList data = newTestDataObject();
159         managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
160             tx -> tx.put(TEST_PATH, data)).get();
161         assertEquals(data, managedNewTransactionRunner.applyWithNewReadOnlyTransactionAndClose(OPERATIONAL,
162             tx -> tx.read(TEST_PATH)).get().get());
163     }
164
165     TopLevelList newTestDataObject() {
166         TreeComplexUsesAugment fooAugment = new TreeComplexUsesAugmentBuilder()
167                 .setContainerWithUses(new ContainerWithUsesBuilder().setLeafFromGrouping("foo").build()).build();
168         return topLevelList(TOP_FOO_KEY, fooAugment);
169     }
170
171     @Test
172     public void testCallWithNewWriteOnlyTransactionAndSubmitPutButLaterException() throws Exception {
173         assertTrue(assertThrows(ExecutionException.class,
174             () -> {
175                 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(writeTx -> {
176                     writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject());
177                     // We now throw an arbitrary kind of checked (not unchecked!) exception here
178                     throw new IOException("something didn't quite go as expected...");
179                 }).get();
180                 fail("This should have led to an ExecutionException!");
181             }).getCause() instanceof IOException);
182         assertThat(
183             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
184     }
185
186     @Test
187     public void testCallWithNewTypedWriteOnlyTransactionAndSubmitPutButLaterException() throws Exception {
188         assertTrue(assertThrows(ExecutionException.class,
189             () -> {
190                 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeTx -> {
191                     writeTx.put(TEST_PATH, newTestDataObject());
192                     // We now throw an arbitrary kind of checked (not unchecked!) exception here
193                     throw new IOException("something didn't quite go as expected...");
194                 }).get();
195                 fail("This should have led to an ExecutionException!");
196             }).getCause() instanceof IOException);
197         assertThat(
198             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
199     }
200
201     @Test
202     public void testCallWithNewReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
203         assertTrue(assertThrows(ExecutionException.class,
204             () -> {
205                 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(writeTx -> {
206                     writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject());
207                     // We now throw an arbitrary kind of checked (not unchecked!) exception here
208                     throw new IOException("something didn't quite go as expected...");
209                 }).get();
210                 fail("This should have led to an ExecutionException!");
211             }).getCause() instanceof IOException);
212         assertThat(
213             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
214     }
215
216     @Test
217     public void testCallWithNewTypedReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
218         assertTrue(assertThrows(ExecutionException.class,
219             () -> {
220                 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, writeTx -> {
221                     writeTx.put(TEST_PATH, newTestDataObject());
222                     // We now throw an arbitrary kind of checked (not unchecked!) exception here
223                     throw new IOException("something didn't quite go as expected...");
224                 }).get();
225                 fail("This should have led to an ExecutionException!");
226             }).getCause() instanceof IOException);
227         assertThat(
228             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
229     }
230
231     @Test
232     public void testApplyWithNewReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
233         assertTrue(assertThrows(ExecutionException.class,
234             () -> {
235                 managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
236                     writeTx -> {
237                         writeTx.put(TEST_PATH, newTestDataObject());
238                         // We now throw an arbitrary kind of checked (not unchecked!) exception here
239                         throw new IOException("something didn't quite go as expected...");
240                     }).get();
241                 fail("This should have led to an ExecutionException!");
242             }).getCause() instanceof IOException);
243         assertThat(
244             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
245     }
246
247     @Test
248     public void testCallWithNewWriteOnlyTransactionCommitFailedException() throws Exception {
249         assertTrue(assertThrows(ExecutionException.class,
250             () -> {
251                 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
252                 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
253                     writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
254                 fail("This should have led to an ExecutionException!");
255             }).getCause() instanceof TransactionCommitFailedException);
256         assertThat(
257             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
258     }
259
260     @Test
261     public void testCallWithNewTypedWriteOnlyTransactionCommitFailedException() throws Exception {
262         assertTrue(assertThrows(ExecutionException.class,
263             () -> {
264                 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
265                 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
266                     writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
267                 fail("This should have led to an ExecutionException!");
268             }).getCause() instanceof TransactionCommitFailedException);
269         assertThat(
270             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
271     }
272
273     @Test
274     public void testCallWithNewReadWriteTransactionCommitFailedException() throws Exception {
275         assertTrue(assertThrows(ExecutionException.class,
276             () -> {
277                 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
278                 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
279                     writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
280                 fail("This should have led to an ExecutionException!");
281             }).getCause() instanceof TransactionCommitFailedException);
282         assertThat(
283             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
284     }
285
286     @Test
287     public void testCallWithNewTypedReadWriteTransactionCommitFailedException() throws Exception {
288         assertTrue(assertThrows(ExecutionException.class,
289             () -> {
290                 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
291                 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
292                     writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
293                 fail("This should have led to an ExecutionException!");
294             }).getCause() instanceof TransactionCommitFailedException);
295         assertThat(
296             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
297     }
298
299     @Test
300     public void testApplyWithNewReadWriteTransactionCommitFailedException() throws Exception {
301         assertTrue(assertThrows(ExecutionException.class,
302             () -> {
303                 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
304                 managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
305                     writeTx -> {
306                         writeTx.put(TEST_PATH, newTestDataObject());
307                         return 1;
308                     }).get();
309                 fail("This should have led to an ExecutionException!");
310             }).getCause() instanceof TransactionCommitFailedException);
311         assertThat(
312             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
313     }
314
315     @Test
316     public void testCallWithNewWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
317         assertTrue(assertThrows(ExecutionException.class,
318             () -> {
319                 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
320                 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
321                     writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
322                 fail("This should have led to an ExecutionException!");
323             }).getCause() instanceof OptimisticLockFailedException);
324         assertThat(
325             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
326     }
327
328     @Test
329     public void testCallWithNewTypedWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
330         assertTrue(assertThrows(ExecutionException.class,
331             () -> {
332                 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
333                 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
334                     writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
335                 fail("This should have led to an ExecutionException!");
336             }).getCause() instanceof OptimisticLockFailedException);
337         assertThat(
338             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
339     }
340
341     @Test
342     public void testCallWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
343         assertTrue(assertThrows(ExecutionException.class,
344             () -> {
345                 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
346                 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
347                     writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
348                 fail("This should have led to an ExecutionException!");
349             }).getCause() instanceof OptimisticLockFailedException);
350         assertThat(
351             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
352     }
353
354     @Test
355     public void testCallWithNewTypedReadWriteTransactionOptimisticLockFailedException() throws Exception {
356         assertTrue(assertThrows(ExecutionException.class,
357             () -> {
358                 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
359                 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
360                     writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
361                 fail("This should have led to an ExecutionException!");
362             }).getCause() instanceof OptimisticLockFailedException);
363         assertThat(
364             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
365     }
366
367     @Test
368     public void testApplyWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
369         try {
370             testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
371             managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
372                 writeTx -> {
373                     writeTx.put(TEST_PATH, newTestDataObject());
374                     return 1;
375                 }).get();
376             fail("This should have led to an ExecutionException!");
377         } catch (ExecutionException e) {
378             assertThat(e.getCause() instanceof OptimisticLockFailedException).isTrue();
379         }
380         assertThat(
381             singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
382     }
383
384     @Test
385     public void testCallWithNewWriteOnlyTransactionAndSubmitCannotCommit() {
386         assertThrows(ExecutionException.class,
387             () -> managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
388                 WriteTransaction::commit).get());
389     }
390
391     @Test
392     public void testCallWithNewReadWriteTransactionAndSubmitCannotCommit() {
393         assertThrows(ExecutionException.class,
394             () -> managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
395                 WriteTransaction::commit).get());
396     }
397
398     @Test
399     public void testCallWithNewWriteOnlyTransactionAndSubmitCannotCancel() {
400         assertThrows(ExecutionException.class,
401             () -> managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
402                 WriteTransaction::cancel).get());
403     }
404
405     @Test
406     public void testCallWithNewReadWriteTransactionAndSubmitCannotCancel() {
407         assertThrows(ExecutionException.class,
408             () -> managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
409                 WriteTransaction::cancel).get());
410     }
411 }