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