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 java.io.FileNotFoundException;
16 import java.net.URISyntaxException;
17 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.SimpleDateFormatUtil;
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.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
37 public class EffectiveBuildTest {
39 private static final YangStatementSourceImpl SIMPLE_MODULE = new YangStatementSourceImpl(
40 "/stmt-test/effective-build/simple-module.yang", false);
41 private static final QNameModule SIMPLE_MODULE_QNAME = QNameModule.create(
42 URI.create("simple.yang"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
43 private static final YangStatementSourceImpl YANG_EXT = new YangStatementSourceImpl(
44 "/stmt-test/extensions/yang-ext.yang", false);
47 public void effectiveBuildTest() throws SourceException, ReactorException {
48 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
49 addSources(reactor, SIMPLE_MODULE);
50 EffectiveSchemaContext result = reactor.buildEffective();
52 assertNotNull(result);
54 Module simpleModule = result.findModuleByName("simple-module", null);
55 assertNotNull(simpleModule);
57 QName q1 = QName.create(SIMPLE_MODULE_QNAME, "root-container");
58 QName q2 = QName.create(SIMPLE_MODULE_QNAME, "sub-container");
59 QName q3 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container");
60 QName q4 = QName.create(SIMPLE_MODULE_QNAME, "root-container2");
61 QName q5 = QName.create(SIMPLE_MODULE_QNAME, "sub-container2");
62 QName q6 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container2");
63 QName q7 = QName.create(SIMPLE_MODULE_QNAME, "grp");
65 ContainerSchemaNode rootCon = (ContainerSchemaNode) simpleModule
66 .getDataChildByName(q1);
67 assertNotNull(rootCon);
69 ContainerSchemaNode subCon = (ContainerSchemaNode) rootCon
70 .getDataChildByName(q2);
71 assertNotNull(subCon);
73 ContainerSchemaNode subSubCon = (ContainerSchemaNode) subCon
74 .getDataChildByName(q3);
75 assertNotNull(subSubCon);
77 ContainerSchemaNode rootCon2 = (ContainerSchemaNode) simpleModule
78 .getDataChildByName(q4);
79 assertNotNull(rootCon2);
81 ContainerSchemaNode subCon2 = (ContainerSchemaNode) rootCon2
82 .getDataChildByName(q5);
83 assertNotNull(subCon2);
85 ContainerSchemaNode subSubCon2 = (ContainerSchemaNode) subCon2
86 .getDataChildByName(q6);
87 assertNotNull(subSubCon2);
89 GroupingDefinition grp = simpleModule.getGroupings().iterator().next();
91 assertEquals(q7, grp.getQName());
93 ContainerSchemaNode grpSubCon2 = (ContainerSchemaNode) grp
94 .getDataChildByName(q5);
95 assertNotNull(grpSubCon2);
97 ContainerSchemaNode grpSubSubCon2 = (ContainerSchemaNode) grpSubCon2
98 .getDataChildByName(q6);
99 assertNotNull(grpSubSubCon2);
101 assertEquals(SchemaPath.create(true, q1, q2, q3), subSubCon.getPath());
102 assertEquals(SchemaPath.create(true, q4, q5, q6), subSubCon2.getPath());
103 assertEquals(SchemaPath.create(true, q7, q5, q6),
104 grpSubSubCon2.getPath());
109 public void extensionsTest() throws SourceException, ReactorException {
110 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
111 addSources(reactor, YANG_EXT);
112 EffectiveSchemaContext result = reactor.buildEffective();
113 assertNotNull(result);
115 Set<GroupingDefinition> groupings = result.getGroupings();
116 assertEquals(1, groupings.size());
118 GroupingDefinition grp = groupings.iterator().next();
120 Collection<DataSchemaNode> childNodes = grp.getChildNodes();
121 assertEquals(1, childNodes.size());
122 DataSchemaNode child = childNodes.iterator().next();
124 assertTrue(child instanceof LeafSchemaNode);
125 LeafSchemaNode leaf = (LeafSchemaNode) child;
127 assertNotNull(leaf.getType());
131 public void mockTest() throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
132 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
133 reactor.addSource(YANG_EXT);
135 SchemaContext result = reactor.buildEffective();
136 assertNotNull(result);
139 private static void addSources(final BuildAction reactor, final YangStatementSourceImpl... sources) {
140 for (YangStatementSourceImpl source : sources) {
141 reactor.addSource(source);