2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.repo.fs;
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;
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;
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;
50 @RunWith(MockitoJUnitRunner.StrictStubs.class)
51 public class FilesystemSchemaSourceCacheTest {
53 public SchemaSourceRegistry registry;
55 public SchemaSourceRegistration<?> registration;
57 public File storageDir;
60 public void setUp() throws Exception {
61 storageDir = Files.createTempDir();
62 doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class),
63 any(PotentialSchemaSource.class));
67 public void testCacheAndRestore() throws Exception {
68 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
69 = new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
71 final String content = "content1";
72 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
75 final String content2 = "content2";
76 final YangTextSchemaSource source2 = new TestingYangSource("test2", null, content);
79 final List<File> storedFiles = getFilesFromCache();
80 assertEquals(2, storedFiles.size());
81 final Collection<String> fileNames = filesToFilenamesWithoutRevision(storedFiles);
83 assertThat(fileNames, both(hasItem("test2")).and(hasItem("test@2012-12-12")));
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)));
90 verify(registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class),
91 any(PotentialSchemaSource.class));
93 // Create new cache from stored sources
94 new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
96 verify(registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class),
97 any(PotentialSchemaSource.class));
99 final List<File> storedFilesAfterNewCache = getFilesFromCache();
100 assertEquals(2, storedFilesAfterNewCache.size());
103 private static Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
104 return Collections2.transform(storedFiles, input -> Files.getNameWithoutExtension(input.getName()));
108 public void testCacheDuplicate() throws Exception {
109 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
110 = new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
112 final String content = "content1";
113 final YangTextSchemaSource source = new TestingYangSource("test", null, content);
118 final List<File> storedFiles = getFilesFromCache();
119 assertEquals(1, storedFiles.size());
120 verify(registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
124 public void testCacheMultipleRevisions() throws Exception {
125 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
126 = new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
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);
134 cache.offer(source2);
135 cache.offer(source3);
137 final List<File> storedFiles = getFilesFromCache();
138 assertEquals(3, storedFiles.size());
140 assertThat(filesToFilenamesWithoutRevision(storedFiles), both(hasItem("test"))
141 .and(hasItem("test@2012-12-12")).and(hasItem("test@2013-12-12")));
143 verify(registry, times(3)).registerSchemaSource(any(SchemaSourceProvider.class),
144 any(PotentialSchemaSource.class));
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());
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);
166 final SourceIdentifier sourceIdentifier = new SourceIdentifier("test");
167 final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier,
169 assertNotNull(sourceIdToFile);
170 final List<File> storedFiles = Arrays.asList(storageDir.listFiles());
171 assertEquals(1, storedFiles.size());
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);
182 cache.offer(source2);
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());
192 public void test() throws Exception {
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);
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());
208 public void test1() throws Exception {
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);
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());
221 private List<File> getFilesFromCache() {
222 return Arrays.asList(storageDir.listFiles());
225 private static class TestingYangSource extends YangTextSchemaSource {
226 private final String content;
228 TestingYangSource(final String name, final String revision, final String content) {
229 super(new SourceIdentifier(name, revision));
230 this.content = content;
234 protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
235 return toStringHelper;
239 public InputStream openStream() throws IOException {
240 return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
244 public Optional<String> getSymbolicName() {
245 return Optional.empty();