Merge "incorrect dependencies and unnecesary export-package removed from pom.xml"
[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.opendaylight.controller.config.persist.api.Persister;
18 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
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
25     Persister tested;
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
63         PropertiesProviderTest pp = new PropertiesProviderTest();
64         pp.addProperty("directoryStorage",folder.getPath());
65         XmlDirectoryStorageAdapter xmlDsa = new XmlDirectoryStorageAdapter();
66         tested = xmlDsa.instantiate(pp);
67
68         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
69         assertEquals(1, results.size());
70         ConfigSnapshotHolder result = results.get(0);
71         assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
72     }
73
74     private void assertResult(ConfigSnapshotHolder result, String s, String... caps) {
75         assertEquals(s, result.getConfigSnapshot().replaceAll("\\s", ""));
76         int i = 0;
77         for (String capFromSnapshot : result.getCapabilities()) {
78             assertEquals(capFromSnapshot, caps[i++]);
79         }
80     }
81
82     @Test
83     public void testTwoFiles() throws Exception {
84         File folder = getFolder("twoFiles");
85
86         PropertiesProviderTest pp = new PropertiesProviderTest();
87         pp.addProperty("directoryStorage",folder.getPath());
88         XmlDirectoryStorageAdapter xmlDsa = new XmlDirectoryStorageAdapter();
89         tested = xmlDsa.instantiate(pp);
90
91         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
92         assertEquals(2, results.size());
93
94         assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
95         assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");
96
97     }
98
99 }