2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.stmt;
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;
16 import java.io.FileNotFoundException;
18 import java.net.URISyntaxException;
19 import java.util.Collection;
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;
37 public class EffectiveBuildTest {
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");
46 public void effectiveBuildTest() throws SourceException, ReactorException {
47 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
48 reactor.addSources(SIMPLE_MODULE);
49 SchemaContext result = reactor.buildEffective();
51 assertNotNull(result);
53 Module simpleModule = result.findModules("simple-module").iterator().next();
54 assertNotNull(simpleModule);
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");
64 ContainerSchemaNode rootCon = (ContainerSchemaNode) simpleModule
65 .getDataChildByName(q1);
66 assertNotNull(rootCon);
68 ContainerSchemaNode subCon = (ContainerSchemaNode) rootCon
69 .getDataChildByName(q2);
70 assertNotNull(subCon);
72 ContainerSchemaNode subSubCon = (ContainerSchemaNode) subCon
73 .getDataChildByName(q3);
74 assertNotNull(subSubCon);
76 ContainerSchemaNode rootCon2 = (ContainerSchemaNode) simpleModule
77 .getDataChildByName(q4);
78 assertNotNull(rootCon2);
80 ContainerSchemaNode subCon2 = (ContainerSchemaNode) rootCon2
81 .getDataChildByName(q5);
82 assertNotNull(subCon2);
84 ContainerSchemaNode subSubCon2 = (ContainerSchemaNode) subCon2
85 .getDataChildByName(q6);
86 assertNotNull(subSubCon2);
88 GroupingDefinition grp = simpleModule.getGroupings().iterator().next();
90 assertEquals(q7, grp.getQName());
92 ContainerSchemaNode grpSubCon2 = (ContainerSchemaNode) grp
93 .getDataChildByName(q5);
94 assertNotNull(grpSubCon2);
96 ContainerSchemaNode grpSubSubCon2 = (ContainerSchemaNode) grpSubCon2
97 .getDataChildByName(q6);
98 assertNotNull(grpSubSubCon2);
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());
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);
114 Set<GroupingDefinition> groupings = result.getGroupings();
115 assertEquals(1, groupings.size());
117 GroupingDefinition grp = groupings.iterator().next();
119 Collection<DataSchemaNode> childNodes = grp.getChildNodes();
120 assertEquals(1, childNodes.size());
121 DataSchemaNode child = childNodes.iterator().next();
123 assertTrue(child instanceof LeafSchemaNode);
124 LeafSchemaNode leaf = (LeafSchemaNode) child;
126 assertNotNull(leaf.getType());
130 public void mockTest() throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
131 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
132 reactor.addSource(YANG_EXT);
134 SchemaContext result = reactor.buildEffective();
135 assertNotNull(result);