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

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