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;
15 import org.junit.Test;
16 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
17 import org.opendaylight.controller.config.persist.api.Persister;
18 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
25 public class DirectoryStorageAdapterTest {
27 Logger logger = LoggerFactory.getLogger(DirectoryStorageAdapterTest.class.toString());
29 private Persister instantiatePersisterFromAdapter(File file){
30 PropertiesProviderTest pp = new PropertiesProviderTest();
31 pp.addProperty("directoryStorage",file.getPath());
32 XmlDirectoryStorageAdapter dsa = new XmlDirectoryStorageAdapter();
33 return dsa.instantiate(pp);
37 public void testEmptyDirectory() throws Exception {
38 File folder = new File("target/emptyFolder");
41 tested = instantiatePersisterFromAdapter(folder);
42 assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
45 tested.persistConfig(new ConfigSnapshotHolder() {
47 public String getConfigSnapshot() {
48 throw new RuntimeException();
52 public SortedSet<String> getCapabilities() {
53 throw new RuntimeException();
57 } catch (UnsupportedOperationException e) {
62 private File getFolder(String folderName) {
63 File result = new File(("src/test/resources/" +
64 folderName).replace("/", File.separator));
65 assertTrue(result + " is not a directory", result.isDirectory());
70 public void testOneFile() throws Exception {
71 File folder = getFolder("oneFile");
72 tested = instantiatePersisterFromAdapter(folder);
74 logger.info("Testing : "+tested.toString());
75 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
76 assertEquals(1, results.size());
77 ConfigSnapshotHolder result = results.get(0);
78 assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
81 private void assertResult(ConfigSnapshotHolder result, String s, String... caps) {
82 assertEquals(s, result.getConfigSnapshot().replaceAll("\\s", ""));
84 for (String capFromSnapshot : result.getCapabilities()) {
85 assertEquals(capFromSnapshot, caps[i++]);
90 public void testTwoFiles() throws Exception {
91 File folder = getFolder("twoFiles");
92 tested = instantiatePersisterFromAdapter(folder);
93 logger.info("Testing : "+tested.toString());
94 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
95 assertEquals(2, results.size());
97 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
98 assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");