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.test;
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.export.YinExportUtils;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
25 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
27 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
28 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
29 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
30 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
31 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
32 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
34 public class SimpleModuleTest {
36 private static final File TEST_MODELS_FOLDER;
40 TEST_MODELS_FOLDER = new File(SimpleModuleTest.class.getResource("/yang/").toURI());
41 } catch (final URISyntaxException e) {
42 throw new IllegalStateException(e);
46 private SharedSchemaRepository schemaRegistry;
47 private FilesystemSchemaSourceCache<YangTextSchemaSource> fileSourceProvider;
48 private SchemaContextFactory schemaContextFactory;
49 private Set<SourceIdentifier> allTestSources;
53 schemaRegistry = new SharedSchemaRepository("test");
54 fileSourceProvider = new FilesystemSchemaSourceCache<>(schemaRegistry,
55 YangTextSchemaSource.class, TEST_MODELS_FOLDER);
56 final TextToASTTransformer astTransformer = TextToASTTransformer.create(schemaRegistry, schemaRegistry);
57 schemaRegistry.registerSchemaSourceListener(astTransformer);
59 schemaContextFactory = schemaRegistry.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
60 allTestSources = new HashSet<>();
61 final SchemaListenerRegistration reg = schemaRegistry.registerSchemaSourceListener(new SchemaSourceListener() {
64 public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
69 public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
70 for (final PotentialSchemaSource<?> source : sources) {
71 allTestSources.add(source.getSourceIdentifier());
76 public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
84 public void testGenerateAll() throws Exception {
85 testSetOfModules(allTestSources);
88 private void testSetOfModules(final Collection<SourceIdentifier> source) throws Exception {
89 final SchemaContext schemaContext = schemaContextFactory.createSchemaContext(source).get();
90 final File outDir = new File("target/collection");
92 for (final Module module : schemaContext.getModules()) {
93 exportModule(schemaContext, module, outDir);
97 private static File exportModule(final SchemaContext schemaContext, final Module module, final File outDir)
99 final File outFile = new File(outDir, YinExportUtils.wellFormedYinName(module.getName(), module.getRevision()));
100 try (OutputStream output = new FileOutputStream(outFile)) {
101 YinExportUtils.writeModuleToOutputStream(schemaContext, module, output);