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