Remove yang-test
[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
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.SortedSet;
23
24 import org.junit.Test;
25 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
26 import org.opendaylight.controller.config.persist.api.Persister;
27 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.xml.sax.SAXException;
31
32 public class DirectoryStorageAdapterTest {
33     private static final Logger LOG = LoggerFactory.getLogger(DirectoryStorageAdapterTest.class);
34     Persister tested;
35
36     private Persister instantiatePersisterFromAdapter(final File file, final 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(final 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 (final UnsupportedOperationException e) {
73             // TODO: empty catch block
74         }
75     }
76
77     private File getFolder(final String folderName) {
78         File result = new File(("src/test/resources/" + folderName).replace("/", File.separator));
79         assertTrue(result + " is not a directory", result.isDirectory());
80         return result;
81     }
82
83     @Test
84     public void testOneFile() throws Exception {
85         File folder = getFolder("oneFile");
86         tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
87
88         LOG.info("Testing : {}", tested);
89         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
90         assertEquals(1, results.size());
91         ConfigSnapshotHolder result = results.get(0);
92         assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
93     }
94
95     @Test
96     public void testOneFileWrongExtension() throws Exception {
97         File folder = getFolder("oneFile");
98         tested = instantiatePersisterFromAdapter(folder, Optional.of("aa, bb"));
99         LOG.info("Testing : {}", tested);
100     }
101
102     private void assertResult(final ConfigSnapshotHolder result,
103                               final String string,
104                               final String... caps) throws SAXException, IOException {
105         assertXMLEqual(string, result.getConfigSnapshot());
106         int index = 0;
107         for (String capFromSnapshot : result.getCapabilities()) {
108             assertEquals(capFromSnapshot, caps[index++]);
109         }
110     }
111
112     @Test
113     public void testTwoFilesAllExtensions() throws Exception {
114         File folder = getFolder("twoFiles");
115         tested = instantiatePersisterFromAdapter(folder);
116         LOG.info("Testing : {}", tested);
117         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
118         assertEquals(2, results.size());
119
120         assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
121         assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");
122     }
123
124     @Test
125     public void testTwoFilesTwoExtensions() throws Exception {
126         File folder = getFolder("twoFiles");
127         tested = instantiatePersisterFromAdapter(folder, Optional.of("xml, xml2"));
128         LOG.info("Testing : {}", tested);
129         assertEquals(2, tested.loadLastConfigs().size());
130     }
131
132     @Test
133     public void testTwoFilesOnlyOneExtension() throws Exception {
134         File folder = getFolder("twoFiles");
135         tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
136         LOG.info("Testing : ", tested);
137         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
138         assertEquals(1, results.size());
139
140         assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
141     }
142
143     @Test
144     public void testTwoFilesOneInvalid() throws Exception {
145         File folder = getFolder("twoFiles_corrupt");
146         tested = instantiatePersisterFromAdapter(folder, Optional.of("xml"));
147         LOG.info("Testing : {}", tested);
148         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
149         assertEquals(1, results.size());
150
151         assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
152     }
153
154 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.