BUG-8618: eliminate SimpleShardDataTreeCohort subclasses
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardRecoveryCoordinatorTest.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.fail;
13
14 import com.google.common.base.Optional;
15 import java.io.IOException;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload;
20 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
21 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
22 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
23 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
24 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
32 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
33 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.slf4j.LoggerFactory;
36
37 public class ShardRecoveryCoordinatorTest extends AbstractTest {
38
39     private ShardDataTree peopleDataTree;
40     private SchemaContext peopleSchemaContext;
41     private SchemaContext carsSchemaContext;
42
43     @Before
44     public void setUp() {
45         peopleSchemaContext = SchemaContextHelper.select(SchemaContextHelper.PEOPLE_YANG);
46         carsSchemaContext = SchemaContextHelper.select(SchemaContextHelper.CARS_YANG);
47
48         final Shard mockShard = Mockito.mock(Shard.class);
49
50         peopleDataTree = new ShardDataTree(mockShard, peopleSchemaContext, TreeType.OPERATIONAL);
51     }
52
53     @Test
54     public void testAppendRecoveredLogEntryCommitTransactionPayload() throws IOException {
55         final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
56                 null, "foobar", LoggerFactory.getLogger("foo"));
57         coordinator.startLogRecoveryBatch(10);
58         try {
59             coordinator.appendRecoveredLogEntry(CommitTransactionPayload.create(nextTransactionId(), createCar()));
60         } catch (final SchemaValidationFailedException e) {
61             fail("SchemaValidationFailedException should not happen if pruning is done");
62         }
63
64         coordinator.applyCurrentLogRecoveryBatch();
65     }
66
67     @Test
68     public void testApplyRecoverySnapshot() {
69         final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
70                 null, "foobar", LoggerFactory.getLogger("foo"));
71         coordinator.startLogRecoveryBatch(10);
72
73         coordinator.applyRecoverySnapshot(createSnapshot());
74
75         assertEquals(false, readCars(peopleDataTree).isPresent());
76         assertEquals(true, readPeople(peopleDataTree).isPresent());
77     }
78
79
80     @Test
81     public void testApplyCurrentLogRecoveryBatch() {
82         final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
83                 null, "foobar", LoggerFactory.getLogger("foo"));
84         coordinator.startLogRecoveryBatch(10);
85
86         try {
87             coordinator.applyCurrentLogRecoveryBatch();
88         } catch (final IllegalArgumentException e) {
89             fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
90         }
91     }
92
93     private DataTreeCandidateTip createCar() {
94         final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
95         dataTree.setSchemaContext(carsSchemaContext);
96
97         final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
98
99         final DataTreeModification modification = snapshot.newModification();
100
101         modification.merge(CarsModel.BASE_PATH, CarsModel.create());
102         modification.ready();
103         return dataTree.prepare(modification);
104     }
105
106     private Optional<NormalizedNode<?,?>> readCars(final ShardDataTree shardDataTree) {
107         final TipProducingDataTree dataTree = shardDataTree.getDataTree();
108         // FIXME: this should not be called here
109         dataTree.setSchemaContext(peopleSchemaContext);
110
111         return shardDataTree.readNode(CarsModel.BASE_PATH);
112     }
113
114     private Optional<NormalizedNode<?,?>> readPeople(final ShardDataTree shardDataTree) {
115         final TipProducingDataTree dataTree = shardDataTree.getDataTree();
116         // FIXME: this should not be called here
117         dataTree.setSchemaContext(peopleSchemaContext);
118
119         return shardDataTree.readNode(PeopleModel.BASE_PATH);
120     }
121
122     private static ShardSnapshotState createSnapshot() {
123         final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
124         dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG,
125                 SchemaContextHelper.PEOPLE_YANG));
126
127         DataTreeSnapshot snapshot = dataTree.takeSnapshot();
128
129         DataTreeModification modification = snapshot.newModification();
130
131         modification.merge(CarsModel.BASE_PATH, CarsModel.create());
132         modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
133         modification.ready();
134         dataTree.commit(dataTree.prepare(modification));
135
136         return new ShardSnapshotState(new MetadataShardDataTreeSnapshot(dataTree.takeSnapshot().readNode(
137                 YangInstanceIdentifier.EMPTY).get()));
138     }
139 }