Populate xpath/ hierarchy
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug1412Test.java
1 /*
2  * Copyright (c) 2016 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.stmt;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import java.util.Collection;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.Empty;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.Revision;
19 import org.opendaylight.yangtools.yang.common.XMLNamespace;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
23
24 /**
25  * Test ANTLR4 grammar capability to parse description statement in unknown node.
26  *
27  * <p>
28  * Note: Everything under unknown node is unknown node.
29  */
30 public class Bug1412Test {
31
32     @Test
33     public void test() throws Exception {
34         final Module bug1412 = TestUtils.findModule(
35             TestUtils.loadModules(getClass().getResource("/bugs/bug1412").toURI()), "bug1412").get();
36
37         final ContainerSchemaNode node = (ContainerSchemaNode) bug1412.getDataChildByName(QName.create(
38                 bug1412.getQNameModule(), "node"));
39         Collection<? extends UnrecognizedStatement> unknownNodes = node.asEffectiveStatement().getDeclared()
40             .declaredSubstatements(UnrecognizedStatement.class);
41         assertEquals(1, unknownNodes.size());
42         final UnrecognizedStatement action = unknownNodes.iterator().next();
43
44         final QNameModule qm = QNameModule.create(XMLNamespace.of("urn:test:bug1412"), Revision.of("2014-07-25"));
45         QName expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "action");
46         assertEquals(expectedNodeType, action.statementDefinition().getStatementName());
47         assertEquals("hello", action.argument());
48
49         unknownNodes = action.declaredSubstatements(UnrecognizedStatement.class);
50         assertEquals(4, unknownNodes.size());
51         UnrecognizedStatement info = null;
52         UnrecognizedStatement description = null;
53         UnrecognizedStatement actionPoint = null;
54         UnrecognizedStatement output = null;
55         for (final UnrecognizedStatement un : unknownNodes) {
56             final String name = un.statementDefinition().getStatementName().getLocalName();
57             if ("info".equals(name)) {
58                 info = un;
59             } else if ("description".equals(name)) {
60                 description = un;
61             } else if ("actionpoint".equals(name)) {
62                 actionPoint = un;
63             } else if ("output".equals(name)) {
64                 output = un;
65             }
66         }
67
68         assertNotNull(info);
69         assertNotNull(description);
70         assertNotNull(actionPoint);
71         assertNotNull(output);
72
73         expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "info");
74         assertEquals(expectedNodeType, info.statementDefinition().getStatementName());
75         assertEquals("greeting", info.argument());
76
77         expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "description");
78         assertEquals(expectedNodeType, description.statementDefinition().getStatementName());
79         assertEquals("say greeting", description.argument());
80
81         expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "actionpoint");
82         assertEquals(expectedNodeType, actionPoint.statementDefinition().getStatementName());
83         assertEquals("entry", actionPoint.argument());
84
85         expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "output");
86         assertEquals(expectedNodeType, output.statementDefinition().getStatementName());
87         assertEquals(Empty.getInstance(), output.argument());
88     }
89 }