Fix yang-export warnings
[yangtools.git] / model / yang-model-export / src / test / java / org / opendaylight / yangtools / yang / model / export / SimpleModuleTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.export;
9
10 import java.io.File;
11 import java.io.FileOutputStream;
12 import java.io.OutputStream;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.Set;
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;
28
29 public class SimpleModuleTest {
30     private SharedSchemaRepository schemaRegistry;
31     private EffectiveModelContextFactory schemaContextFactory;
32     private Set<SourceIdentifier> allTestSources;
33
34     @Before
35     public void init() {
36         schemaRegistry = new SharedSchemaRepository("test");
37         final TextToIRTransformer astTransformer = TextToIRTransformer.create(schemaRegistry, schemaRegistry);
38         schemaRegistry.registerSchemaSourceListener(astTransformer);
39
40         schemaContextFactory = schemaRegistry.createEffectiveModelContextFactory();
41         allTestSources = new HashSet<>();
42         final SchemaListenerRegistration reg = schemaRegistry.registerSchemaSourceListener(new SchemaSourceListener() {
43
44             @Override
45             public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
46                 // NOOP
47             }
48
49             @Override
50             public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
51                 for (final PotentialSchemaSource<?> source : sources) {
52                     allTestSources.add(source.getSourceIdentifier());
53                 }
54             }
55
56             @Override
57             public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
58                 // NOOP
59             }
60         });
61         reg.close();
62     }
63
64     @Test
65     public void testGenerateAll() throws Exception {
66         testSetOfModules(allTestSources);
67     }
68
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");
72         outDir.mkdirs();
73         for (final Module module : schemaContext.getModules()) {
74             exportModule(module, outDir);
75         }
76     }
77
78     private static File exportModule(final Module module, final File outDir)
79             throws Exception {
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);
83         }
84         return outFile;
85     }
86 }