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.StatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
36 public class EffectiveBuildTest {
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");
45 public void effectiveBuildTest() throws ReactorException {
46 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
47 reactor.addSources(SIMPLE_MODULE);
48 SchemaContext result = reactor.buildEffective();
50 assertNotNull(result);
52 Module simpleModule = result.findModules("simple-module").iterator().next();
53 assertNotNull(simpleModule);
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");
63 ContainerSchemaNode rootCon = (ContainerSchemaNode) simpleModule.getDataChildByName(q1);
64 assertNotNull(rootCon);
66 ContainerSchemaNode subCon = (ContainerSchemaNode) rootCon.getDataChildByName(q2);
67 assertNotNull(subCon);
69 ContainerSchemaNode subSubCon = (ContainerSchemaNode) subCon.getDataChildByName(q3);
70 assertNotNull(subSubCon);
72 ContainerSchemaNode rootCon2 = (ContainerSchemaNode) simpleModule.getDataChildByName(q4);
73 assertNotNull(rootCon2);
75 ContainerSchemaNode subCon2 = (ContainerSchemaNode) rootCon2.getDataChildByName(q5);
76 assertNotNull(subCon2);
78 ContainerSchemaNode subSubCon2 = (ContainerSchemaNode) subCon2.getDataChildByName(q6);
79 assertNotNull(subSubCon2);
81 GroupingDefinition grp = simpleModule.getGroupings().iterator().next();
83 assertEquals(q7, grp.getQName());
85 ContainerSchemaNode grpSubCon2 = (ContainerSchemaNode) grp.getDataChildByName(q5);
86 assertNotNull(grpSubCon2);
88 ContainerSchemaNode grpSubSubCon2 = (ContainerSchemaNode) grpSubCon2.getDataChildByName(q6);
89 assertNotNull(grpSubSubCon2);
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());
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);
104 Set<GroupingDefinition> groupings = result.getGroupings();
105 assertEquals(1, groupings.size());
107 GroupingDefinition grp = groupings.iterator().next();
109 Collection<DataSchemaNode> childNodes = grp.getChildNodes();
110 assertEquals(1, childNodes.size());
111 DataSchemaNode child = childNodes.iterator().next();
113 assertTrue(child instanceof LeafSchemaNode);
114 LeafSchemaNode leaf = (LeafSchemaNode) child;
116 assertNotNull(leaf.getType());
120 public void mockTest() throws ReactorException, FileNotFoundException, URISyntaxException {
121 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
122 reactor.addSource(YANG_EXT);
124 SchemaContext result = reactor.buildEffective();
125 assertNotNull(result);