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;
17 import java.io.IOException;
18 import java.util.Collections;
19 import java.util.List;
20 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 import com.google.common.base.Optional;
32 public class DirectoryStorageAdapterTest {
34 Logger logger = LoggerFactory.getLogger(DirectoryStorageAdapterTest.class.toString());
36 private Persister instantiatePersisterFromAdapter(File file, 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(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 (UnsupportedOperationException e) {
77 private File getFolder(String folderName) {
78 File result = new File(("src/test/resources/" +
79 folderName).replace("/", File.separator));
80 assertTrue(result + " is not a directory", result.isDirectory());
85 public void testOneFile() throws Exception {
86 File folder = getFolder("oneFile");
87 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
89 logger.info("Testing : " + tested.toString());
90 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
91 assertEquals(1, results.size());
92 ConfigSnapshotHolder result = results.get(0);
93 assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
97 public void testOneFileWrongExtension() throws Exception {
98 File folder = getFolder("oneFile");
99 tested = instantiatePersisterFromAdapter(folder, Optional.of("aa, bb"));
100 logger.info("Testing : " + tested.toString());
103 private void assertResult(ConfigSnapshotHolder result, String s, String... caps) throws SAXException, IOException {
104 assertXMLEqual(s, result.getConfigSnapshot());
106 for (String capFromSnapshot : result.getCapabilities()) {
107 assertEquals(capFromSnapshot, caps[i++]);
112 public void testTwoFilesAllExtensions() throws Exception {
113 File folder = getFolder("twoFiles");
114 tested = instantiatePersisterFromAdapter(folder);
115 logger.info("Testing : " + tested.toString());
116 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
117 assertEquals(2, results.size());
119 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
120 assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");
124 public void testTwoFilesTwoExtensions() throws Exception {
125 File folder = getFolder("twoFiles");
126 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml, xml2"));
127 logger.info("Testing : " + tested.toString());
128 assertEquals(2, tested.loadLastConfigs().size());
132 public void testTwoFilesOnlyOneExtension() throws Exception {
133 File folder = getFolder("twoFiles");
134 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
135 logger.info("Testing : " + tested.toString());
136 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
137 assertEquals(1, results.size());
139 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
143 public void testTwoFilesOneInvalid() throws Exception {
144 File folder = getFolder("twoFiles_corrupt");
145 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
146 logger.info("Testing : " + tested.toString());
147 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
148 assertEquals(1, results.size());
150 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");