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;
18 import java.io.IOException;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.SortedSet;
22 import org.junit.Test;
23 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
24 import org.opendaylight.controller.config.persist.api.Persister;
25 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.xml.sax.SAXException;
30 public class DirectoryStorageAdapterTest {
31 private static final Logger LOG = LoggerFactory.getLogger(DirectoryStorageAdapterTest.class);
34 private Persister instantiatePersisterFromAdapter(final File file, final Optional<String> extensions){
35 PropertiesProviderTest pp = new PropertiesProviderTest();
36 pp.addProperty(XmlDirectoryStorageAdapter.DIRECTORY_STORAGE_PROP,file.getPath());
37 if(extensions.isPresent()) {
38 pp.addProperty(XmlDirectoryStorageAdapter.INCLUDE_EXT_PROP, extensions.get());
41 XmlDirectoryStorageAdapter dsa = new XmlDirectoryStorageAdapter();
42 return dsa.instantiate(pp);
45 private Persister instantiatePersisterFromAdapter(final File file){
46 return instantiatePersisterFromAdapter(file, Optional.<String>absent());
50 public void testEmptyDirectory() throws Exception {
51 File folder = new File("target/emptyFolder");
54 tested = instantiatePersisterFromAdapter(folder);
55 assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
58 tested.persistConfig(new ConfigSnapshotHolder() {
60 public String getConfigSnapshot() {
61 throw new RuntimeException();
65 public SortedSet<String> getCapabilities() {
66 throw new RuntimeException();
70 } catch (UnsupportedOperationException e) {
75 private File getFolder(final String folderName) {
76 File result = new File(("src/test/resources/" +
77 folderName).replace("/", File.separator));
78 assertTrue(result + " is not a directory", result.isDirectory());
83 public void testOneFile() throws Exception {
84 File folder = getFolder("oneFile");
85 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
87 LOG.info("Testing : {}", tested);
88 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
89 assertEquals(1, results.size());
90 ConfigSnapshotHolder result = results.get(0);
91 assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
95 public void testOneFileWrongExtension() throws Exception {
96 File folder = getFolder("oneFile");
97 tested = instantiatePersisterFromAdapter(folder, Optional.of("aa, bb"));
98 LOG.info("Testing : {}", tested);
101 private void assertResult(final ConfigSnapshotHolder result, final String s, final String... caps) throws SAXException, IOException {
102 assertXMLEqual(s, result.getConfigSnapshot());
104 for (String capFromSnapshot : result.getCapabilities()) {
105 assertEquals(capFromSnapshot, caps[i++]);
110 public void testTwoFilesAllExtensions() throws Exception {
111 File folder = getFolder("twoFiles");
112 tested = instantiatePersisterFromAdapter(folder);
113 LOG.info("Testing : {}", tested);
114 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
115 assertEquals(2, results.size());
117 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
118 assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");
122 public void testTwoFilesTwoExtensions() throws Exception {
123 File folder = getFolder("twoFiles");
124 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml, xml2"));
125 LOG.info("Testing : {}", tested);
126 assertEquals(2, tested.loadLastConfigs().size());
130 public void testTwoFilesOnlyOneExtension() throws Exception {
131 File folder = getFolder("twoFiles");
132 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
133 LOG.info("Testing : ", tested);
134 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
135 assertEquals(1, results.size());
137 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
141 public void testTwoFilesOneInvalid() throws Exception {
142 File folder = getFolder("twoFiles_corrupt");
143 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
144 LOG.info("Testing : {}", tested);
145 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
146 assertEquals(1, results.size());
148 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");