Fix checkstyle in yang-model-export
[yangtools.git] / yang / 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.net.URISyntaxException;
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.Set;
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;
32
33 public class SimpleModuleTest {
34
35     private static final File TEST_MODELS_FOLDER;
36
37     static {
38         try {
39             TEST_MODELS_FOLDER = new File(SimpleModuleTest.class.getResource("/yang/").toURI());
40         } catch (final URISyntaxException e) {
41             throw new IllegalStateException(e);
42         }
43     }
44
45     private SharedSchemaRepository schemaRegistry;
46     private FilesystemSchemaSourceCache<YangTextSchemaSource> fileSourceProvider;
47     private SchemaContextFactory schemaContextFactory;
48     private Set<SourceIdentifier> allTestSources;
49
50     @Before
51     public void init() {
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);
57
58         schemaContextFactory = schemaRegistry.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
59         allTestSources = new HashSet<>();
60         final SchemaListenerRegistration reg = schemaRegistry.registerSchemaSourceListener(new SchemaSourceListener() {
61
62             @Override
63             public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
64                 // NOOP
65             }
66
67             @Override
68             public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
69                 for (final PotentialSchemaSource<?> source : sources) {
70                     allTestSources.add(source.getSourceIdentifier());
71                 }
72             }
73
74             @Override
75             public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
76                 // NOOP
77             }
78         });
79         reg.close();
80     }
81
82     @Test
83     public void testGenerateAll() throws Exception {
84         testSetOfModules(allTestSources);
85     }
86
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");
90         outDir.mkdirs();
91         for (final Module module : schemaContext.getModules()) {
92             exportModule(schemaContext, module, outDir);
93         }
94     }
95
96     private static File exportModule(final SchemaContext schemaContext, final Module module, final File outDir)
97             throws Exception {
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);
101         }
102         return outFile;
103     }
104 }