b17f4545fae0f1d6fccc928b96c9e4269a31f6c9
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / YT1195Test.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.assertNotSame;
11 import static org.junit.Assert.assertSame;
12
13 import org.junit.Test;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.common.XMLNamespace;
16 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
20
21 public class YT1195Test {
22     @Test
23     public void testKeyStatementReuse() throws Exception {
24         final ModuleEffectiveStatement module = StmtTestUtils.parseYangSource("/bugs/YT1195/key.yang")
25             .getModuleStatement(QNameModule.create(XMLNamespace.of("foo")));
26
27         final ListEffectiveStatement grpFoo = module
28             .findFirstEffectiveSubstatement(GroupingEffectiveStatement.class).orElseThrow()
29             .findFirstEffectiveSubstatement(ListEffectiveStatement.class).orElseThrow();
30         final ListEffectiveStatement foo = module
31             .findFirstEffectiveSubstatement(ListEffectiveStatement.class).orElseThrow();
32
33         // The statements should not be the same due history being part of ListSchemaNode
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         // The statements' key substatement should be reused
38         assertSame(foo.findFirstEffectiveSubstatement(KeyEffectiveStatement.class).orElseThrow(),
39             grpFoo.findFirstEffectiveSubstatement(KeyEffectiveStatement.class).orElseThrow());
40     }
41 }