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.common.SimpleDateFormatUtil;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
37 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
39 public class EffectiveBuildTest {
41 private static final StatementStreamSource SIMPLE_MODULE = sourceForResource(
42 "/stmt-test/effective-build/simple-module.yang");
43 private static final QNameModule SIMPLE_MODULE_QNAME = QNameModule.create(
44 URI.create("simple.yang"), SimpleDateFormatUtil.DEFAULT_DATE_REV);
45 private static final StatementStreamSource YANG_EXT = sourceForResource(
46 "/stmt-test/extensions/yang-ext.yang");
49 public void effectiveBuildTest() throws SourceException, ReactorException {
50 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
51 reactor.addSources(SIMPLE_MODULE);
52 EffectiveSchemaContext result = reactor.buildEffective();
54 assertNotNull(result);
56 Module simpleModule = result.findModuleByName("simple-module", null);
57 assertNotNull(simpleModule);
59 QName q1 = QName.create(SIMPLE_MODULE_QNAME, "root-container");
60 QName q2 = QName.create(SIMPLE_MODULE_QNAME, "sub-container");
61 QName q3 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container");
62 QName q4 = QName.create(SIMPLE_MODULE_QNAME, "root-container2");
63 QName q5 = QName.create(SIMPLE_MODULE_QNAME, "sub-container2");
64 QName q6 = QName.create(SIMPLE_MODULE_QNAME, "sub-sub-container2");
65 QName q7 = QName.create(SIMPLE_MODULE_QNAME, "grp");
67 ContainerSchemaNode rootCon = (ContainerSchemaNode) simpleModule
68 .getDataChildByName(q1);
69 assertNotNull(rootCon);
71 ContainerSchemaNode subCon = (ContainerSchemaNode) rootCon
72 .getDataChildByName(q2);
73 assertNotNull(subCon);
75 ContainerSchemaNode subSubCon = (ContainerSchemaNode) subCon
76 .getDataChildByName(q3);
77 assertNotNull(subSubCon);
79 ContainerSchemaNode rootCon2 = (ContainerSchemaNode) simpleModule
80 .getDataChildByName(q4);
81 assertNotNull(rootCon2);
83 ContainerSchemaNode subCon2 = (ContainerSchemaNode) rootCon2
84 .getDataChildByName(q5);
85 assertNotNull(subCon2);
87 ContainerSchemaNode subSubCon2 = (ContainerSchemaNode) subCon2
88 .getDataChildByName(q6);
89 assertNotNull(subSubCon2);
91 GroupingDefinition grp = simpleModule.getGroupings().iterator().next();
93 assertEquals(q7, grp.getQName());
95 ContainerSchemaNode grpSubCon2 = (ContainerSchemaNode) grp
96 .getDataChildByName(q5);
97 assertNotNull(grpSubCon2);
99 ContainerSchemaNode grpSubSubCon2 = (ContainerSchemaNode) grpSubCon2
100 .getDataChildByName(q6);
101 assertNotNull(grpSubSubCon2);
103 assertEquals(SchemaPath.create(true, q1, q2, q3), subSubCon.getPath());
104 assertEquals(SchemaPath.create(true, q4, q5, q6), subSubCon2.getPath());
105 assertEquals(SchemaPath.create(true, q7, q5, q6),
106 grpSubSubCon2.getPath());
111 public void extensionsTest() throws SourceException, ReactorException {
112 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
113 reactor.addSources(YANG_EXT);
114 EffectiveSchemaContext result = reactor.buildEffective();
115 assertNotNull(result);
117 Set<GroupingDefinition> groupings = result.getGroupings();
118 assertEquals(1, groupings.size());
120 GroupingDefinition grp = groupings.iterator().next();
122 Collection<DataSchemaNode> childNodes = grp.getChildNodes();
123 assertEquals(1, childNodes.size());
124 DataSchemaNode child = childNodes.iterator().next();
126 assertTrue(child instanceof LeafSchemaNode);
127 LeafSchemaNode leaf = (LeafSchemaNode) child;
129 assertNotNull(leaf.getType());
133 public void mockTest() throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
134 BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
135 reactor.addSource(YANG_EXT);
137 SchemaContext result = reactor.buildEffective();
138 assertNotNull(result);