Populate xpath/ hierarchy
[yangtools.git] / yang / rfc8528-parser-support / src / test / java / org / opendaylight / yangtools / rfc8528 / parser / MountPointTest.java
1 /*
2  * Copyright (c) 2019 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.rfc8528.parser;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13
14 import java.io.IOException;
15 import java.util.List;
16 import java.util.stream.Collectors;
17 import org.junit.AfterClass;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.rfc8528.model.api.MountPointSchemaNode;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.XMLNamespace;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
30 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
36
37 public class MountPointTest {
38     private static final QNameModule EXAMPLE_USES =
39         QNameModule.create(XMLNamespace.of("http://example.org/example-uses"));
40     private static final QName EXAMPLE_CONT = QName.create(EXAMPLE_USES, "cont");
41     private static final QName EXAMPLE_GRP = QName.create(EXAMPLE_USES, "grp");
42     private static final QName EXAMPLE_GRP_CONT = QName.create(EXAMPLE_USES, "grp-cont");
43     private static final QName EXAMPLE_LIST = QName.create(EXAMPLE_USES, "list");
44
45     private static CrossSourceStatementReactor reactor;
46
47     @BeforeClass
48     public static void createReactor() {
49         reactor = RFC7950Reactors.vanillaReactorBuilder()
50                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
51                     new MountPointStatementSupport(YangParserConfiguration.DEFAULT))
52                 .build();
53     }
54
55     @AfterClass
56     public static void freeReactor() {
57         reactor = null;
58     }
59
60     @Test
61     public void testMountPointResolution() throws ReactorException, IOException, YangSyntaxErrorException {
62         final SchemaContext context = reactor.newBuild()
63                 .addLibSources(
64                     YangStatementStreamSource.create(YangTextSchemaSource.forResource(
65                             "/ietf-inet-types@2013-07-15.yang")),
66                     YangStatementStreamSource.create(YangTextSchemaSource.forResource(
67                             "/ietf-yang-schema-mount@2019-01-14.yang")),
68                     YangStatementStreamSource.create(YangTextSchemaSource.forResource(
69                             "/ietf-yang-types@2013-07-15.yang")))
70                 .addSources(
71                     YangStatementStreamSource.create(YangTextSchemaSource.forResource("/example-grp.yang")),
72                     YangStatementStreamSource.create(YangTextSchemaSource.forResource("/example-uses.yang")))
73                 .buildEffective();
74
75         assertEquals(5, context.getModules().size());
76
77         DataSchemaNode child = context.findDataTreeChild(EXAMPLE_CONT).get();
78         assertThat(child, instanceOf(ContainerSchemaNode.class));
79         List<MountPointSchemaNode> mps = MountPointSchemaNode.streamAll((ContainerSchemaNode) child)
80                 .collect(Collectors.toList());
81         assertEquals(2, mps.size());
82         assertEquals(EXAMPLE_CONT, mps.get(0).getQName());
83         assertEquals(EXAMPLE_CONT, mps.get(1).getQName());
84
85         child = context.findDataTreeChild(EXAMPLE_GRP_CONT).get();
86         assertThat(child, instanceOf(ContainerSchemaNode.class));
87         mps = MountPointSchemaNode.streamAll((ContainerSchemaNode) child).collect(Collectors.toList());
88         assertEquals(1, mps.size());
89         assertEquals(EXAMPLE_GRP, mps.get(0).getQName());
90
91         child = context.findDataTreeChild(EXAMPLE_LIST).get();
92         assertThat(child, instanceOf(ListSchemaNode.class));
93         mps = MountPointSchemaNode.streamAll((ListSchemaNode) child).collect(Collectors.toList());
94         assertEquals(1, mps.size());
95         assertEquals(EXAMPLE_LIST, mps.get(0).getQName());
96     }
97 }