Update junit Assert imports
[controller.git] / opendaylight / config / config-persister-file-xml-adapter / src / test / java / org / opendaylight / controller / config / persist / storage / file / xml / FileStorageAdapterTest.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.file.xml;
10
11 import static org.junit.Assert.assertFalse;
12 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
13 import static org.hamcrest.CoreMatchers.is;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertThat;
16 import com.google.common.base.Charsets;
17 import java.io.File;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.nio.file.Files;
21 import java.util.List;
22 import java.util.SortedSet;
23 import java.util.TreeSet;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
28 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
29
30 public class FileStorageAdapterTest {
31
32     private static int i;
33     private File file;
34     private static final String NON_EXISTENT_DIRECTORY = "./nonExistentDir/";
35     private static final String NON_EXISTENT_FILE = "nonExistent.txt";
36
37     @Before
38     public void setUp() throws Exception {
39         file = Files.createTempFile("testFilePersist", ".txt").toFile();
40         if (!file.exists()) {
41             return;
42         }
43         com.google.common.io.Files.write("", file, Charsets.UTF_8);
44         i = 1;
45     }
46
47     @Test
48     public void testNewFile() throws Exception {
49         XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
50         PropertiesProviderTest pp = new PropertiesProviderTest();
51         pp.addProperty("fileStorage",NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE);
52         pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
53         storage.instantiate(pp);
54
55         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
56             @Override
57             public String getConfigSnapshot() {
58                 return createConfig();
59             }
60
61             @Override
62             public SortedSet<String> getCapabilities() {
63                 return createCaps();
64             }
65         };
66         storage.persistConfig(holder);
67
68         storage.persistConfig(holder);
69
70         assertEquals(storage.toString().replace("\\","/"),"XmlFileStorageAdapter [storage="+NON_EXISTENT_DIRECTORY+NON_EXISTENT_FILE+"]");
71         delete(new File(NON_EXISTENT_DIRECTORY));
72     }
73     @Test
74     public void testFileAdapter() throws Exception {
75         XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
76         PropertiesProviderTest pp = new PropertiesProviderTest();
77         pp.addProperty("fileStorage",file.getPath());
78         pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
79         storage.instantiate(pp);
80
81         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
82             @Override
83             public String getConfigSnapshot() {
84                 return createConfig();
85             }
86
87             @Override
88             public SortedSet<String> getCapabilities() {
89                 return createCaps();
90             }
91         };
92         storage.persistConfig(holder);
93
94         storage.persistConfig(holder);
95
96         assertEquals(27, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size());
97         List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
98         assertEquals(1, lastConf.size());
99         ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
100         assertXMLEqual("<config>2</config>", configSnapshotHolder.getConfigSnapshot());
101         assertEquals(createCaps(), configSnapshotHolder.getCapabilities());
102
103         storage = new XmlFileStorageAdapter();
104         storage.setFileStorage(file);
105         storage.setNumberOfBackups(Integer.MAX_VALUE);
106
107         List<ConfigSnapshotHolder> last = storage.loadLastConfigs();
108         assertEquals(createCaps(), last.get(0).getCapabilities());
109     }
110
111     private SortedSet<String> createCaps() {
112         SortedSet<String> caps = new TreeSet<>();
113
114         caps.add("cap1" + i);
115         caps.add("cap2" + i);
116         caps.add("urn:opendaylight:params:xml:ns:yang:controller:netty?module=netty&revision=2013-11-19" + i);
117         caps.add("capaaaa as dasfasdf s2" + i);
118         return caps;
119     }
120
121     @Test
122     public void testFileAdapterOneBackup() throws Exception {
123         XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
124
125         PropertiesProviderTest pp = new PropertiesProviderTest();
126         pp.addProperty("fileStorage",file.getPath());
127         pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
128         storage.instantiate(pp);
129
130         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
131             @Override
132             public String getConfigSnapshot() {
133                 return createConfig();
134             }
135
136             @Override
137             public SortedSet<String> getCapabilities() {
138                 return createCaps();
139             }
140         };
141         storage.persistConfig(holder);
142
143         storage.persistConfig(holder);
144
145         assertEquals(27, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size());
146
147         List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
148         assertEquals(1, lastConf.size());
149         ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
150         assertXMLEqual("<config>2</config>", configSnapshotHolder.getConfigSnapshot());
151     }
152
153     @Test
154     public void testFileAdapterOnlyTwoBackups() throws Exception {
155         XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
156         storage.setFileStorage(file);
157         storage.setNumberOfBackups(2);
158         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
159             @Override
160             public String getConfigSnapshot() {
161                 return createConfig();
162             }
163
164             @Override
165             public SortedSet<String> getCapabilities() {
166                 return createCaps();
167             }
168         };
169         storage.persistConfig(holder);
170
171         storage.persistConfig(holder);
172         storage.persistConfig(holder);
173
174         List<String> readLines = com.google.common.io.Files.readLines(file, Charsets.UTF_8);
175         assertEquals(27, readLines.size());
176
177         List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
178         assertEquals(1, lastConf.size());
179         ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
180         assertXMLEqual("<config>3</config>", configSnapshotHolder.getConfigSnapshot());
181         assertFalse(readLines.contains(holder.getConfigSnapshot()));
182     }
183
184     @Test
185     public void testNoLastConfig() throws Exception {
186         File file = Files.createTempFile("testFilePersist", ".txt").toFile();
187         if (!file.exists()) {
188             return;
189         }
190         XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
191         storage.setFileStorage(file);
192
193         List<ConfigSnapshotHolder> elementOptional = storage.loadLastConfigs();
194         assertThat(elementOptional.size(), is(0));
195     }
196
197     @Test(expected = NullPointerException.class)
198     public void testNoProperties() throws Exception {
199         XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
200         storage.loadLastConfigs();
201     }
202
203     @Test(expected = NullPointerException.class)
204     public void testNoProperties2() throws Exception {
205         XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
206         storage.persistConfig(new ConfigSnapshotHolder() {
207             @Override
208             public String getConfigSnapshot() {
209                 return Mockito.mock(String.class);
210             }
211
212             @Override
213             public SortedSet<String> getCapabilities() {
214                 return new TreeSet<>();
215             }
216         } );
217     }
218
219     static String createConfig() {
220         return "<config>" + i++ + "</config>";
221     }
222
223     private void delete(final File f) throws IOException {
224         if (f.isDirectory()) {
225             for (File c : f.listFiles()) {
226                 delete(c);
227             }
228         }
229         if (!f.delete()) {
230             throw new FileNotFoundException("Failed to delete file: " + f);
231         }
232     }
233 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.