116e5e75b50d261e3d9673378ee643cfe2f2e31f
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DistributedDataStoreIntegrationTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import akka.actor.ActorSystem;
4 import akka.testkit.JavaTestKit;
5 import com.google.common.base.Optional;
6 import com.google.common.util.concurrent.ListenableFuture;
7 import org.junit.After;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
11 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
12 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
13 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
14 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
15 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
16 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
21
22 import java.util.concurrent.ExecutionException;
23
24 import static junit.framework.Assert.assertEquals;
25 import static junit.framework.Assert.assertTrue;
26
27 public class DistributedDataStoreIntegrationTest{
28
29     private static ActorSystem system;
30
31     @Before
32     public void setUp() {
33         System.setProperty("shard.persistent", "false");
34         system = ActorSystem.create("test");
35     }
36
37     @After
38     public void tearDown() {
39         JavaTestKit.shutdownActorSystem(system);
40         system = null;
41     }
42
43     protected ActorSystem getSystem() {
44         return system;
45     }
46
47     @Test
48     public void integrationTest() throws Exception {
49         ShardStrategyFactory.setConfiguration(new MockConfiguration());
50         DistributedDataStore distributedDataStore =
51             new DistributedDataStore(getSystem(), "config", new MockClusterWrapper(), new MockConfiguration());
52
53         distributedDataStore.onGlobalContextUpdated(TestModel.createTestContext());
54
55         DOMStoreReadWriteTransaction transaction =
56             distributedDataStore.newReadWriteTransaction();
57
58         transaction.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
59
60         ListenableFuture<Optional<NormalizedNode<?, ?>>> future =
61             transaction.read(TestModel.TEST_PATH);
62
63         Optional<NormalizedNode<?, ?>> optional = future.get();
64
65         NormalizedNode<?, ?> normalizedNode = optional.get();
66
67         assertEquals(TestModel.TEST_QNAME, normalizedNode.getNodeType());
68
69         DOMStoreThreePhaseCommitCohort ready = transaction.ready();
70
71         ListenableFuture<Boolean> canCommit = ready.canCommit();
72
73         assertTrue(canCommit.get());
74
75         ListenableFuture<Void> preCommit = ready.preCommit();
76
77         preCommit.get();
78
79         ListenableFuture<Void> commit = ready.commit();
80
81         commit.get();
82
83     }
84
85
86     @Test
87     public void integrationTestWithMultiShardConfiguration()
88         throws ExecutionException, InterruptedException {
89         Configuration configuration = new ConfigurationImpl("module-shards.conf", "modules.conf");
90
91         ShardStrategyFactory.setConfiguration(configuration);
92         DistributedDataStore distributedDataStore =
93             new DistributedDataStore(getSystem(), "config", new MockClusterWrapper(), configuration);
94
95
96         distributedDataStore.onGlobalContextUpdated(SchemaContextHelper.full());
97
98         DOMStoreReadWriteTransaction transaction =
99             distributedDataStore.newReadWriteTransaction();
100
101         transaction.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
102         transaction.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
103
104         DOMStoreThreePhaseCommitCohort ready = transaction.ready();
105
106         ListenableFuture<Boolean> canCommit = ready.canCommit();
107
108         assertTrue(canCommit.get());
109
110         ListenableFuture<Void> preCommit = ready.preCommit();
111
112         preCommit.get();
113
114         ListenableFuture<Void> commit = ready.commit();
115
116         commit.get();
117
118     }
119
120 }