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