Populate data/ hierarchy
[yangtools.git] / model / yang-model-export / src / test / java / org / opendaylight / yangtools / yang / model / export / YangTextSnippetTest.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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 static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.opendaylight.yangtools.yang.model.export.DeclaredStatementFormatter.defaultInstance;
13
14 import java.util.Collection;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement.NameToEffectiveSubmoduleNamespace;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
22
23 public class YangTextSnippetTest {
24     @Test
25     public void testNotification() {
26         final SchemaContext schema = YangParserTestUtils.parseYangResource("/bugs/bug2444/yang/notification.yang");
27         assertFormat(schema.getModules());
28     }
29
30     @Test
31     public void testSubmoduleNamespaces() throws Exception {
32         SchemaContext schema = YangParserTestUtils.parseYangResourceDirectory("/bugs/yt992");
33         assertFormat(schema.getModules());
34     }
35
36     private static void assertFormat(final Collection<? extends Module> modules) {
37         for (Module module : modules) {
38             assertTrue(module instanceof ModuleEffectiveStatement);
39             final ModuleEffectiveStatement stmt = (ModuleEffectiveStatement) module;
40             assertNotNull(formatModule(stmt));
41
42             for (SubmoduleEffectiveStatement substmt : stmt.getAll(NameToEffectiveSubmoduleNamespace.class).values()) {
43                 assertNotNull(formatSubmodule(substmt));
44             }
45         }
46     }
47
48     private static String formatModule(final ModuleEffectiveStatement stmt) {
49         return defaultInstance().toYangTextSnippet(stmt, stmt.getDeclared()).toString();
50     }
51
52     private static String formatSubmodule(final SubmoduleEffectiveStatement stmt) {
53         return defaultInstance().toYangTextSnippet(stmt, stmt.getDeclared()).toString();
54     }
55 }