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