Fix intermittent unit test failures
[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 import com.google.common.base.Optional;
14 import java.io.IOException;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
18 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
19 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
20 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload;
21 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
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 {
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         peopleDataTree = new ShardDataTree(peopleSchemaContext, TreeType.OPERATIONAL);
49     }
50
51     @Test
52     public void testAppendRecoveredLogEntryDataTreeCandidatePayload(){
53         final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
54                 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
55         coordinator.startLogRecoveryBatch(10);
56         try {
57             coordinator.appendRecoveredLogEntry(DataTreeCandidatePayload.create(createCar()));
58         } catch(final SchemaValidationFailedException e){
59             fail("SchemaValidationFailedException should not happen if pruning is done");
60         }
61
62         coordinator.applyCurrentLogRecoveryBatch();
63     }
64
65     @Test
66     public void testAppendRecoveredLogEntryCompositeModificationPayload() throws IOException {
67         final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
68                 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
69         coordinator.startLogRecoveryBatch(10);
70         try {
71             final MutableCompositeModification modification  = new MutableCompositeModification((short) 1);
72             modification.addModification(new WriteModification(CarsModel.BASE_PATH, CarsModel.create()));
73             coordinator.appendRecoveredLogEntry(new CompositeModificationPayload(modification.toSerializable()));
74         } catch(final SchemaValidationFailedException e){
75             fail("SchemaValidationFailedException should not happen if pruning is done");
76         }
77     }
78
79     @Test
80     public void testAppendRecoveredLogEntryCompositeModificationByteStringPayload() throws IOException {
81         final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
82                 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
83         coordinator.startLogRecoveryBatch(10);
84         try {
85             final MutableCompositeModification modification  = new MutableCompositeModification((short) 1);
86             modification.addModification(new WriteModification(CarsModel.BASE_PATH, CarsModel.create()));
87             coordinator.appendRecoveredLogEntry(new CompositeModificationByteStringPayload(modification.toSerializable()));
88         } catch(final SchemaValidationFailedException e){
89             fail("SchemaValidationFailedException should not happen if pruning is done");
90         }
91
92         assertEquals(false, readCars(peopleDataTree).isPresent());
93     }
94
95     @Test
96     public void testApplyRecoverySnapshot(){
97         final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
98                 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
99         coordinator.startLogRecoveryBatch(10);
100
101         coordinator.applyRecoverySnapshot(createSnapshot());
102
103         assertEquals(false, readCars(peopleDataTree).isPresent());
104         assertEquals(true, readPeople(peopleDataTree).isPresent());
105     }
106
107
108     @Test
109     public void testApplyCurrentLogRecoveryBatch(){
110         final ShardRecoveryCoordinator coordinator = new ShardRecoveryCoordinator(peopleDataTree,
111                 peopleSchemaContext, null, "foobar", LoggerFactory.getLogger("foo"));
112         coordinator.startLogRecoveryBatch(10);
113
114         try {
115             coordinator.applyCurrentLogRecoveryBatch();
116         } catch(final IllegalArgumentException e){
117             fail("IllegalArgumentException should not happen - if the pruning modification delegate is passed");
118         }
119     }
120
121     private DataTreeCandidateTip createCar(){
122         final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
123         dataTree.setSchemaContext(carsSchemaContext);
124
125         final DataTreeSnapshot snapshot = dataTree.takeSnapshot();
126
127         final DataTreeModification modification = snapshot.newModification();
128
129         modification.merge(CarsModel.BASE_PATH, CarsModel.create());
130         modification.ready();
131         return dataTree.prepare(modification);
132     }
133
134     private Optional<NormalizedNode<?,?>> readCars(final ShardDataTree shardDataTree){
135         final TipProducingDataTree dataTree = shardDataTree.getDataTree();
136         // FIXME: this should not be called here
137         dataTree.setSchemaContext(peopleSchemaContext);
138
139         return shardDataTree.readNode(CarsModel.BASE_PATH);
140     }
141
142     private Optional<NormalizedNode<?,?>> readPeople(final ShardDataTree shardDataTree){
143         final TipProducingDataTree dataTree = shardDataTree.getDataTree();
144         // FIXME: this should not be called here
145         dataTree.setSchemaContext(peopleSchemaContext);
146
147         return shardDataTree.readNode(PeopleModel.BASE_PATH);
148     }
149
150     private static byte[] createSnapshot(){
151         final TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
152         dataTree.setSchemaContext(SchemaContextHelper.select(SchemaContextHelper.CARS_YANG, SchemaContextHelper.PEOPLE_YANG));
153
154         DataTreeSnapshot snapshot = dataTree.takeSnapshot();
155
156         DataTreeModification modification = snapshot.newModification();
157
158         modification.merge(CarsModel.BASE_PATH, CarsModel.create());
159         modification.merge(PeopleModel.BASE_PATH, PeopleModel.create());
160         modification.ready();
161         final DataTreeCandidateTip prepare = dataTree.prepare(modification);
162
163         dataTree.commit(prepare);
164
165         snapshot = dataTree.takeSnapshot();
166
167         modification = snapshot.newModification();
168
169         final Optional<NormalizedNode<?, ?>> optional = modification.readNode(YangInstanceIdentifier.EMPTY);
170
171         final byte[] bytes = SerializationUtils.serializeNormalizedNode(optional.get());
172
173         return bytes;
174
175
176     }
177 }