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;
11 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
16 import com.google.common.base.Optional;
19 import java.io.IOException;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.SortedSet;
24 import org.junit.Test;
25 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
26 import org.opendaylight.controller.config.persist.api.Persister;
27 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.xml.sax.SAXException;
32 public class DirectoryStorageAdapterTest {
33 private static final Logger LOG = LoggerFactory.getLogger(DirectoryStorageAdapterTest.class);
36 private Persister instantiatePersisterFromAdapter(final File file, final Optional<String> extensions) {
37 PropertiesProviderTest pp = new PropertiesProviderTest();
38 pp.addProperty(XmlDirectoryStorageAdapter.DIRECTORY_STORAGE_PROP, file.getPath());
39 if (extensions.isPresent()) {
40 pp.addProperty(XmlDirectoryStorageAdapter.INCLUDE_EXT_PROP, extensions.get());
43 XmlDirectoryStorageAdapter dsa = new XmlDirectoryStorageAdapter();
44 return dsa.instantiate(pp);
47 private Persister instantiatePersisterFromAdapter(final File file) {
48 return instantiatePersisterFromAdapter(file, Optional.<String>absent());
52 public void testEmptyDirectory() throws Exception {
53 File folder = new File("target/emptyFolder");
56 tested = instantiatePersisterFromAdapter(folder);
57 assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
60 tested.persistConfig(new ConfigSnapshotHolder() {
62 public String getConfigSnapshot() {
63 throw new RuntimeException();
67 public SortedSet<String> getCapabilities() {
68 throw new RuntimeException();
72 } catch (final UnsupportedOperationException e) {
73 // TODO: empty catch block
77 private File getFolder(final String folderName) {
78 File result = new File(("src/test/resources/" + folderName).replace("/", File.separator));
79 assertTrue(result + " is not a directory", result.isDirectory());
84 public void testOneFile() throws Exception {
85 File folder = getFolder("oneFile");
86 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
88 LOG.info("Testing : {}", tested);
89 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
90 assertEquals(1, results.size());
91 ConfigSnapshotHolder result = results.get(0);
92 assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
96 public void testOneFileWrongExtension() throws Exception {
97 File folder = getFolder("oneFile");
98 tested = instantiatePersisterFromAdapter(folder, Optional.of("aa, bb"));
99 LOG.info("Testing : {}", tested);
102 private void assertResult(final ConfigSnapshotHolder result,
104 final String... caps) throws SAXException, IOException {
105 assertXMLEqual(string, result.getConfigSnapshot());
107 for (String capFromSnapshot : result.getCapabilities()) {
108 assertEquals(capFromSnapshot, caps[index++]);
113 public void testTwoFilesAllExtensions() throws Exception {
114 File folder = getFolder("twoFiles");
115 tested = instantiatePersisterFromAdapter(folder);
116 LOG.info("Testing : {}", tested);
117 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
118 assertEquals(2, results.size());
120 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
121 assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");
125 public void testTwoFilesTwoExtensions() throws Exception {
126 File folder = getFolder("twoFiles");
127 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml, xml2"));
128 LOG.info("Testing : {}", tested);
129 assertEquals(2, tested.loadLastConfigs().size());
133 public void testTwoFilesOnlyOneExtension() throws Exception {
134 File folder = getFolder("twoFiles");
135 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
136 LOG.info("Testing : ", tested);
137 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
138 assertEquals(1, results.size());
140 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
144 public void testTwoFilesOneInvalid() throws Exception {
145 File folder = getFolder("twoFiles_corrupt");
146 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
147 LOG.info("Testing : {}", tested);
148 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
149 assertEquals(1, results.size());
151 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");