Promote SchemaSourceRepresentation
[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.util.HashSet;
13 import java.util.Set;
14 import org.junit.jupiter.api.BeforeEach;
15 import org.junit.jupiter.api.Test;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
19 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
20 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
21 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
22 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
23 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
24
25 public class SimpleModuleTest {
26     private SharedSchemaRepository schemaRegistry;
27     private EffectiveModelContextFactory schemaContextFactory;
28     private Set<SourceIdentifier> allTestSources;
29
30     @BeforeEach
31     public void init() {
32         schemaRegistry = new SharedSchemaRepository("test");
33         final TextToIRTransformer astTransformer = TextToIRTransformer.create(schemaRegistry, schemaRegistry);
34         schemaRegistry.registerSchemaSourceListener(astTransformer);
35
36         schemaContextFactory = schemaRegistry.createEffectiveModelContextFactory();
37         allTestSources = new HashSet<>();
38         try (var reg = schemaRegistry.registerSchemaSourceListener(
39             new SchemaSourceListener() {
40                 @Override
41                 public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
42                     // NOOP
43                 }
44
45                 @Override
46                 public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
47                     for (final PotentialSchemaSource<?> source : sources) {
48                         allTestSources.add(source.getSourceIdentifier());
49                     }
50                 }
51
52                 @Override
53                 public void schemaSourceEncountered(final SourceRepresentation source) {
54                     // NOOP
55                 }
56             })) {
57             // Noop
58         }
59     }
60
61     @Test
62     public void testGenerateAll() throws Exception {
63         testSetOfModules(allTestSources);
64     }
65
66     private void testSetOfModules(final Set<SourceIdentifier> source) throws Exception {
67         final var schemaContext = schemaContextFactory.createEffectiveModelContext(source).get();
68         final var outDir = new File("target/collection");
69         outDir.mkdirs();
70         for (final Module module : schemaContext.getModules()) {
71             exportModule(module, outDir);
72         }
73     }
74
75     private static File exportModule(final Module module, final File outDir)
76             throws Exception {
77         final var outFile = new File(outDir, YinExportUtils.wellFormedYinName(module.getName(), module.getRevision()));
78         try (var output = new FileOutputStream(outFile)) {
79             YinExportUtils.writeModuleAsYinText(module.asEffectiveStatement(), output);
80         }
81         return outFile;
82     }
83 }