Merge "Add missing headers to config, netconf subsystems"
[controller.git] / opendaylight / config / config-persister-directory-xml-adapter / src / test / java / org / opendaylight / controller / config / persist / storage / directory / xml / DirectoryStorageAdapterTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.config.persist.storage.directory.xml;
10
11 import java.io.File;
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;
22
23 public class DirectoryStorageAdapterTest {
24     XmlDirectoryPersister tested;
25     Logger logger = LoggerFactory.getLogger(DirectoryStorageAdapterTest.class.toString());
26
27     @Test
28     public void testEmptyDirectory() throws Exception {
29         File folder = new File("target/emptyFolder");
30         folder.mkdir();
31         tested = new XmlDirectoryPersister((folder));
32         assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
33
34         try {
35             tested.persistConfig(new ConfigSnapshotHolder() {
36                 @Override
37                 public String getConfigSnapshot() {
38                     throw new RuntimeException();
39                 }
40
41                 @Override
42                 public SortedSet<String> getCapabilities() {
43                     throw new RuntimeException();
44                 }
45             });
46             fail();
47         } catch (UnsupportedOperationException e) {
48
49         }
50     }
51
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());
56         return result;
57     }
58
59     @Test
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");
68     }
69
70     private void assertResult(ConfigSnapshotHolder result, String s, String... caps) {
71         assertEquals(s, result.getConfigSnapshot().replaceAll("\\s", ""));
72         int i = 0;
73         for (String capFromSnapshot : result.getCapabilities()) {
74             assertEquals(capFromSnapshot, caps[i++]);
75         }
76     }
77
78     @Test
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());
85
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");
88
89     }
90
91 }