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.util.Collection;
14 import java.util.HashSet;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
22 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
24 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
25 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
26 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
29 public class SimpleModuleTest {
30 private SharedSchemaRepository schemaRegistry;
31 private EffectiveModelContextFactory schemaContextFactory;
32 private Set<SourceIdentifier> allTestSources;
36 schemaRegistry = new SharedSchemaRepository("test");
37 final TextToIRTransformer astTransformer = TextToIRTransformer.create(schemaRegistry, schemaRegistry);
38 schemaRegistry.registerSchemaSourceListener(astTransformer);
40 schemaContextFactory = schemaRegistry.createEffectiveModelContextFactory();
41 allTestSources = new HashSet<>();
42 final SchemaListenerRegistration reg = schemaRegistry.registerSchemaSourceListener(new SchemaSourceListener() {
45 public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
50 public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
51 for (final PotentialSchemaSource<?> source : sources) {
52 allTestSources.add(source.getSourceIdentifier());
57 public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
65 public void testGenerateAll() throws Exception {
66 testSetOfModules(allTestSources);
69 private void testSetOfModules(final Collection<SourceIdentifier> source) throws Exception {
70 final EffectiveModelContext schemaContext = schemaContextFactory.createEffectiveModelContext(source).get();
71 final File outDir = new File("target/collection");
73 for (final Module module : schemaContext.getModules()) {
74 exportModule(module, outDir);
78 private static File exportModule(final Module module, final File outDir)
80 final File outFile = new File(outDir, YinExportUtils.wellFormedYinName(module.getName(), module.getRevision()));
81 try (OutputStream output = new FileOutputStream(outFile)) {
82 YinExportUtils.writeModuleAsYinText(module.asEffectiveStatement(), output);