Promote SchemaSourceRepresentation
[yangtools.git] / parser / 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.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12
13 import org.junit.jupiter.api.Test;
14 import org.opendaylight.yangtools.rfc8528.model.api.MountPointSchemaNode;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.common.XMLNamespace;
18 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
20 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
21 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
23 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
25
26 class MountPointTest {
27     private static final QNameModule EXAMPLE_USES =
28         QNameModule.create(XMLNamespace.of("http://example.org/example-uses"));
29     private static final QName EXAMPLE_CONT = QName.create(EXAMPLE_USES, "cont");
30     private static final QName EXAMPLE_GRP = QName.create(EXAMPLE_USES, "grp");
31     private static final QName EXAMPLE_GRP_CONT = QName.create(EXAMPLE_USES, "grp-cont");
32     private static final QName EXAMPLE_LIST = QName.create(EXAMPLE_USES, "list");
33
34     @Test
35     void testMountPointResolution() throws Exception {
36         final var reactor = RFC7950Reactors.vanillaReactorBuilder()
37             .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
38                 new MountPointStatementSupport(YangParserConfiguration.DEFAULT))
39             .build();
40
41         final var context = reactor.newBuild()
42             .addLibSources(
43                 YangStatementStreamSource.create(
44                     YangTextSource.forResource(MountPointTest.class, "/ietf-inet-types@2013-07-15.yang")),
45                 YangStatementStreamSource.create(
46                     YangTextSource.forResource(MountPointTest.class, "/ietf-yang-schema-mount@2019-01-14.yang")),
47                 YangStatementStreamSource.create(
48                     YangTextSource.forResource(MountPointTest.class, "/ietf-yang-types@2013-07-15.yang")))
49             .addSources(
50                 YangStatementStreamSource.create(YangTextSource.forResource(MountPointTest.class, "/example-grp.yang")),
51                 YangStatementStreamSource.create(
52                     YangTextSource.forResource(MountPointTest.class, "/example-uses.yang")))
53             .buildEffective();
54
55         assertEquals(5, context.getModules().size());
56
57         var child = context.findDataTreeChild(EXAMPLE_CONT).orElseThrow();
58         var mps = MountPointSchemaNode.streamAll(assertInstanceOf(ContainerSchemaNode.class, child)).toList();
59         assertEquals(2, mps.size());
60         assertEquals(EXAMPLE_CONT, mps.get(0).getQName());
61         assertEquals(EXAMPLE_CONT, mps.get(1).getQName());
62
63         child = context.findDataTreeChild(EXAMPLE_GRP_CONT).orElseThrow();
64         mps = MountPointSchemaNode.streamAll(assertInstanceOf(ContainerSchemaNode.class, child)).toList();
65         assertEquals(1, mps.size());
66         assertEquals(EXAMPLE_GRP, mps.get(0).getQName());
67
68         child = context.findDataTreeChild(EXAMPLE_LIST).orElseThrow();
69         mps = MountPointSchemaNode.streamAll(assertInstanceOf(ListSchemaNode.class, child)).toList();
70         assertEquals(1, mps.size());
71         assertEquals(EXAMPLE_LIST, mps.get(0).getQName());
72     }
73 }