2 * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.util;
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.mdsal.binding.util.Datastore.OPERATIONAL;
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;
21 * Test for {@link RetryingManagedNewTransactionRunner}.
22 * Note that this test (intentionally) extends the {@link ManagedNewTransactionRunnerImplTest}.
24 * @author Michael Vorburger.ch
26 public class RetryingManagedNewTransactionRunnerTest extends ManagedNewTransactionRunnerImplTest {
29 protected ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(final DataBroker dataBroker) {
30 return new RetryingManagedNewTransactionRunner(dataBroker);
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));
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));
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);
66 assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
70 public void testCallWithNewTypedReadWriteTransactionReadFailedException() throws Exception {
71 testableDataBroker.failReads(2, new ReadFailedException("bada boum bam!"));
72 TopLevelList data = newTestDataObject();
73 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
75 tx.put(TEST_PATH, data);
76 assertEquals(data, tx.read(TEST_PATH).get().get());
78 assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
82 public void testApplyWithNewReadWriteTransactionReadFailedException() throws Exception {
83 testableDataBroker.failReads(2, new ReadFailedException("bada boum bam!"));
84 TopLevelList data = newTestDataObject();
85 assertEquals(data, managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(
88 tx.put(TEST_PATH, data);
89 return tx.read(TEST_PATH).get().get();
91 assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));