2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
18 import java.io.FileOutputStream;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Objects;
22 import org.apache.commons.lang3.SerializationUtils;
23 import org.junit.After;
24 import org.junit.Test;
25 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
26 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot;
27 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshotList;
30 * Unit tests for DatastoreSnapshotRestore.
32 * @author Thomas Pantelis
34 public class DatastoreSnapshotRestoreTest {
35 String restoreDirectoryPath = "target/DatastoreSnapshotRestoreTest-" + System.nanoTime();
36 File restoreDirectoryFile = new File(restoreDirectoryPath);
37 File backupFile = new File(restoreDirectoryFile, "backup");
40 public void tearDown() {
42 restoreDirectoryFile.delete();
46 public void test() throws Exception {
47 assertTrue("Failed to mkdir " + restoreDirectoryPath, restoreDirectoryFile.mkdirs());
49 List<ShardSnapshot> shardSnapshots = new ArrayList<>();
50 shardSnapshots.add(new ShardSnapshot("cars", new byte[]{1,2}));
51 shardSnapshots.add(new ShardSnapshot("people", new byte[]{3,4}));
52 final DatastoreSnapshot configSnapshot = new DatastoreSnapshot("config", null, shardSnapshots);
54 shardSnapshots = new ArrayList<>();
55 shardSnapshots.add(new ShardSnapshot("cars", new byte[]{5,6}));
56 shardSnapshots.add(new ShardSnapshot("people", new byte[]{7,8}));
57 shardSnapshots.add(new ShardSnapshot("bikes", new byte[]{9,0}));
58 DatastoreSnapshot operSnapshot = new DatastoreSnapshot("oper", null, shardSnapshots);
60 DatastoreSnapshotList snapshotList = new DatastoreSnapshotList();
61 snapshotList.add(configSnapshot);
62 snapshotList.add(operSnapshot);
64 try (FileOutputStream fos = new FileOutputStream(backupFile)) {
65 SerializationUtils.serialize(snapshotList, fos);
68 DatastoreSnapshotRestore instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath);
70 verifySnapshot(configSnapshot, instance.getAndRemove("config"));
71 verifySnapshot(operSnapshot, instance.getAndRemove("oper"));
73 assertNull("DatastoreSnapshot was not removed", instance.getAndRemove("config"));
75 assertFalse(backupFile + " was not deleted", backupFile.exists());
77 instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath);
78 assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("config"));
79 assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("oper"));
82 private static void verifySnapshot(DatastoreSnapshot expected, DatastoreSnapshot actual) {
83 assertNotNull("DatastoreSnapshot is null", actual);
84 assertEquals("getType", expected.getType(), actual.getType());
85 assertTrue("ShardManager snapshots don't match", Objects.deepEquals(expected.getShardManagerSnapshot(),
86 actual.getShardManagerSnapshot()));
87 assertEquals("ShardSnapshots size", expected.getShardSnapshots().size(), actual.getShardSnapshots().size());
88 for (int i = 0; i < expected.getShardSnapshots().size(); i++) {
89 assertEquals("ShardSnapshot " + (i + 1) + " name", expected.getShardSnapshots().get(i).getName(),
90 actual.getShardSnapshots().get(i).getName());
91 assertArrayEquals("ShardSnapshot " + (i + 1) + " snapshot",
92 expected.getShardSnapshots().get(i).getSnapshot(), actual.getShardSnapshots().get(i).getSnapshot());