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