Merge "Add support for enums as configuration attributes in config and netconf subsys...
[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 com.google.common.base.Optional;
12 import org.apache.commons.io.IOUtils;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.util.SortedSet;
20 import java.util.TreeSet;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 public class DirectoryStorageAdapterTest {
27     DirectoryPersister tested;
28     SortedSet<String> expectedCapabilities;
29     String expectedSnapshot;
30
31     @Before
32     public void setUp() throws Exception {
33         expectedCapabilities = new TreeSet<>(IOUtils.readLines(getClass().getResourceAsStream("/expectedCapabilities.txt")));
34         expectedSnapshot = IOUtils.toString(getClass().getResourceAsStream("/expectedSnapshot.xml"));
35     }
36
37     @Test
38     public void testEmptyDirectory() throws Exception {
39         File folder = new File("target/emptyFolder");
40         folder.mkdir();
41         tested = new DirectoryPersister((folder));
42         assertEquals(Optional.<ConfigSnapshotHolder>absent(), tested.loadLastConfig());
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 = new DirectoryPersister((folder));
73         assertExpected();
74     }
75
76     private void assertExpected() throws IOException {
77         Optional<ConfigSnapshotHolder> maybeResult = tested.loadLastConfig();
78         assertTrue(maybeResult.isPresent());
79         ConfigSnapshotHolder result = maybeResult.get();
80         assertEquals(expectedCapabilities, result.getCapabilities());
81         assertEquals(expectedSnapshot, result.getConfigSnapshot());
82     }
83
84     @Test
85     public void testTwoFiles() throws Exception {
86         File folder = getFolder("twoFiles");
87         tested = new DirectoryPersister((folder));
88         assertExpected();
89     }
90
91 }