BUG 2970 : Recovery fails with SchemaValidationException when removing modules
[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 com.google.common.base.Optional;
6 import java.util.concurrent.ExecutionException;
7 import org.junit.Before;
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     SchemaContext fullSchema;
20
21     @Before
22     public void setUp(){
23         fullSchema = SchemaContextHelper.full();
24     }
25
26     @Test
27     public void testWrite() throws ExecutionException, InterruptedException {
28         modify(new ShardDataTree(fullSchema), false, true, true);
29     }
30
31     @Test
32     public void testMerge() throws ExecutionException, InterruptedException {
33         modify(new ShardDataTree(fullSchema), true, true, true);
34     }
35
36
37     private void modify(ShardDataTree shardDataTree, boolean merge, boolean expectedCarsPresent, boolean expectedPeoplePresent) throws ExecutionException, InterruptedException {
38
39         assertEquals(fullSchema, shardDataTree.getSchemaContext());
40
41         ReadWriteShardDataTreeTransaction transaction = shardDataTree.newReadWriteTransaction("txn-1", null);
42
43         DataTreeModification snapshot = transaction.getSnapshot();
44
45         assertNotNull(snapshot);
46
47         if(merge){
48             snapshot.merge(CarsModel.BASE_PATH, CarsModel.create());
49             snapshot.merge(PeopleModel.BASE_PATH, PeopleModel.create());
50         } else {
51             snapshot.write(CarsModel.BASE_PATH, CarsModel.create());
52             snapshot.write(PeopleModel.BASE_PATH, PeopleModel.create());
53         }
54
55         ShardDataTreeCohort cohort = shardDataTree.finishTransaction(transaction);
56
57         cohort.preCommit().get();
58         cohort.commit().get();
59
60
61         ReadOnlyShardDataTreeTransaction readOnlyShardDataTreeTransaction = shardDataTree.newReadOnlyTransaction("txn-2", null);
62
63         DataTreeSnapshot snapshot1 = readOnlyShardDataTreeTransaction.getSnapshot();
64
65         Optional<NormalizedNode<?, ?>> optional = snapshot1.readNode(CarsModel.BASE_PATH);
66
67         assertEquals(expectedCarsPresent, optional.isPresent());
68
69         Optional<NormalizedNode<?, ?>> optional1 = snapshot1.readNode(PeopleModel.BASE_PATH);
70
71         assertEquals(expectedPeoplePresent, optional1.isPresent());
72
73     }
74
75 }