Bug 499: Initial draft of in-memory datastore and data broker
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMBrokerTest.java
1 package org.opendaylight.controller.md.sal.dom.broker.impl;
2
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
7 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
8
9 import java.util.concurrent.ExecutionException;
10 import java.util.concurrent.Executors;
11
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
18 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23
24 import com.google.common.base.Optional;
25 import com.google.common.collect.ImmutableMap;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import com.google.common.util.concurrent.ListeningExecutorService;
28 import com.google.common.util.concurrent.MoreExecutors;
29
30 public class DOMBrokerTest {
31
32     private SchemaContext schemaContext;
33     private DOMDataBrokerImpl domBroker;
34
35     @Before
36     public void setupStore() {
37         InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
38         InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
39         schemaContext = TestModel.createTestContext();
40
41         operStore.onGlobalContextUpdated(schemaContext);
42         configStore.onGlobalContextUpdated(schemaContext);
43
44         ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
45                 .put(CONFIGURATION, configStore) //
46                 .put(OPERATIONAL, operStore) //
47                 .build();
48
49         ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
50         domBroker = new DOMDataBrokerImpl(stores, executor);
51     }
52
53     @Test
54     public void testTransactionIsolation() throws InterruptedException, ExecutionException {
55
56         assertNotNull(domBroker);
57
58         DOMDataReadTransaction readTx = domBroker.newReadOnlyTransaction();
59         assertNotNull(readTx);
60
61         DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
62         assertNotNull(writeTx);
63         /**
64          *
65          * Writes /test in writeTx
66          *
67          */
68         writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
69
70         /**
71          *
72          * Reads /test from writeTx Read should return container.
73          *
74          */
75         ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
76                 TestModel.TEST_PATH);
77         assertTrue(writeTxContainer.get().isPresent());
78
79         /**
80          *
81          * Reads /test from readTx Read should return Absent.
82          *
83          */
84         ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx
85                 .read(OPERATIONAL, TestModel.TEST_PATH);
86         assertFalse(readTxContainer.get().isPresent());
87     }
88
89     @Test
90     public void testTransactionCommit() throws InterruptedException, ExecutionException {
91
92         DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
93         assertNotNull(writeTx);
94         /**
95          *
96          * Writes /test in writeTx
97          *
98          */
99         writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
100
101         /**
102          *
103          * Reads /test from writeTx Read should return container.
104          *
105          */
106         ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
107                 TestModel.TEST_PATH);
108         assertTrue(writeTxContainer.get().isPresent());
109
110         writeTx.commit().get();
111
112         Optional<NormalizedNode<?, ?>> afterCommitRead = domBroker.newReadOnlyTransaction()
113                 .read(OPERATIONAL, TestModel.TEST_PATH).get();
114         assertTrue(afterCommitRead.isPresent());
115     }
116
117
118
119 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.