54ccc2ad0bb84dcaa8a4047f0c517ddec3671728
[yangtools.git] / yang / yang-repo-fs / src / test / java / org / opendaylight / yangtools / yang / model / repo / fs / 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 package org.opendaylight.yangtools.yang.model.repo.fs;
9
10 import static org.hamcrest.CoreMatchers.both;
11 import static org.hamcrest.CoreMatchers.containsString;
12 import static org.hamcrest.CoreMatchers.either;
13 import static org.hamcrest.CoreMatchers.hasItem;
14 import static org.hamcrest.MatcherAssert.assertThat;
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertThrows;
18 import static org.junit.Assert.assertTrue;
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23
24 import com.google.common.base.MoreObjects;
25 import com.google.common.collect.Collections2;
26 import com.google.common.io.Files;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import java.io.ByteArrayInputStream;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.nio.charset.StandardCharsets;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.List;
36 import java.util.concurrent.ExecutionException;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.opendaylight.yangtools.yang.common.Revision;
43 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
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 @RunWith(MockitoJUnitRunner.StrictStubs.class)
52 public class FilesystemSchemaSourceCacheTest {
53     @Mock
54     public SchemaSourceRegistry registry;
55     @Mock
56     public SchemaSourceRegistration<?> registration;
57
58     public File storageDir;
59
60     @Before
61     public void setUp() throws Exception {
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         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         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         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", Revision.of("2013-12-12"));
201         final ListenableFuture<? extends YangTextSchemaSource> checked = cache.getSource(sourceIdentifier);
202         assertNotNull(checked);
203         final YangTextSchemaSource checkedGet = checked.get();
204         assertEquals(sourceIdentifier, checkedGet.getIdentifier());
205         assertTrue(checked.isDone());
206     }
207
208     @Test
209     public void test1() throws Exception {
210
211         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
212                 YangTextSchemaSource.class, this.storageDir);
213         final String content = "content1";
214         final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
215         cache.offer(source);
216         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test1", Revision.of("2012-12-12"));
217         final ListenableFuture<? extends YangTextSchemaSource> checked = cache.getSource(sourceIdentifier);
218         assertNotNull(checked);
219         assertThrows(ExecutionException.class, () -> checked.get());
220     }
221
222     private List<File> getFilesFromCache() {
223         return Arrays.asList(this.storageDir.listFiles());
224     }
225
226     private class TestingYangSource extends YangTextSchemaSource {
227
228         private final String content;
229
230         TestingYangSource(final String name, final String revision, final String content) {
231             super(RevisionSourceIdentifier.create(name, Revision.ofNullable(revision)));
232             this.content = content;
233         }
234
235         @Override
236         protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
237             return toStringHelper;
238         }
239
240         @Override
241         public InputStream openStream() throws IOException {
242             return new ByteArrayInputStream(this.content.getBytes(StandardCharsets.UTF_8));
243         }
244     }
245 }