Further cleanups of code
[yangtools.git] / yang / yang-model-export / src / test / java / org / opendaylight / yangtools / yang / model / export / test / SimpleModuleTest.java
1 package org.opendaylight.yangtools.yang.model.export.test;
2
3 import java.io.File;
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;
9 import java.util.Set;
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;
26
27 public class SimpleModuleTest {
28
29     private static final File TEST_MODELS_FOLDER;
30
31     static {
32         try {
33             TEST_MODELS_FOLDER = new File(SimpleModuleTest.class.getResource("/yang/").toURI());
34         } catch (final URISyntaxException e) {
35             throw new IllegalStateException(e);
36         }
37     }
38
39     private SharedSchemaRepository schemaRegistry;
40     private FilesystemSchemaSourceCache<YangTextSchemaSource> fileSourceProvider;
41     private SchemaContextFactory schemaContextFactory;
42     private Set<SourceIdentifier> allTestSources;
43
44     @Before
45     public void init() {
46         schemaRegistry = new SharedSchemaRepository("test");
47         fileSourceProvider = new FilesystemSchemaSourceCache<>(schemaRegistry,
48                 YangTextSchemaSource.class, TEST_MODELS_FOLDER);
49         final TextToASTTransformer astTransformer = TextToASTTransformer.create(schemaRegistry, schemaRegistry);
50         schemaRegistry.registerSchemaSourceListener(astTransformer);
51
52         schemaContextFactory = schemaRegistry.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
53         allTestSources = new HashSet<>();
54         final SchemaListenerRegistration reg = schemaRegistry.registerSchemaSourceListener(new SchemaSourceListener() {
55
56             @Override
57             public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
58                 // NOOP
59             }
60
61             @Override
62             public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
63                 for (final PotentialSchemaSource<?> source : sources) {
64                     allTestSources.add(source.getSourceIdentifier());
65                 }
66             }
67
68             @Override
69             public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
70                 // NOOP
71             }
72         });
73         reg.close();
74     }
75
76     @Test
77     public void testGenerateAll() throws Exception {
78         testSetOfModules(allTestSources);
79     }
80
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");
84         outDir.mkdirs();
85         for (final Module module : schemaContext.getModules()) {
86             exportModule(schemaContext, module, outDir);
87         }
88     }
89
90     private static File exportModule(final SchemaContext schemaContext, final Module module, final File outDir)
91             throws Exception {
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);
95         }
96         return outFile;
97     }
98 }