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 com.google.common.base.MoreObjects;
23 import com.google.common.base.Optional;
24 import com.google.common.collect.Collections2;
25 import com.google.common.io.Files;
26 import com.google.common.util.concurrent.CheckedFuture;
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.concurrent.ExecutionException;
37 import org.junit.Assert;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
43 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
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;
51 public class FilesystemSchemaSourceCacheTest {
54 private SchemaSourceRegistry registry;
56 private SchemaSourceRegistration<?> registration;
57 private File storageDir;
60 public void setUp() throws Exception {
61 MockitoAnnotations.initMocks(this);
62 this.storageDir = Files.createTempDir();
63 doReturn(this.registration).when(this.registry).registerSchemaSource(any(SchemaSourceProvider.class),
64 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")).and(hasItem("test@2012-12-12")));
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)));
91 verify(this.registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class),
92 any(PotentialSchemaSource.class));
94 // Create new cache from stored sources
95 new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
97 verify(this.registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class),
98 any(PotentialSchemaSource.class));
100 final List<File> storedFilesAfterNewCache = getFilesFromCache();
101 assertEquals(2, storedFilesAfterNewCache.size());
104 private static Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
105 return Collections2.transform(storedFiles, input -> Files.getNameWithoutExtension(input.getName()));
109 public void testCacheDuplicate() throws Exception {
110 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
111 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
113 final String content = "content1";
114 final YangTextSchemaSource source = new TestingYangSource("test", null, content);
119 final List<File> storedFiles = getFilesFromCache();
120 assertEquals(1, storedFiles.size());
121 verify(this.registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
125 public void testCacheMultipleRevisions() throws Exception {
126 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
127 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
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);
135 cache.offer(source2);
136 cache.offer(source3);
138 final List<File> storedFiles = getFilesFromCache();
139 assertEquals(3, storedFiles.size());
141 assertThat(filesToFilenamesWithoutRevision(storedFiles), both(hasItem("test"))
142 .and(hasItem("test@2012-12-12")).and(hasItem("test@2013-12-12")));
144 verify(this.registry, times(3)).registerSchemaSource(any(SchemaSourceProvider.class),
145 any(PotentialSchemaSource.class));
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 Assert.assertNotNull(cache);
155 final List<File> storedFiles = Arrays.asList(sourceIdToFile.listFiles());
156 assertEquals(0, storedFiles.size());
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);
167 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "");
168 final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier,
170 Assert.assertNotNull(sourceIdToFile);
171 final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
172 assertEquals(1, storedFiles.size());
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);
183 cache.offer(source2);
185 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "");
186 final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, this.storageDir);
187 Assert.assertNotNull(sourceIdToFile);
188 final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
189 assertEquals(2, storedFiles.size());
193 public void test() throws Exception {
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);
200 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "2013-12-12");
201 final CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> checked = cache
202 .getSource(sourceIdentifier);
203 Assert.assertNotNull(checked);
204 final YangTextSchemaSource checkedGet = checked.checkedGet();
205 Assert.assertEquals(sourceIdentifier, checkedGet.getIdentifier());
206 Assert.assertTrue(checked.isDone());
209 @Test(expected = ExecutionException.class)
210 public void test1() throws Exception {
212 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
213 YangTextSchemaSource.class, this.storageDir);
214 final String content = "content1";
215 final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
217 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test1", "2012-12-12");
218 final ListenableFuture<? extends YangTextSchemaSource> checked = cache.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));