Add filtering capability to config.ini in order to reference logging bridge version.
[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 import org.junit.Test;
16 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
17 import org.opendaylight.controller.config.persist.api.Persister;
18 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 public class DirectoryStorageAdapterTest {
26     Persister tested;
27     Logger logger = LoggerFactory.getLogger(DirectoryStorageAdapterTest.class.toString());
28
29     private Persister instantiatePersisterFromAdapter(File file){
30         PropertiesProviderTest pp = new PropertiesProviderTest();
31         pp.addProperty("directoryStorage",file.getPath());
32         XmlDirectoryStorageAdapter dsa = new XmlDirectoryStorageAdapter();
33         return dsa.instantiate(pp);
34     }
35
36     @Test
37     public void testEmptyDirectory() throws Exception {
38         File folder = new File("target/emptyFolder");
39         folder.mkdir();
40
41         tested = instantiatePersisterFromAdapter(folder);
42         assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
43
44         try {
45             tested.persistConfig(new ConfigSnapshotHolder() {
46                 @Override
47                 public String getConfigSnapshot() {
48                     throw new RuntimeException();
49                 }
50
51                 @Override
52                 public SortedSet<String> getCapabilities() {
53                     throw new RuntimeException();
54                 }
55             });
56             fail();
57         } catch (UnsupportedOperationException e) {
58
59         }
60     }
61
62     private File getFolder(String folderName) {
63         File result = new File(("src/test/resources/" +
64                 folderName).replace("/", File.separator));
65         assertTrue(result + " is not a directory", result.isDirectory());
66         return result;
67     }
68
69     @Test
70     public void testOneFile() throws Exception {
71         File folder = getFolder("oneFile");
72         tested = instantiatePersisterFromAdapter(folder);
73
74         logger.info("Testing : "+tested.toString());
75         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
76         assertEquals(1, results.size());
77         ConfigSnapshotHolder result = results.get(0);
78         assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
79     }
80
81     private void assertResult(ConfigSnapshotHolder result, String s, String... caps) {
82         assertEquals(s, result.getConfigSnapshot().replaceAll("\\s", ""));
83         int i = 0;
84         for (String capFromSnapshot : result.getCapabilities()) {
85             assertEquals(capFromSnapshot, caps[i++]);
86         }
87     }
88
89     @Test
90     public void testTwoFiles() throws Exception {
91         File folder = getFolder("twoFiles");
92         tested = instantiatePersisterFromAdapter(folder);
93         logger.info("Testing : "+tested.toString());
94         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
95         assertEquals(2, results.size());
96
97         assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
98         assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");
99
100     }
101
102 }