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;
17 import java.io.FileOutputStream;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Objects;
21 import org.apache.commons.lang3.SerializationUtils;
22 import org.junit.After;
23 import org.junit.Test;
24 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
25 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot.ShardSnapshot;
26 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshotList;
29 * Unit tests for DatastoreSnapshotRestore.
31 * @author Thomas Pantelis
33 public class DatastoreSnapshotRestoreTest {
34 String restoreDirectoryPath = "target/DatastoreSnapshotRestoreTest-" + System.nanoTime();
35 File restoreDirectoryFile = new File(restoreDirectoryPath);
36 File backupFile = new File(restoreDirectoryFile, "backup");
39 public void tearDown() {
41 restoreDirectoryFile.delete();
45 public void test() throws Exception {
46 assertTrue("Failed to mkdir " + restoreDirectoryPath, restoreDirectoryFile.mkdirs());
48 List<ShardSnapshot> shardSnapshots = new ArrayList<>();
49 shardSnapshots.add(new ShardSnapshot("cars", new byte[]{1,2}));
50 shardSnapshots.add(new ShardSnapshot("people", new byte[]{3,4}));
51 DatastoreSnapshot configSnapshot = new DatastoreSnapshot("config", null, shardSnapshots );
53 shardSnapshots = new ArrayList<>();
54 shardSnapshots.add(new ShardSnapshot("cars", new byte[]{5,6}));
55 shardSnapshots.add(new ShardSnapshot("people", new byte[]{7,8}));
56 shardSnapshots.add(new ShardSnapshot("bikes", new byte[]{9,0}));
57 DatastoreSnapshot operSnapshot = new DatastoreSnapshot("oper", null, shardSnapshots );
59 DatastoreSnapshotList snapshotList = new DatastoreSnapshotList();
60 snapshotList.add(configSnapshot);
61 snapshotList.add(operSnapshot);
63 File backupFile = new File(restoreDirectoryFile, "backup");
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", expected.getShardSnapshots().get(i).getSnapshot(),
92 actual.getShardSnapshots().get(i).getSnapshot());