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;
17 import java.net.URISyntaxException;
18 import java.util.Collection;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
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.model.api.SchemaPath;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
33 public class EffectiveBuildTest {
35 private static final StatementStreamSource SIMPLE_MODULE = sourceForResource(
36 "/stmt-test/effective-build/simple-module.yang");
37 private static final QNameModule SIMPLE_MODULE_QNAME = QNameModule.create(URI.create("simple.yang"));
38 private static final StatementStreamSource YANG_EXT = sourceForResource(
39 "/stmt-test/extensions/yang-ext.yang");
42 public void effectiveBuildTest() throws ReactorException {
43 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild().addSources(SIMPLE_MODULE)
46 assertNotNull(result);
48 Module simpleModule = result.findModules("simple-module").iterator().next();
49 assertNotNull(simpleModule);
51 final QName q1 = QName.create(SIMPLE_MODULE_QNAME, "root-container");
52 final QName q2 = QName.create(SIMPLE_MODULE_QNAME, "sub-container");
53 final QName q3 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container");
54 final QName q4 = QName.create(SIMPLE_MODULE_QNAME, "root-container2");
55 final QName q5 = QName.create(SIMPLE_MODULE_QNAME, "sub-container2");
56 final QName q6 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container2");
57 final QName q7 = QName.create(SIMPLE_MODULE_QNAME, "grp");
59 ContainerSchemaNode rootCon = (ContainerSchemaNode) simpleModule.getDataChildByName(q1);
60 assertNotNull(rootCon);
62 ContainerSchemaNode subCon = (ContainerSchemaNode) rootCon.getDataChildByName(q2);
63 assertNotNull(subCon);
65 ContainerSchemaNode subSubCon = (ContainerSchemaNode) subCon.getDataChildByName(q3);
66 assertNotNull(subSubCon);
68 ContainerSchemaNode rootCon2 = (ContainerSchemaNode) simpleModule.getDataChildByName(q4);
69 assertNotNull(rootCon2);
71 ContainerSchemaNode subCon2 = (ContainerSchemaNode) rootCon2.getDataChildByName(q5);
72 assertNotNull(subCon2);
74 ContainerSchemaNode subSubCon2 = (ContainerSchemaNode) subCon2.getDataChildByName(q6);
75 assertNotNull(subSubCon2);
77 GroupingDefinition grp = simpleModule.getGroupings().iterator().next();
79 assertEquals(q7, grp.getQName());
81 ContainerSchemaNode grpSubCon2 = (ContainerSchemaNode) grp.getDataChildByName(q5);
82 assertNotNull(grpSubCon2);
84 ContainerSchemaNode grpSubSubCon2 = (ContainerSchemaNode) grpSubCon2.getDataChildByName(q6);
85 assertNotNull(grpSubSubCon2);
87 assertEquals(SchemaPath.create(true, q1, q2, q3), subSubCon.getPath());
88 assertEquals(SchemaPath.create(true, q4, q5, q6), subSubCon2.getPath());
89 assertEquals(SchemaPath.create(true, q7, q5, q6), grpSubSubCon2.getPath());
93 public void extensionsTest() throws ReactorException {
94 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild().addSource(YANG_EXT).buildEffective();
95 assertNotNull(result);
97 Collection<? extends GroupingDefinition> groupings = result.getGroupings();
98 assertEquals(1, groupings.size());
100 GroupingDefinition grp = groupings.iterator().next();
102 Collection<? extends DataSchemaNode> childNodes = grp.getChildNodes();
103 assertEquals(1, childNodes.size());
104 DataSchemaNode child = childNodes.iterator().next();
106 assertTrue(child instanceof LeafSchemaNode);
107 LeafSchemaNode leaf = (LeafSchemaNode) child;
109 assertNotNull(leaf.getType());
113 public void mockTest() throws ReactorException, FileNotFoundException, URISyntaxException {
114 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild().addSource(YANG_EXT).buildEffective();
115 assertNotNull(result);