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.genius.infra.tests;
10 import static com.google.common.truth.Truth.assertThat;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
16 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
17 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList;
18 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
20 import java.io.IOException;
21 import java.util.concurrent.ExecutionException;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
27 import org.opendaylight.genius.datastoreutils.testutils.DataBrokerFailuresImpl;
28 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
30 import org.opendaylight.infrautils.testutils.LogCaptureRule;
31 import org.opendaylight.infrautils.testutils.LogRule;
32 import org.opendaylight.mdsal.binding.api.DataBroker;
33 import org.opendaylight.mdsal.binding.api.WriteTransaction;
34 import org.opendaylight.mdsal.binding.testutils.DataBrokerTestModule;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
37 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugmentBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.complex.from.grouping.ContainerWithUsesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 * Test for {@link ManagedNewTransactionRunnerImpl}.
47 * @author Michael Vorburger.ch
48 * @author Stephen Kitt
50 public class ManagedNewTransactionRunnerImplTest {
52 static final InstanceIdentifier<TopLevelList> TEST_PATH = path(TOP_FOO_KEY);
54 public @Rule LogRule logRule = new LogRule();
55 public @Rule LogCaptureRule logCaptureRule = new LogCaptureRule();
57 DataBrokerFailuresImpl testableDataBroker;
58 SingleTransactionDataBroker singleTransactionDataBroker;
59 ManagedNewTransactionRunner managedNewTransactionRunner;
61 protected ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(DataBroker dataBroker) {
62 return new ManagedNewTransactionRunnerImpl(dataBroker);
66 public void beforeTest() {
67 testableDataBroker = new DataBrokerFailuresImpl(new DataBrokerTestModule(true).getDataBroker());
68 managedNewTransactionRunner = createManagedNewTransactionRunnerToTest(testableDataBroker);
69 singleTransactionDataBroker = new SingleTransactionDataBroker(testableDataBroker);
73 public void testApplyWithNewReadTransactionAndCloseEmptySuccessfully() throws Exception {
74 Assert.assertEquals(Long.valueOf(1),
75 managedNewTransactionRunner.applyWithNewReadOnlyTransactionAndClose(OPERATIONAL, tx -> 1L));
79 public void testCallWithNewReadTransactionAndCloseEmptySuccessfully() throws Exception {
80 managedNewTransactionRunner.callWithNewReadOnlyTransactionAndClose(OPERATIONAL, tx -> { });
84 public void testCallWithNewWriteOnlyTransactionAndSubmitEmptySuccessfully() throws Exception {
85 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(writeTx -> { }).get();
89 public void testCallWithNewTypedWriteOnlyTransactionAndSubmitEmptySuccessfully() throws Exception {
90 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeTx -> {
95 public void testCallWithNewReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
96 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(tx -> { }).get();
100 public void testCallWithNewTypedReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
101 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
106 public void testApplyWithNewReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
108 (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
113 public void testCallWithNewWriteOnlyTransactionAndSubmitPutSuccessfully() throws Exception {
114 TopLevelList data = newTestDataObject();
115 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
116 writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data)).get();
117 assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
121 public void testCallWithNewTypedWriteOnlyTransactionAndSubmitPutSuccessfully() throws Exception {
122 TopLevelList data = newTestDataObject();
123 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
124 writeTx -> writeTx.put(TEST_PATH, data)).get();
125 assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
129 public void testCallWithNewReadWriteTransactionAndSubmitPutSuccessfully() throws Exception {
130 TopLevelList data = newTestDataObject();
131 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
132 tx -> tx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data)).get();
133 assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
137 public void testCallWithNewTypedReadWriteTransactionAndSubmitPutSuccessfully() throws Exception {
138 TopLevelList data = newTestDataObject();
139 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
140 tx -> tx.put(TEST_PATH, data)).get();
141 assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
145 public void testApplyWithNewReadWriteTransactionAndSubmitPutSuccessfully() throws Exception {
146 TopLevelList data = newTestDataObject();
147 assertEquals(1, (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(
150 tx.put(TEST_PATH, data);
153 assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
157 public void testCallWithNewReadTransactionAndCloseReadSuccessfully() throws Exception {
158 TopLevelList data = newTestDataObject();
159 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
160 tx -> tx.put(TEST_PATH, data)).get();
161 assertEquals(data, managedNewTransactionRunner.applyWithNewReadOnlyTransactionAndClose(OPERATIONAL,
162 tx -> tx.read(TEST_PATH)).get().get());
165 TopLevelList newTestDataObject() {
166 TreeComplexUsesAugment fooAugment = new TreeComplexUsesAugmentBuilder()
167 .setContainerWithUses(new ContainerWithUsesBuilder().setLeafFromGrouping("foo").build()).build();
168 return topLevelList(TOP_FOO_KEY, fooAugment);
172 public void testCallWithNewWriteOnlyTransactionAndSubmitPutButLaterException() throws Exception {
173 assertTrue(assertThrows(ExecutionException.class,
175 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(writeTx -> {
176 writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject());
177 // We now throw an arbitrary kind of checked (not unchecked!) exception here
178 throw new IOException("something didn't quite go as expected...");
180 fail("This should have led to an ExecutionException!");
181 }).getCause() instanceof IOException);
183 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
187 public void testCallWithNewTypedWriteOnlyTransactionAndSubmitPutButLaterException() throws Exception {
188 assertTrue(assertThrows(ExecutionException.class,
190 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeTx -> {
191 writeTx.put(TEST_PATH, newTestDataObject());
192 // We now throw an arbitrary kind of checked (not unchecked!) exception here
193 throw new IOException("something didn't quite go as expected...");
195 fail("This should have led to an ExecutionException!");
196 }).getCause() instanceof IOException);
198 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
202 public void testCallWithNewReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
203 assertTrue(assertThrows(ExecutionException.class,
205 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(writeTx -> {
206 writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject());
207 // We now throw an arbitrary kind of checked (not unchecked!) exception here
208 throw new IOException("something didn't quite go as expected...");
210 fail("This should have led to an ExecutionException!");
211 }).getCause() instanceof IOException);
213 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
217 public void testCallWithNewTypedReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
218 assertTrue(assertThrows(ExecutionException.class,
220 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, writeTx -> {
221 writeTx.put(TEST_PATH, newTestDataObject());
222 // We now throw an arbitrary kind of checked (not unchecked!) exception here
223 throw new IOException("something didn't quite go as expected...");
225 fail("This should have led to an ExecutionException!");
226 }).getCause() instanceof IOException);
228 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
232 public void testApplyWithNewReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
233 assertTrue(assertThrows(ExecutionException.class,
235 managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
237 writeTx.put(TEST_PATH, newTestDataObject());
238 // We now throw an arbitrary kind of checked (not unchecked!) exception here
239 throw new IOException("something didn't quite go as expected...");
241 fail("This should have led to an ExecutionException!");
242 }).getCause() instanceof IOException);
244 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
248 public void testCallWithNewWriteOnlyTransactionCommitFailedException() throws Exception {
249 assertTrue(assertThrows(ExecutionException.class,
251 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
252 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
253 writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
254 fail("This should have led to an ExecutionException!");
255 }).getCause() instanceof TransactionCommitFailedException);
257 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
261 public void testCallWithNewTypedWriteOnlyTransactionCommitFailedException() throws Exception {
262 assertTrue(assertThrows(ExecutionException.class,
264 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
265 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
266 writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
267 fail("This should have led to an ExecutionException!");
268 }).getCause() instanceof TransactionCommitFailedException);
270 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
274 public void testCallWithNewReadWriteTransactionCommitFailedException() throws Exception {
275 assertTrue(assertThrows(ExecutionException.class,
277 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
278 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
279 writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
280 fail("This should have led to an ExecutionException!");
281 }).getCause() instanceof TransactionCommitFailedException);
283 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
287 public void testCallWithNewTypedReadWriteTransactionCommitFailedException() throws Exception {
288 assertTrue(assertThrows(ExecutionException.class,
290 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
291 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
292 writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
293 fail("This should have led to an ExecutionException!");
294 }).getCause() instanceof TransactionCommitFailedException);
296 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
300 public void testApplyWithNewReadWriteTransactionCommitFailedException() throws Exception {
301 assertTrue(assertThrows(ExecutionException.class,
303 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
304 managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
306 writeTx.put(TEST_PATH, newTestDataObject());
309 fail("This should have led to an ExecutionException!");
310 }).getCause() instanceof TransactionCommitFailedException);
312 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
316 public void testCallWithNewWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
317 assertTrue(assertThrows(ExecutionException.class,
319 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
320 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
321 writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
322 fail("This should have led to an ExecutionException!");
323 }).getCause() instanceof OptimisticLockFailedException);
325 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
329 public void testCallWithNewTypedWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
330 assertTrue(assertThrows(ExecutionException.class,
332 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
333 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
334 writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
335 fail("This should have led to an ExecutionException!");
336 }).getCause() instanceof OptimisticLockFailedException);
338 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
342 public void testCallWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
343 assertTrue(assertThrows(ExecutionException.class,
345 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
346 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
347 writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
348 fail("This should have led to an ExecutionException!");
349 }).getCause() instanceof OptimisticLockFailedException);
351 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
355 public void testCallWithNewTypedReadWriteTransactionOptimisticLockFailedException() throws Exception {
356 assertTrue(assertThrows(ExecutionException.class,
358 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
359 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
360 writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
361 fail("This should have led to an ExecutionException!");
362 }).getCause() instanceof OptimisticLockFailedException);
364 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
368 public void testApplyWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
370 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
371 managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
373 writeTx.put(TEST_PATH, newTestDataObject());
376 fail("This should have led to an ExecutionException!");
377 } catch (ExecutionException e) {
378 assertThat(e.getCause() instanceof OptimisticLockFailedException).isTrue();
381 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
385 public void testCallWithNewWriteOnlyTransactionAndSubmitCannotCommit() {
386 assertThrows(ExecutionException.class,
387 () -> managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
388 WriteTransaction::commit).get());
392 public void testCallWithNewReadWriteTransactionAndSubmitCannotCommit() {
393 assertThrows(ExecutionException.class,
394 () -> managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
395 WriteTransaction::commit).get());
399 public void testCallWithNewWriteOnlyTransactionAndSubmitCannotCancel() {
400 assertThrows(ExecutionException.class,
401 () -> managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
402 WriteTransaction::cancel).get());
406 public void testCallWithNewReadWriteTransactionAndSubmitCannotCancel() {
407 assertThrows(ExecutionException.class,
408 () -> managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
409 WriteTransaction::cancel).get());