Add a proper grouping statement policy
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / YT1208Test.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, 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.stmt;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertSame;
12
13 import java.net.URI;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
21
22 public class YT1208Test {
23     @Test
24     public void testGroupingStatementReuse() throws Exception {
25         final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/YT1208/grouping.yang")
26             .getModuleStatements()
27             .get(QNameModule.create(URI.create("foo")));
28         assertNotNull(module);
29
30         final NotificationEffectiveStatement notif = module
31             .findFirstEffectiveSubstatement(NotificationEffectiveStatement.class).orElseThrow();
32
33         final GroupingEffectiveStatement grpBar = notif
34             .findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow()
35             .findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).orElseThrow()
36             .findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow();
37         final GroupingEffectiveStatement contBar = notif
38             .findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).orElseThrow()
39             .findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).orElseThrow()
40             .findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow();
41
42         assertSame(contBar, grpBar);
43     }
44
45     @Test
46     public void testLeafStatementReuse() throws Exception {
47         final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/YT1208/leaf.yang")
48             .getModuleStatements()
49             .get(QNameModule.create(URI.create("foo")));
50         assertNotNull(module);
51
52         final NotificationEffectiveStatement notif = module
53             .findFirstEffectiveSubstatement(NotificationEffectiveStatement.class).orElseThrow();
54
55         final LeafEffectiveStatement grpBar = notif
56             .findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow()
57             .findFirstEffectiveSubstatement(LeafEffectiveStatement.class).orElseThrow();
58         final LeafEffectiveStatement contBar = notif
59             .findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).orElseThrow()
60             .findFirstEffectiveSubstatement(LeafEffectiveStatement.class).orElseThrow();
61
62         assertSame(contBar, grpBar);
63     }
64 }