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
9 package org.opendaylight.yangtools.yang.model.repo.util;
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.ArgumentMatchers.any;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
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.ListenableFuture;
26 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.charset.StandardCharsets;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.concurrent.ExecutionException;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.opendaylight.yangtools.yang.common.Revision;
41 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
42 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
43 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
44 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
45 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
46 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
47 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
49 public class FilesystemSchemaSourceCacheTest {
52 private SchemaSourceRegistry registry;
54 private SchemaSourceRegistration<?> registration;
55 private File storageDir;
58 public void setUp() throws Exception {
59 MockitoAnnotations.initMocks(this);
60 this.storageDir = Files.createTempDir();
61 doReturn(this.registration).when(this.registry).registerSchemaSource(any(SchemaSourceProvider.class),
62 any(PotentialSchemaSource.class));
66 public void testCacheAndRestore() throws Exception {
67 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
68 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
70 final String content = "content1";
71 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
74 final String content2 = "content2";
75 final YangTextSchemaSource source2 = new TestingYangSource("test2", null, content);
78 final List<File> storedFiles = getFilesFromCache();
79 assertEquals(2, storedFiles.size());
80 final Collection<String> fileNames = filesToFilenamesWithoutRevision(storedFiles);
82 assertThat(fileNames, both(hasItem("test2")).and(hasItem("test@2012-12-12")));
84 assertThat(Files.asCharSource(storedFiles.get(0), StandardCharsets.UTF_8).read(),
85 either(containsString(content)).or(containsString(content2)));
86 assertThat(Files.asCharSource(storedFiles.get(1), StandardCharsets.UTF_8).read(),
87 either(containsString(content)).or(containsString(content2)));
89 verify(this.registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class),
90 any(PotentialSchemaSource.class));
92 // Create new cache from stored sources
93 new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
95 verify(this.registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class),
96 any(PotentialSchemaSource.class));
98 final List<File> storedFilesAfterNewCache = getFilesFromCache();
99 assertEquals(2, storedFilesAfterNewCache.size());
102 private static Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
103 return Collections2.transform(storedFiles, input -> Files.getNameWithoutExtension(input.getName()));
107 public void testCacheDuplicate() throws Exception {
108 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
109 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
111 final String content = "content1";
112 final YangTextSchemaSource source = new TestingYangSource("test", null, content);
117 final List<File> storedFiles = getFilesFromCache();
118 assertEquals(1, storedFiles.size());
119 verify(this.registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
123 public void testCacheMultipleRevisions() throws Exception {
124 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
125 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
127 final String content = "content1";
128 final YangTextSchemaSource source = new TestingYangSource("test", null, content);
129 final YangTextSchemaSource source2 = new TestingYangSource("test", "2012-12-12", content);
130 final YangTextSchemaSource source3 = new TestingYangSource("test", "2013-12-12", content);
133 cache.offer(source2);
134 cache.offer(source3);
136 final List<File> storedFiles = getFilesFromCache();
137 assertEquals(3, storedFiles.size());
139 assertThat(filesToFilenamesWithoutRevision(storedFiles), both(hasItem("test"))
140 .and(hasItem("test@2012-12-12")).and(hasItem("test@2013-12-12")));
142 verify(this.registry, times(3)).registerSchemaSource(any(SchemaSourceProvider.class),
143 any(PotentialSchemaSource.class));
147 public void sourceIdToFileEmptyRevWithEmptyDir() {
148 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test");
149 final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, this.storageDir);
150 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
151 YangTextSchemaSource.class, sourceIdToFile);
152 Assert.assertNotNull(cache);
153 final List<File> storedFiles = Arrays.asList(sourceIdToFile.listFiles());
154 assertEquals(0, storedFiles.size());
158 public void sourceIdToFileEmptyRevWithOneItemInDir() {
159 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
160 YangTextSchemaSource.class, this.storageDir);
161 final String content = "content1";
162 final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
165 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test");
166 final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier,
168 Assert.assertNotNull(sourceIdToFile);
169 final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
170 assertEquals(1, storedFiles.size());
174 public void sourceIdToFileEmptyRevWithMoreItemsInDir() {
175 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
176 YangTextSchemaSource.class, this.storageDir);
177 final String content = "content1";
178 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
179 final YangTextSchemaSource source2 = new TestingYangSource("test", "2013-12-12", content);
181 cache.offer(source2);
183 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test");
184 final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, this.storageDir);
185 Assert.assertNotNull(sourceIdToFile);
186 final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
187 assertEquals(2, storedFiles.size());
191 public void test() throws Exception {
193 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
194 YangTextSchemaSource.class, this.storageDir);
195 final String content = "content1";
196 final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
198 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", Revision.of("2013-12-12"));
199 final ListenableFuture<? extends YangTextSchemaSource> checked = cache.getSource(sourceIdentifier);
200 Assert.assertNotNull(checked);
201 final YangTextSchemaSource checkedGet = checked.get();
202 Assert.assertEquals(sourceIdentifier, checkedGet.getIdentifier());
203 Assert.assertTrue(checked.isDone());
206 @Test(expected = ExecutionException.class)
207 public void test1() throws Exception {
209 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
210 YangTextSchemaSource.class, this.storageDir);
211 final String content = "content1";
212 final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
214 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test1", Revision.of("2012-12-12"));
215 final ListenableFuture<? extends YangTextSchemaSource> checked = cache.getSource(sourceIdentifier);
216 Assert.assertNotNull(checked);
220 private List<File> getFilesFromCache() {
221 return Arrays.asList(this.storageDir.listFiles());
224 private class TestingYangSource extends YangTextSchemaSource {
226 private final String content;
228 TestingYangSource(final String name, final String revision, final String content) {
229 super(RevisionSourceIdentifier.create(name, Revision.ofNullable(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(this.content.getBytes(StandardCharsets.UTF_8));