Clean up mdsal-binding-util test
[mdsal.git] / binding / mdsal-binding-util-tests / src / test / java / org / opendaylight / mdsal / binding / util / TransactionAdapterTest.java
1 /*
2  * Copyright © 2018 Red Hat, Inc. and others.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.util;
9
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;
18
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;
34
35 /**
36  * Test for {@link TransactionAdapter}.
37  */
38 // This is a test for a deprecated class
39 @SuppressWarnings("deprecation")
40 public class TransactionAdapterTest extends AbstractConcurrentDataBrokerTest {
41
42     private static final InstanceIdentifier<TopLevelList> TEST_PATH = path(TOP_FOO_KEY);
43
44     private ManagedNewTransactionRunner managedNewTransactionRunner;
45     private DataBrokerFailuresImpl testableDataBroker;
46
47     private ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(DataBroker dataBroker) {
48         return new ManagedNewTransactionRunnerImpl(dataBroker);
49     }
50
51     @Before
52     public void beforeTest() throws Exception {
53         setup();
54         testableDataBroker = new DataBrokerFailuresImpl(getDataBroker());
55         managedNewTransactionRunner = createManagedNewTransactionRunnerToTest(testableDataBroker);
56     }
57
58     @Test
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));
65     }
66
67     @Test
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));
74     }
75
76     @Test
77     public void testAdaptedWriteTransactionFailsOnInvalidDatastore() throws Exception {
78         try {
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);
85         }
86         assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
87     }
88
89     @Test
90     public void testAdaptedReadWriteTransactionFailsOnInvalidDatastore() throws Exception {
91         try {
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);
98         }
99         assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
100     }
101
102     @Test(expected = ExecutionException.class)
103     public void testAdaptedWriteTransactionCannotCommit() throws Exception {
104         managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
105             tx -> TransactionAdapter.toWriteTransaction(tx).commit()).get();
106     }
107
108     @Test(expected = ExecutionException.class)
109     public void testAdaptedReadWriteTransactionCannotCommit() throws Exception {
110         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
111             tx -> TransactionAdapter.toReadWriteTransaction(tx).commit()).get();
112     }
113
114     @Test(expected = ExecutionException.class)
115     public void testAdaptedWriteTransactionCannotCancel() throws Exception {
116         managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
117             tx -> TransactionAdapter.toWriteTransaction(tx).cancel()).get();
118     }
119
120     @Test(expected = ExecutionException.class)
121     public void testAdaptedReadWriteTransactionCannotCancel() throws Exception {
122         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
123             tx -> TransactionAdapter.toReadWriteTransaction(tx).cancel()).get();
124     }
125
126     private TopLevelList newTestDataObject() {
127         TreeComplexUsesAugment fooAugment = new TreeComplexUsesAugmentBuilder()
128             .setContainerWithUses(new ContainerWithUsesBuilder().setLeafFromGrouping("foo").build()).build();
129         return topLevelList(TOP_FOO_KEY, fooAugment);
130     }
131
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();
136         }
137     }
138
139     private <T extends DataObject> T syncRead(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path)
140             throws ExecutionException, InterruptedException {
141         return syncReadOptional(datastoreType, path).get();
142     }
143 }