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