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