BUG 2970 : Create a PruningDataTreeModification
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardDataTreeTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import com.google.common.base.Optional;
7 import java.util.concurrent.ExecutionException;
8 import org.junit.Test;
9 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
10 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
11 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
12 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
13 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
15 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
16
17 public class ShardDataTreeTest {
18
19     @Test
20     public void testWrite() throws ExecutionException, InterruptedException {
21
22         SchemaContext schemaContext = SchemaContextHelper.full();
23
24         modify(new ShardDataTree(schemaContext), false, true, true);
25     }
26
27     @Test
28     public void testWriteWithMissingSchema() throws ExecutionException, InterruptedException {
29
30         SchemaContext schemaContext = SchemaContextHelper.select(SchemaContextHelper.ODL_DATASTORE_TEST_YANG, SchemaContextHelper.PEOPLE_YANG);
31
32         modify(new ShardDataTree(schemaContext), false, false, true);
33     }
34
35     @Test
36     public void testMerge() throws ExecutionException, InterruptedException {
37
38         SchemaContext schemaContext = SchemaContextHelper.full();
39
40         modify(new ShardDataTree(schemaContext), true, true, true);
41     }
42
43     @Test
44     public void testMergeWithMissingSchema() throws ExecutionException, InterruptedException {
45
46         SchemaContext schemaContext = SchemaContextHelper.select(SchemaContextHelper.ODL_DATASTORE_TEST_YANG, SchemaContextHelper.PEOPLE_YANG);
47
48         modify(new ShardDataTree(schemaContext), true, false, true);
49     }
50
51     private void modify(ShardDataTree shardDataTree, boolean merge, boolean expectedCarsPresent, boolean expectedPeoplePresent) throws ExecutionException, InterruptedException {
52         ReadWriteShardDataTreeTransaction transaction = shardDataTree.newReadWriteTransaction("txn-1", null);
53
54         DataTreeModification snapshot = transaction.getSnapshot();
55
56         assertNotNull(snapshot);
57
58         if(merge){
59             snapshot.merge(CarsModel.BASE_PATH, CarsModel.create());
60             snapshot.merge(PeopleModel.BASE_PATH, PeopleModel.create());
61         } else {
62             snapshot.write(CarsModel.BASE_PATH, CarsModel.create());
63             snapshot.write(PeopleModel.BASE_PATH, PeopleModel.create());
64         }
65
66         ShardDataTreeCohort cohort = shardDataTree.finishTransaction(transaction);
67
68         cohort.preCommit().get();
69         cohort.commit().get();
70
71
72         ReadOnlyShardDataTreeTransaction readOnlyShardDataTreeTransaction = shardDataTree.newReadOnlyTransaction("txn-2", null);
73
74         DataTreeSnapshot snapshot1 = readOnlyShardDataTreeTransaction.getSnapshot();
75
76         Optional<NormalizedNode<?, ?>> optional = snapshot1.readNode(CarsModel.BASE_PATH);
77
78         assertEquals(expectedCarsPresent, optional.isPresent());
79
80         Optional<NormalizedNode<?, ?>> optional1 = snapshot1.readNode(PeopleModel.BASE_PATH);
81
82         assertEquals(expectedPeoplePresent, optional1.isPresent());
83
84     }
85
86     @Test(expected = IllegalArgumentException.class)
87     public void testAfterRecoveryDone() throws ExecutionException, InterruptedException {
88         SchemaContext schemaContext = SchemaContextHelper.select(SchemaContextHelper.ODL_DATASTORE_TEST_YANG, SchemaContextHelper.PEOPLE_YANG);
89         ShardDataTree shardDataTree = new ShardDataTree(schemaContext);
90         assertTrue("transaction factory must be the recovery transaction factory",
91                 shardDataTree.getTransactionFactory() instanceof ShardDataTree.RecoveryShardDataTreeTransactionFactory);
92         shardDataTree.recoveryDone();
93         assertTrue("transaction factory must be the regular transaction factory",
94                 shardDataTree.getTransactionFactory() instanceof ShardDataTree.RegularShardDataTreeTransactionFactory);
95
96         modify(shardDataTree, true, false, true);
97
98     }
99
100 }