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 com.google.common.base.Optional;
12 import org.apache.commons.io.IOUtils;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
18 import java.io.IOException;
19 import java.util.SortedSet;
20 import java.util.TreeSet;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
26 public class DirectoryStorageAdapterTest {
27 DirectoryPersister tested;
28 SortedSet<String> expectedCapabilities;
29 String expectedSnapshot;
32 public void setUp() throws Exception {
33 expectedCapabilities = new TreeSet<>(IOUtils.readLines(getClass().getResourceAsStream("/expectedCapabilities.txt")));
34 expectedSnapshot = IOUtils.toString(getClass().getResourceAsStream("/expectedSnapshot.xml"));
38 public void testEmptyDirectory() throws Exception {
39 File folder = new File("target/emptyFolder");
41 tested = new DirectoryPersister((folder));
42 assertEquals(Optional.<ConfigSnapshotHolder>absent(), tested.loadLastConfig());
45 tested.persistConfig(new ConfigSnapshotHolder() {
47 public String getConfigSnapshot() {
48 throw new RuntimeException();
52 public SortedSet<String> getCapabilities() {
53 throw new RuntimeException();
57 } catch (UnsupportedOperationException e) {
62 private File getFolder(String folderName) {
63 File result = new File(("src/test/resources/" +
64 folderName).replace("/", File.separator));
65 assertTrue(result + " is not a directory", result.isDirectory());
70 public void testOneFile() throws Exception {
71 File folder = getFolder("oneFile");
72 tested = new DirectoryPersister((folder));
76 private void assertExpected() throws IOException {
77 Optional<ConfigSnapshotHolder> maybeResult = tested.loadLastConfig();
78 assertTrue(maybeResult.isPresent());
79 ConfigSnapshotHolder result = maybeResult.get();
80 assertEquals(expectedCapabilities, result.getCapabilities());
81 assertEquals(expectedSnapshot, result.getConfigSnapshot());
85 public void testTwoFiles() throws Exception {
86 File folder = getFolder("twoFiles");
87 tested = new DirectoryPersister((folder));