Merge "Updated Json Stream Writer to use gson JsonWriter."
[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 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;
28 import java.io.File;
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;
44
45 public class FilesystemSchemaSourceCacheTest {
46
47     @Mock
48     private SchemaSourceRegistry registry;
49     @Mock
50     private SchemaSourceRegistration<?> registration;
51     private File storageDir;
52
53     @Before
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));
58     }
59
60     @Test
61     public void testCacheAndRestore() throws Exception {
62         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
63                 = new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
64
65         final String content = "content1";
66         final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
67         cache.offer(source);
68
69         final String content2 = "content2";
70         final YangTextSchemaSource source2 = new TestingYangSource("test2", null, content);
71         cache.offer(source2);
72
73         final List<File> storedFiles = getFilesFromCache();
74         assertEquals(2, storedFiles.size());
75         final Collection<String> fileNames = filesToFilenamesWithoutRevision(storedFiles);
76
77         assertThat(fileNames, both(hasItem("test2@0000-00-00")).and(hasItem("test@2012-12-12")));
78
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)));
81
82         verify(registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
83
84         // Create new cache from stored sources
85         new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
86
87         verify(registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
88
89         final List<File> storedFilesAfterNewCache = getFilesFromCache();
90         assertEquals(2, storedFilesAfterNewCache.size());
91     }
92
93     private Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
94         return Collections2.transform(storedFiles, new Function<File, String>() {
95             @Override
96             public String apply(final File input) {
97                 return Files.getNameWithoutExtension(input.getName());
98             }
99         });
100     }
101
102     @Test
103     public void testCacheDuplicate() throws Exception {
104         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
105                 = new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
106
107         final String content = "content1";
108         final YangTextSchemaSource source = new TestingYangSource("test", null, content);
109         // Double offer
110         cache.offer(source);
111         cache.offer(source);
112
113         final List<File> storedFiles = getFilesFromCache();
114         assertEquals(1, storedFiles.size());
115         verify(registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
116     }
117
118     @Test
119     public void testCacheMultipleRevisions() throws Exception {
120         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache
121                 = new FilesystemSchemaSourceCache<>(registry, YangTextSchemaSource.class, storageDir);
122
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);
127         // Double offer
128         cache.offer(source);
129         cache.offer(source2);
130         cache.offer(source3);
131
132         final List<File> storedFiles = getFilesFromCache();
133         assertEquals(3, storedFiles.size());
134
135         assertThat(filesToFilenamesWithoutRevision(storedFiles), both(hasItem("test@0000-00-00")).and(hasItem("test@2012-12-12")).and(hasItem("test@2013-12-12")));
136
137         verify(registry, times(3)).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
138     }
139
140     private List<File> getFilesFromCache() {
141         return Arrays.asList(storageDir.listFiles());
142     }
143
144     private class TestingYangSource extends YangTextSchemaSource {
145
146         private final String content;
147
148         protected TestingYangSource(final String name, final String revision, final String content) {
149             super(new SourceIdentifier(name, Optional.fromNullable(revision)));
150             this.content = content;
151         }
152
153         @Override
154         protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
155             return toStringHelper;
156         }
157
158         @Override
159         public InputStream openStream() throws IOException {
160             return new ByteArrayInputStream(content.getBytes(Charsets.UTF_8));
161         }
162     }
163 }