BUG-997 Implement Filesystem source cache for schema repository
[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 com.google.common.base.Charsets;
23 import com.google.common.base.Function;
24 import com.google.common.base.Objects;
25 import com.google.common.base.Optional;
26 import com.google.common.collect.Collections2;
27 import com.google.common.io.Files;
28 import java.io.ByteArrayInputStream;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.List;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
40 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
41 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
42 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
43 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
44 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
45
46 public class FilesystemSchemaSourceCacheTest {
47
48     @Mock
49     private SchemaSourceRegistry registry;
50     @Mock
51     private SchemaSourceRegistration<?> registration;
52     private File storageDir;
53
54     @Before
55     public void setUp() throws Exception {
56         MockitoAnnotations.initMocks(this);
57         storageDir = Files.createTempDir();
58         doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
59     }
60
61     @Test
62     public void testCacheAndRestore() throws Exception {
63         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
64                 = new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
65
66         final String content = "content1";
67         final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
68         cache.offer(source);
69
70         final String content2 = "content2";
71         final YangTextSchemaSource source2 = new TestingYangSource("test2", null, content);
72         cache.offer(source2);
73
74         final List<File> storedFiles = getFilesFromCache();
75         assertEquals(2, storedFiles.size());
76         final Collection<String> fileNames = filesToFilenamesWithoutRevision(storedFiles);
77
78         assertThat(fileNames, both(hasItem("test2@0000-00-00")).and(hasItem("test@2012-12-12")));
79
80         assertThat(Files.toString(storedFiles.get(0), Charsets.UTF_8), either(containsString(content)).or(containsString(content2)));
81         assertThat(Files.toString(storedFiles.get(1), Charsets.UTF_8), either(containsString(content)).or(containsString(content2)));
82
83         verify(registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
84
85         // Create new cache from stored sources
86         new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
87
88         verify(registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
89
90         final List<File> storedFilesAfterNewCache = getFilesFromCache();
91         assertEquals(2, storedFilesAfterNewCache.size());
92     }
93
94     private Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
95         return Collections2.transform(storedFiles, new Function<File, String>() {
96             @Override
97             public String apply(final File input) {
98                 return Files.getNameWithoutExtension(input.getName());
99             }
100         });
101     }
102
103     @Test
104     public void testCacheDuplicate() throws Exception {
105         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
106                 = new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, 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(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<>(registry, YangTextSchemaSource.class, 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(registry, times(3)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
139     }
140
141     private List<File> getFilesFromCache() {
142         return Arrays.asList(storageDir.listFiles());
143     }
144
145     private class TestingYangSource extends YangTextSchemaSource {
146
147         private final String content;
148
149         protected TestingYangSource(final String name, final String revision, final String content) {
150             super(new SourceIdentifier(name, Optional.fromNullable(revision)));
151             this.content = content;
152         }
153
154         @Override
155         protected Objects.ToStringHelper addToStringAttributes(final Objects.ToStringHelper toStringHelper) {
156             return toStringHelper;
157         }
158
159         @Override
160         public InputStream openStream() throws IOException {
161             return new ByteArrayInputStream(content.getBytes(Charsets.UTF_8));
162         }
163     }
164 }