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.xml;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.SortedSet;
15 import org.junit.Test;
16 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
17 import org.opendaylight.controller.config.persist.api.Persister;
18 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
23 public class DirectoryStorageAdapterTest {
28 public void testEmptyDirectory() throws Exception {
29 File folder = new File("target/emptyFolder");
31 tested = new XmlDirectoryPersister((folder));
32 assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
35 tested.persistConfig(new ConfigSnapshotHolder() {
37 public String getConfigSnapshot() {
38 throw new RuntimeException();
42 public SortedSet<String> getCapabilities() {
43 throw new RuntimeException();
47 } catch (UnsupportedOperationException e) {
52 private File getFolder(String folderName) {
53 File result = new File(("src/test/resources/" +
54 folderName).replace("/", File.separator));
55 assertTrue(result + " is not a directory", result.isDirectory());
60 public void testOneFile() throws Exception {
61 File folder = getFolder("oneFile");
63 PropertiesProviderTest pp = new PropertiesProviderTest();
64 pp.addProperty("directoryStorage",folder.getPath());
65 XmlDirectoryStorageAdapter xmlDsa = new XmlDirectoryStorageAdapter();
66 tested = xmlDsa.instantiate(pp);
68 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
69 assertEquals(1, results.size());
70 ConfigSnapshotHolder result = results.get(0);
71 assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
74 private void assertResult(ConfigSnapshotHolder result, String s, String... caps) {
75 assertEquals(s, result.getConfigSnapshot().replaceAll("\\s", ""));
77 for (String capFromSnapshot : result.getCapabilities()) {
78 assertEquals(capFromSnapshot, caps[i++]);
83 public void testTwoFiles() throws Exception {
84 File folder = getFolder("twoFiles");
86 PropertiesProviderTest pp = new PropertiesProviderTest();
87 pp.addProperty("directoryStorage",folder.getPath());
88 XmlDirectoryStorageAdapter xmlDsa = new XmlDirectoryStorageAdapter();
89 tested = xmlDsa.instantiate(pp);
91 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
92 assertEquals(2, results.size());
94 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
95 assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");