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
8 package org.opendaylight.yangtools.yang.stmt;
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;
15 import java.io.FileNotFoundException;
16 import java.net.URISyntaxException;
17 import java.util.Collection;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.common.XMLNamespace;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
32 public class EffectiveBuildTest {
34 private static final StatementStreamSource SIMPLE_MODULE = sourceForResource(
35 "/stmt-test/effective-build/simple-module.yang");
36 private static final QNameModule SIMPLE_MODULE_QNAME = QNameModule.create(XMLNamespace.of("simple.yang"));
37 private static final StatementStreamSource YANG_EXT = sourceForResource(
38 "/stmt-test/extensions/yang-ext.yang");
41 public void effectiveBuildTest() throws ReactorException {
42 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild().addSources(SIMPLE_MODULE)
45 assertNotNull(result);
47 Module simpleModule = result.findModules("simple-module").iterator().next();
48 assertNotNull(simpleModule);
50 final QName q1 = QName.create(SIMPLE_MODULE_QNAME, "root-container");
51 final QName q2 = QName.create(SIMPLE_MODULE_QNAME, "sub-container");
52 final QName q3 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container");
53 final QName q4 = QName.create(SIMPLE_MODULE_QNAME, "root-container2");
54 final QName q5 = QName.create(SIMPLE_MODULE_QNAME, "sub-container2");
55 final QName q6 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container2");
56 final QName q7 = QName.create(SIMPLE_MODULE_QNAME, "grp");
58 ContainerSchemaNode rootCon = (ContainerSchemaNode) simpleModule.getDataChildByName(q1);
59 assertNotNull(rootCon);
61 ContainerSchemaNode subCon = (ContainerSchemaNode) rootCon.getDataChildByName(q2);
62 assertNotNull(subCon);
64 ContainerSchemaNode subSubCon = (ContainerSchemaNode) subCon.getDataChildByName(q3);
65 assertNotNull(subSubCon);
67 ContainerSchemaNode rootCon2 = (ContainerSchemaNode) simpleModule.getDataChildByName(q4);
68 assertNotNull(rootCon2);
70 ContainerSchemaNode subCon2 = (ContainerSchemaNode) rootCon2.getDataChildByName(q5);
71 assertNotNull(subCon2);
73 ContainerSchemaNode subSubCon2 = (ContainerSchemaNode) subCon2.getDataChildByName(q6);
74 assertNotNull(subSubCon2);
76 GroupingDefinition grp = simpleModule.getGroupings().iterator().next();
78 assertEquals(q7, grp.getQName());
80 ContainerSchemaNode grpSubCon2 = (ContainerSchemaNode) grp.getDataChildByName(q5);
81 assertNotNull(grpSubCon2);
83 ContainerSchemaNode grpSubSubCon2 = (ContainerSchemaNode) grpSubCon2.getDataChildByName(q6);
84 assertNotNull(grpSubSubCon2);
86 assertEquals(q3, subSubCon.getQName());
87 assertEquals(q6, subSubCon2.getQName());
88 assertEquals(q6, grpSubSubCon2.getQName());
92 public void extensionsTest() throws ReactorException {
93 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild().addSource(YANG_EXT).buildEffective();
94 assertNotNull(result);
96 Collection<? extends GroupingDefinition> groupings = result.getGroupings();
97 assertEquals(1, groupings.size());
99 GroupingDefinition grp = groupings.iterator().next();
101 Collection<? extends DataSchemaNode> childNodes = grp.getChildNodes();
102 assertEquals(1, childNodes.size());
103 DataSchemaNode child = childNodes.iterator().next();
105 assertTrue(child instanceof LeafSchemaNode);
106 LeafSchemaNode leaf = (LeafSchemaNode) child;
108 assertNotNull(leaf.getType());
112 public void mockTest() throws ReactorException, FileNotFoundException, URISyntaxException {
113 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild().addSource(YANG_EXT).buildEffective();
114 assertNotNull(result);