2 * Copyright (c) 2015 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.test;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
22 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
27 public class ExtensionStmtTest {
29 private static final YangStatementSourceImpl EXT_DEF_MODULE = new YangStatementSourceImpl("/model/bar.yang",
31 private static final YangStatementSourceImpl EXT_DEF_MODULE2 = new YangStatementSourceImpl
32 ("/semantic-statement-parser/ext-typedef.yang",
34 private static final YangStatementSourceImpl EXT_USE_MODULE = new YangStatementSourceImpl
35 ("/semantic-statement-parser/ext-use.yang",
39 public void testExtensionDefinition() throws ReactorException {
40 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
41 StmtTestUtils.addSources(reactor, EXT_DEF_MODULE);
43 EffectiveSchemaContext result = reactor.buildEffective();
44 assertNotNull(result);
46 Module testModule = result.findModuleByName("bar", null);
47 assertNotNull(testModule);
49 assertEquals(1, testModule.getExtensionSchemaNodes().size());
51 List<ExtensionDefinition> extensions = testModule.getExtensionSchemaNodes();
52 ExtensionDefinition extension = extensions.get(0);
53 assertEquals("opendaylight", extension.getQName().getLocalName());
54 assertEquals("name", extension.getArgument());
55 assertTrue(extension.isYinElement());
59 public void testExtensionUsage() throws ReactorException {
60 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
61 StmtTestUtils.addSources(reactor, EXT_DEF_MODULE2, EXT_USE_MODULE);
63 EffectiveSchemaContext result = reactor.buildEffective();
64 assertNotNull(result);
66 Module testModule1 = result.findModuleByName("ext-typedef", null);
67 assertNotNull(testModule1);
69 assertEquals(1, testModule1.getExtensionSchemaNodes().size());
71 List<ExtensionDefinition> extensions = testModule1.getExtensionSchemaNodes();
72 ExtensionDefinition extensionDefinition = extensions.get(0);
74 Module testModule2 = result.findModuleByName("ext-use", null);
75 assertNotNull(testModule2);
77 LeafSchemaNode leaf = (LeafSchemaNode) testModule2.getDataChildByName("value");
80 assertEquals(1, leaf.getUnknownSchemaNodes().size());
81 List<UnknownSchemaNode> unknownNodes = leaf.getUnknownSchemaNodes();
82 UnknownSchemaNode extensionUse = unknownNodes.get(0);
83 assertEquals(extensionDefinition.getQName().getLocalName(), extensionUse.getExtensionDefinition().getQName()
85 assertEquals(extensionDefinition.getArgument(), extensionUse.getExtensionDefinition().getArgument());
87 assertEquals("key:value", extensionUse.getNodeParameter());