Fix license header violations in sal-distributed-datastore
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardDataTreeTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import com.google.common.base.Optional;
14 import java.util.concurrent.ExecutionException;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
18 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
19 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24
25 public class ShardDataTreeTest {
26
27     SchemaContext fullSchema;
28
29     @Before
30     public void setUp(){
31         fullSchema = SchemaContextHelper.full();
32     }
33
34     @Test
35     public void testWrite() throws ExecutionException, InterruptedException {
36         modify(new ShardDataTree(fullSchema), false, true, true);
37     }
38
39     @Test
40     public void testMerge() throws ExecutionException, InterruptedException {
41         modify(new ShardDataTree(fullSchema), true, true, true);
42     }
43
44
45     private void modify(ShardDataTree shardDataTree, boolean merge, boolean expectedCarsPresent, boolean expectedPeoplePresent) throws ExecutionException, InterruptedException {
46
47         assertEquals(fullSchema, shardDataTree.getSchemaContext());
48
49         ReadWriteShardDataTreeTransaction transaction = shardDataTree.newReadWriteTransaction("txn-1", null);
50
51         DataTreeModification snapshot = transaction.getSnapshot();
52
53         assertNotNull(snapshot);
54
55         if(merge){
56             snapshot.merge(CarsModel.BASE_PATH, CarsModel.create());
57             snapshot.merge(PeopleModel.BASE_PATH, PeopleModel.create());
58         } else {
59             snapshot.write(CarsModel.BASE_PATH, CarsModel.create());
60             snapshot.write(PeopleModel.BASE_PATH, PeopleModel.create());
61         }
62
63         ShardDataTreeCohort cohort = shardDataTree.finishTransaction(transaction);
64
65         cohort.preCommit().get();
66         cohort.commit().get();
67
68
69         ReadOnlyShardDataTreeTransaction readOnlyShardDataTreeTransaction = shardDataTree.newReadOnlyTransaction("txn-2", null);
70
71         DataTreeSnapshot snapshot1 = readOnlyShardDataTreeTransaction.getSnapshot();
72
73         Optional<NormalizedNode<?, ?>> optional = snapshot1.readNode(CarsModel.BASE_PATH);
74
75         assertEquals(expectedCarsPresent, optional.isPresent());
76
77         Optional<NormalizedNode<?, ?>> optional1 = snapshot1.readNode(PeopleModel.BASE_PATH);
78
79         assertEquals(expectedPeoplePresent, optional1.isPresent());
80
81     }
82
83 }