Migrate Optional.get() callers
[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 org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThrows;
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.io.IOException;
20 import java.util.Optional;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Future;
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(final 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(Optional.of(data), managedNewTransactionRunner.applyWithNewReadOnlyTransactionAndClose(OPERATIONAL,
128             tx -> tx.read(TEST_PATH)).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         Future<?> future = managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
140             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             });
145         ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
146         assertThat(ex.getCause(), instanceOf(IOException.class));
147         assertEquals(Optional.empty(), syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
148     }
149
150     @Test
151     public void testCallWithNewTypedReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
152         Future<?> future = managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
153             writeTx -> {
154                 writeTx.put(TEST_PATH, newTestDataObject());
155                 // We now throw an arbitrary kind of checked (not unchecked!) exception here
156                 throw new IOException("something didn't quite go as expected...");
157             });
158         ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
159         assertThat(ex.getCause(), instanceOf(IOException.class));
160         assertEquals(Optional.empty(), syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
161     }
162
163     @Test
164     public void testApplyWithNewReadWriteTransactionAndSubmitPutButLaterException() throws Exception {
165         Future<?> future = managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
166             writeTx -> {
167                 writeTx.put(TEST_PATH, newTestDataObject());
168                 // We now throw an arbitrary kind of checked (not unchecked!) exception here
169                 throw new IOException("something didn't quite go as expected...");
170             });
171         ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
172         assertThat(ex.getCause(), instanceOf(IOException.class));
173         assertEquals(Optional.empty(), syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
174     }
175
176     @Test
177     public void testCallWithNewTypedWriteOnlyTransactionCommitFailedException() throws Exception {
178         testableDataBroker.failCommits(new TransactionCommitFailedException("bada boum bam!"));
179         Future<?> future = managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
180             writeTx -> writeTx.put(TEST_PATH, newTestDataObject()));
181         ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
182         assertThat(ex.getCause(), instanceOf(TransactionCommitFailedException.class));
183         assertEquals(Optional.empty(), syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
184     }
185
186     @Test
187     public void testCallWithNewTypedReadWriteTransactionCommitFailedException() throws Exception {
188         testableDataBroker.failCommits(new TransactionCommitFailedException("bada boum bam!"));
189         Future<?> future = managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
190             writeTx -> writeTx.put(TEST_PATH, newTestDataObject()));
191         ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
192         assertThat(ex.getCause(), instanceOf(TransactionCommitFailedException.class));
193         assertEquals(Optional.empty(), syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
194     }
195
196     @Test
197     public void testApplyWithNewReadWriteTransactionCommitFailedException() throws Exception {
198         testableDataBroker.failCommits(new TransactionCommitFailedException("bada boum bam!"));
199         Future<?> future = managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
200             writeTx -> {
201                 writeTx.put(TEST_PATH, newTestDataObject());
202                 return 1;
203             });
204         ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
205         assertThat(ex.getCause(), instanceOf(TransactionCommitFailedException.class));
206         assertEquals(Optional.empty(), syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
207     }
208
209     @Test
210     public void testCallWithNewTypedWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
211         testableDataBroker.failCommits(2, new OptimisticLockFailedException("bada boum bam!"));
212         Future<?> future = managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
213             writeTx -> writeTx.put(TEST_PATH, newTestDataObject()));
214         ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
215         assertThat(ex.getCause(), instanceOf(OptimisticLockFailedException.class));
216         assertEquals(Optional.empty(), syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
217     }
218
219     @Test
220     public void testCallWithNewTypedReadWriteTransactionOptimisticLockFailedException() throws Exception {
221         testableDataBroker.failCommits(2, new OptimisticLockFailedException("bada boum bam!"));
222         Future<?> future = managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
223             writeTx -> writeTx.put(TEST_PATH, newTestDataObject()));
224         ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
225         assertThat(ex.getCause(), instanceOf(OptimisticLockFailedException.class));
226         assertEquals(Optional.empty(), syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
227     }
228
229     @Test
230     public void testApplyWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
231         testableDataBroker.failCommits(2, new OptimisticLockFailedException("bada boum bam!"));
232         Future<?> future = managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
233             writeTx -> {
234                 writeTx.put(TEST_PATH, newTestDataObject());
235                 return 1;
236             });
237         ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
238         assertThat(ex.getCause(), instanceOf(OptimisticLockFailedException.class));
239         assertEquals(Optional.empty(), syncReadOptional(LogicalDatastoreType.OPERATIONAL, TEST_PATH));
240     }
241
242     private <T extends DataObject> Optional<T> syncReadOptional(final LogicalDatastoreType datastoreType,
243             final InstanceIdentifier<T> path) throws ExecutionException, InterruptedException {
244         try (ReadTransaction tx = getDataBroker().newReadOnlyTransaction()) {
245             return tx.read(datastoreType, path).get();
246         }
247     }
248
249     <T extends DataObject> T syncRead(final LogicalDatastoreType datastoreType, final InstanceIdentifier<T> path)
250             throws ExecutionException, InterruptedException {
251         return syncReadOptional(datastoreType, path).orElseThrow();
252     }
253 }