Merge changes I1474351f,I2ddc5ffa
[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 org.junit.Test;
12 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
13
14 import java.io.File;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.SortedSet;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22
23 public class DirectoryStorageAdapterTest {
24     XmlDirectoryPersister tested;
25
26     @Test
27     public void testEmptyDirectory() throws Exception {
28         File folder = new File("target/emptyFolder");
29         folder.mkdir();
30         tested = new XmlDirectoryPersister((folder));
31         assertEquals(Collections.<ConfigSnapshotHolder>emptyList(), tested.loadLastConfigs());
32
33         try {
34             tested.persistConfig(new ConfigSnapshotHolder() {
35                 @Override
36                 public String getConfigSnapshot() {
37                     throw new RuntimeException();
38                 }
39
40                 @Override
41                 public SortedSet<String> getCapabilities() {
42                     throw new RuntimeException();
43                 }
44             });
45             fail();
46         } catch (UnsupportedOperationException e) {
47
48         }
49     }
50
51     private File getFolder(String folderName) {
52         File result = new File(("src/test/resources/" +
53                 folderName).replace("/", File.separator));
54         assertTrue(result + " is not a directory", result.isDirectory());
55         return result;
56     }
57
58     @Test
59     public void testOneFile() throws Exception {
60         File folder = getFolder("oneFile");
61         tested = new XmlDirectoryPersister((folder));
62         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
63         assertEquals(1, results.size());
64         ConfigSnapshotHolder result = results.get(0);
65         assertResult(result, "<config>1</config>", "cap1&rev", "cap2", "capa a");
66     }
67
68     private void assertResult(ConfigSnapshotHolder result, String s, String... caps) {
69         assertEquals(s, result.getConfigSnapshot().replaceAll("\\s", ""));
70         int i = 0;
71         for (String capFromSnapshot : result.getCapabilities()) {
72             assertEquals(capFromSnapshot, caps[i++]);
73         }
74     }
75
76     @Test
77     public void testTwoFiles() throws Exception {
78         File folder = getFolder("twoFiles");
79         tested = new XmlDirectoryPersister((folder));
80         List<ConfigSnapshotHolder> results = tested.loadLastConfigs();
81         assertEquals(2, results.size());
82
83         assertResult(results.get(0), "<config>1</config>", "cap1-a", "cap2-a", "capa a-a");
84         assertResult(results.get(1), "<config>2</config>", "cap1-b", "cap2-b", "capa a-b");
85
86     }
87
88 }