ec956dbb98d59007ce3f75b28fe2e434622abfb7
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / repo / util / FilesystemSchemaSourceCacheTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.model.repo.util;
10
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
22 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
23
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;
31 import java.io.File;
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;
51
52 public class FilesystemSchemaSourceCacheTest {
53
54     @Mock
55     private SchemaSourceRegistry registry;
56     @Mock
57     private SchemaSourceRegistration<?> registration;
58     private File storageDir;
59
60     @Before
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));
65     }
66
67     @Test
68     public void testCacheAndRestore() throws Exception {
69         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
70                 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
71
72         final String content = "content1";
73         final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
74         cache.offer(source);
75
76         final String content2 = "content2";
77         final YangTextSchemaSource source2 = new TestingYangSource("test2", null, content);
78         cache.offer(source2);
79
80         final List<File> storedFiles = getFilesFromCache();
81         assertEquals(2, storedFiles.size());
82         final Collection<String> fileNames = filesToFilenamesWithoutRevision(storedFiles);
83
84         assertThat(fileNames, both(hasItem("test2@0000-00-00")).and(hasItem("test@2012-12-12")));
85
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)));
88
89         verify(this.registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
90
91         // Create new cache from stored sources
92         new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
93
94         verify(this.registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
95
96         final List<File> storedFilesAfterNewCache = getFilesFromCache();
97         assertEquals(2, storedFilesAfterNewCache.size());
98     }
99
100     private static Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
101         return Collections2.transform(storedFiles, new Function<File, String>() {
102             @Override
103             public String apply(final File input) {
104                 return Files.getNameWithoutExtension(input.getName());
105             }
106         });
107     }
108
109     @Test
110     public void testCacheDuplicate() throws Exception {
111         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
112                 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
113
114         final String content = "content1";
115         final YangTextSchemaSource source = new TestingYangSource("test", null, content);
116         // Double offer
117         cache.offer(source);
118         cache.offer(source);
119
120         final List<File> storedFiles = getFilesFromCache();
121         assertEquals(1, storedFiles.size());
122         verify(this.registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
123     }
124
125     @Test
126     public void testCacheMultipleRevisions() throws Exception {
127         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
128                 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
129
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);
134         // Double offer
135         cache.offer(source);
136         cache.offer(source2);
137         cache.offer(source3);
138
139         final List<File> storedFiles = getFilesFromCache();
140         assertEquals(3, storedFiles.size());
141
142         assertThat(filesToFilenamesWithoutRevision(storedFiles), both(hasItem("test@0000-00-00")).and(hasItem("test@2012-12-12")).and(hasItem("test@2013-12-12")));
143
144         verify(this.registry, times(3)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
145     }
146
147     @Test
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());
156     }
157
158     @Test
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);
164         cache.offer(source);
165
166         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "");
167         final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier,
168                 this.storageDir);
169         Assert.assertNotNull(sourceIdToFile);
170         final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
171         assertEquals(1, storedFiles.size());
172     }
173
174     @Test
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);
181         cache.offer(source);
182         cache.offer(source2);
183
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());
189     }
190
191     @Test
192     public void test() throws Exception {
193
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);
198         cache.offer(source);
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());
206     }
207
208     @Test(expected = ExecutionException.class)
209     public void test1() throws Exception {
210
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);
215         cache.offer(source);
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);
220         checked.get();
221     }
222
223     private List<File> getFilesFromCache() {
224         return Arrays.asList(this.storageDir.listFiles());
225     }
226
227     private class TestingYangSource extends YangTextSchemaSource {
228
229         private final String content;
230
231         protected TestingYangSource(final String name, final String revision, final String content) {
232             super(RevisionSourceIdentifier.create(name, Optional.fromNullable(revision)));
233             this.content = content;
234         }
235
236         @Override
237         protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
238             return toStringHelper;
239         }
240
241         @Override
242         public InputStream openStream() throws IOException {
243             return new ByteArrayInputStream(this.content.getBytes(StandardCharsets.UTF_8));
244         }
245     }
246 }