2 * Copyright (c) 2013 Cisco 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
9 package org.opendaylight.controller.config.persist.storage.directory;
11 import org.apache.commons.io.IOUtils;
12 import org.junit.Test;
13 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.SortedSet;
19 import java.util.TreeSet;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
25 public class DirectoryStorageAdapterTest {
26 DirectoryPersister tested;
29 public void testEmptyDirectory() throws Exception {
30 File folder = new File("target/emptyFolder");
32 tested = new DirectoryPersister((folder));
33 assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
36 tested.persistConfig(new ConfigSnapshotHolder() {
38 public String getConfigSnapshot() {
39 throw new RuntimeException();
43 public SortedSet<String> getCapabilities() {
44 throw new RuntimeException();
48 } catch (UnsupportedOperationException e) {
53 private File getFolder(String folderName) {
54 File result = new File(("src/test/resources/" +
55 folderName).replace("/", File.separator));
56 assertTrue(result + " is not a directory", result.isDirectory());
61 public void testOneFile() throws Exception {
62 File folder = getFolder("oneFile");
63 tested = new DirectoryPersister((folder));
64 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
65 assertEquals(1, results.size());
66 ConfigSnapshotHolder result = results.get(0);
67 assertSnapshot(result, "oneFileExpected");
72 public void testTwoFiles() throws Exception {
73 File folder = getFolder("twoFiles");
74 tested = new DirectoryPersister((folder));
75 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
76 assertEquals(2, results.size());
77 assertSnapshot(results.get(0), "twoFilesExpected1");
78 assertSnapshot(results.get(1), "twoFilesExpected2");
81 private void assertSnapshot(ConfigSnapshotHolder result, String directory) throws Exception {
82 SortedSet<String> expectedCapabilities = new TreeSet<>(IOUtils.readLines(getClass().getResourceAsStream("/" + directory + "/expectedCapabilities.txt")));
83 String expectedSnapshot = IOUtils.toString(getClass().getResourceAsStream("/" + directory + "/expectedSnapshot.xml"));
84 assertEquals(expectedCapabilities, result.getCapabilities());
85 assertEquals(expectedSnapshot, result.getConfigSnapshot());