Bug 7954: Throw an exception when parsing duplicate (sub)modules
[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 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
38
39 public class EffectiveBuildTest {
40
41     private static final StatementStreamSource SIMPLE_MODULE = sourceForResource(
42             "/stmt-test/effective-build/simple-module.yang");
43     private static final QNameModule SIMPLE_MODULE_QNAME = QNameModule.create(
44             URI.create("simple.yang"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
45     private static final StatementStreamSource YANG_EXT = sourceForResource(
46             "/stmt-test/extensions/yang-ext.yang");
47
48     @Test
49     public void effectiveBuildTest() throws SourceException, ReactorException {
50         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
51         reactor.addSources(SIMPLE_MODULE);
52         EffectiveSchemaContext result = reactor.buildEffective();
53
54         assertNotNull(result);
55
56         Module simpleModule = result.findModuleByName("simple-module", null);
57         assertNotNull(simpleModule);
58
59         QName q1 = QName.create(SIMPLE_MODULE_QNAME, "root-container");
60         QName q2 = QName.create(SIMPLE_MODULE_QNAME, "sub-container");
61         QName q3 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container");
62         QName q4 = QName.create(SIMPLE_MODULE_QNAME, "root-container2");
63         QName q5 = QName.create(SIMPLE_MODULE_QNAME, "sub-container2");
64         QName q6 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container2");
65         QName q7 = QName.create(SIMPLE_MODULE_QNAME, "grp");
66
67         ContainerSchemaNode rootCon = (ContainerSchemaNode) simpleModule
68                 .getDataChildByName(q1);
69         assertNotNull(rootCon);
70
71         ContainerSchemaNode subCon = (ContainerSchemaNode) rootCon
72                 .getDataChildByName(q2);
73         assertNotNull(subCon);
74
75         ContainerSchemaNode subSubCon = (ContainerSchemaNode) subCon
76                 .getDataChildByName(q3);
77         assertNotNull(subSubCon);
78
79         ContainerSchemaNode rootCon2 = (ContainerSchemaNode) simpleModule
80                 .getDataChildByName(q4);
81         assertNotNull(rootCon2);
82
83         ContainerSchemaNode subCon2 = (ContainerSchemaNode) rootCon2
84                 .getDataChildByName(q5);
85         assertNotNull(subCon2);
86
87         ContainerSchemaNode subSubCon2 = (ContainerSchemaNode) subCon2
88                 .getDataChildByName(q6);
89         assertNotNull(subSubCon2);
90
91         GroupingDefinition grp = simpleModule.getGroupings().iterator().next();
92         assertNotNull(grp);
93         assertEquals(q7, grp.getQName());
94
95         ContainerSchemaNode grpSubCon2 = (ContainerSchemaNode) grp
96                 .getDataChildByName(q5);
97         assertNotNull(grpSubCon2);
98
99         ContainerSchemaNode grpSubSubCon2 = (ContainerSchemaNode) grpSubCon2
100                 .getDataChildByName(q6);
101         assertNotNull(grpSubSubCon2);
102
103         assertEquals(SchemaPath.create(true, q1, q2, q3), subSubCon.getPath());
104         assertEquals(SchemaPath.create(true, q4, q5, q6), subSubCon2.getPath());
105         assertEquals(SchemaPath.create(true, q7, q5, q6),
106                 grpSubSubCon2.getPath());
107
108     }
109
110     @Test
111     public void extensionsTest() throws SourceException, ReactorException {
112         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
113         reactor.addSources(YANG_EXT);
114         EffectiveSchemaContext result = reactor.buildEffective();
115         assertNotNull(result);
116
117         Set<GroupingDefinition> groupings = result.getGroupings();
118         assertEquals(1, groupings.size());
119
120         GroupingDefinition grp = groupings.iterator().next();
121
122         Collection<DataSchemaNode> childNodes = grp.getChildNodes();
123         assertEquals(1, childNodes.size());
124         DataSchemaNode child = childNodes.iterator().next();
125
126         assertTrue(child instanceof LeafSchemaNode);
127         LeafSchemaNode leaf = (LeafSchemaNode) child;
128
129         assertNotNull(leaf.getType());
130     }
131
132     @Test
133     public void mockTest() throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
134         BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
135         reactor.addSource(YANG_EXT);
136
137         SchemaContext result = reactor.buildEffective();
138         assertNotNull(result);
139     }
140 }