2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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;
25 public class ShardDataTreeTest {
27 SchemaContext fullSchema;
31 fullSchema = SchemaContextHelper.full();
35 public void testWrite() throws ExecutionException, InterruptedException {
36 modify(new ShardDataTree(fullSchema), false, true, true);
40 public void testMerge() throws ExecutionException, InterruptedException {
41 modify(new ShardDataTree(fullSchema), true, true, true);
45 private void modify(ShardDataTree shardDataTree, boolean merge, boolean expectedCarsPresent, boolean expectedPeoplePresent) throws ExecutionException, InterruptedException {
47 assertEquals(fullSchema, shardDataTree.getSchemaContext());
49 ReadWriteShardDataTreeTransaction transaction = shardDataTree.newReadWriteTransaction("txn-1", null);
51 DataTreeModification snapshot = transaction.getSnapshot();
53 assertNotNull(snapshot);
56 snapshot.merge(CarsModel.BASE_PATH, CarsModel.create());
57 snapshot.merge(PeopleModel.BASE_PATH, PeopleModel.create());
59 snapshot.write(CarsModel.BASE_PATH, CarsModel.create());
60 snapshot.write(PeopleModel.BASE_PATH, PeopleModel.create());
63 ShardDataTreeCohort cohort = shardDataTree.finishTransaction(transaction);
65 cohort.preCommit().get();
66 cohort.commit().get();
69 ReadOnlyShardDataTreeTransaction readOnlyShardDataTreeTransaction = shardDataTree.newReadOnlyTransaction("txn-2", null);
71 DataTreeSnapshot snapshot1 = readOnlyShardDataTreeTransaction.getSnapshot();
73 Optional<NormalizedNode<?, ?>> optional = snapshot1.readNode(CarsModel.BASE_PATH);
75 assertEquals(expectedCarsPresent, optional.isPresent());
77 Optional<NormalizedNode<?, ?>> optional1 = snapshot1.readNode(PeopleModel.BASE_PATH);
79 assertEquals(expectedPeoplePresent, optional1.isPresent());