Add XMLNamespace
[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.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 =
38         QNameModule.create(XMLNamespace.of("http://example.org/example-uses"));
39     private static final QName EXAMPLE_CONT = QName.create(EXAMPLE_USES, "cont");
40     private static final QName EXAMPLE_GRP = QName.create(EXAMPLE_USES, "grp");
41     private static final QName EXAMPLE_GRP_CONT = QName.create(EXAMPLE_USES, "grp-cont");
42     private static final QName EXAMPLE_LIST = QName.create(EXAMPLE_USES, "list");
43
44     private static CrossSourceStatementReactor reactor;
45
46     @BeforeClass
47     public static void createReactor() {
48         reactor = RFC7950Reactors.vanillaReactorBuilder()
49                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, MountPointStatementSupport.getInstance())
50                 .build();
51     }
52
53     @AfterClass
54     public static void freeReactor() {
55         reactor = null;
56     }
57
58     @Test
59     public void testMountPointResolution() throws ReactorException, IOException, YangSyntaxErrorException {
60         final SchemaContext context = reactor.newBuild()
61                 .addLibSources(
62                     YangStatementStreamSource.create(YangTextSchemaSource.forResource(
63                             "/ietf-inet-types@2013-07-15.yang")),
64                     YangStatementStreamSource.create(YangTextSchemaSource.forResource(
65                             "/ietf-yang-schema-mount@2019-01-14.yang")),
66                     YangStatementStreamSource.create(YangTextSchemaSource.forResource(
67                             "/ietf-yang-types@2013-07-15.yang")))
68                 .addSources(
69                     YangStatementStreamSource.create(YangTextSchemaSource.forResource("/example-grp.yang")),
70                     YangStatementStreamSource.create(YangTextSchemaSource.forResource("/example-uses.yang")))
71                 .buildEffective();
72
73         assertEquals(5, context.getModules().size());
74
75         DataSchemaNode child = context.findDataTreeChild(EXAMPLE_CONT).get();
76         assertThat(child, instanceOf(ContainerSchemaNode.class));
77         List<MountPointSchemaNode> mps = MountPointSchemaNode.streamAll((ContainerSchemaNode) child)
78                 .collect(Collectors.toList());
79         assertEquals(2, mps.size());
80         assertEquals(EXAMPLE_CONT, mps.get(0).getQName());
81         assertEquals(EXAMPLE_CONT, mps.get(1).getQName());
82
83         child = context.findDataTreeChild(EXAMPLE_GRP_CONT).get();
84         assertThat(child, instanceOf(ContainerSchemaNode.class));
85         mps = MountPointSchemaNode.streamAll((ContainerSchemaNode) child).collect(Collectors.toList());
86         assertEquals(1, mps.size());
87         assertEquals(EXAMPLE_GRP, mps.get(0).getQName());
88
89         child = context.findDataTreeChild(EXAMPLE_LIST).get();
90         assertThat(child, instanceOf(ListSchemaNode.class));
91         mps = MountPointSchemaNode.streamAll((ListSchemaNode) child).collect(Collectors.toList());
92         assertEquals(1, mps.size());
93         assertEquals(EXAMPLE_LIST, mps.get(0).getQName());
94     }
95 }