Clean up mdsal-binding-util test
[mdsal.git] / binding / mdsal-binding-util / src / test / java / org / opendaylight / mdsal / binding / util / ManagedNewTransactionRunnerImplTest.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
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.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;
19
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;
38
39 /**
40  * Test for {@link ManagedNewTransactionRunnerImpl}.
41  *
42  * @author Michael Vorburger.ch
43  * @author Stephen Kitt
44  */
45 public class ManagedNewTransactionRunnerImplTest extends AbstractConcurrentDataBrokerTest {
46
47     static final InstanceIdentifier<TopLevelList> TEST_PATH = path(TOP_FOO_KEY);
48
49     DataBrokerFailuresImpl testableDataBroker;
50     ManagedNewTransactionRunner managedNewTransactionRunner;
51
52     public ManagedNewTransactionRunnerImplTest() {
53         super(true);
54     }
55
56     protected ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(DataBroker dataBroker) {
57         return new ManagedNewTransactionRunnerImpl(dataBroker);
58     }
59
60     @Before
61     public void beforeTest() throws Exception {
62         setup();
63         testableDataBroker = new DataBrokerFailuresImpl(getDataBroker());
64         managedNewTransactionRunner = createManagedNewTransactionRunnerToTest(testableDataBroker);
65     }
66
67     @Test
68     public void testApplyWithNewReadTransactionAndCloseEmptySuccessfully() {
69         assertEquals(Long.valueOf(1),
70             managedNewTransactionRunner.applyWithNewReadOnlyTransactionAndClose(OPERATIONAL, tx -> 1L));
71     }
72
73     @Test
74     public void testCallWithNewReadTransactionAndCloseEmptySuccessfully() {
75         managedNewTransactionRunner.callWithNewReadOnlyTransactionAndClose(OPERATIONAL, tx -> { });
76     }
77
78     @Test
79     public void testCallWithNewTypedWriteOnlyTransactionAndSubmitEmptySuccessfully() throws Exception {
80         managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeTx -> { }).get();
81     }
82
83     @Test
84     public void testCallWithNewTypedReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
85         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> { }).get();
86     }
87
88     @Test
89     public void testApplyWithNewReadWriteTransactionAndSubmitEmptySuccessfully() throws Exception {
90         assertEquals(1,
91             (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
92                 tx -> 1).get());
93     }
94
95     @Test
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));
101     }
102
103     @Test
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));
109     }
110
111     @Test
112     public void testApplyWithNewReadWriteTransactionAndSubmitPutSuccessfully() throws Exception {
113         TopLevelList data = newTestDataObject();
114         assertEquals(1, (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(
115             OPERATIONAL, tx -> {
116                 tx.put(TEST_PATH, data);
117                 return 1;
118             }).get());
119         assertEquals(data, syncRead(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
120     }
121
122     @Test
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());
129     }
130
131     TopLevelList newTestDataObject() {
132         TreeComplexUsesAugment fooAugment = new TreeComplexUsesAugmentBuilder()
133                 .setContainerWithUses(new ContainerWithUsesBuilder().setLeafFromGrouping("foo").build()).build();
134         return topLevelList(TOP_FOO_KEY, fooAugment);
135     }
136
137     @Test
138     public void testCallWithNewTypedWriteOnlyTransactionAndSubmitPutButLaterException() throws Exception {
139         try {
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...");
144             }).get();
145             fail("This should have led to an ExecutionException!");
146         } catch (ExecutionException e) {
147             assertTrue(e.getCause() instanceof IOException);
148         }
149         assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
150     }
151
152     @Test
153     public void testCallWithNewTypedReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
154         try {
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...");
159             }).get();
160             fail("This should have led to an ExecutionException!");
161         } catch (ExecutionException e) {
162             assertTrue(e.getCause() instanceof IOException);
163         }
164         assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
165     }
166
167     @Test
168     public void testApplyWithNewReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
169         try {
170             managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
171                 writeTx -> {
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...");
175                 }).get();
176             fail("This should have led to an ExecutionException!");
177         } catch (ExecutionException e) {
178             assertTrue(e.getCause() instanceof IOException);
179         }
180         assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
181     }
182
183     @Test
184     public void testCallWithNewTypedWriteOnlyTransactionCommitFailedException() throws Exception {
185         try {
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);
192         }
193         assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
194     }
195
196     @Test
197     public void testCallWithNewTypedReadWriteTransactionCommitFailedException() throws Exception {
198         try {
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);
205         }
206         assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
207     }
208
209     @Test
210     public void testApplyWithNewReadWriteTransactionCommitFailedException() throws Exception {
211         try {
212             testableDataBroker.failCommits(new TransactionCommitFailedException("bada boum bam!"));
213             managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
214                 writeTx -> {
215                     writeTx.put(TEST_PATH, newTestDataObject());
216                     return 1;
217                 }).get();
218             fail("This should have led to an ExecutionException!");
219         } catch (ExecutionException e) {
220             assertTrue(e.getCause() instanceof TransactionCommitFailedException);
221         }
222         assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
223     }
224
225     @Test
226     public void testCallWithNewTypedWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
227         try {
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);
234         }
235         assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
236     }
237
238     @Test
239     public void testCallWithNewTypedReadWriteTransactionOptimisticLockFailedException() throws Exception {
240         try {
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);
247         }
248         assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
249     }
250
251     @Test
252     public void testApplyWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
253         try {
254             testableDataBroker.failCommits(2, new OptimisticLockFailedException("bada boum bam!"));
255             managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
256                 writeTx -> {
257                     writeTx.put(TEST_PATH, newTestDataObject());
258                     return 1;
259                 }).get();
260             fail("This should have led to an ExecutionException!");
261         } catch (ExecutionException e) {
262             assertThat(e.getCause() instanceof OptimisticLockFailedException).isTrue();
263         }
264         assertThat(syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH)).isEmpty();
265     }
266
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();
271         }
272     }
273
274     <T extends DataObject> T syncRead(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path)
275             throws ExecutionException, InterruptedException {
276         return syncReadOptional(datastoreType, path).get();
277     }
278 }