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