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.MoreObjects;
25 import com.google.common.base.Optional;
26 import com.google.common.collect.Collections2;
27 import com.google.common.io.Files;
28 import com.google.common.util.concurrent.CheckedFuture;
29 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.nio.charset.StandardCharsets;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.List;
37 import java.util.concurrent.ExecutionException;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
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), any(PotentialSchemaSource.class));
67 public void testCacheAndRestore() throws Exception {
68 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
69 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.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@0000-00-00")).and(hasItem("test@2012-12-12")));
85 assertThat(Files.toString(storedFiles.get(0), StandardCharsets.UTF_8), either(containsString(content)).or(containsString(content2)));
86 assertThat(Files.toString(storedFiles.get(1), StandardCharsets.UTF_8), either(containsString(content)).or(containsString(content2)));
88 verify(this.registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
90 // Create new cache from stored sources
91 new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
93 verify(this.registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
95 final List<File> storedFilesAfterNewCache = getFilesFromCache();
96 assertEquals(2, storedFilesAfterNewCache.size());
99 private static Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
100 return Collections2.transform(storedFiles, input -> Files.getNameWithoutExtension(input.getName()));
104 public void testCacheDuplicate() throws Exception {
105 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
106 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
108 final String content = "content1";
109 final YangTextSchemaSource source = new TestingYangSource("test", null, content);
114 final List<File> storedFiles = getFilesFromCache();
115 assertEquals(1, storedFiles.size());
116 verify(this.registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
120 public void testCacheMultipleRevisions() throws Exception {
121 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
122 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
124 final String content = "content1";
125 final YangTextSchemaSource source = new TestingYangSource("test", null, content);
126 final YangTextSchemaSource source2 = new TestingYangSource("test", "2012-12-12", content);
127 final YangTextSchemaSource source3 = new TestingYangSource("test", "2013-12-12", content);
130 cache.offer(source2);
131 cache.offer(source3);
133 final List<File> storedFiles = getFilesFromCache();
134 assertEquals(3, storedFiles.size());
136 assertThat(filesToFilenamesWithoutRevision(storedFiles), both(hasItem("test@0000-00-00")).and(hasItem("test@2012-12-12")).and(hasItem("test@2013-12-12")));
138 verify(this.registry, times(3)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
142 public void sourceIdToFileEmptyRevWithEmptyDir() {
143 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "");
144 final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, this.storageDir);
145 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
146 YangTextSchemaSource.class, sourceIdToFile);
147 Assert.assertNotNull(cache);
148 final List<File> storedFiles = Arrays.asList(sourceIdToFile.listFiles());
149 assertEquals(0, storedFiles.size());
153 public void sourceIdToFileEmptyRevWithOneItemInDir() {
154 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
155 YangTextSchemaSource.class, this.storageDir);
156 final String content = "content1";
157 final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
160 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "");
161 final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier,
163 Assert.assertNotNull(sourceIdToFile);
164 final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
165 assertEquals(1, storedFiles.size());
169 public void sourceIdToFileEmptyRevWithMoreItemsInDir() {
170 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
171 YangTextSchemaSource.class, this.storageDir);
172 final String content = "content1";
173 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
174 final YangTextSchemaSource source2 = new TestingYangSource("test", "2013-12-12", content);
176 cache.offer(source2);
178 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "");
179 final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, this.storageDir);
180 Assert.assertNotNull(sourceIdToFile);
181 final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
182 assertEquals(2, storedFiles.size());
186 public void test() throws Exception {
188 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
189 YangTextSchemaSource.class, this.storageDir);
190 final String content = "content1";
191 final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
193 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "2013-12-12");
194 final CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> checked = cache
195 .getSource(sourceIdentifier);
196 Assert.assertNotNull(checked);
197 final YangTextSchemaSource checkedGet = checked.checkedGet();
198 Assert.assertEquals(sourceIdentifier, checkedGet.getIdentifier());
199 Assert.assertTrue(checked.isDone());
202 @Test(expected = ExecutionException.class)
203 public void test1() throws Exception {
205 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
206 YangTextSchemaSource.class, this.storageDir);
207 final String content = "content1";
208 final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
210 final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test1", "2012-12-12");
211 final CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> checked = cache
212 .getSource(sourceIdentifier);
213 Assert.assertNotNull(checked);
217 private List<File> getFilesFromCache() {
218 return Arrays.asList(this.storageDir.listFiles());
221 private class TestingYangSource extends YangTextSchemaSource {
223 private final String content;
225 protected TestingYangSource(final String name, final String revision, final String content) {
226 super(RevisionSourceIdentifier.create(name, Optional.fromNullable(revision)));
227 this.content = content;
231 protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
232 return toStringHelper;
236 public InputStream openStream() throws IOException {
237 return new ByteArrayInputStream(this.content.getBytes(StandardCharsets.UTF_8));