Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / retest / Bug1412Test.java
1 /*
2  * Copyright (c) 2013 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.retest;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import java.net.URI;
14 import java.text.SimpleDateFormat;
15 import java.util.Date;
16 import java.util.List;
17 import java.util.Set;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
24
25 /**
26  * Test ANTLR4 grammar capability to parse description statement in unknown node.
27  *
28  * Note: Everything under unknown node is unknown node.
29  */
30 public class Bug1412Test {
31
32     @Test
33     public void test() throws Exception {
34
35         Set<Module> modules = null;
36
37
38         modules = TestUtils.loadModules(getClass().getResource("/bugs/bug1412").toURI());
39
40
41         Module bug1412 = TestUtils.findModule(modules, "bug1412");
42         assertNotNull(bug1412);
43
44         ContainerSchemaNode node = (ContainerSchemaNode) bug1412.getDataChildByName("node");
45         List<UnknownSchemaNode> unknownNodes = node.getUnknownSchemaNodes();
46         assertEquals(1, unknownNodes.size());
47         UnknownSchemaNode action = unknownNodes.get(0);
48
49         Date revision = new SimpleDateFormat("yyyy-MM-dd").parse("2014-07-25");
50         QNameModule qm = QNameModule.create(URI.create("urn:test:bug1412"), revision);
51         QName expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "action");
52         assertEquals(expectedNodeType, action.getNodeType());
53         assertEquals("hello", action.getNodeParameter());
54         QName expectedQName = QName.create(qm, "hello");
55         assertEquals(expectedQName, action.getQName());
56
57         unknownNodes = action.getUnknownSchemaNodes();
58         assertEquals(4, unknownNodes.size());
59         UnknownSchemaNode info = null;
60         UnknownSchemaNode description = null;
61         UnknownSchemaNode actionPoint = null;
62         UnknownSchemaNode output = null;
63         for (UnknownSchemaNode un : unknownNodes) {
64             if ("info".equals(un.getNodeType().getLocalName())) {
65                 info = un;
66             } else if ("description".equals(un.getNodeType().getLocalName())) {
67                 description = un;
68             } else if ("actionpoint".equals(un.getNodeType().getLocalName())) {
69                 actionPoint = un;
70             } else if ("output".equals(un.getNodeType().getLocalName())) {
71                 output = un;
72             }
73         }
74         assertNotNull(info);
75         assertNotNull(description);
76         assertNotNull(actionPoint);
77         assertNotNull(output);
78
79         expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "info");
80         assertEquals(expectedNodeType, info.getNodeType());
81         assertEquals("greeting", info.getNodeParameter());
82
83         expectedNodeType = QName.create(qm, "description");
84         assertEquals(expectedNodeType, description.getNodeType());
85         assertEquals("say greeting", description.getNodeParameter());
86
87         expectedNodeType = QName.create("urn:test:bug1412:ext:definitions", "2014-07-25", "actionpoint");
88         assertEquals(expectedNodeType, actionPoint.getNodeType());
89         assertEquals("entry", actionPoint.getNodeParameter());
90
91         expectedNodeType = QName.create(qm, "output");
92         assertEquals(expectedNodeType, output.getNodeType());
93         assertEquals("", output.getNodeParameter());
94     }
95
96 }