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