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