a094f91f14d2859e4bc47ac1bddfe6bb1159b1bd
[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.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;
30 import java.io.File;
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;
50
51 public class FilesystemSchemaSourceCacheTest {
52
53     @Mock
54     private SchemaSourceRegistry registry;
55     @Mock
56     private SchemaSourceRegistration<?> registration;
57     private File storageDir;
58
59     @Before
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));
64     }
65
66     @Test
67     public void testCacheAndRestore() throws Exception {
68         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
69                 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
70
71         final String content = "content1";
72         final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
73         cache.offer(source);
74
75         final String content2 = "content2";
76         final YangTextSchemaSource source2 = new TestingYangSource("test2", null, content);
77         cache.offer(source2);
78
79         final List<File> storedFiles = getFilesFromCache();
80         assertEquals(2, storedFiles.size());
81         final Collection<String> fileNames = filesToFilenamesWithoutRevision(storedFiles);
82
83         assertThat(fileNames, both(hasItem("test2@0000-00-00")).and(hasItem("test@2012-12-12")));
84
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)));
87
88         verify(this.registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
89
90         // Create new cache from stored sources
91         new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
92
93         verify(this.registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
94
95         final List<File> storedFilesAfterNewCache = getFilesFromCache();
96         assertEquals(2, storedFilesAfterNewCache.size());
97     }
98
99     private static Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
100         return Collections2.transform(storedFiles, input -> Files.getNameWithoutExtension(input.getName()));
101     }
102
103     @Test
104     public void testCacheDuplicate() throws Exception {
105         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
106                 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
107
108         final String content = "content1";
109         final YangTextSchemaSource source = new TestingYangSource("test", null, content);
110         // Double offer
111         cache.offer(source);
112         cache.offer(source);
113
114         final List<File> storedFiles = getFilesFromCache();
115         assertEquals(1, storedFiles.size());
116         verify(this.registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
117     }
118
119     @Test
120     public void testCacheMultipleRevisions() throws Exception {
121         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
122                 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
123
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);
128         // Double offer
129         cache.offer(source);
130         cache.offer(source2);
131         cache.offer(source3);
132
133         final List<File> storedFiles = getFilesFromCache();
134         assertEquals(3, storedFiles.size());
135
136         assertThat(filesToFilenamesWithoutRevision(storedFiles), both(hasItem("test@0000-00-00")).and(hasItem("test@2012-12-12")).and(hasItem("test@2013-12-12")));
137
138         verify(this.registry, times(3)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
139     }
140
141     @Test
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());
150     }
151
152     @Test
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);
158         cache.offer(source);
159
160         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "");
161         final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier,
162                 this.storageDir);
163         Assert.assertNotNull(sourceIdToFile);
164         final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
165         assertEquals(1, storedFiles.size());
166     }
167
168     @Test
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);
175         cache.offer(source);
176         cache.offer(source2);
177
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());
183     }
184
185     @Test
186     public void test() throws Exception {
187
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);
192         cache.offer(source);
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());
200     }
201
202     @Test(expected = ExecutionException.class)
203     public void test1() throws Exception {
204
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);
209         cache.offer(source);
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);
214         checked.get();
215     }
216
217     private List<File> getFilesFromCache() {
218         return Arrays.asList(this.storageDir.listFiles());
219     }
220
221     private class TestingYangSource extends YangTextSchemaSource {
222
223         private final String content;
224
225         protected TestingYangSource(final String name, final String revision, final String content) {
226             super(RevisionSourceIdentifier.create(name, Optional.fromNullable(revision)));
227             this.content = content;
228         }
229
230         @Override
231         protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
232             return toStringHelper;
233         }
234
235         @Override
236         public InputStream openStream() throws IOException {
237             return new ByteArrayInputStream(this.content.getBytes(StandardCharsets.UTF_8));
238         }
239     }
240 }