Merge "Fixed add/delete/modify RPC for group/flow/remove reach the test provider"
[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.Predicate;
13 import com.google.common.collect.Collections2;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mockito;
17 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
18
19 import java.io.File;
20 import java.nio.file.Files;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.SortedSet;
24 import java.util.TreeSet;
25
26 import static junit.framework.Assert.assertFalse;
27 import static org.hamcrest.CoreMatchers.is;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertThat;
30
31 public class FileStorageAdapterTest {
32
33     private static int i;
34     private File file;
35
36     @Before
37     public void setUp() throws Exception {
38         file = Files.createTempFile("testFilePersist", ".txt").toFile();
39         if (!file.exists())
40             return;
41         com.google.common.io.Files.write("", file, Charsets.UTF_8);
42         i = 1;
43     }
44
45     @Test
46     public void testFileAdapter() throws Exception {
47         FileStorageAdapter storage = new FileStorageAdapter();
48         storage.setFileStorage(file);
49         storage.setNumberOfBackups(Integer.MAX_VALUE);
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         Collection<String> readLines = Collections2.filter(com.google.common.io.Files.readLines(file, Charsets.UTF_8),
66                 new Predicate<String>() {
67
68                     @Override
69                     public boolean apply(String input) {
70                         if (input.equals(""))
71                             return false;
72                         return true;
73                     }
74                 });
75         assertEquals(14, readLines.size());
76
77         List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
78         assertEquals(1, lastConf.size());
79         ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
80         assertEquals("<config>2</config>",
81                 configSnapshotHolder.getConfigSnapshot().replaceAll("\\s", ""));
82         assertEquals(createCaps(), configSnapshotHolder.getCapabilities());
83     }
84
85     private SortedSet<String> createCaps() {
86         SortedSet<String> caps = new TreeSet<>();
87
88         caps.add("cap1");
89         caps.add("cap2");
90         caps.add("capaaaa as dasfasdf s2");
91         return caps;
92     }
93
94     @Test
95     public void testFileAdapterOneBackup() throws Exception {
96         FileStorageAdapter storage = new FileStorageAdapter();
97         storage.setFileStorage(file);
98         storage.setNumberOfBackups(1);
99         final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
100             @Override
101             public String getConfigSnapshot() {
102                 return createConfig();
103             }
104
105             @Override
106             public SortedSet<String> getCapabilities() {
107                 return createCaps();
108             }
109         };
110         storage.persistConfig(holder);
111
112         storage.persistConfig(holder);
113
114         Collection<String> readLines = Collections2.filter(com.google.common.io.Files.readLines(file, Charsets.UTF_8),
115                 new Predicate<String>() {
116
117                     @Override
118                     public boolean apply(String input) {
119                         if (input.equals(""))
120                             return false;
121                         return true;
122                     }
123                 });
124         assertEquals(7, readLines.size());
125
126         List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
127         assertEquals(1, lastConf.size());
128         ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
129         assertEquals("<config>2</config>",
130                 configSnapshotHolder.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 ConfigSnapshotHolder holder = new ConfigSnapshotHolder() {
139             @Override
140             public String getConfigSnapshot() {
141                 return createConfig();
142             }
143
144             @Override
145             public SortedSet<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         List<ConfigSnapshotHolder> lastConf = storage.loadLastConfigs();
168         assertEquals(1, lastConf.size());
169         ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0);
170         assertEquals("<config>3</config>",
171                 configSnapshotHolder.getConfigSnapshot().replaceAll("\\s", ""));
172         assertFalse(readLines.contains(holder.getConfigSnapshot()));
173     }
174
175     @Test
176     public void testNoLastConfig() throws Exception {
177         File file = Files.createTempFile("testFilePersist", ".txt").toFile();
178         if (!file.exists())
179             return;
180         FileStorageAdapter storage = new FileStorageAdapter();
181         storage.setFileStorage(file);
182
183         List<ConfigSnapshotHolder> elementOptional = storage.loadLastConfigs();
184         assertThat(elementOptional.size(), is(0));
185     }
186
187     @Test(expected = NullPointerException.class)
188     public void testNoProperties() throws Exception {
189         FileStorageAdapter storage = new FileStorageAdapter();
190         storage.loadLastConfigs();
191     }
192
193     @Test(expected = NullPointerException.class)
194     public void testNoProperties2() throws Exception {
195         FileStorageAdapter storage = new FileStorageAdapter();
196         storage.persistConfig(new ConfigSnapshotHolder() {
197             @Override
198             public String getConfigSnapshot() {
199                 return Mockito.mock(String.class);
200             }
201
202             @Override
203             public SortedSet<String> getCapabilities() {
204                 return new TreeSet<>();
205             }
206         } );
207     }
208
209     static String createConfig() {
210         return "<config>" + i++ + "</config>";
211     }
212
213 }