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