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