2 * Copyright © 2018 Red Hat, Inc. and others.
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.Truth8.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.mdsal.binding.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
15 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.path;
16 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.topLevelList;
17 import static org.opendaylight.mdsal.binding.util.Datastore.OPERATIONAL;
19 import java.util.Optional;
20 import java.util.concurrent.ExecutionException;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.binding.api.ReadTransaction;
25 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
26 import org.opendaylight.mdsal.binding.testutils.DataBrokerFailuresImpl;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugmentBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.complex.from.grouping.ContainerWithUsesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 * Test for {@link TransactionAdapter}.
38 // This is a test for a deprecated class
39 @SuppressWarnings("deprecation")
40 public class TransactionAdapterTest extends AbstractConcurrentDataBrokerTest {
42 private static final InstanceIdentifier<TopLevelList> TEST_PATH = path(TOP_FOO_KEY);
44 private ManagedNewTransactionRunner managedNewTransactionRunner;
45 private DataBrokerFailuresImpl testableDataBroker;
47 private ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(DataBroker dataBroker) {
48 return new ManagedNewTransactionRunnerImpl(dataBroker);
52 public void beforeTest() throws Exception {
54 testableDataBroker = new DataBrokerFailuresImpl(getDataBroker());
55 managedNewTransactionRunner = createManagedNewTransactionRunnerToTest(testableDataBroker);
59 public void testAdaptedWriteTransactionPutsSuccessfully() throws Exception {
60 TopLevelList data = newTestDataObject();
61 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
62 writeTx -> TransactionAdapter.toWriteTransaction(writeTx).put(LogicalDatastoreType.OPERATIONAL,
63 TEST_PATH, data)).get();
64 assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
68 public void testAdaptedReadWriteTransactionPutsSuccessfully() throws Exception {
69 TopLevelList data = newTestDataObject();
70 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
71 writeTx -> TransactionAdapter.toReadWriteTransaction(writeTx).put(LogicalDatastoreType.OPERATIONAL,
72 TEST_PATH, data)).get();
73 assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
77 public void testAdaptedWriteTransactionFailsOnInvalidDatastore() throws Exception {
79 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
80 writeTx -> TransactionAdapter.toWriteTransaction(writeTx).put(LogicalDatastoreType.CONFIGURATION,
81 TEST_PATH, newTestDataObject())).get();
82 fail("This should have led to an ExecutionException!");
83 } catch (ExecutionException e) {
84 assertTrue(e.getCause() instanceof IllegalArgumentException);
86 assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
90 public void testAdaptedReadWriteTransactionFailsOnInvalidDatastore() throws Exception {
92 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
93 writeTx -> TransactionAdapter.toReadWriteTransaction(writeTx).put(LogicalDatastoreType.CONFIGURATION,
94 TEST_PATH, newTestDataObject())).get();
95 fail("This should have led to an ExecutionException!");
96 } catch (ExecutionException e) {
97 assertTrue(e.getCause() instanceof IllegalArgumentException);
99 assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
102 @Test(expected = ExecutionException.class)
103 public void testAdaptedWriteTransactionCannotCommit() throws Exception {
104 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
105 tx -> TransactionAdapter.toWriteTransaction(tx).commit()).get();
108 @Test(expected = ExecutionException.class)
109 public void testAdaptedReadWriteTransactionCannotCommit() throws Exception {
110 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
111 tx -> TransactionAdapter.toReadWriteTransaction(tx).commit()).get();
114 @Test(expected = ExecutionException.class)
115 public void testAdaptedWriteTransactionCannotCancel() throws Exception {
116 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
117 tx -> TransactionAdapter.toWriteTransaction(tx).cancel()).get();
120 @Test(expected = ExecutionException.class)
121 public void testAdaptedReadWriteTransactionCannotCancel() throws Exception {
122 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
123 tx -> TransactionAdapter.toReadWriteTransaction(tx).cancel()).get();
126 private TopLevelList newTestDataObject() {
127 TreeComplexUsesAugment fooAugment = new TreeComplexUsesAugmentBuilder()
128 .setContainerWithUses(new ContainerWithUsesBuilder().setLeafFromGrouping("foo").build()).build();
129 return topLevelList(TOP_FOO_KEY, fooAugment);
132 private <T extends DataObject> Optional<T> syncReadOptional(LogicalDatastoreType datastoreType,
133 InstanceIdentifier<T> path) throws ExecutionException, InterruptedException {
134 try (ReadTransaction tx = getDataBroker().newReadOnlyTransaction()) {
135 return tx.read(datastoreType, path).get();
139 private <T extends DataObject> T syncRead(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path)
140 throws ExecutionException, InterruptedException {
141 return syncReadOptional(datastoreType, path).get();