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.assertPathEquals;
14 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
16 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.common.XMLNamespace;
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
34 public class EffectiveBuildTest {
36 private static final StatementStreamSource SIMPLE_MODULE = sourceForResource(
37 "/stmt-test/effective-build/simple-module.yang");
38 private static final QNameModule SIMPLE_MODULE_QNAME = QNameModule.create(XMLNamespace.of("simple.yang"));
39 private static final StatementStreamSource YANG_EXT = sourceForResource(
40 "/stmt-test/extensions/yang-ext.yang");
43 public void effectiveBuildTest() throws ReactorException {
44 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild().addSources(SIMPLE_MODULE)
47 assertNotNull(result);
49 Module simpleModule = result.findModules("simple-module").iterator().next();
50 assertNotNull(simpleModule);
52 final QName q1 = QName.create(SIMPLE_MODULE_QNAME, "root-container");
53 final QName q2 = QName.create(SIMPLE_MODULE_QNAME, "sub-container");
54 final QName q3 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container");
55 final QName q4 = QName.create(SIMPLE_MODULE_QNAME, "root-container2");
56 final QName q5 = QName.create(SIMPLE_MODULE_QNAME, "sub-container2");
57 final QName q6 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container2");
58 final QName q7 = QName.create(SIMPLE_MODULE_QNAME, "grp");
60 ContainerSchemaNode rootCon = (ContainerSchemaNode) simpleModule.getDataChildByName(q1);
61 assertNotNull(rootCon);
63 ContainerSchemaNode subCon = (ContainerSchemaNode) rootCon.getDataChildByName(q2);
64 assertNotNull(subCon);
66 ContainerSchemaNode subSubCon = (ContainerSchemaNode) subCon.getDataChildByName(q3);
67 assertNotNull(subSubCon);
69 ContainerSchemaNode rootCon2 = (ContainerSchemaNode) simpleModule.getDataChildByName(q4);
70 assertNotNull(rootCon2);
72 ContainerSchemaNode subCon2 = (ContainerSchemaNode) rootCon2.getDataChildByName(q5);
73 assertNotNull(subCon2);
75 ContainerSchemaNode subSubCon2 = (ContainerSchemaNode) subCon2.getDataChildByName(q6);
76 assertNotNull(subSubCon2);
78 GroupingDefinition grp = simpleModule.getGroupings().iterator().next();
80 assertEquals(q7, grp.getQName());
82 ContainerSchemaNode grpSubCon2 = (ContainerSchemaNode) grp.getDataChildByName(q5);
83 assertNotNull(grpSubCon2);
85 ContainerSchemaNode grpSubSubCon2 = (ContainerSchemaNode) grpSubCon2.getDataChildByName(q6);
86 assertNotNull(grpSubSubCon2);
88 assertPathEquals(SchemaPath.create(true, q1, q2, q3), subSubCon);
89 assertPathEquals(SchemaPath.create(true, q4, q5, q6), subSubCon2);
90 assertPathEquals(SchemaPath.create(true, q7, q5, q6), grpSubSubCon2);
94 public void extensionsTest() throws ReactorException {
95 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild().addSource(YANG_EXT).buildEffective();
96 assertNotNull(result);
98 Collection<? extends GroupingDefinition> groupings = result.getGroupings();
99 assertEquals(1, groupings.size());
101 GroupingDefinition grp = groupings.iterator().next();
103 Collection<? extends DataSchemaNode> childNodes = grp.getChildNodes();
104 assertEquals(1, childNodes.size());
105 DataSchemaNode child = childNodes.iterator().next();
107 assertTrue(child instanceof LeafSchemaNode);
108 LeafSchemaNode leaf = (LeafSchemaNode) child;
110 assertNotNull(leaf.getType());
114 public void mockTest() throws ReactorException, FileNotFoundException, URISyntaxException {
115 SchemaContext result = RFC7950Reactors.defaultReactor().newBuild().addSource(YANG_EXT).buildEffective();
116 assertNotNull(result);