1 package org.opendaylight.controller.cluster.datastore;
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;
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;
17 public class ShardDataTreeTest {
20 public void testWrite() throws ExecutionException, InterruptedException {
22 SchemaContext schemaContext = SchemaContextHelper.full();
24 modify(new ShardDataTree(schemaContext), false, true, true);
28 public void testWriteWithMissingSchema() throws ExecutionException, InterruptedException {
30 SchemaContext schemaContext = SchemaContextHelper.select(SchemaContextHelper.ODL_DATASTORE_TEST_YANG, SchemaContextHelper.PEOPLE_YANG);
32 modify(new ShardDataTree(schemaContext), false, false, true);
36 public void testMerge() throws ExecutionException, InterruptedException {
38 SchemaContext schemaContext = SchemaContextHelper.full();
40 modify(new ShardDataTree(schemaContext), true, true, true);
44 public void testMergeWithMissingSchema() throws ExecutionException, InterruptedException {
46 SchemaContext schemaContext = SchemaContextHelper.select(SchemaContextHelper.ODL_DATASTORE_TEST_YANG, SchemaContextHelper.PEOPLE_YANG);
48 modify(new ShardDataTree(schemaContext), true, false, true);
51 private void modify(ShardDataTree shardDataTree, boolean merge, boolean expectedCarsPresent, boolean expectedPeoplePresent) throws ExecutionException, InterruptedException {
52 ReadWriteShardDataTreeTransaction transaction = shardDataTree.newReadWriteTransaction("txn-1", null);
54 DataTreeModification snapshot = transaction.getSnapshot();
56 assertNotNull(snapshot);
59 snapshot.merge(CarsModel.BASE_PATH, CarsModel.create());
60 snapshot.merge(PeopleModel.BASE_PATH, PeopleModel.create());
62 snapshot.write(CarsModel.BASE_PATH, CarsModel.create());
63 snapshot.write(PeopleModel.BASE_PATH, PeopleModel.create());
66 ShardDataTreeCohort cohort = shardDataTree.finishTransaction(transaction);
68 cohort.preCommit().get();
69 cohort.commit().get();
72 ReadOnlyShardDataTreeTransaction readOnlyShardDataTreeTransaction = shardDataTree.newReadOnlyTransaction("txn-2", null);
74 DataTreeSnapshot snapshot1 = readOnlyShardDataTreeTransaction.getSnapshot();
76 Optional<NormalizedNode<?, ?>> optional = snapshot1.readNode(CarsModel.BASE_PATH);
78 assertEquals(expectedCarsPresent, optional.isPresent());
80 Optional<NormalizedNode<?, ?>> optional1 = snapshot1.readNode(PeopleModel.BASE_PATH);
82 assertEquals(expectedPeoplePresent, optional1.isPresent());
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);
96 modify(shardDataTree, true, false, true);