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.assertFalse;
13 import static org.junit.Assert.assertNotEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.Revision;
22 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
31 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
34 public class RpcStmtTest {
36 private static final StatementStreamSource RPC_MODULE = sourceForResource("/model/baz.yang");
37 private static final StatementStreamSource IMPORTED_MODULE = sourceForResource("/model/bar.yang");
38 private static final StatementStreamSource FOO_MODULE = sourceForResource("/rpc-stmt-test/foo.yang");
41 public void rpcTest() throws ReactorException {
42 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
43 reactor.addSources(RPC_MODULE, IMPORTED_MODULE, FOO_MODULE);
45 final SchemaContext result = reactor.buildEffective();
46 assertNotNull(result);
48 final Module testModule = result.findModules("baz").iterator().next();
49 assertEquals(1, testModule.getRpcs().size());
51 final RpcDefinition rpc = testModule.getRpcs().iterator().next();
52 assertEquals("get-config", rpc.getQName().getLocalName());
54 final ContainerSchemaNode input = rpc.getInput();
56 assertEquals(2, input.getChildNodes().size());
58 final ContainerSchemaNode container = (ContainerSchemaNode) input.getDataChildByName(
59 QName.create(testModule.getQNameModule(), "source"));
60 assertNotNull(container);
61 AnyXmlSchemaNode anyXml = (AnyXmlSchemaNode) input.getDataChildByName(
62 QName.create(testModule.getQNameModule(), "filter"));
63 assertNotNull(anyXml);
65 final ContainerSchemaNode output = rpc.getOutput();
66 assertNotNull(output);
67 assertEquals(1, output.getChildNodes().size());
69 anyXml = (AnyXmlSchemaNode) output.getDataChildByName(QName.create(testModule.getQNameModule(), "data"));
70 assertNotNull(anyXml);
72 final Module fooModule = result.findModule("foo", Revision.valueOf("2016-09-23")).get();
73 final Set<RpcDefinition> rpcs = fooModule.getRpcs();
74 assertEquals(2, rpcs.size());
76 RpcDefinition fooRpc1 = null;
77 RpcDefinition fooRpc2 = null;
79 for (RpcDefinition rpcDefinition : rpcs) {
80 if ("foo-rpc-1".equals(rpcDefinition.getQName().getLocalName())) {
81 fooRpc1 = rpcDefinition;
82 } else if ("foo-rpc-2".equals(rpcDefinition.getQName().getLocalName())) {
83 fooRpc2 = rpcDefinition;
87 assertFalse(fooRpc1.equals(null));
88 assertFalse(fooRpc1.equals("str"));
89 assertFalse(fooRpc1.equals(fooRpc2));
91 assertNotEquals(fooRpc1.getInput().hashCode(), fooRpc2.getInput().hashCode());
92 assertNotEquals(fooRpc1.getOutput().hashCode(), fooRpc2.getOutput().hashCode());
94 assertTrue(fooRpc1.getInput().equals(fooRpc1.getInput()));
95 assertFalse(fooRpc1.getInput().equals(null));
96 assertFalse(fooRpc1.getInput().equals("str"));
97 assertFalse(fooRpc1.getInput().equals(fooRpc2.getInput()));
99 assertTrue(fooRpc1.getOutput().equals(fooRpc1.getOutput()));
100 assertFalse(fooRpc1.getOutput().equals(null));
101 assertFalse(fooRpc1.getOutput().equals("str"));
102 assertFalse(fooRpc1.getOutput().equals(fooRpc2.getOutput()));
106 public void testImplicitInputAndOutput() throws Exception {
107 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rpc-stmt-test/bar.yang");
108 assertNotNull(schemaContext);
110 final Module barModule = schemaContext.findModule("bar", Revision.valueOf("2016-11-25")).get();
111 final Set<RpcDefinition> rpcs = barModule.getRpcs();
112 assertEquals(1, rpcs.size());
114 final RpcDefinition barRpc = rpcs.iterator().next();
116 final ContainerSchemaNode input = barRpc.getInput();
117 assertNotNull(input);
118 assertEquals(2, input.getChildNodes().size());
119 assertEquals(StatementSource.CONTEXT, ((EffectiveStatement<?, ?>) input).getDeclared().getStatementSource());
121 final ContainerSchemaNode output = barRpc.getOutput();
122 assertNotNull(output);
123 assertEquals(2, output.getChildNodes().size());
124 assertEquals(StatementSource.CONTEXT, ((EffectiveStatement<?, ?>) output).getDeclared().getStatementSource());