2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.persist.storage.file.xml;
11 import com.google.common.base.Charsets;
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;
28 public class FileStorageAdapterTest {
34 public void setUp() throws Exception {
35 file = Files.createTempFile("testFilePersist", ".txt").toFile();
38 com.google.common.io.Files.write("", file, Charsets.UTF_8);
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);
50 final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
52 public String getConfigSnapshot() {
53 return createConfig();
57 public SortedSet<String> getCapabilities() {
61 storage.persistConfig(holder);
63 storage.persistConfig(holder);
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());
73 storage = new XmlFileStorageAdapter();
74 storage.setFileStorage(file);
75 storage.setNumberOfBackups(Integer.MAX_VALUE);
77 List<ConfigSnapshotHolder> last = storage.loadLastConfigs();
78 Assert.assertEquals(createCaps(), last.get(0).getCapabilities());
81 private SortedSet<String> createCaps() {
82 SortedSet<String> caps = new TreeSet<>();
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);
92 public void testFileAdapterOneBackup() throws Exception {
93 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
95 PropertiesProviderTest pp = new PropertiesProviderTest();
96 pp.addProperty("fileStorage",file.getPath());
97 pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE));
98 storage.instantiate(pp);
100 final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
102 public String getConfigSnapshot() {
103 return createConfig();
107 public SortedSet<String> getCapabilities() {
111 storage.persistConfig(holder);
113 storage.persistConfig(holder);
115 assertEquals(27, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size());
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", ""));
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() {
131 public String getConfigSnapshot() {
132 return createConfig();
136 public SortedSet<String> getCapabilities() {
140 storage.persistConfig(holder);
142 storage.persistConfig(holder);
143 storage.persistConfig(holder);
145 List<String> readLines = com.google.common.io.Files.readLines(file, Charsets.UTF_8);
146 assertEquals(27, readLines.size());
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()));
157 public void testNoLastConfig() throws Exception {
158 File file = Files.createTempFile("testFilePersist", ".txt").toFile();
161 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
162 storage.setFileStorage(file);
164 List<ConfigSnapshotHolder> elementOptional = storage.loadLastConfigs();
165 assertThat(elementOptional.size(), is(0));
168 @Test(expected = NullPointerException.class)
169 public void testNoProperties() throws Exception {
170 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
171 storage.loadLastConfigs();
174 @Test(expected = NullPointerException.class)
175 public void testNoProperties2() throws Exception {
176 XmlFileStorageAdapter storage = new XmlFileStorageAdapter();
177 storage.persistConfig(new ConfigSnapshotHolder() {
179 public String getConfigSnapshot() {
180 return Mockito.mock(String.class);
184 public SortedSet<String> getCapabilities() {
185 return new TreeSet<>();
190 static String createConfig() {
191 return "<config>" + i++ + "</config>";