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;
16 import com.google.common.base.Optional;
17 import org.junit.Test;
18 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
19 import org.opendaylight.controller.config.persist.api.Persister;
20 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
27 public class DirectoryStorageAdapterTest {
29 Logger logger = LoggerFactory.getLogger(DirectoryStorageAdapterTest.class.toString());
31 private Persister instantiatePersisterFromAdapter(File file, Optional<String> extensions){
32 PropertiesProviderTest pp = new PropertiesProviderTest();
33 pp.addProperty(XmlDirectoryStorageAdapter.DIRECTORY_STORAGE_PROP,file.getPath());
34 if(extensions.isPresent()) {
35 pp.addProperty(XmlDirectoryStorageAdapter.INCLUDE_EXT_PROP, extensions.get());
38 XmlDirectoryStorageAdapter dsa = new XmlDirectoryStorageAdapter();
39 return dsa.instantiate(pp);
42 private Persister instantiatePersisterFromAdapter(File file){
43 return instantiatePersisterFromAdapter(file, Optional.<String>absent());
47 public void testEmptyDirectory() throws Exception {
48 File folder = new File("target/emptyFolder");
51 tested = instantiatePersisterFromAdapter(folder);
52 assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
55 tested.persistConfig(new ConfigSnapshotHolder() {
57 public String getConfigSnapshot() {
58 throw new RuntimeException();
62 public SortedSet<String> getCapabilities() {
63 throw new RuntimeException();
67 } catch (UnsupportedOperationException e) {
72 private File getFolder(String folderName) {
73 File result = new File(("src/test/resources/" +
74 folderName).replace("/", File.separator));
75 assertTrue(result + " is not a directory", result.isDirectory());
80 public void testOneFile() throws Exception {
81 File folder = getFolder("oneFile");
82 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
84 logger.info("Testing : " + tested.toString());
85 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
86 assertEquals(1, results.size());
87 ConfigSnapshotHolder result = results.get(0);
88 assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
92 public void testOneFileWrongExtension() throws Exception {
93 File folder = getFolder("oneFile");
94 tested = instantiatePersisterFromAdapter(folder, Optional.of("aa, bb"));
95 logger.info("Testing : " + tested.toString());
98 private void assertResult(ConfigSnapshotHolder result, String s, String... caps) {
99 assertEquals(s, result.getConfigSnapshot().replaceAll("\\s", ""));
101 for (String capFromSnapshot : result.getCapabilities()) {
102 assertEquals(capFromSnapshot, caps[i++]);
107 public void testTwoFilesAllExtensions() throws Exception {
108 File folder = getFolder("twoFiles");
109 tested = instantiatePersisterFromAdapter(folder);
110 logger.info("Testing : " + tested.toString());
111 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
112 assertEquals(2, results.size());
114 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
115 assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");
119 public void testTwoFilesTwoExtensions() throws Exception {
120 File folder = getFolder("twoFiles");
121 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml, xml2"));
122 logger.info("Testing : " + tested.toString());
123 assertEquals(2, tested.loadLastConfigs().size());
127 public void testTwoFilesOnlyOneExtension() throws Exception {
128 File folder = getFolder("twoFiles");
129 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
130 logger.info("Testing : " + tested.toString());
131 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
132 assertEquals(1, results.size());
134 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
138 public void testTwoFilesOneInvalid() throws Exception {
139 File folder = getFolder("twoFiles_corrupt");
140 tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
141 logger.info("Testing : " + tested.toString());
142 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
143 assertEquals(1, results.size());
145 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");