MDSAL-API Migration
[genius.git] / mdsalutil / mdsalutil-testutils / src / test / java / org / opendaylight / genius / infra / tests / 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.genius.infra.tests;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
12
13 import org.junit.Test;
14 import org.opendaylight.genius.infra.Datastore;
15 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
16 import org.opendaylight.genius.infra.RetryingManagedNewTransactionRunner;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
20 import org.opendaylight.mdsal.common.api.ReadFailedException;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
22
23 /**
24  * Test for {@link RetryingManagedNewTransactionRunner}.
25  * Note that this test (intentionally) extends the {@link ManagedNewTransactionRunnerImplTest}.
26  *
27  * @author Michael Vorburger.ch
28  */
29 public class RetryingManagedNewTransactionRunnerTest extends ManagedNewTransactionRunnerImplTest {
30
31     @Override
32     protected ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(DataBroker dataBroker) {
33         return new RetryingManagedNewTransactionRunner(dataBroker);
34     }
35
36     @Override
37     public void testCallWithNewWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
38         // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
39         // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
40         testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
41         TopLevelList data = newTestDataObject();
42         managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
43             writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data)).get();
44         assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
45     }
46
47     @Override
48     public void testCallWithNewTypedWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
49         // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
50         // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
51         testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
52         TopLevelList data = newTestDataObject();
53         managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.OPERATIONAL,
54             writeTx -> writeTx.put(TEST_PATH, data)).get();
55         assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
56     }
57
58     @Override
59     public void testCallWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
60         // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
61         // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
62         testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
63         TopLevelList data = newTestDataObject();
64         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
65             writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data)).get();
66         assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
67     }
68
69     @Override
70     public void testCallWithNewTypedReadWriteTransactionOptimisticLockFailedException() throws Exception {
71         // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
72         // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
73         testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
74         TopLevelList data = newTestDataObject();
75         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.OPERATIONAL,
76             writeTx -> writeTx.put(TEST_PATH, data)).get();
77         assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
78     }
79
80     @Override
81     public void testApplyWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
82         // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
83         // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
84         testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
85         TopLevelList data = newTestDataObject();
86         assertEquals(1, (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(
87             Datastore.OPERATIONAL,
88             writeTx -> {
89                 writeTx.put(TEST_PATH, data);
90                 return 1;
91             }).get());
92         assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
93     }
94
95     @Test
96     public void testCallWithNewReadWriteTransactionReadFailedException() throws Exception {
97         testableDataBroker.failReads(2, new ReadFailedException("bada boum bam!"));
98         TopLevelList data = newTestDataObject();
99         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
100             tx -> {
101                 tx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data);
102                 assertEquals(data, tx.read(LogicalDatastoreType.OPERATIONAL, TEST_PATH).get().get());
103             }).get();
104         assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
105     }
106
107     @Test
108     public void testCallWithNewTypedReadWriteTransactionReadFailedException() throws Exception {
109         testableDataBroker.failReads(2, new ReadFailedException("bada boum bam!"));
110         TopLevelList data = newTestDataObject();
111         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.OPERATIONAL,
112             tx -> {
113                 tx.put(TEST_PATH, data);
114                 assertEquals(data, tx.read(TEST_PATH).get().get());
115             }).get();
116         assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
117     }
118
119     @Test
120     public void testApplyWithNewReadWriteTransactionReadFailedException() throws Exception {
121         testableDataBroker.failReads(2, new ReadFailedException("bada boum bam!"));
122         TopLevelList data = newTestDataObject();
123         assertEquals(data, managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(
124             Datastore.OPERATIONAL,
125             tx -> {
126                 tx.put(TEST_PATH, data);
127                 return tx.read(TEST_PATH).get().get();
128             }).get());
129         assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
130     }
131 }