Bug 7521: Move DatastoreSnapshotList et al to persisted package
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / messages / DatastoreSnapshotListTest.java
1 /*
2  * Copyright (c) 2017 Brocade Communications 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 package org.opendaylight.controller.cluster.datastore.messages;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13
14 import java.io.ByteArrayOutputStream;
15 import java.math.BigInteger;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import org.apache.commons.lang.SerializationUtils;
19 import org.junit.Test;
20 import org.opendaylight.controller.cluster.datastore.AbstractShardTest;
21 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot;
22 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
23 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerSnapshot;
24 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
25 import org.opendaylight.controller.cluster.raft.Snapshot;
26 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
27 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
28 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
29 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
35 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
36
37 /**
38  * Unit tests for DatastoreSnapshotList.
39  *
40  * @author Thomas Pantelis
41  */
42 @Deprecated
43 public class DatastoreSnapshotListTest {
44     @Test
45     public void testSerialization() throws Exception {
46         DatastoreSnapshot legacyConfigSnapshot = new DatastoreSnapshot("config",
47                 SerializationUtils.serialize(newLegacyShardManagerSnapshot("config-one", "config-two")),
48                 Arrays.asList(newLegacyShardSnapshot("config-one", newLegacySnapshot(CarsModel.BASE_PATH,
49                         CarsModel.newCarsNode(CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima",
50                             BigInteger.valueOf(20000L)),CarsModel.newCarEntry("sportage",
51                                 BigInteger.valueOf(30000L)))))),
52                     newLegacyShardSnapshot("config-two", newLegacySnapshot(PeopleModel.BASE_PATH,
53                             PeopleModel.emptyContainer()))));
54
55         DatastoreSnapshot legacyOperSnapshot = new DatastoreSnapshot("oper",
56                 null, Arrays.asList(newLegacyShardSnapshot("oper-one", newLegacySnapshot(TestModel.TEST_PATH,
57                         ImmutableNodes.containerNode(TestModel.TEST_QNAME)))));
58
59         DatastoreSnapshotList legacy = new DatastoreSnapshotList(Arrays.asList(legacyConfigSnapshot,
60                 legacyOperSnapshot));
61
62         org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList cloned =
63             (org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList)
64                 SerializationUtils.clone(legacy);
65
66         assertEquals("DatastoreSnapshotList size", 2, cloned.size());
67         assertDatastoreSnapshotEquals(legacyConfigSnapshot, cloned.get(0));
68         assertDatastoreSnapshotEquals(legacyOperSnapshot, cloned.get(1));
69     }
70
71     private void assertDatastoreSnapshotEquals(DatastoreSnapshot legacy,
72             org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot actual) {
73         assertEquals("Type", legacy.getType(), actual.getType());
74
75         if (legacy.getShardManagerSnapshot() == null) {
76             assertNull("Expected null ShardManagerSnapshot", actual.getShardManagerSnapshot());
77         } else {
78             ShardManagerSnapshot legacyShardManagerSnapshot =
79                     (ShardManagerSnapshot) SerializationUtils.deserialize(legacy.getShardManagerSnapshot());
80             ShardManagerSnapshot actualShardManagerSnapshot =
81                     (ShardManagerSnapshot) SerializationUtils.deserialize(actual.getShardManagerSnapshot());
82             assertEquals("ShardManagerSnapshot", legacyShardManagerSnapshot.getShardList(),
83                     actualShardManagerSnapshot.getShardList());
84         }
85
86         assertEquals("ShardSnapshots size", legacy.getShardSnapshots().size(), actual.getShardSnapshots().size());
87
88         for (int i = 0; i < actual.getShardSnapshots().size(); i++) {
89             ShardSnapshot legacyShardSnapshot = legacy.getShardSnapshots().get(i);
90             org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot
91                 actualShardSnapshot = actual.getShardSnapshots().get(i);
92             assertEquals("Shard name", legacyShardSnapshot.getName(), actualShardSnapshot.getName());
93             assertSnapshotEquals((Snapshot) SerializationUtils.deserialize(legacyShardSnapshot.getSnapshot()),
94                     (Snapshot) SerializationUtils.deserialize(actualShardSnapshot.getSnapshot()));
95         }
96     }
97
98     private static void assertSnapshotEquals(Snapshot expected, Snapshot actual) {
99         assertEquals("lastIndex", expected.getLastIndex(), actual.getLastIndex());
100         assertEquals("lastTerm", expected.getLastTerm(), actual.getLastTerm());
101         assertEquals("lastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex());
102         assertEquals("lastAppliedTerm", expected.getLastAppliedTerm(), actual.getLastAppliedTerm());
103         assertEquals("unAppliedEntries", expected.getUnAppliedEntries(), actual.getUnAppliedEntries());
104         assertEquals("electionTerm", expected.getElectionTerm(), actual.getElectionTerm());
105         assertEquals("electionVotedFor", expected.getElectionVotedFor(), actual.getElectionVotedFor());
106         assertArrayEquals("state", expected.getState(), actual.getState());
107     }
108
109     private static ShardManagerSnapshot newLegacyShardManagerSnapshot(String... shards) {
110         return ShardManagerSnapshot.forShardList(Arrays.asList(shards));
111     }
112
113     private static DatastoreSnapshot.ShardSnapshot newLegacyShardSnapshot(String name,
114             org.opendaylight.controller.cluster.raft.Snapshot snapshot) {
115         return new DatastoreSnapshot.ShardSnapshot(name, SerializationUtils.serialize(snapshot));
116     }
117
118     private static Snapshot newLegacySnapshot(YangInstanceIdentifier path, NormalizedNode<?, ?> node)
119             throws Exception {
120         DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
121         dataTree.setSchemaContext(SchemaContextHelper.full());
122         AbstractShardTest.writeToStore(dataTree, path, node);
123         NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
124
125         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
126         new MetadataShardDataTreeSnapshot(root).serialize(bos);
127         return Snapshot.create(bos.toByteArray(), Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1,
128                 "member-1", null);
129     }
130 }