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.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
15 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
16 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList;
17 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
18 import static org.opendaylight.infrautils.testutils.Asserts.assertThrows;
20 import com.google.common.util.concurrent.ListeningExecutorService;
21 import com.google.common.util.concurrent.MoreExecutors;
22 import java.io.IOException;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.Executors;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
30 import org.opendaylight.genius.datastoreutils.testutils.DataBrokerFailuresImpl;
31 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
32 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
33 import org.opendaylight.infrautils.testutils.LogCaptureRule;
34 import org.opendaylight.infrautils.testutils.LogRule;
35 import org.opendaylight.mdsal.binding.api.DataBroker;
36 import org.opendaylight.mdsal.binding.api.WriteTransaction;
37 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractBaseDataBrokerTest;
38 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTestCustomizer;
39 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
40 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
41 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugmentBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.complex.from.grouping.ContainerWithUsesBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 * Test for {@link ManagedNewTransactionRunnerImpl}.
51 * @author Michael Vorburger.ch
52 * @author Stephen Kitt
54 public class ManagedNewTransactionRunnerImplTest {
56 static final InstanceIdentifier<TopLevelList> TEST_PATH = path(TOP_FOO_KEY);
58 public @Rule LogRule logRule = new LogRule();
59 public @Rule LogCaptureRule logCaptureRule = new LogCaptureRule();
61 DataBrokerFailuresImpl testableDataBroker;
62 SingleTransactionDataBroker singleTransactionDataBroker;
63 ManagedNewTransactionRunner managedNewTransactionRunner;
65 protected ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(DataBroker dataBroker) {
66 return new ManagedNewTransactionRunnerImpl(dataBroker);
70 public void beforeTest() throws Exception {
71 AbstractBaseDataBrokerTest test = new AbstractBaseDataBrokerTest() {
73 protected AbstractDataBrokerTestCustomizer createDataBrokerTestCustomizer() {
74 return new AbstractDataBrokerTestCustomizer() {
76 public ListeningExecutorService getCommitCoordinatorExecutor() {
77 return MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
83 testableDataBroker = new DataBrokerFailuresImpl(test.getDataBroker());
84 managedNewTransactionRunner = createManagedNewTransactionRunnerToTest(testableDataBroker);
85 singleTransactionDataBroker = new SingleTransactionDataBroker(testableDataBroker);
89 public void testApplyWithNewReadTransactionAndCloseEmptySuccessfully() throws Exception {
90 Assert.assertEquals(Long.valueOf(1),
91 managedNewTransactionRunner.applyWithNewReadOnlyTransactionAndClose(OPERATIONAL, tx -> 1L));
95 public void testCallWithNewReadTransactionAndCloseEmptySuccessfully() throws Exception {
96 managedNewTransactionRunner.callWithNewReadOnlyTransactionAndClose(OPERATIONAL, tx -> { });
100 public void testCallWithNewWriteOnlyTransactionAndSubmitEmptySuccessfully() throws Exception {
101 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(writeTx -> { }).get();
105 public void testCallWithNewTypedWriteOnlyTransactionAndSubmitEmptySuccessfully() throws Exception {
106 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeTx -> {
111 public void testCallWithNewReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
112 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(tx -> { }).get();
116 public void testCallWithNewTypedReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
117 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
122 public void testApplyWithNewReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
124 (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
129 public void testCallWithNewWriteOnlyTransactionAndSubmitPutSuccessfully() throws Exception {
130 TopLevelList data = newTestDataObject();
131 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
132 writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data)).get();
133 assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
137 public void testCallWithNewTypedWriteOnlyTransactionAndSubmitPutSuccessfully() throws Exception {
138 TopLevelList data = newTestDataObject();
139 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
140 writeTx -> writeTx.put(TEST_PATH, data)).get();
141 assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
145 public void testCallWithNewReadWriteTransactionAndSubmitPutSuccessfully() throws Exception {
146 TopLevelList data = newTestDataObject();
147 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
148 tx -> tx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data)).get();
149 assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
153 public void testCallWithNewTypedReadWriteTransactionAndSubmitPutSuccessfully() throws Exception {
154 TopLevelList data = newTestDataObject();
155 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
156 tx -> tx.put(TEST_PATH, data)).get();
157 assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
161 public void testApplyWithNewReadWriteTransactionAndSubmitPutSuccessfully() throws Exception {
162 TopLevelList data = newTestDataObject();
163 assertEquals(1, (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(
166 tx.put(TEST_PATH, data);
169 assertEquals(data, singleTransactionDataBroker.syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
173 public void testCallWithNewReadTransactionAndCloseReadSuccessfully() throws Exception {
174 TopLevelList data = newTestDataObject();
175 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
176 tx -> tx.put(TEST_PATH, data)).get();
177 assertEquals(data, managedNewTransactionRunner.applyWithNewReadOnlyTransactionAndClose(OPERATIONAL,
178 tx -> tx.read(TEST_PATH)).get().get());
181 TopLevelList newTestDataObject() {
182 TreeComplexUsesAugment fooAugment = new TreeComplexUsesAugmentBuilder()
183 .setContainerWithUses(new ContainerWithUsesBuilder().setLeafFromGrouping("foo").build()).build();
184 return topLevelList(TOP_FOO_KEY, fooAugment);
188 public void testCallWithNewWriteOnlyTransactionAndSubmitPutButLaterException() throws Exception {
189 assertTrue(assertThrows(ExecutionException.class,
191 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(writeTx -> {
192 writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject());
193 // We now throw an arbitrary kind of checked (not unchecked!) exception here
194 throw new IOException("something didn't quite go as expected...");
196 fail("This should have led to an ExecutionException!");
197 }).getCause() instanceof IOException);
199 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
203 public void testCallWithNewTypedWriteOnlyTransactionAndSubmitPutButLaterException() throws Exception {
204 assertTrue(assertThrows(ExecutionException.class,
206 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeTx -> {
207 writeTx.put(TEST_PATH, newTestDataObject());
208 // We now throw an arbitrary kind of checked (not unchecked!) exception here
209 throw new IOException("something didn't quite go as expected...");
211 fail("This should have led to an ExecutionException!");
212 }).getCause() instanceof IOException);
214 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
218 public void testCallWithNewReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
219 assertTrue(assertThrows(ExecutionException.class,
221 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(writeTx -> {
222 writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject());
223 // We now throw an arbitrary kind of checked (not unchecked!) exception here
224 throw new IOException("something didn't quite go as expected...");
226 fail("This should have led to an ExecutionException!");
227 }).getCause() instanceof IOException);
229 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
233 public void testCallWithNewTypedReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
234 assertTrue(assertThrows(ExecutionException.class,
236 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, writeTx -> {
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 testApplyWithNewReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
249 assertTrue(assertThrows(ExecutionException.class,
251 managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
253 writeTx.put(TEST_PATH, newTestDataObject());
254 // We now throw an arbitrary kind of checked (not unchecked!) exception here
255 throw new IOException("something didn't quite go as expected...");
257 fail("This should have led to an ExecutionException!");
258 }).getCause() instanceof IOException);
260 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
264 public void testCallWithNewWriteOnlyTransactionCommitFailedException() throws Exception {
265 assertTrue(assertThrows(ExecutionException.class,
267 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
268 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
269 writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
270 fail("This should have led to an ExecutionException!");
271 }).getCause() instanceof TransactionCommitFailedException);
273 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
277 public void testCallWithNewTypedWriteOnlyTransactionCommitFailedException() throws Exception {
278 assertTrue(assertThrows(ExecutionException.class,
280 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
281 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
282 writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
283 fail("This should have led to an ExecutionException!");
284 }).getCause() instanceof TransactionCommitFailedException);
286 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
290 public void testCallWithNewReadWriteTransactionCommitFailedException() throws Exception {
291 assertTrue(assertThrows(ExecutionException.class,
293 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
294 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
295 writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
296 fail("This should have led to an ExecutionException!");
297 }).getCause() instanceof TransactionCommitFailedException);
299 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
303 public void testCallWithNewTypedReadWriteTransactionCommitFailedException() throws Exception {
304 assertTrue(assertThrows(ExecutionException.class,
306 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
307 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
308 writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
309 fail("This should have led to an ExecutionException!");
310 }).getCause() instanceof TransactionCommitFailedException);
312 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
316 public void testApplyWithNewReadWriteTransactionCommitFailedException() throws Exception {
317 assertTrue(assertThrows(ExecutionException.class,
319 testableDataBroker.failSubmits(new TransactionCommitFailedException("bada boum bam!"));
320 managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
322 writeTx.put(TEST_PATH, newTestDataObject());
325 fail("This should have led to an ExecutionException!");
326 }).getCause() instanceof TransactionCommitFailedException);
328 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
332 public void testCallWithNewWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
333 assertTrue(assertThrows(ExecutionException.class,
335 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
336 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
337 writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
338 fail("This should have led to an ExecutionException!");
339 }).getCause() instanceof OptimisticLockFailedException);
341 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
345 public void testCallWithNewTypedWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
346 assertTrue(assertThrows(ExecutionException.class,
348 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
349 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
350 writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
351 fail("This should have led to an ExecutionException!");
352 }).getCause() instanceof OptimisticLockFailedException);
354 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
358 public void testCallWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
359 assertTrue(assertThrows(ExecutionException.class,
361 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
362 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
363 writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, newTestDataObject())).get();
364 fail("This should have led to an ExecutionException!");
365 }).getCause() instanceof OptimisticLockFailedException);
367 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
371 public void testCallWithNewTypedReadWriteTransactionOptimisticLockFailedException() throws Exception {
372 assertTrue(assertThrows(ExecutionException.class,
374 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
375 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
376 writeTx -> writeTx.put(TEST_PATH, newTestDataObject())).get();
377 fail("This should have led to an ExecutionException!");
378 }).getCause() instanceof OptimisticLockFailedException);
380 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
384 public void testApplyWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
386 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
387 managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
389 writeTx.put(TEST_PATH, newTestDataObject());
392 fail("This should have led to an ExecutionException!");
393 } catch (ExecutionException e) {
394 assertThat(e.getCause() instanceof OptimisticLockFailedException).isTrue();
397 singleTransactionDataBroker.syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
401 public void testCallWithNewWriteOnlyTransactionAndSubmitCannotCommit() {
402 assertThrows(ExecutionException.class,
403 () -> managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
404 WriteTransaction::commit).get());
408 public void testCallWithNewReadWriteTransactionAndSubmitCannotCommit() {
409 assertThrows(ExecutionException.class,
410 () -> managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
411 WriteTransaction::commit).get());
415 public void testCallWithNewWriteOnlyTransactionAndSubmitCannotCancel() {
416 assertThrows(ExecutionException.class,
417 () -> managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
418 WriteTransaction::cancel).get());
422 public void testCallWithNewReadWriteTransactionAndSubmitCannotCancel() {
423 assertThrows(ExecutionException.class,
424 () -> managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
425 WriteTransaction::cancel).get());