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

©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.