Remove unused exceptions
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMTransactionChainTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, 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.controller.md.sal.dom.broker.impl;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
14 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
15
16 import com.google.common.base.Optional;
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.ListeningExecutorService;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
30 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
31 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
32 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37
38 public class DOMTransactionChainTest {
39
40     private SchemaContext schemaContext;
41     private AbstractDOMDataBroker domBroker;
42
43     @Before
44     public void setupStore() {
45         InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
46         InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
47         schemaContext = TestModel.createTestContext();
48
49         operStore.onGlobalContextUpdated(schemaContext);
50         configStore.onGlobalContextUpdated(schemaContext);
51
52         ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore>builder() //
53                 .put(CONFIGURATION, configStore) //
54                 .put(OPERATIONAL, operStore) //
55                 .build();
56
57         ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
58         domBroker = new SerializedDOMDataBroker(stores, executor);
59     }
60
61     @Test
62     public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
63         BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
64         DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
65         assertNotNull(txChain);
66
67         /**
68          * We alocate new read-write transaction and write /test.
69          */
70         DOMDataReadWriteTransaction firstTx = allocateAndWrite(txChain);
71
72         /**
73          * First transaction is marked as ready, we are able to allocate chained
74          * transactions.
75          */
76         ListenableFuture<Void> firstWriteTxFuture = firstTx.submit();
77
78         /**
79          * We alocate chained transaction - read transaction.
80          */
81         DOMDataReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
82
83         /**
84          *
85          * We test if we are able to read data from tx, read should not fail
86          * since we are using chained transaction.
87          */
88         assertTestContainerExists(secondReadTx);
89
90         /**
91          * We alocate next transaction, which is still based on first one, but
92          * is read-write.
93          *
94          */
95         DOMDataReadWriteTransaction thirdDeleteTx = allocateAndDelete(txChain);
96
97         /**
98          * We commit first transaction.
99          *
100          */
101         assertCommitSuccessful(firstWriteTxFuture);
102
103         /**
104          * Allocates transaction from data store.
105          */
106         DOMDataReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
107
108         /**
109          * We verify transaction is commited to store, container should exists
110          * in datastore.
111          */
112         assertTestContainerExists(storeReadTx);
113
114         /**
115          * third transaction is sealed and commited.
116          */
117         ListenableFuture<Void> thirdDeleteTxFuture = thirdDeleteTx.submit();
118         assertCommitSuccessful(thirdDeleteTxFuture);
119
120         /**
121          * We close transaction chain.
122          */
123         txChain.close();
124
125         listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
126     }
127
128     @Test
129     @SuppressWarnings("checkstyle:IllegalCatch")
130     public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException {
131         BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
132         DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
133         assertNotNull(txChain);
134
135         /**
136          * We alocate new read-write transaction and write /test
137          */
138         allocateAndWrite(txChain);
139
140         /**
141          * We alocate chained transaction - read transaction, note first one is
142          * still not commited to datastore, so this allocation should fail with
143          * IllegalStateException.
144          */
145         try {
146             txChain.newReadOnlyTransaction();
147             fail("Allocation of secondReadTx should fail with IllegalStateException");
148         } catch (Exception e) {
149             assertTrue(e instanceof IllegalStateException);
150         }
151     }
152
153     private static DOMDataReadWriteTransaction allocateAndDelete(
154             final DOMTransactionChain txChain) throws InterruptedException, ExecutionException {
155         DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
156
157         /**
158          * We test existence of /test in third transaction container should
159          * still be visible from first one (which is still uncommmited).
160          *
161          */
162         assertTestContainerExists(tx);
163
164         /**
165          * We delete node in third transaction
166          */
167         tx.delete(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
168         return tx;
169     }
170
171     private static DOMDataReadWriteTransaction allocateAndWrite(
172             final DOMTransactionChain txChain) throws InterruptedException, ExecutionException {
173         DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
174         assertTestContainerWrite(tx);
175         return tx;
176     }
177
178     private static void assertCommitSuccessful(
179             final ListenableFuture<Void> future) throws InterruptedException, ExecutionException {
180         future.get();
181     }
182
183     private static void assertTestContainerExists(
184             final DOMDataReadTransaction readTx) throws InterruptedException, ExecutionException {
185         ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
186         Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
187         assertTrue(readedData.isPresent());
188     }
189
190     private static void assertTestContainerWrite(
191             final DOMDataReadWriteTransaction tx) throws InterruptedException, ExecutionException {
192         tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
193         assertTestContainerExists(tx);
194     }
195 }