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;
15 import com.google.common.base.Optional;
17 import java.io.IOException;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.SortedSet;
21 import org.junit.Test;
22 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
23 import org.opendaylight.controller.config.persist.api.Persister;
24 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.xml.sax.SAXException;
29 public class DirectoryStorageAdapterTest {
30 private static final Logger LOG = LoggerFactory.getLogger(DirectoryStorageAdapterTest.class);
33 private Persister instantiatePersisterFromAdapter(final File file, final Optional<String> extensions){
34 PropertiesProviderTest pp = new PropertiesProviderTest();
35 pp.addProperty(XmlDirectoryStorageAdapter.DIRECTORY_STORAGE_PROP,file.getPath());
36 if(extensions.isPresent()) {
37 pp.addProperty(XmlDirectoryStorageAdapter.INCLUDE_EXT_PROP, extensions.get());
40 XmlDirectoryStorageAdapter dsa = new XmlDirectoryStorageAdapter();
41 return dsa.instantiate(pp);
44 private Persister instantiatePersisterFromAdapter(final File file){
45 return instantiatePersisterFromAdapter(file, Optional.<String>absent());
49 public void testEmptyDirectory() throws Exception {
50 File folder = new File("target/emptyFolder");
53 tested = instantiatePersisterFromAdapter(folder);
54 assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
57 tested.persistConfig(new ConfigSnapshotHolder() {
59 public String getConfigSnapshot() {
60 throw new RuntimeException();
64 public SortedSet<String> getCapabilities() {
65 throw new RuntimeException();
69 } catch (UnsupportedOperationException e) {
74 private File getFolder(final String folderName) {
75 File result = new File(("src/test/resources/" +
76 folderName).replace("/", File.separator));
77 assertTrue(result + " is not a directory", result.isDirectory());
82 public void testOneFile() throws Exception {
83 File folder = getFolder("oneFile");
84 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
86 LOG.info("Testing : {}", tested);
87 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
88 assertEquals(1, results.size());
89 ConfigSnapshotHolder result = results.get(0);
90 assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
94 public void testOneFileWrongExtension() throws Exception {
95 File folder = getFolder("oneFile");
96 tested = instantiatePersisterFromAdapter(folder, Optional.of("aa, bb"));
97 LOG.info("Testing : {}", tested);
100 private void assertResult(final ConfigSnapshotHolder result, final String s, final String... caps) throws SAXException, IOException {
101 assertXMLEqual(s, result.getConfigSnapshot());
103 for (String capFromSnapshot : result.getCapabilities()) {
104 assertEquals(capFromSnapshot, caps[i++]);
109 public void testTwoFilesAllExtensions() throws Exception {
110 File folder = getFolder("twoFiles");
111 tested = instantiatePersisterFromAdapter(folder);
112 LOG.info("Testing : {}", tested);
113 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
114 assertEquals(2, results.size());
116 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
117 assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");
121 public void testTwoFilesTwoExtensions() throws Exception {
122 File folder = getFolder("twoFiles");
123 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml, xml2"));
124 LOG.info("Testing : {}", tested);
125 assertEquals(2, tested.loadLastConfigs().size());
129 public void testTwoFilesOnlyOneExtension() throws Exception {
130 File folder = getFolder("twoFiles");
131 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
132 LOG.info("Testing : ", tested);
133 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
134 assertEquals(1, results.size());
136 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
140 public void testTwoFilesOneInvalid() throws Exception {
141 File folder = getFolder("twoFiles_corrupt");
142 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
143 LOG.info("Testing : {}", tested);
144 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
145 assertEquals(1, results.size());
147 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");