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