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