BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / EffectiveBuildTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
15
16 import java.io.FileNotFoundException;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.util.Collection;
20 import java.util.Set;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
37
38 public class EffectiveBuildTest {
39
40     private static final StatementStreamSource SIMPLE_MODULE = sourceForResource(
41             "/stmt-test/effective-build/simple-module.yang");
42     private static final QNameModule SIMPLE_MODULE_QNAME = QNameModule.create(
43             URI.create("simple.yang"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
44     private static final StatementStreamSource YANG_EXT = sourceForResource(
45             "/stmt-test/extensions/yang-ext.yang");
46
47     @Test
48     public void effectiveBuildTest() throws SourceException, ReactorException {
49         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
50         reactor.addSources(SIMPLE_MODULE);
51         SchemaContext result = reactor.buildEffective();
52
53         assertNotNull(result);
54
55         Module simpleModule = result.findModules("simple-module").iterator().next();
56         assertNotNull(simpleModule);
57
58         QName q1 = QName.create(SIMPLE_MODULE_QNAME, "root-container");
59         QName q2 = QName.create(SIMPLE_MODULE_QNAME, "sub-container");
60         QName q3 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container");
61         QName q4 = QName.create(SIMPLE_MODULE_QNAME, "root-container2");
62         QName q5 = QName.create(SIMPLE_MODULE_QNAME, "sub-container2");
63         QName q6 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container2");
64         QName q7 = QName.create(SIMPLE_MODULE_QNAME, "grp");
65
66         ContainerSchemaNode rootCon = (ContainerSchemaNode) simpleModule
67                 .getDataChildByName(q1);
68         assertNotNull(rootCon);
69
70         ContainerSchemaNode subCon = (ContainerSchemaNode) rootCon
71                 .getDataChildByName(q2);
72         assertNotNull(subCon);
73
74         ContainerSchemaNode subSubCon = (ContainerSchemaNode) subCon
75                 .getDataChildByName(q3);
76         assertNotNull(subSubCon);
77
78         ContainerSchemaNode rootCon2 = (ContainerSchemaNode) simpleModule
79                 .getDataChildByName(q4);
80         assertNotNull(rootCon2);
81
82         ContainerSchemaNode subCon2 = (ContainerSchemaNode) rootCon2
83                 .getDataChildByName(q5);
84         assertNotNull(subCon2);
85
86         ContainerSchemaNode subSubCon2 = (ContainerSchemaNode) subCon2
87                 .getDataChildByName(q6);
88         assertNotNull(subSubCon2);
89
90         GroupingDefinition grp = simpleModule.getGroupings().iterator().next();
91         assertNotNull(grp);
92         assertEquals(q7, grp.getQName());
93
94         ContainerSchemaNode grpSubCon2 = (ContainerSchemaNode) grp
95                 .getDataChildByName(q5);
96         assertNotNull(grpSubCon2);
97
98         ContainerSchemaNode grpSubSubCon2 = (ContainerSchemaNode) grpSubCon2
99                 .getDataChildByName(q6);
100         assertNotNull(grpSubSubCon2);
101
102         assertEquals(SchemaPath.create(true, q1, q2, q3), subSubCon.getPath());
103         assertEquals(SchemaPath.create(true, q4, q5, q6), subSubCon2.getPath());
104         assertEquals(SchemaPath.create(true, q7, q5, q6),
105                 grpSubSubCon2.getPath());
106
107     }
108
109     @Test
110     public void extensionsTest() throws SourceException, ReactorException {
111         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
112         reactor.addSources(YANG_EXT);
113         SchemaContext result = reactor.buildEffective();
114         assertNotNull(result);
115
116         Set<GroupingDefinition> groupings = result.getGroupings();
117         assertEquals(1, groupings.size());
118
119         GroupingDefinition grp = groupings.iterator().next();
120
121         Collection<DataSchemaNode> childNodes = grp.getChildNodes();
122         assertEquals(1, childNodes.size());
123         DataSchemaNode child = childNodes.iterator().next();
124
125         assertTrue(child instanceof LeafSchemaNode);
126         LeafSchemaNode leaf = (LeafSchemaNode) child;
127
128         assertNotNull(leaf.getType());
129     }
130
131     @Test
132     public void mockTest() throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
133         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
134         reactor.addSource(YANG_EXT);
135
136         SchemaContext result = reactor.buildEffective();
137         assertNotNull(result);
138     }
139 }