2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.export;
11 import java.io.FileOutputStream;
12 import java.io.OutputStream;
13 import java.net.URISyntaxException;
14 import java.util.Collection;
15 import java.util.HashSet;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
26 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
27 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
28 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
29 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
30 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToASTTransformer;
33 public class SimpleModuleTest {
35 private static final File TEST_MODELS_FOLDER;
39 TEST_MODELS_FOLDER = new File(SimpleModuleTest.class.getResource("/yang/").toURI());
40 } catch (final URISyntaxException e) {
41 throw new IllegalStateException(e);
45 private SharedSchemaRepository schemaRegistry;
46 private FilesystemSchemaSourceCache<YangTextSchemaSource> fileSourceProvider;
47 private SchemaContextFactory schemaContextFactory;
48 private Set<SourceIdentifier> allTestSources;
52 schemaRegistry = new SharedSchemaRepository("test");
53 fileSourceProvider = new FilesystemSchemaSourceCache<>(schemaRegistry,
54 YangTextSchemaSource.class, TEST_MODELS_FOLDER);
55 final TextToASTTransformer astTransformer = TextToASTTransformer.create(schemaRegistry, schemaRegistry);
56 schemaRegistry.registerSchemaSourceListener(astTransformer);
58 schemaContextFactory = schemaRegistry.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
59 allTestSources = new HashSet<>();
60 final SchemaListenerRegistration reg = schemaRegistry.registerSchemaSourceListener(new SchemaSourceListener() {
63 public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
68 public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
69 for (final PotentialSchemaSource<?> source : sources) {
70 allTestSources.add(source.getSourceIdentifier());
75 public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
83 public void testGenerateAll() throws Exception {
84 testSetOfModules(allTestSources);
87 private void testSetOfModules(final Collection<SourceIdentifier> source) throws Exception {
88 final SchemaContext schemaContext = schemaContextFactory.createSchemaContext(source).get();
89 final File outDir = new File("target/collection");
91 for (final Module module : schemaContext.getModules()) {
92 exportModule(schemaContext, module, outDir);
96 private static File exportModule(final SchemaContext schemaContext, final Module module, final File outDir)
98 final File outFile = new File(outDir, YinExportUtils.wellFormedYinName(module.getName(), module.getRevision()));
99 try (OutputStream output = new FileOutputStream(outFile)) {
100 YinExportUtils.writeModuleToOutputStream(schemaContext, module, output);