Cleanup use of Guava library
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / repo / util / FilesystemSchemaSourceCacheTest.java
1 /*
2  * Copyright (c) 2014 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.yangtools.yang.model.repo.util;
10
11 import static org.hamcrest.CoreMatchers.both;
12 import static org.hamcrest.CoreMatchers.containsString;
13 import static org.hamcrest.CoreMatchers.either;
14 import static org.hamcrest.CoreMatchers.hasItem;
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertThat;
17 import static org.mockito.Matchers.any;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21
22 import com.google.common.base.MoreObjects;
23 import com.google.common.collect.Collections2;
24 import com.google.common.io.Files;
25 import com.google.common.util.concurrent.CheckedFuture;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import java.io.ByteArrayInputStream;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.Optional;
36 import java.util.concurrent.ExecutionException;
37 import org.junit.Assert;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
43 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
44 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
45 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
46 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
47 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
48 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
49 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
50
51 public class FilesystemSchemaSourceCacheTest {
52
53     @Mock
54     private SchemaSourceRegistry registry;
55     @Mock
56     private SchemaSourceRegistration<?> registration;
57     private File storageDir;
58
59     @Before
60     public void setUp() throws Exception {
61         MockitoAnnotations.initMocks(this);
62         this.storageDir = Files.createTempDir();
63         doReturn(this.registration).when(this.registry).registerSchemaSource(any(SchemaSourceProvider.class),
64             any(PotentialSchemaSource.class));
65     }
66
67     @Test
68     public void testCacheAndRestore() throws Exception {
69         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
70                 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
71
72         final String content = "content1";
73         final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
74         cache.offer(source);
75
76         final String content2 = "content2";
77         final YangTextSchemaSource source2 = new TestingYangSource("test2", null, content);
78         cache.offer(source2);
79
80         final List<File> storedFiles = getFilesFromCache();
81         assertEquals(2, storedFiles.size());
82         final Collection<String> fileNames = filesToFilenamesWithoutRevision(storedFiles);
83
84         assertThat(fileNames, both(hasItem("test2")).and(hasItem("test@2012-12-12")));
85
86         assertThat(Files.asCharSource(storedFiles.get(0), StandardCharsets.UTF_8).read(),
87             either(containsString(content)).or(containsString(content2)));
88         assertThat(Files.asCharSource(storedFiles.get(1), StandardCharsets.UTF_8).read(),
89             either(containsString(content)).or(containsString(content2)));
90
91         verify(this.registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class),
92             any(PotentialSchemaSource.class));
93
94         // Create new cache from stored sources
95         new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
96
97         verify(this.registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class),
98             any(PotentialSchemaSource.class));
99
100         final List<File> storedFilesAfterNewCache = getFilesFromCache();
101         assertEquals(2, storedFilesAfterNewCache.size());
102     }
103
104     private static Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
105         return Collections2.transform(storedFiles, input -> Files.getNameWithoutExtension(input.getName()));
106     }
107
108     @Test
109     public void testCacheDuplicate() throws Exception {
110         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
111                 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
112
113         final String content = "content1";
114         final YangTextSchemaSource source = new TestingYangSource("test", null, content);
115         // Double offer
116         cache.offer(source);
117         cache.offer(source);
118
119         final List<File> storedFiles = getFilesFromCache();
120         assertEquals(1, storedFiles.size());
121         verify(this.registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
122     }
123
124     @Test
125     public void testCacheMultipleRevisions() throws Exception {
126         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
127                 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
128
129         final String content = "content1";
130         final YangTextSchemaSource source = new TestingYangSource("test", null, content);
131         final YangTextSchemaSource source2 = new TestingYangSource("test", "2012-12-12", content);
132         final YangTextSchemaSource source3 = new TestingYangSource("test", "2013-12-12", content);
133         // Double offer
134         cache.offer(source);
135         cache.offer(source2);
136         cache.offer(source3);
137
138         final List<File> storedFiles = getFilesFromCache();
139         assertEquals(3, storedFiles.size());
140
141         assertThat(filesToFilenamesWithoutRevision(storedFiles), both(hasItem("test"))
142             .and(hasItem("test@2012-12-12")).and(hasItem("test@2013-12-12")));
143
144         verify(this.registry, times(3)).registerSchemaSource(any(SchemaSourceProvider.class),
145             any(PotentialSchemaSource.class));
146     }
147
148     @Test
149     public void sourceIdToFileEmptyRevWithEmptyDir() {
150         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "");
151         final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, this.storageDir);
152         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
153                 YangTextSchemaSource.class, sourceIdToFile);
154         Assert.assertNotNull(cache);
155         final List<File> storedFiles = Arrays.asList(sourceIdToFile.listFiles());
156         assertEquals(0, storedFiles.size());
157     }
158
159     @Test
160     public void sourceIdToFileEmptyRevWithOneItemInDir() {
161         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
162                 YangTextSchemaSource.class, this.storageDir);
163         final String content = "content1";
164         final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
165         cache.offer(source);
166
167         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "");
168         final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier,
169                 this.storageDir);
170         Assert.assertNotNull(sourceIdToFile);
171         final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
172         assertEquals(1, storedFiles.size());
173     }
174
175     @Test
176     public void sourceIdToFileEmptyRevWithMoreItemsInDir() {
177         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
178                 YangTextSchemaSource.class, this.storageDir);
179         final String content = "content1";
180         final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
181         final YangTextSchemaSource source2 = new TestingYangSource("test", "2013-12-12", content);
182         cache.offer(source);
183         cache.offer(source2);
184
185         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "");
186         final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, this.storageDir);
187         Assert.assertNotNull(sourceIdToFile);
188         final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
189         assertEquals(2, storedFiles.size());
190     }
191
192     @Test
193     public void test() throws Exception {
194
195         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
196                 YangTextSchemaSource.class, this.storageDir);
197         final String content = "content1";
198         final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
199         cache.offer(source);
200         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "2013-12-12");
201         final CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> checked = cache
202                 .getSource(sourceIdentifier);
203         Assert.assertNotNull(checked);
204         final YangTextSchemaSource checkedGet = checked.checkedGet();
205         Assert.assertEquals(sourceIdentifier, checkedGet.getIdentifier());
206         Assert.assertTrue(checked.isDone());
207     }
208
209     @Test(expected = ExecutionException.class)
210     public void test1() throws Exception {
211
212         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
213                 YangTextSchemaSource.class, this.storageDir);
214         final String content = "content1";
215         final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
216         cache.offer(source);
217         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test1", "2012-12-12");
218         final ListenableFuture<? extends YangTextSchemaSource> checked = cache.getSource(sourceIdentifier);
219         Assert.assertNotNull(checked);
220         checked.get();
221     }
222
223     private List<File> getFilesFromCache() {
224         return Arrays.asList(this.storageDir.listFiles());
225     }
226
227     private class TestingYangSource extends YangTextSchemaSource {
228
229         private final String content;
230
231         protected TestingYangSource(final String name, final String revision, final String content) {
232             super(RevisionSourceIdentifier.create(name, Optional.ofNullable(revision)));
233             this.content = content;
234         }
235
236         @Override
237         protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
238             return toStringHelper;
239         }
240
241         @Override
242         public InputStream openStream() throws IOException {
243             return new ByteArrayInputStream(this.content.getBytes(StandardCharsets.UTF_8));
244         }
245     }
246 }