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