Merge "Fix for Bug 3"
[controller.git] / opendaylight / config / config-persister-directory-adapter / src / test / java / org / opendaylight / controller / config / persist / storage / directory / 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;
10
11 import java.io.File;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.SortedSet;
15 import java.util.TreeSet;
16 import org.apache.commons.io.IOUtils;
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 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
28     @Test
29     public void testEmptyDirectory() throws Exception {
30         File folder = new File("target/emptyFolder");
31         folder.mkdir();
32
33         PropertiesProviderTest pp = new PropertiesProviderTest();
34         pp.addProperty("directoryStorage",folder.getPath());
35         DirectoryStorageAdapter dsa = new DirectoryStorageAdapter();
36         tested = dsa.instantiate(pp);
37         assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
38
39         try {
40             tested.persistConfig(new ConfigSnapshotHolder() {
41                 @Override
42                 public String getConfigSnapshot() {
43                     throw new RuntimeException();
44                 }
45
46                 @Override
47                 public SortedSet<String> getCapabilities() {
48                     throw new RuntimeException();
49                 }
50             });
51             fail();
52         } catch (UnsupportedOperationException e) {
53
54         }
55     }
56
57     private File getFolder(String folderName) {
58         File result = new File(("src/test/resources/" +
59                 folderName).replace("/", File.separator));
60         assertTrue(result + " is not a directory", result.isDirectory());
61         return result;
62     }
63
64     @Test
65     public void testOneFile() throws Exception {
66         File folder = getFolder("oneFile");
67         PropertiesProviderTest pp = new PropertiesProviderTest();
68         pp.addProperty("directoryStorage",folder.getPath());
69         DirectoryStorageAdapter dsa = new DirectoryStorageAdapter();
70         tested = dsa.instantiate(pp);
71
72         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
73         assertEquals(1, results.size());
74         ConfigSnapshotHolder result = results.get(0);
75         assertSnapshot(result, "oneFileExpected");
76     }
77
78
79     @Test
80     public void testTwoFiles() throws Exception {
81         File folder = getFolder("twoFiles");
82         PropertiesProviderTest pp = new PropertiesProviderTest();
83         pp.addProperty("directoryStorage",folder.getPath());
84         DirectoryStorageAdapter dsa = new DirectoryStorageAdapter();
85         tested = dsa.instantiate(pp);
86
87         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
88         assertEquals(2, results.size());
89         assertSnapshot(results.get(0), "twoFilesExpected1");
90         assertSnapshot(results.get(1), "twoFilesExpected2");
91     }
92
93     private void assertSnapshot(ConfigSnapshotHolder result, String directory) throws Exception {
94         SortedSet<String> expectedCapabilities = new TreeSet<>(IOUtils.readLines(getClass().getResourceAsStream("/" + directory + "/expectedCapabilities.txt")));
95         String expectedSnapshot = IOUtils.toString(getClass().getResourceAsStream("/" + directory + "/expectedSnapshot.xml"));
96         assertEquals(expectedCapabilities, result.getCapabilities());
97         assertEquals(expectedSnapshot, result.getConfigSnapshot());
98     }
99
100 }