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 {
28 private Persister instantiatePersisterFromAdapter(File file){
29 PropertiesProviderTest pp = new PropertiesProviderTest();
30 pp.addProperty("directoryStorage",file.getPath());
31 DirectoryStorageAdapter dsa = new DirectoryStorageAdapter();
32 return dsa.instantiate(pp);
35 public void testEmptyDirectory() throws Exception {
36 File folder = new File("target/emptyFolder");
39 tested = instantiatePersisterFromAdapter(folder);
40 assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
43 tested.persistConfig(new ConfigSnapshotHolder() {
45 public String getConfigSnapshot() {
46 throw new RuntimeException();
50 public SortedSet<String> getCapabilities() {
51 throw new RuntimeException();
55 } catch (UnsupportedOperationException e) {
60 private File getFolder(String folderName) {
61 File result = new File(("src/test/resources/" +
62 folderName).replace("/", File.separator));
63 assertTrue(result + " is not a directory", result.isDirectory());
68 public void testOneFile() throws Exception {
69 File folder = getFolder("oneFile");
71 tested = instantiatePersisterFromAdapter(folder);
73 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
74 assertEquals(1, results.size());
75 ConfigSnapshotHolder result = results.get(0);
76 assertSnapshot(result, "oneFileExpected");
81 public void testTwoFiles() throws Exception {
82 File folder = getFolder("twoFiles");
83 tested = instantiatePersisterFromAdapter(folder);
85 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
86 assertEquals(2, results.size());
87 assertSnapshot(results.get(0), "twoFilesExpected1");
88 assertSnapshot(results.get(1), "twoFilesExpected2");
91 private void assertSnapshot(ConfigSnapshotHolder result, String directory) throws Exception {
92 SortedSet<String> expectedCapabilities = new TreeSet<>(IOUtils.readLines(getClass().getResourceAsStream("/" + directory + "/expectedCapabilities.txt")));
93 String expectedSnapshot = IOUtils.toString(getClass().getResourceAsStream("/" + directory + "/expectedSnapshot.xml"));
94 expectedSnapshot = expectedSnapshot.replaceAll("\r","");
95 String _snapshot = result.getConfigSnapshot();
96 _snapshot = _snapshot.replaceAll("\r","");
97 assertEquals(expectedCapabilities, result.getCapabilities());
98 assertEquals(expectedSnapshot, _snapshot);