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;
21 import com.google.common.base.Charsets;
22 import com.google.common.base.Function;
23 import com.google.common.base.MoreObjects;
24 import com.google.common.base.Optional;
25 import com.google.common.collect.Collections2;
26 import com.google.common.io.Files;
27 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.List;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
39 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
40 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
41 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
42 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
43 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
45 public class FilesystemSchemaSourceCacheTest {
48 private SchemaSourceRegistry registry;
50 private SchemaSourceRegistration<?> registration;
51 private File storageDir;
54 public void setUp() throws Exception {
55 MockitoAnnotations.initMocks(this);
56 storageDir = Files.createTempDir();
57 doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
61 public void testCacheAndRestore() throws Exception {
62 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
63 = new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
65 final String content = "content1";
66 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
69 final String content2 = "content2";
70 final YangTextSchemaSource source2 = new TestingYangSource("test2", null, content);
73 final List<File> storedFiles = getFilesFromCache();
74 assertEquals(2, storedFiles.size());
75 final Collection<String> fileNames = filesToFilenamesWithoutRevision(storedFiles);
77 assertThat(fileNames, both(hasItem("test2@0000-00-00")).and(hasItem("test@2012-12-12")));
79 assertThat(Files.toString(storedFiles.get(0), Charsets.UTF_8), either(containsString(content)).or(containsString(content2)));
80 assertThat(Files.toString(storedFiles.get(1), Charsets.UTF_8), either(containsString(content)).or(containsString(content2)));
82 verify(registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
84 // Create new cache from stored sources
85 new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
87 verify(registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
89 final List<File> storedFilesAfterNewCache = getFilesFromCache();
90 assertEquals(2, storedFilesAfterNewCache.size());
93 private static Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
94 return Collections2.transform(storedFiles, new Function<File, String>() {
96 public String apply(final File input) {
97 return Files.getNameWithoutExtension(input.getName());
103 public void testCacheDuplicate() throws Exception {
104 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
105 = new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
107 final String content = "content1";
108 final YangTextSchemaSource source = new TestingYangSource("test", null, content);
113 final List<File> storedFiles = getFilesFromCache();
114 assertEquals(1, storedFiles.size());
115 verify(registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
119 public void testCacheMultipleRevisions() throws Exception {
120 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
121 = new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
123 final String content = "content1";
124 final YangTextSchemaSource source = new TestingYangSource("test", null, content);
125 final YangTextSchemaSource source2 = new TestingYangSource("test", "2012-12-12", content);
126 final YangTextSchemaSource source3 = new TestingYangSource("test", "2013-12-12", content);
129 cache.offer(source2);
130 cache.offer(source3);
132 final List<File> storedFiles = getFilesFromCache();
133 assertEquals(3, storedFiles.size());
135 assertThat(filesToFilenamesWithoutRevision(storedFiles), both(hasItem("test@0000-00-00")).and(hasItem("test@2012-12-12")).and(hasItem("test@2013-12-12")));
137 verify(registry, times(3)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
140 private List<File> getFilesFromCache() {
141 return Arrays.asList(storageDir.listFiles());
144 private class TestingYangSource extends YangTextSchemaSource {
146 private final String content;
148 protected TestingYangSource(final String name, final String revision, final String content) {
149 super(new SourceIdentifier(name, Optional.fromNullable(revision)));
150 this.content = content;
154 protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
155 return toStringHelper;
159 public InputStream openStream() throws IOException {
160 return new ByteArrayInputStream(content.getBytes(Charsets.UTF_8));