f479026ef48ddd5fd92a7db57a0f35ede5e16d13
[yangtools.git] / yang / yang-repo-fs / src / test / java / org / opendaylight / yangtools / yang / model / repo / fs / FilesystemSchemaSourceCacheIntegrationTest.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 package org.opendaylight.yangtools.yang.model.repo.fs;
9
10 import static org.hamcrest.CoreMatchers.both;
11 import static org.hamcrest.CoreMatchers.hasItem;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
15 import static org.junit.jupiter.api.Assertions.assertThrows;
16 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
17
18 import com.google.common.base.MoreObjects.ToStringHelper;
19 import com.google.common.collect.Lists;
20 import com.google.common.io.Files;
21 import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.Reader;
26 import java.io.StringReader;
27 import java.nio.charset.StandardCharsets;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Optional;
32 import java.util.concurrent.ExecutionException;
33 import org.junit.jupiter.api.Test;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
36 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
37 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
38 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
39 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
40 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
41 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
42 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
43
44 public class FilesystemSchemaSourceCacheIntegrationTest {
45     @Test
46     public void testWithCacheStartup() throws IOException {
47         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
48
49         class CountingSchemaListener implements SchemaSourceListener {
50             List<PotentialSchemaSource<?>> registeredSources = new ArrayList<>();
51
52             @Override
53             public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
54             }
55
56             @Override
57             public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
58                 for (final PotentialSchemaSource<?> source : sources) {
59                     registeredSources.add(source);
60                 }
61             }
62
63             @Override
64             public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
65             }
66         }
67
68         final File tempDir = Files.createTempDir();
69
70         final CountingSchemaListener listener = new CountingSchemaListener();
71         sharedSchemaRepository.registerSchemaSourceListener(listener);
72
73         final File test = new File(tempDir, "test.yang");
74         Files.asCharSink(test, StandardCharsets.UTF_8).write("content-test");
75
76         final File test2 = new File(tempDir, "test@2012-12-12.yang");
77         Files.asCharSink(test2, StandardCharsets.UTF_8).write("content-test-2012");
78
79         final File test3 = new File(tempDir, "test@2013-12-12.yang");
80         Files.asCharSink(test3, StandardCharsets.UTF_8).write("content-test-2013");
81
82         final File test4 = new File(tempDir, "module@2010-12-12.yang");
83         Files.asCharSink(test4, StandardCharsets.UTF_8).write("content-module-2010");
84
85         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(
86                 sharedSchemaRepository, YangTextSchemaSource.class, tempDir);
87         sharedSchemaRepository.registerSchemaSourceListener(cache);
88
89         assertEquals(4, listener.registeredSources.size());
90
91         assertThat(Lists.transform(listener.registeredSources, PotentialSchemaSource::getSourceIdentifier),
92                 both(hasItem(new SourceIdentifier("test")))
93                         .and(hasItem(new SourceIdentifier("test", "2012-12-12")))
94                         .and(hasItem(new SourceIdentifier("test", "2013-12-12")))
95                         .and(hasItem(new SourceIdentifier("module", "2010-12-12")))
96         );
97     }
98
99     @Test
100     public void testWithCacheRunning() {
101         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
102
103         final File storageDir = Files.createTempDir();
104
105         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(
106                 sharedSchemaRepository, YangTextSchemaSource.class, storageDir);
107         sharedSchemaRepository.registerSchemaSourceListener(cache);
108
109         final SourceIdentifier runningId = new SourceIdentifier("running", "2012-12-12");
110
111         sharedSchemaRepository.registerSchemaSource(sourceIdentifier -> immediateFluentFuture(
112             new YangTextSchemaSource(runningId) {
113                 @Override
114                 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
115                     return toStringHelper;
116                 }
117
118                 @Override
119                 public Reader openStream() throws IOException {
120                     return new StringReader("running");
121                 }
122
123                 @Override
124                 public Optional<String> getSymbolicName() {
125                     return Optional.empty();
126                 }
127             }), PotentialSchemaSource.create(runningId, YangTextSchemaSource.class,
128                 PotentialSchemaSource.Costs.REMOTE_IO.getValue()));
129
130         final TextToIRTransformer transformer = TextToIRTransformer.create(sharedSchemaRepository,
131             sharedSchemaRepository);
132         sharedSchemaRepository.registerSchemaSourceListener(transformer);
133
134         // Request schema to make repository notify the cache
135         final ListenableFuture<EffectiveModelContext> schemaFuture = sharedSchemaRepository
136                 .createEffectiveModelContextFactory()
137                 .createEffectiveModelContext(runningId);
138
139         final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(schemaFuture)).getCause();
140         assertInstanceOf(MissingSchemaSourceException.class, cause);
141
142         // Creation of schema context fails, since we do not provide regular sources, but we just want
143         // to check cache
144         final List<File> cachedSchemas = Arrays.asList(storageDir.listFiles());
145         assertEquals(1, cachedSchemas.size());
146         assertEquals(Files.getNameWithoutExtension(cachedSchemas.get(0).getName()), "running@2012-12-12");
147     }
148 }