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 com.google.common.truth.Truth.assertThat;
11 import static com.google.common.truth.Truth8.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
16 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.path;
17 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.topLevelList;
18 import static org.opendaylight.mdsal.binding.util.Datastore.OPERATIONAL;
20 import java.io.IOException;
21 import java.util.Optional;
22 import java.util.concurrent.ExecutionException;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.binding.api.ReadTransaction;
27 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
28 import org.opendaylight.mdsal.binding.testutils.DataBrokerFailuresImpl;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
31 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugmentBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.complex.from.grouping.ContainerWithUsesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 * Test for {@link ManagedNewTransactionRunnerImpl}.
42 * @author Michael Vorburger.ch
43 * @author Stephen Kitt
45 public class ManagedNewTransactionRunnerImplTest extends AbstractConcurrentDataBrokerTest {
47 static final InstanceIdentifier<TopLevelList> TEST_PATH = path(TOP_FOO_KEY);
49 DataBrokerFailuresImpl testableDataBroker;
50 ManagedNewTransactionRunner managedNewTransactionRunner;
52 public ManagedNewTransactionRunnerImplTest() {
56 protected ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(DataBroker dataBroker) {
57 return new ManagedNewTransactionRunnerImpl(dataBroker);
61 public void beforeTest() throws Exception {
63 testableDataBroker = new DataBrokerFailuresImpl(getDataBroker());
64 managedNewTransactionRunner = createManagedNewTransactionRunnerToTest(testableDataBroker);
68 public void testApplyWithNewReadTransactionAndCloseEmptySuccessfully() {
69 assertEquals(Long.valueOf(1),
70 managedNewTransactionRunner.applyWithNewReadOnlyTransactionAndClose(OPERATIONAL, tx -> 1L));
74 public void testCallWithNewReadTransactionAndCloseEmptySuccessfully() {
75 managedNewTransactionRunner.callWithNewReadOnlyTransactionAndClose(OPERATIONAL, tx -> { });
79 public void testCallWithNewTypedWriteOnlyTransactionAndSubmitEmptySuccessfully() throws Exception {
80 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeTx -> { }).get();
84 public void testCallWithNewTypedReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
85 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> { }).get();
89 public void testApplyWithNewReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
91 (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
96 public void testCallWithNewTypedWriteOnlyTransactionAndSubmitPutSuccessfully() throws Exception {
97 TopLevelList data = newTestDataObject();
98 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
99 writeTx -> writeTx.put(TEST_PATH, data)).get();
100 assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
104 public void testCallWithNewTypedReadWriteTransactionAndSubmitPutSuccessfully() throws Exception {
105 TopLevelList data = newTestDataObject();
106 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
107 tx -> tx.put(TEST_PATH, data)).get();
108 assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
112 public void testApplyWithNewReadWriteTransactionAndSubmitPutSuccessfully() throws Exception {
113 TopLevelList data = newTestDataObject();
114 assertEquals(1, (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(
116 tx.put(TEST_PATH, data);
119 assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
123 public void testCallWithNewReadTransactionAndCloseReadSuccessfully() throws Exception {
124 TopLevelList data = newTestDataObject();
125 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
126 tx -> tx.put(TEST_PATH, data)).get();
127 assertEquals(data, managedNewTransactionRunner.applyWithNewReadOnlyTransactionAndClose(OPERATIONAL,
128 tx -> tx.read(TEST_PATH)).get().get());
131 TopLevelList newTestDataObject() {
132 TreeComplexUsesAugment fooAugment = new TreeComplexUsesAugmentBuilder()
133 .setContainerWithUses(new ContainerWithUsesBuilder().setLeafFromGrouping("foo").build()).build();
134 return topLevelList(TOP_FOO_KEY, fooAugment);
138 public void testCallWithNewTypedWriteOnlyTransactionAndSubmitPutButLaterException() throws Exception {
140 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeTx -> {
141 writeTx.put(TEST_PATH, newTestDataObject());
142 // We now throw an arbitrary kind of checked (not unchecked!) exception here
143 throw new IOException("something didn't quite go as expected...");
145 fail("This should have led to an ExecutionException!");
146 } catch (ExecutionException e) {
147 assertTrue(e.getCause() instanceof IOException);
149 assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
153 public void testCallWithNewTypedReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
155 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, writeTx -> {
156 writeTx.put(TEST_PATH, newTestDataObject());
157 // We now throw an arbitrary kind of checked (not unchecked!) exception here
158 throw new IOException("something didn't quite go as expected...");
160 fail("This should have led to an ExecutionException!");
161 } catch (ExecutionException e) {
162 assertTrue(e.getCause() instanceof IOException);
164 assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
168 public void testApplyWithNewReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
170 managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
172 writeTx.put(TEST_PATH, newTestDataObject());
173 // We now throw an arbitrary kind of checked (not unchecked!) exception here
174 throw new IOException("something didn't quite go as expected...");
176 fail("This should have led to an ExecutionException!");
177 } catch (ExecutionException e) {
178 assertTrue(e.getCause() instanceof IOException);
180 assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
184 public void testCallWithNewTypedWriteOnlyTransactionCommitFailedException() throws Exception {
186 testableDataBroker.failCommits(new TransactionCommitFailedException("bada boum bam!"));
187 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
188 writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
189 fail("This should have led to an ExecutionException!");
190 } catch (ExecutionException e) {
191 assertTrue(e.getCause() instanceof TransactionCommitFailedException);
193 assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
197 public void testCallWithNewTypedReadWriteTransactionCommitFailedException() throws Exception {
199 testableDataBroker.failCommits(new TransactionCommitFailedException("bada boum bam!"));
200 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
201 writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
202 fail("This should have led to an ExecutionException!");
203 } catch (ExecutionException e) {
204 assertTrue(e.getCause() instanceof TransactionCommitFailedException);
206 assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
210 public void testApplyWithNewReadWriteTransactionCommitFailedException() throws Exception {
212 testableDataBroker.failCommits(new TransactionCommitFailedException("bada boum bam!"));
213 managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
215 writeTx.put(TEST_PATH, newTestDataObject());
218 fail("This should have led to an ExecutionException!");
219 } catch (ExecutionException e) {
220 assertTrue(e.getCause() instanceof TransactionCommitFailedException);
222 assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
226 public void testCallWithNewTypedWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
228 testableDataBroker.failCommits(2, new OptimisticLockFailedException("bada boum bam!"));
229 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
230 writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
231 fail("This should have led to an ExecutionException!");
232 } catch (ExecutionException e) {
233 assertTrue(e.getCause() instanceof OptimisticLockFailedException);
235 assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
239 public void testCallWithNewTypedReadWriteTransactionOptimisticLockFailedException() throws Exception {
241 testableDataBroker.failCommits(2, new OptimisticLockFailedException("bada boum bam!"));
242 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
243 writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
244 fail("This should have led to an ExecutionException!");
245 } catch (ExecutionException e) {
246 assertTrue(e.getCause() instanceof OptimisticLockFailedException);
248 assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
252 public void testApplyWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
254 testableDataBroker.failCommits(2, new OptimisticLockFailedException("bada boum bam!"));
255 managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
257 writeTx.put(TEST_PATH, newTestDataObject());
260 fail("This should have led to an ExecutionException!");
261 } catch (ExecutionException e) {
262 assertThat(e.getCause() instanceof OptimisticLockFailedException).isTrue();
264 assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
267 private <T extends DataObject> Optional<T> syncReadOptional(LogicalDatastoreType datastoreType,
268 InstanceIdentifier<T> path) throws ExecutionException, InterruptedException {
269 try (ReadTransaction tx = getDataBroker().newReadOnlyTransaction()) {
270 return tx.read(datastoreType, path).get();
274 <T extends DataObject> T syncRead(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path)
275 throws ExecutionException, InterruptedException {
276 return syncReadOptional(datastoreType, path).get();