X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FDatastoreSnapshotRestoreTest.java;h=67af9bb3487ff3ca31f886eacfee98e7d87c483c;hp=1c262ff72e2a752bc88b0431681b875bbc0ece19;hb=95c296a7c1e8e186a88a0a0dc82e080b2185db33;hpb=917283d6f5633ad981bebb6dfc6d6e0245876ab5 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestoreTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestoreTest.java index 1c262ff72e..67af9bb348 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestoreTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestoreTest.java @@ -7,7 +7,6 @@ */ package org.opendaylight.controller.cluster.datastore; -import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -16,15 +15,30 @@ import static org.junit.Assert.assertTrue; import java.io.File; import java.io.FileOutputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Collections; import org.apache.commons.lang3.SerializationUtils; import org.junit.After; import org.junit.Test; -import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot; -import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot; -import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshotList; +import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot; +import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList; +import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot; +import org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot; +import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState; +import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry; +import org.opendaylight.controller.cluster.raft.persisted.Snapshot; +import org.opendaylight.controller.md.cluster.datastore.model.CarsModel; +import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel; +import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper; +import org.opendaylight.controller.md.cluster.datastore.model.TestModel; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; +import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; +import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; +import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; + /** * Unit tests for DatastoreSnapshotRestore. @@ -46,20 +60,20 @@ public class DatastoreSnapshotRestoreTest { public void test() throws Exception { assertTrue("Failed to mkdir " + restoreDirectoryPath, restoreDirectoryFile.mkdirs()); - List shardSnapshots = new ArrayList<>(); - shardSnapshots.add(new ShardSnapshot("cars", new byte[]{1,2})); - shardSnapshots.add(new ShardSnapshot("people", new byte[]{3,4})); - final DatastoreSnapshot configSnapshot = new DatastoreSnapshot("config", null, shardSnapshots); + final DatastoreSnapshot configSnapshot = new DatastoreSnapshot("config", + newShardManagerSnapshot("config-one", "config-two"), + Arrays.asList(new DatastoreSnapshot.ShardSnapshot("config-one", newSnapshot(CarsModel.BASE_PATH, + CarsModel.newCarsNode(CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", + BigInteger.valueOf(20000L)),CarsModel.newCarEntry("sportage", + BigInteger.valueOf(30000L)))))), + new DatastoreSnapshot.ShardSnapshot("config-two", newSnapshot(PeopleModel.BASE_PATH, + PeopleModel.emptyContainer())))); - shardSnapshots = new ArrayList<>(); - shardSnapshots.add(new ShardSnapshot("cars", new byte[]{5,6})); - shardSnapshots.add(new ShardSnapshot("people", new byte[]{7,8})); - shardSnapshots.add(new ShardSnapshot("bikes", new byte[]{9,0})); - DatastoreSnapshot operSnapshot = new DatastoreSnapshot("oper", null, shardSnapshots); + DatastoreSnapshot operSnapshot = new DatastoreSnapshot("oper", + null, Arrays.asList(new DatastoreSnapshot.ShardSnapshot("oper-one", newSnapshot(TestModel.TEST_PATH, + ImmutableNodes.containerNode(TestModel.TEST_QNAME))))); - DatastoreSnapshotList snapshotList = new DatastoreSnapshotList(); - snapshotList.add(configSnapshot); - snapshotList.add(operSnapshot); + DatastoreSnapshotList snapshotList = new DatastoreSnapshotList(Arrays.asList(configSnapshot, operSnapshot)); try (FileOutputStream fos = new FileOutputStream(backupFile)) { SerializationUtils.serialize(snapshotList, fos); @@ -67,8 +81,8 @@ public class DatastoreSnapshotRestoreTest { DatastoreSnapshotRestore instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath); - verifySnapshot(configSnapshot, instance.getAndRemove("config")); - verifySnapshot(operSnapshot, instance.getAndRemove("oper")); + assertDatastoreSnapshotEquals(configSnapshot, instance.getAndRemove("config")); + assertDatastoreSnapshotEquals(operSnapshot, instance.getAndRemove("oper")); assertNull("DatastoreSnapshot was not removed", instance.getAndRemove("config")); @@ -79,17 +93,50 @@ public class DatastoreSnapshotRestoreTest { assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("oper")); } - private static void verifySnapshot(DatastoreSnapshot expected, DatastoreSnapshot actual) { + private static void assertDatastoreSnapshotEquals(DatastoreSnapshot expected, DatastoreSnapshot actual) { assertNotNull("DatastoreSnapshot is null", actual); assertEquals("getType", expected.getType(), actual.getType()); - assertTrue("ShardManager snapshots don't match", Objects.deepEquals(expected.getShardManagerSnapshot(), - actual.getShardManagerSnapshot())); + + if (expected.getShardManagerSnapshot() == null) { + assertNull("Expected null ShardManagerSnapshot", actual.getShardManagerSnapshot()); + } else { + assertEquals("ShardManagerSnapshot", expected.getShardManagerSnapshot().getShardList(), + actual.getShardManagerSnapshot().getShardList()); + } + assertEquals("ShardSnapshots size", expected.getShardSnapshots().size(), actual.getShardSnapshots().size()); for (int i = 0; i < expected.getShardSnapshots().size(); i++) { assertEquals("ShardSnapshot " + (i + 1) + " name", expected.getShardSnapshots().get(i).getName(), actual.getShardSnapshots().get(i).getName()); - assertArrayEquals("ShardSnapshot " + (i + 1) + " snapshot", + assertSnapshotEquals("ShardSnapshot " + (i + 1) + " snapshot", expected.getShardSnapshots().get(i).getSnapshot(), actual.getShardSnapshots().get(i).getSnapshot()); } } + + private static void assertSnapshotEquals(String prefix, Snapshot expected, Snapshot actual) { + assertEquals(prefix + " lastIndex", expected.getLastIndex(), actual.getLastIndex()); + assertEquals(prefix + " lastTerm", expected.getLastTerm(), actual.getLastTerm()); + assertEquals(prefix + " lastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex()); + assertEquals(prefix + " lastAppliedTerm", expected.getLastAppliedTerm(), actual.getLastAppliedTerm()); + assertEquals(prefix + " unAppliedEntries", expected.getUnAppliedEntries(), actual.getUnAppliedEntries()); + assertEquals(prefix + " electionTerm", expected.getElectionTerm(), actual.getElectionTerm()); + assertEquals(prefix + " electionVotedFor", expected.getElectionVotedFor(), actual.getElectionVotedFor()); + assertEquals(prefix + " Root node", ((ShardSnapshotState)expected.getState()).getSnapshot().getRootNode(), + ((ShardSnapshotState)actual.getState()).getSnapshot().getRootNode()); + } + + private static ShardManagerSnapshot newShardManagerSnapshot(String... shards) { + return new ShardManagerSnapshot(Arrays.asList(shards)); + } + + private static Snapshot newSnapshot(YangInstanceIdentifier path, NormalizedNode node) + throws Exception { + DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL); + dataTree.setSchemaContext(SchemaContextHelper.full()); + AbstractShardTest.writeToStore(dataTree, path, node); + NormalizedNode root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY); + + return Snapshot.create(new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)), + Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null); + } }