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;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.SortedSet;
15 import java.util.TreeSet;
16 import org.apache.commons.io.IOUtils;
17 import org.junit.Test;
18 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
19 import org.opendaylight.controller.config.persist.api.Persister;
20 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
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 {
29 public void testEmptyDirectory() throws Exception {
30 File folder = new File("target/emptyFolder");
33 PropertiesProviderTest pp = new PropertiesProviderTest();
34 pp.addProperty("directoryStorage",folder.getPath());
35 DirectoryStorageAdapter dsa = new DirectoryStorageAdapter();
36 tested = dsa.instantiate(pp);
37 assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
40 tested.persistConfig(new ConfigSnapshotHolder() {
42 public String getConfigSnapshot() {
43 throw new RuntimeException();
47 public SortedSet<String> getCapabilities() {
48 throw new RuntimeException();
52 } catch (UnsupportedOperationException e) {
57 private File getFolder(String folderName) {
58 File result = new File(("src/test/resources/" +
59 folderName).replace("/", File.separator));
60 assertTrue(result + " is not a directory", result.isDirectory());
65 public void testOneFile() throws Exception {
66 File folder = getFolder("oneFile");
67 PropertiesProviderTest pp = new PropertiesProviderTest();
68 pp.addProperty("directoryStorage",folder.getPath());
69 DirectoryStorageAdapter dsa = new DirectoryStorageAdapter();
70 tested = dsa.instantiate(pp);
72 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
73 assertEquals(1, results.size());
74 ConfigSnapshotHolder result = results.get(0);
75 assertSnapshot(result, "oneFileExpected");
80 public void testTwoFiles() throws Exception {
81 File folder = getFolder("twoFiles");
82 PropertiesProviderTest pp = new PropertiesProviderTest();
83 pp.addProperty("directoryStorage",folder.getPath());
84 DirectoryStorageAdapter dsa = new DirectoryStorageAdapter();
85 tested = dsa.instantiate(pp);
87 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
88 assertEquals(2, results.size());
89 assertSnapshot(results.get(0), "twoFilesExpected1");
90 assertSnapshot(results.get(1), "twoFilesExpected2");
93 private void assertSnapshot(ConfigSnapshotHolder result, String directory) throws Exception {
94 SortedSet<String> expectedCapabilities = new TreeSet<>(IOUtils.readLines(getClass().getResourceAsStream("/" + directory + "/expectedCapabilities.txt")));
95 String expectedSnapshot = IOUtils.toString(getClass().getResourceAsStream("/" + directory + "/expectedSnapshot.xml"));
96 assertEquals(expectedCapabilities, result.getCapabilities());
97 assertEquals(expectedSnapshot, result.getConfigSnapshot());