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

©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.