Merge branch 'master' of ../controller
[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.ArgumentMatchers.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 com.google.common.base.MoreObjects;
23 import com.google.common.collect.Collections2;
24 import com.google.common.io.Files;
25 import com.google.common.util.concurrent.ListenableFuture;
26 import java.io.ByteArrayInputStream;
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.charset.StandardCharsets;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.concurrent.ExecutionException;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.opendaylight.yangtools.yang.common.Revision;
41 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
42 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
43 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
44 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
45 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
46 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
47 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
48
49 public class FilesystemSchemaSourceCacheTest {
50
51     @Mock
52     private SchemaSourceRegistry registry;
53     @Mock
54     private SchemaSourceRegistration<?> registration;
55     private File storageDir;
56
57     @Before
58     public void setUp() throws Exception {
59         MockitoAnnotations.initMocks(this);
60         this.storageDir = Files.createTempDir();
61         doReturn(this.registration).when(this.registry).registerSchemaSource(any(SchemaSourceProvider.class),
62             any(PotentialSchemaSource.class));
63     }
64
65     @Test
66     public void testCacheAndRestore() throws Exception {
67         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
68                 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
69
70         final String content = "content1";
71         final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
72         cache.offer(source);
73
74         final String content2 = "content2";
75         final YangTextSchemaSource source2 = new TestingYangSource("test2", null, content);
76         cache.offer(source2);
77
78         final List<File> storedFiles = getFilesFromCache();
79         assertEquals(2, storedFiles.size());
80         final Collection<String> fileNames = filesToFilenamesWithoutRevision(storedFiles);
81
82         assertThat(fileNames, both(hasItem("test2")).and(hasItem("test@2012-12-12")));
83
84         assertThat(Files.asCharSource(storedFiles.get(0), StandardCharsets.UTF_8).read(),
85             either(containsString(content)).or(containsString(content2)));
86         assertThat(Files.asCharSource(storedFiles.get(1), StandardCharsets.UTF_8).read(),
87             either(containsString(content)).or(containsString(content2)));
88
89         verify(this.registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class),
90             any(PotentialSchemaSource.class));
91
92         // Create new cache from stored sources
93         new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
94
95         verify(this.registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class),
96             any(PotentialSchemaSource.class));
97
98         final List<File> storedFilesAfterNewCache = getFilesFromCache();
99         assertEquals(2, storedFilesAfterNewCache.size());
100     }
101
102     private static Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
103         return Collections2.transform(storedFiles, input -> Files.getNameWithoutExtension(input.getName()));
104     }
105
106     @Test
107     public void testCacheDuplicate() throws Exception {
108         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
109                 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
110
111         final String content = "content1";
112         final YangTextSchemaSource source = new TestingYangSource("test", null, content);
113         // Double offer
114         cache.offer(source);
115         cache.offer(source);
116
117         final List<File> storedFiles = getFilesFromCache();
118         assertEquals(1, storedFiles.size());
119         verify(this.registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
120     }
121
122     @Test
123     public void testCacheMultipleRevisions() throws Exception {
124         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
125                 = new FilesystemSchemaSourceCache<>(this.registry, YangTextSchemaSource.class, this.storageDir);
126
127         final String content = "content1";
128         final YangTextSchemaSource source = new TestingYangSource("test", null, content);
129         final YangTextSchemaSource source2 = new TestingYangSource("test", "2012-12-12", content);
130         final YangTextSchemaSource source3 = new TestingYangSource("test", "2013-12-12", content);
131         // Double offer
132         cache.offer(source);
133         cache.offer(source2);
134         cache.offer(source3);
135
136         final List<File> storedFiles = getFilesFromCache();
137         assertEquals(3, storedFiles.size());
138
139         assertThat(filesToFilenamesWithoutRevision(storedFiles), both(hasItem("test"))
140             .and(hasItem("test@2012-12-12")).and(hasItem("test@2013-12-12")));
141
142         verify(this.registry, times(3)).registerSchemaSource(any(SchemaSourceProvider.class),
143             any(PotentialSchemaSource.class));
144     }
145
146     @Test
147     public void sourceIdToFileEmptyRevWithEmptyDir() {
148         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test");
149         final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, this.storageDir);
150         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
151                 YangTextSchemaSource.class, sourceIdToFile);
152         Assert.assertNotNull(cache);
153         final List<File> storedFiles = Arrays.asList(sourceIdToFile.listFiles());
154         assertEquals(0, storedFiles.size());
155     }
156
157     @Test
158     public void sourceIdToFileEmptyRevWithOneItemInDir() {
159         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
160                 YangTextSchemaSource.class, this.storageDir);
161         final String content = "content1";
162         final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
163         cache.offer(source);
164
165         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test");
166         final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier,
167                 this.storageDir);
168         Assert.assertNotNull(sourceIdToFile);
169         final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
170         assertEquals(1, storedFiles.size());
171     }
172
173     @Test
174     public void sourceIdToFileEmptyRevWithMoreItemsInDir() {
175         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
176                 YangTextSchemaSource.class, this.storageDir);
177         final String content = "content1";
178         final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
179         final YangTextSchemaSource source2 = new TestingYangSource("test", "2013-12-12", content);
180         cache.offer(source);
181         cache.offer(source2);
182
183         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test");
184         final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, this.storageDir);
185         Assert.assertNotNull(sourceIdToFile);
186         final List<File> storedFiles = Arrays.asList(this.storageDir.listFiles());
187         assertEquals(2, storedFiles.size());
188     }
189
190     @Test
191     public void test() throws Exception {
192
193         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
194                 YangTextSchemaSource.class, this.storageDir);
195         final String content = "content1";
196         final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
197         cache.offer(source);
198         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", Revision.of("2013-12-12"));
199         final ListenableFuture<? extends YangTextSchemaSource> checked = cache.getSource(sourceIdentifier);
200         Assert.assertNotNull(checked);
201         final YangTextSchemaSource checkedGet = checked.get();
202         Assert.assertEquals(sourceIdentifier, checkedGet.getIdentifier());
203         Assert.assertTrue(checked.isDone());
204     }
205
206     @Test(expected = ExecutionException.class)
207     public void test1() throws Exception {
208
209         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(this.registry,
210                 YangTextSchemaSource.class, this.storageDir);
211         final String content = "content1";
212         final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
213         cache.offer(source);
214         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test1", Revision.of("2012-12-12"));
215         final ListenableFuture<? extends YangTextSchemaSource> checked = cache.getSource(sourceIdentifier);
216         Assert.assertNotNull(checked);
217         checked.get();
218     }
219
220     private List<File> getFilesFromCache() {
221         return Arrays.asList(this.storageDir.listFiles());
222     }
223
224     private class TestingYangSource extends YangTextSchemaSource {
225
226         private final String content;
227
228         TestingYangSource(final String name, final String revision, final String content) {
229             super(RevisionSourceIdentifier.create(name, Revision.ofNullable(revision)));
230             this.content = content;
231         }
232
233         @Override
234         protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
235             return toStringHelper;
236         }
237
238         @Override
239         public InputStream openStream() throws IOException {
240             return new ByteArrayInputStream(this.content.getBytes(StandardCharsets.UTF_8));
241         }
242     }
243 }