Bug 7521: Add custom local snapshot store
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / persistence / LocalSnapshotStoreSpecTest.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.persistence;
9
10 import akka.persistence.snapshot.SnapshotStoreSpec;
11 import com.typesafe.config.ConfigFactory;
12 import java.io.File;
13 import java.io.IOException;
14 import org.apache.commons.io.FileUtils;
15 import org.junit.runner.RunWith;
16 import org.scalatest.junit.JUnitRunner;
17
18 /**
19  * Tests the LocalSnapshotStore using akka's standard test suite for snapshot store plugins via SnapshotStoreSpec.
20  * This class basically does the setup and tear down with SnapshotStoreSpec doing the rest. SnapshotStoreSpec uses
21  * ScalaTest so needs to be run with scala's JUnitRunner.
22  *
23  * @author Thomas Pantelis
24  */
25 @RunWith(JUnitRunner.class)
26 public class LocalSnapshotStoreSpecTest extends SnapshotStoreSpec {
27     private static final long serialVersionUID = 1L;
28     static final File SNAPSHOT_DIR = new File("target/snapshots");
29
30     public LocalSnapshotStoreSpecTest() {
31         super(ConfigFactory.load("LocalSnapshotStoreTest.conf"));
32     }
33
34     @Override
35     public void afterAll() {
36         super.afterAll();
37         FileUtils.deleteQuietly(SNAPSHOT_DIR);
38     }
39
40     static void cleanSnapshotDir() {
41         if (!SNAPSHOT_DIR.exists()) {
42             return;
43         }
44
45         try {
46             FileUtils.cleanDirectory(SNAPSHOT_DIR);
47         } catch (IOException e) {
48             // Ignore
49         }
50     }
51
52     static void createSnapshotDir() {
53         if (!SNAPSHOT_DIR.exists() && !SNAPSHOT_DIR.mkdirs()) {
54             throw new RuntimeException("Failed to create " + SNAPSHOT_DIR);
55         }
56
57         cleanSnapshotDir();
58     }
59 }