1 package org.opendaylight.yangtools.yang.model.export.test;
4 import java.io.FileOutputStream;
5 import java.io.OutputStream;
6 import java.net.URISyntaxException;
7 import java.util.Collection;
8 import java.util.HashSet;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.opendaylight.yangtools.yang.model.api.Module;
13 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
14 import org.opendaylight.yangtools.yang.model.export.YinExportUtils;
15 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
16 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
17 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
18 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
19 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
20 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
21 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
22 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
23 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
24 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
25 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
27 public class SimpleModuleTest {
29 private static final File TEST_MODELS_FOLDER;
33 TEST_MODELS_FOLDER = new File(SimpleModuleTest.class.getResource("/yang/").toURI());
34 } catch (final URISyntaxException e) {
35 throw new IllegalStateException(e);
39 private SharedSchemaRepository schemaRegistry;
40 private FilesystemSchemaSourceCache<YangTextSchemaSource> fileSourceProvider;
41 private SchemaContextFactory schemaContextFactory;
42 private Set<SourceIdentifier> allTestSources;
46 schemaRegistry = new SharedSchemaRepository("test");
47 fileSourceProvider = new FilesystemSchemaSourceCache<YangTextSchemaSource>(schemaRegistry,
48 YangTextSchemaSource.class, TEST_MODELS_FOLDER);
49 final TextToASTTransformer astTransformer = TextToASTTransformer.create(schemaRegistry, schemaRegistry);
50 schemaRegistry.registerSchemaSourceListener(astTransformer);
52 schemaContextFactory = schemaRegistry.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
53 allTestSources = new HashSet<>();
54 final SchemaListenerRegistration reg = schemaRegistry.registerSchemaSourceListener(new SchemaSourceListener() {
57 public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
62 public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
63 for (final PotentialSchemaSource<?> source : sources) {
64 allTestSources.add(source.getSourceIdentifier());
69 public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
77 public void testGenerateAll() throws Exception {
78 testSetOfModules(allTestSources);
81 private void testSetOfModules(final Collection<SourceIdentifier> source) throws Exception {
82 final SchemaContext schemaContext = schemaContextFactory.createSchemaContext(source).checkedGet();
83 final File outDir = new File("target/collection");
85 for (final Module module : schemaContext.getModules()) {
86 exportModule(schemaContext, module, outDir);
90 private File exportModule(final SchemaContext schemaContext, final Module module, final File outDir)
92 final File outFile = new File(outDir, YinExportUtils.wellFormedYinName(module.getName(), module.getRevision()));
93 try (OutputStream output = new FileOutputStream(outFile)) {
94 YinExportUtils.writeModuleToOutputStream(schemaContext, module, output);