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