Use Assert.assertThrows()
[genius.git] / mdsalutil / mdsalutil-testutils / src / test / java / org / opendaylight / genius / infra / tests / 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 /* org.opendaylight.genius.infra.tests;
9
10 import static com.google.common.truth.Truth.assertThat;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
16 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
17 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList;
18 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
19 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
20
21 import java.util.concurrent.ExecutionException;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.binding.test.DataBrokerTestModule;
26 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
27 import org.opendaylight.genius.datastoreutils.testutils.DataBrokerFailuresImpl;
28 import org.opendaylight.genius.infra.Datastore;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
30 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
31 import org.opendaylight.genius.infra.TransactionAdapter;
32 import org.opendaylight.infrautils.testutils.LogCaptureRule;
33 import org.opendaylight.infrautils.testutils.LogRule;
34 import org.opendaylight.mdsal.binding.api.DataBroker;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugmentBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.complex.from.grouping.ContainerWithUsesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41
42 /**
43  * Test for {@link TransactionAdapter}.
44  */
45 // This is a test for a deprecated class
46 /*@SuppressWarnings("deprecation")
47 public class TransactionAdapterTest {
48
49     private static final InstanceIdentifier<TopLevelList> TEST_PATH = path(TOP_FOO_KEY);
50
51     public @Rule
52         LogRule logRule = new LogRule();
53     public @Rule
54         LogCaptureRule logCaptureRule = new LogCaptureRule();
55
56     private SingleTransactionDataBroker singleTransactionDataBroker;
57     private ManagedNewTransactionRunner managedNewTransactionRunner;
58
59     private ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(DataBroker dataBroker) {
60         return new ManagedNewTransactionRunnerImpl(dataBroker);
61     }
62
63     @Before
64     public void beforeTest() {
65         DataBrokerFailuresImpl testableDataBroker =
66             new DataBrokerFailuresImpl(new DataBrokerTestModule(true).getDataBroker());
67         managedNewTransactionRunner = createManagedNewTransactionRunnerToTest(testableDataBroker);
68         singleTransactionDataBroker = new SingleTransactionDataBroker(testableDataBroker);
69     }
70
71     @Test
72     public void testAdaptedWriteTransactionPutsSuccessfully() throws Exception {
73         TopLevelList data = newTestDataObject();
74         managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.OPERATIONAL,
75             writeTx -> TransactionAdapter.toWriteTransaction(writeTx).put(LogicalDatastoreType.OPERATIONAL,
76                     TEST_PATH, data)).get();
77         assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
78     }
79
80     @Test
81     public void testAdaptedReadWriteTransactionPutsSuccessfully() throws Exception {
82         TopLevelList data = newTestDataObject();
83         managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.OPERATIONAL,
84             writeTx -> TransactionAdapter.toReadWriteTransaction(writeTx).put(LogicalDatastoreType.OPERATIONAL,
85                     TEST_PATH, data)).get();
86         assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
87     }
88
89     @Test
90     public void testAdaptedWriteTransactionFailsOnInvalidDatastore() throws Exception {
91         assertTrue(assertThrows(ExecutionException.class, () -> {
92             managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.OPERATIONAL,
93                 writeTx -> TransactionAdapter.toWriteTransaction(writeTx).put(CONFIGURATION, TEST_PATH,
94                     newTestDataObject())).get();
95             fail("This should have led to an ExecutionException!");
96         }).getCause() instanceof IllegalArgumentException);
97         assertThat(singleTransactionDataBroker.syncReadOptional(OPERATIONAL, TEST_PATH)).isAbsent();
98     }
99
100     @Test
101     public void testAdaptedReadWriteTransactionFailsOnInvalidDatastore() throws Exception {
102         assertTrue(assertThrows(ExecutionException.class, () -> {
103             managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.OPERATIONAL,
104                 writeTx -> TransactionAdapter.toReadWriteTransaction(writeTx).put(CONFIGURATION, TEST_PATH,
105                     newTestDataObject())).get();
106             fail("This should have led to an ExecutionException!");
107         }).getCause() instanceof IllegalArgumentException);
108         assertThat(singleTransactionDataBroker.syncReadOptional(OPERATIONAL, TEST_PATH)).isAbsent();
109     }
110
111     @Test
112     public void testAdaptedWriteTransactionCannotCommit() {
113         assertThrows(ExecutionException.class,
114             () -> managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.OPERATIONAL,
115                 tx -> TransactionAdapter.toWriteTransaction(tx).commit()).get());
116     }
117
118     @Test
119     public void testAdaptedReadWriteTransactionCannotCommit() {
120         assertThrows(ExecutionException.class,
121             () -> managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.OPERATIONAL,
122                 tx -> TransactionAdapter.toReadWriteTransaction(tx).commit()).get());
123     }
124
125     @Test
126     public void testAdaptedWriteTransactionCannotCancel() {
127         assertThrows(ExecutionException.class,
128             () -> managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.OPERATIONAL,
129                 tx -> TransactionAdapter.toWriteTransaction(tx).cancel()).get());
130     }
131
132     @Test
133     public void testAdaptedReadWriteTransactionCannotCancel() {
134         assertThrows(ExecutionException.class,
135             () -> managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.OPERATIONAL,
136                 tx -> TransactionAdapter.toReadWriteTransaction(tx).cancel()).get());
137     }
138
139     private TopLevelList newTestDataObject() {
140         TreeComplexUsesAugment fooAugment = new TreeComplexUsesAugmentBuilder()
141             .setContainerWithUses(new ContainerWithUsesBuilder().setLeafFromGrouping("foo").build()).build();
142         return topLevelList(TOP_FOO_KEY, fooAugment);
143     }
144
145 }*/