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