Remove raw references to Map in XSQL
[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 static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import com.google.common.base.Optional;
16 import java.io.File;
17 import java.io.IOException;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.SortedSet;
21 import org.junit.Test;
22 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
23 import org.opendaylight.controller.config.persist.api.Persister;
24 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.xml.sax.SAXException;
28
29 public class DirectoryStorageAdapterTest {
30     private static final Logger LOG = LoggerFactory.getLogger(DirectoryStorageAdapterTest.class);
31     Persister tested;
32
33     private Persister instantiatePersisterFromAdapter(final File file, final Optional<String> extensions){
34         PropertiesProviderTest pp = new PropertiesProviderTest();
35         pp.addProperty(XmlDirectoryStorageAdapter.DIRECTORY_STORAGE_PROP,file.getPath());
36         if(extensions.isPresent()) {
37             pp.addProperty(XmlDirectoryStorageAdapter.INCLUDE_EXT_PROP, extensions.get());
38         }
39
40         XmlDirectoryStorageAdapter dsa = new XmlDirectoryStorageAdapter();
41         return dsa.instantiate(pp);
42     }
43
44     private Persister instantiatePersisterFromAdapter(final File file){
45         return instantiatePersisterFromAdapter(file, Optional.<String>absent());
46     }
47
48     @Test
49     public void testEmptyDirectory() throws Exception {
50         File folder = new File("target/emptyFolder");
51         folder.mkdir();
52
53         tested = instantiatePersisterFromAdapter(folder);
54         assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
55
56         try {
57             tested.persistConfig(new ConfigSnapshotHolder() {
58                 @Override
59                 public String getConfigSnapshot() {
60                     throw new RuntimeException();
61                 }
62
63                 @Override
64                 public SortedSet<String> getCapabilities() {
65                     throw new RuntimeException();
66                 }
67             });
68             fail();
69         } catch (UnsupportedOperationException e) {
70
71         }
72     }
73
74     private File getFolder(final String folderName) {
75         File result = new File(("src/test/resources/" +
76                 folderName).replace("/", File.separator));
77         assertTrue(result + " is not a directory", result.isDirectory());
78         return result;
79     }
80
81     @Test
82     public void testOneFile() throws Exception {
83         File folder = getFolder("oneFile");
84         tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
85
86         LOG.info("Testing : {}", tested);
87         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
88         assertEquals(1, results.size());
89         ConfigSnapshotHolder result = results.get(0);
90         assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
91     }
92
93     @Test
94     public void testOneFileWrongExtension() throws Exception {
95         File folder = getFolder("oneFile");
96         tested = instantiatePersisterFromAdapter(folder, Optional.of("aa, bb"));
97         LOG.info("Testing : {}", tested);
98     }
99
100     private void assertResult(final ConfigSnapshotHolder result, final String s, final String... caps) throws SAXException, IOException {
101         assertXMLEqual(s, result.getConfigSnapshot());
102         int i = 0;
103         for (String capFromSnapshot : result.getCapabilities()) {
104             assertEquals(capFromSnapshot, caps[i++]);
105         }
106     }
107
108     @Test
109     public void testTwoFilesAllExtensions() throws Exception {
110         File folder = getFolder("twoFiles");
111         tested = instantiatePersisterFromAdapter(folder);
112         LOG.info("Testing : {}", tested);
113         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
114         assertEquals(2, results.size());
115
116         assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
117         assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");
118     }
119
120     @Test
121     public void testTwoFilesTwoExtensions() throws Exception {
122         File folder = getFolder("twoFiles");
123         tested = instantiatePersisterFromAdapter(folder, Optional.of("xml, xml2"));
124         LOG.info("Testing : {}", tested);
125         assertEquals(2, tested.loadLastConfigs().size());
126     }
127
128     @Test
129     public void testTwoFilesOnlyOneExtension() throws Exception {
130         File folder = getFolder("twoFiles");
131         tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
132         LOG.info("Testing : ", tested);
133         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
134         assertEquals(1, results.size());
135
136         assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
137     }
138
139     @Test
140     public void testTwoFilesOneInvalid() throws Exception {
141         File folder = getFolder("twoFiles_corrupt");
142         tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
143         LOG.info("Testing : {}", tested);
144         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
145         assertEquals(1, results.size());
146
147         assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
148     }
149
150 }