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