Clean up binding-util tests
[mdsal.git] / binding / mdsal-binding-util / src / test / java / org / opendaylight / mdsal / binding / util / RetryingManagedNewTransactionRunnerTest.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.mdsal.binding.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.mdsal.binding.util.Datastore.OPERATIONAL;
12
13 import org.junit.Test;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
17 import org.opendaylight.mdsal.common.api.ReadFailedException;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
19
20 /**
21  * Test for {@link RetryingManagedNewTransactionRunner}.
22  * Note that this test (intentionally) extends the {@link ManagedNewTransactionRunnerImplTest}.
23  *
24  * @author Michael Vorburger.ch
25  */
26 public class RetryingManagedNewTransactionRunnerTest extends ManagedNewTransactionRunnerImplTest {
27
28     @Override
29     protected ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(final DataBroker dataBroker) {
30         return new RetryingManagedNewTransactionRunner(dataBroker);
31     }
32
33     @Override
34     public void testCallWithNewTypedWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
35         // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
36         // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
37         testableDataBroker.failCommits(2, new OptimisticLockFailedException("bada boum bam!"));
38         TopLevelList data = newTestDataObject();
39         managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
40             writeTx -> writeTx.put(TEST_PATH, data)).get();
41         assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
42     }
43
44     @Override
45     public void testCallWithNewTypedReadWriteTransactionOptimisticLockFailedException() throws Exception {
46         // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
47         // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
48         testableDataBroker.failCommits(2, new OptimisticLockFailedException("bada boum bam!"));
49         TopLevelList data = newTestDataObject();
50         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
51             writeTx -> writeTx.put(TEST_PATH, data)).get();
52         assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
53     }
54
55     @Override
56     public void testApplyWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
57         // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
58         // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
59         testableDataBroker.failCommits(2, new OptimisticLockFailedException("bada boum bam!"));
60         TopLevelList data = newTestDataObject();
61         assertEquals(1, (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(
62             OPERATIONAL, writeTx -> {
63                 writeTx.put(TEST_PATH, data);
64                 return 1;
65             }).get());
66         assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
67     }
68
69     @Test
70     public void testCallWithNewTypedReadWriteTransactionReadFailedException() throws Exception {
71         testableDataBroker.failReads(2, new ReadFailedException("bada boum bam!"));
72         TopLevelList data = newTestDataObject();
73         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
74             tx -> {
75                 tx.put(TEST_PATH, data);
76                 assertEquals(data, tx.read(TEST_PATH).get().get());
77             }).get();
78         assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
79     }
80
81     @Test
82     public void testApplyWithNewReadWriteTransactionReadFailedException() throws Exception {
83         testableDataBroker.failReads(2, new ReadFailedException("bada boum bam!"));
84         TopLevelList data = newTestDataObject();
85         assertEquals(data, managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(
86             OPERATIONAL,
87             tx -> {
88                 tx.put(TEST_PATH, data);
89                 return tx.read(TEST_PATH).get().get();
90             }).get());
91         assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
92     }
93 }