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.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
23 public class DirectoryStorageAdapterTest {
24 XmlDirectoryPersister tested;
25 Logger logger = LoggerFactory.getLogger(DirectoryStorageAdapterTest.class.toString());
28 public void testEmptyDirectory() throws Exception {
29 File folder = new File("target/emptyFolder");
31 tested = new XmlDirectoryPersister((folder));
32 assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
35 tested.persistConfig(new ConfigSnapshotHolder() {
37 public String getConfigSnapshot() {
38 throw new RuntimeException();
42 public SortedSet<String> getCapabilities() {
43 throw new RuntimeException();
47 } catch (UnsupportedOperationException e) {
52 private File getFolder(String folderName) {
53 File result = new File(("src/test/resources/" +
54 folderName).replace("/", File.separator));
55 assertTrue(result + " is not a directory", result.isDirectory());
60 public void testOneFile() throws Exception {
61 File folder = getFolder("oneFile");
62 tested = new XmlDirectoryPersister(folder);
63 logger.info("Testing : "+tested.toString());
64 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
65 assertEquals(1, results.size());
66 ConfigSnapshotHolder result = results.get(0);
67 assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
70 private void assertResult(ConfigSnapshotHolder result, String s, String... caps) {
71 assertEquals(s, result.getConfigSnapshot().replaceAll("\\s", ""));
73 for (String capFromSnapshot : result.getCapabilities()) {
74 assertEquals(capFromSnapshot, caps[i++]);
79 public void testTwoFiles() throws Exception {
80 File folder = getFolder("twoFiles");
81 tested = new XmlDirectoryPersister((folder));
82 logger.info("Testing : "+tested.toString());
83 List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
84 assertEquals(2, results.size());
86 assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
87 assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");