acb44ba441bbeccce605b3d1b3659d645d3c801d
[controller.git] / opendaylight / config / config-persister-file-adapter / src / test / java / org / opendaylight / controller / config / persist / storage / file / 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;
10
11 import com.google.common.base.Charsets;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Predicate;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.Sets;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.config.persist.api.Persister;
20
21 import java.io.File;
22 import java.nio.file.Files;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Set;
26
27 import static org.hamcrest.CoreMatchers.is;
28 import static org.junit.Assert.*;
29
30 public class FileStorageAdapterTest {
31
32     private static int i;
33     private File file;
34
35     @Before
36     public void setUp() throws Exception {
37         file = Files.createTempFile("testFilePersist", ".txt").toFile();
38         if (!file.exists())
39             return;
40         com.google.common.io.Files.write("", file, Charsets.UTF_8);
41         i = 1;
42     }
43
44     @Test
45     public void testFileAdapter() throws Exception {
46         FileStorageAdapter storage = new FileStorageAdapter();
47         storage.setFileStorage(file);
48         storage.setNumberOfBackups(Integer.MAX_VALUE);
49         final Persister.ConfigSnapshotHolder holder = new Persister.ConfigSnapshotHolder() {
50             @Override
51             public String getConfigSnapshot() {
52                 return createConfig();
53             }
54
55             @Override
56             public Set<String> getCapabilities() {
57                 return createCaps();
58             }
59         };
60         storage.persistConfig(holder);
61
62         storage.persistConfig(holder);
63
64         Collection<String> readLines = Collections2.filter(com.google.common.io.Files.readLines(file, Charsets.UTF_8),
65                 new Predicate<String>() {
66
67                     @Override
68                     public boolean apply(String input) {
69                         if (input.equals(""))
70                             return false;
71                         return true;
72                     }
73                 });
74         assertEquals(14, readLines.size());
75
76         Optional<Persister.ConfigSnapshotHolder> lastConf = storage.loadLastConfig();
77         assertTrue(lastConf.isPresent());
78         assertEquals("<config>2</config>",
79                 lastConf.get().getConfigSnapshot().replaceAll("\\s", ""));
80         assertEquals(createCaps(), lastConf.get().getCapabilities());
81     }
82
83     private Set<String> createCaps() {
84         Set<String> caps = Sets.newHashSet();
85
86         caps.add("cap1");
87         caps.add("cap2");
88         caps.add("capaaaa as dasfasdf s2");
89         return caps;
90     }
91
92     @Test
93     public void testFileAdapterOneBackup() throws Exception {
94         FileStorageAdapter storage = new FileStorageAdapter();
95         storage.setFileStorage(file);
96         storage.setNumberOfBackups(1);
97         final Persister.ConfigSnapshotHolder holder = new Persister.ConfigSnapshotHolder() {
98             @Override
99             public String getConfigSnapshot() {
100                 return createConfig();
101             }
102
103             @Override
104             public Set<String> getCapabilities() {
105                 return createCaps();
106             }
107         };
108         storage.persistConfig(holder);
109
110         storage.persistConfig(holder);
111
112         Collection<String> readLines = Collections2.filter(com.google.common.io.Files.readLines(file, Charsets.UTF_8),
113                 new Predicate<String>() {
114
115                     @Override
116                     public boolean apply(String input) {
117                         if (input.equals(""))
118                             return false;
119                         return true;
120                     }
121                 });
122         assertEquals(7, readLines.size());
123
124         Optional<Persister.ConfigSnapshotHolder> lastConf = storage.loadLastConfig();
125         assertTrue(lastConf.isPresent());
126         assertEquals("<config>2</config>",
127                 lastConf.get().getConfigSnapshot().replaceAll("\\s", ""));
128     }
129
130     @Test
131     public void testFileAdapterOnlyTwoBackups() throws Exception {
132         FileStorageAdapter storage = new FileStorageAdapter();
133         storage.setFileStorage(file);
134         storage.setNumberOfBackups(2);
135         final Persister.ConfigSnapshotHolder holder = new Persister.ConfigSnapshotHolder() {
136             @Override
137             public String getConfigSnapshot() {
138                 return createConfig();
139             }
140
141             @Override
142             public Set<String> getCapabilities() {
143                 return createCaps();
144             }
145         };
146         storage.persistConfig(holder);
147
148         storage.persistConfig(holder);
149         storage.persistConfig(holder);
150
151         Collection<String> readLines = Collections2.filter(com.google.common.io.Files.readLines(file, Charsets.UTF_8),
152                 new Predicate<String>() {
153
154                     @Override
155                     public boolean apply(String input) {
156                         if (input.equals(""))
157                             return false;
158                         return true;
159                     }
160                 });
161
162         assertEquals(14, readLines.size());
163
164         Optional<Persister.ConfigSnapshotHolder> lastConf = storage.loadLastConfig();
165         assertTrue(lastConf.isPresent());
166         assertEquals("<config>3</config>",
167                lastConf.get().getConfigSnapshot().replaceAll("\\s", ""));
168         assertFalse(readLines.contains(holder.getConfigSnapshot()));
169     }
170
171     @Test
172     public void testNoLastConfig() throws Exception {
173         File file = Files.createTempFile("testFilePersist", ".txt").toFile();
174         if (!file.exists())
175             return;
176         FileStorageAdapter storage = new FileStorageAdapter();
177         storage.setFileStorage(file);
178
179         Optional<Persister.ConfigSnapshotHolder> elementOptional = storage.loadLastConfig();
180         assertThat(elementOptional.isPresent(), is(false));
181     }
182
183     @Test(expected = NullPointerException.class)
184     public void testNoProperties() throws Exception {
185         FileStorageAdapter storage = new FileStorageAdapter();
186         storage.loadLastConfig();
187     }
188
189     @Test(expected = NullPointerException.class)
190     public void testNoProperties2() throws Exception {
191         FileStorageAdapter storage = new FileStorageAdapter();
192         storage.persistConfig(new Persister.ConfigSnapshotHolder() {
193             @Override
194             public String getConfigSnapshot() {
195                 return Mockito.mock(String.class);
196             }
197
198             @Override
199             public Set<String> getCapabilities() {
200                 return Collections.<String> emptySet();
201             }
202         } );
203     }
204
205     static String createConfig() {
206         return "<config>" + i++ + "</config>";
207     }
208
209 }

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