Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / Bug4610Test.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.stmt.test;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12
13 import java.net.URI;
14 import java.util.Date;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerStatement;
23 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ContainerEffectiveStatementImpl;
25
26 public class Bug4610Test {
27
28     @Test
29     public void test() throws Exception {
30         SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug4610");
31
32         Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2015-12-12");
33         QNameModule foo = QNameModule.create(new URI("foo"), revision);
34         QNameModule bar = QNameModule.create(new URI("bar"), revision);
35
36         QName g1 = QName.create(bar, "g1");
37         QName g2 = QName.create(bar, "g2");
38         QName c1Bar = QName.create(bar, "c1");
39
40         QName c1Foo = QName.create(foo, "c1");
41         QName g3 = QName.create(foo, "g3");
42         QName root = QName.create(foo, "root");
43
44         ContainerEffectiveStatementImpl effectiveContainerStatementG1 = findContainer(context, g1, c1Bar);
45         ContainerEffectiveStatementImpl effectiveContainerStatementG2 = findContainer(context, g2, c1Bar);
46         ContainerEffectiveStatementImpl effectiveContainerStatementG3 = findContainer(context, g3, c1Foo);
47         ContainerEffectiveStatementImpl effectiveContainerStatementRoot = findContainer(context, root, c1Foo);
48
49         // check arguments
50         QName originalStatementArgument = effectiveContainerStatementG1.argument();
51         assertTrue(originalStatementArgument.equals(effectiveContainerStatementG2.argument()));
52         assertFalse(originalStatementArgument.equals(effectiveContainerStatementG3.argument()));
53         assertFalse(originalStatementArgument.equals(effectiveContainerStatementRoot.argument()));
54
55         ContainerStatement originalContainerStatement = effectiveContainerStatementG1.getDeclared();
56         ContainerStatement inGrouping2ContainerStatement = effectiveContainerStatementG2.getDeclared();
57         ContainerStatement inGrouping3ContainerStatement = effectiveContainerStatementG3.getDeclared();
58         ContainerStatement inRootContainerStatement = effectiveContainerStatementRoot.getDeclared();
59
60         // check declared instances
61         assertTrue(originalContainerStatement == inGrouping2ContainerStatement);
62         assertTrue(originalContainerStatement == inGrouping3ContainerStatement);
63         assertTrue(originalContainerStatement == inRootContainerStatement);
64
65     }
66
67     private ContainerEffectiveStatementImpl findContainer(SchemaContext context, QName... path) {
68         SchemaNode node = SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(true, path));
69         assertTrue(node instanceof ContainerEffectiveStatementImpl);
70         ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) node;
71         return container;
72     }
73 }