30957d9c1c5bc4e663c89b76e1c9d148b20a8112
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DatastoreSnapshotRestoreTest.java
1 /*
2  * Copyright (c) 2015 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;
9
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;
16
17 import java.io.File;
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.persisted.DatastoreSnapshot;
26 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot.ShardSnapshot;
27 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshotList;
28
29 /**
30  * Unit tests for DatastoreSnapshotRestore.
31  *
32  * @author Thomas Pantelis
33  */
34 public class DatastoreSnapshotRestoreTest {
35     String restoreDirectoryPath = "target/DatastoreSnapshotRestoreTest-" + System.nanoTime();
36     File restoreDirectoryFile = new File(restoreDirectoryPath);
37     File backupFile = new File(restoreDirectoryFile, "backup");
38
39     @After
40     public void tearDown() {
41         backupFile.delete();
42         restoreDirectoryFile.delete();
43     }
44
45     @Test
46     public void test() throws Exception {
47         assertTrue("Failed to mkdir " + restoreDirectoryPath, restoreDirectoryFile.mkdirs());
48
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);
53
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);
59
60         DatastoreSnapshotList snapshotList = new DatastoreSnapshotList();
61         snapshotList.add(configSnapshot);
62         snapshotList.add(operSnapshot);
63
64         try (FileOutputStream fos = new FileOutputStream(backupFile)) {
65             SerializationUtils.serialize(snapshotList, fos);
66         }
67
68         DatastoreSnapshotRestore instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath);
69
70         verifySnapshot(configSnapshot, instance.getAndRemove("config"));
71         verifySnapshot(operSnapshot, instance.getAndRemove("oper"));
72
73         assertNull("DatastoreSnapshot was not removed", instance.getAndRemove("config"));
74
75         assertFalse(backupFile + " was not deleted", backupFile.exists());
76
77         instance = DatastoreSnapshotRestore.instance(restoreDirectoryPath);
78         assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("config"));
79         assertNull("Expected null DatastoreSnapshot", instance.getAndRemove("oper"));
80     }
81
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());
93         }
94     }
95 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.