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.Matchers.any;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
22 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
24 import com.google.common.base.Function;
25 import com.google.common.base.MoreObjects;
26 import com.google.common.base.Optional;
27 import com.google.common.collect.Collections2;
28 import com.google.common.io.Files;
29 import com.google.common.util.concurrent.CheckedFuture;
30 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.charset.StandardCharsets;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.List;
38 import java.util.concurrent.ExecutionException;
39 import org.junit.Assert;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
45 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
46 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
47 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
48 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
49 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
50 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
52 public class FilesystemSchemaSourceCacheTest {
55 private SchemaSourceRegistry registry;
57 private SchemaSourceRegistration<?> registration;
58 private File storageDir;
61 public void setUp() throws Exception {
62 MockitoAnnotations.initMocks(this);
63 this.storageDir = Files.createTempDir();
64 doReturn(this.registration).when(this.registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
68 public void testCacheAndRestore() throws Exception {
69 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
70 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
72 final String content = "content1";
73 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
76 final String content2 = "content2";
77 final YangTextSchemaSource source2 = new TestingYangSource("test2", null, content);
80 final List<File> storedFiles = getFilesFromCache();
81 assertEquals(2, storedFiles.size());
82 final Collection<String> fileNames = filesToFilenamesWithoutRevision(storedFiles);
84 assertThat(fileNames, both(hasItem("test2@0000-00-00")).and(hasItem("test@2012-12-12")));
86 assertThat(Files.toString(storedFiles.get(0), StandardCharsets.UTF_8), either(containsString(content)).or(containsString(content2)));
87 assertThat(Files.toString(storedFiles.get(1), StandardCharsets.UTF_8), either(containsString(content)).or(containsString(content2)));
89 verify(this.registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
91 // Create new cache from stored sources
92 new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
94 verify(this.registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
96 final List<File> storedFilesAfterNewCache = getFilesFromCache();
97 assertEquals(2, storedFilesAfterNewCache.size());
100 private static Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
101 return Collections2.transform(storedFiles, new Function<File, String>() {
103 public String apply(final File input) {
104 return Files.getNameWithoutExtension(input.getName());
110 public void testCacheDuplicate() throws Exception {
111 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
112 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
114 final String content = "content1";
115 final YangTextSchemaSource source = new TestingYangSource("test", null, content);
120 final List<File> storedFiles = getFilesFromCache();
121 assertEquals(1, storedFiles.size());
122 verify(this.registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
126 public void testCacheMultipleRevisions() throws Exception {
127 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
128 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
130 final String content = "content1";
131 final YangTextSchemaSource source = new TestingYangSource("test", null, content);
132 final YangTextSchemaSource source2 = new TestingYangSource("test", "2012-12-12", content);
133 final YangTextSchemaSource source3 = new TestingYangSource("test", "2013-12-12", content);
136 cache.offer(source2);
137 cache.offer(source3);
139 final List<File> storedFiles = getFilesFromCache();
140 assertEquals(3, storedFiles.size());
142 assertThat(filesToFilenamesWithoutRevision(storedFiles), both(hasItem("test@0000-00-00")).and(hasItem("test@2012-12-12")).and(hasItem("test@2013-12-12")));
144 verify(this.registry, times(3)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
148 public void sourceIdToFileEmptyRevWithEmptyDir() {
149 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "");
150 final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, this.storageDir);
151 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
152 YangTextSchemaSource.class, sourceIdToFile);
153 Assert.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<>(this.registry,
161 YangTextSchemaSource.class, this.storageDir);
162 final String content = "content1";
163 final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
166 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "");
167 final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier,
169 Assert.assertNotNull(sourceIdToFile);
170 final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
171 assertEquals(1, storedFiles.size());
175 public void sourceIdToFileEmptyRevWithMoreItemsInDir() {
176 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
177 YangTextSchemaSource.class, this.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 = RevisionSourceIdentifier.create("test", "");
185 final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, this.storageDir);
186 Assert.assertNotNull(sourceIdToFile);
187 final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
188 assertEquals(2, storedFiles.size());
192 public void test() throws Exception {
194 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
195 YangTextSchemaSource.class, this.storageDir);
196 final String content = "content1";
197 final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
199 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "2013-12-12");
200 final CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> checked = cache
201 .getSource(sourceIdentifier);
202 Assert.assertNotNull(checked);
203 final YangTextSchemaSource checkedGet = checked.checkedGet();
204 Assert.assertEquals(sourceIdentifier, checkedGet.getIdentifier());
205 Assert.assertTrue(checked.isDone());
208 @Test(expected = ExecutionException.class)
209 public void test1() throws Exception {
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);
216 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test1", "2012-12-12");
217 final CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> checked = cache
218 .getSource(sourceIdentifier);
219 Assert.assertNotNull(checked);
223 private List<File> getFilesFromCache() {
224 return Arrays.asList(this.storageDir.listFiles());
227 private class TestingYangSource extends YangTextSchemaSource {
229 private final String content;
231 protected TestingYangSource(final String name, final String revision, final String content) {
232 super(RevisionSourceIdentifier.create(name, Optional.fromNullable(revision)));
233 this.content = content;
237 protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
238 return toStringHelper;
242 public InputStream openStream() throws IOException {
243 return new ByteArrayInputStream(this.content.getBytes(StandardCharsets.UTF_8));