70c2d67d5e62ee140af5c1c9bec60e5d41345260
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / YT1212Test.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.assertEquals;
11 import static org.junit.Assert.assertNotSame;
12 import static org.junit.Assert.assertSame;
13
14 import java.util.List;
15 import java.util.stream.Collectors;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.XMLNamespace;
19 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
25
26 public class YT1212Test {
27     @Test
28     public void testActiontatementReuse() throws Exception {
29         final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/YT1212/anyxml.yang")
30             .getModuleStatement(QNameModule.create(XMLNamespace.of("foo")));
31
32         final AnyxmlEffectiveStatement grpFoo = module
33             .findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow()
34             .findFirstEffectiveSubstatement(AnyxmlEffectiveStatement.class).orElseThrow();
35         final AnyxmlEffectiveStatement foo = module
36             .findFirstEffectiveSubstatement(AnyxmlEffectiveStatement.class).orElseThrow();
37
38         // The statements should not be the same due SchemaPath being part of ActionDefinition
39         assertNotSame(foo, grpFoo);
40         // The statements are instantiated in the same module, hence they should have the same argument
41         assertSame(foo.argument(), grpFoo.argument());
42         // All substatements are context-independent, hence they get reused
43         assertSame(foo.effectiveSubstatements(), grpFoo.effectiveSubstatements());
44     }
45
46     @Test
47     public void testLeafStatementReuse() throws Exception {
48         final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/YT1212/leaf.yang")
49             .getModuleStatement(QNameModule.create(XMLNamespace.of("foo")));
50
51         final LeafEffectiveStatement grpFoo = module
52             .findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow()
53             .findFirstEffectiveSubstatement(LeafEffectiveStatement.class).orElseThrow();
54         final LeafEffectiveStatement foo = module
55             .findFirstEffectiveSubstatement(LeafEffectiveStatement.class).orElseThrow();
56
57         // The statements should not be the same due SchemaPath being part of LeafSchemaNode
58         assertNotSame(foo, grpFoo);
59         // The statements are instantiated in the same module, hence they should have the same argument
60         assertSame(foo.argument(), grpFoo.argument());
61         // The 'type' is not context-independent, but it being copy-insensitive and statements get reused
62         assertSame(foo.effectiveSubstatements(), grpFoo.effectiveSubstatements());
63     }
64
65     @Test
66     public void testContainerStatementReuse() throws Exception {
67         final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/YT1212/container.yang")
68             .getModuleStatement(QNameModule.create(XMLNamespace.of("foo")));
69
70         final NotificationEffectiveStatement notif =
71             module.findFirstEffectiveSubstatement(NotificationEffectiveStatement.class).orElseThrow();
72         final List<GroupingEffectiveStatement> groupings = notif.effectiveSubstatements().stream()
73             .filter(GroupingEffectiveStatement.class::isInstance).map(GroupingEffectiveStatement.class::cast)
74             .collect(Collectors.toList());
75         assertEquals(2, groupings.size());
76         final GroupingEffectiveStatement grp = groupings.get(0);
77         assertEquals("grp", grp.argument().getLocalName());
78         final GroupingEffectiveStatement barGrp = groupings.get(1);
79         assertEquals("bar", barGrp.argument().getLocalName());
80         final ContainerEffectiveStatement bar = notif.findFirstEffectiveSubstatement(ContainerEffectiveStatement.class)
81             .orElseThrow();
82
83         // Container needs to be reused
84         assertSame(bar.findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).orElseThrow(),
85             barGrp.findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).orElseThrow());
86     }
87 }