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.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;
22 * Test for {@link RetryingManagedNewTransactionRunner}.
23 * Note that this test (intentionally) extends the {@link ManagedNewTransactionRunnerImplTest}.
25 * @author Michael Vorburger.ch
27 public class RetryingManagedNewTransactionRunnerTest extends ManagedNewTransactionRunnerImplTest {
30 protected ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(DataBroker dataBroker) {
31 return new RetryingManagedNewTransactionRunner(dataBroker);
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));
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));
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);
67 Assert.assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
71 public void testCallWithNewTypedReadWriteTransactionReadFailedException() throws Exception {
72 testableDataBroker.failReads(2, new ReadFailedException("bada boum bam!"));
73 TopLevelList data = newTestDataObject();
74 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
76 tx.put(TEST_PATH, data);
77 Assert.assertEquals(data, tx.read(TEST_PATH).get().get());
79 Assert.assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
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(
89 tx.put(TEST_PATH, data);
90 return tx.read(TEST_PATH).get().get();
92 Assert.assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));