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.model.api.AnyXmlSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
33 public class RpcStmtTest {
35 private static final StatementStreamSource RPC_MODULE = sourceForResource("/model/baz.yang");
36 private static final StatementStreamSource IMPORTED_MODULE = sourceForResource("/model/bar.yang");
37 private static final StatementStreamSource FOO_MODULE = sourceForResource("/rpc-stmt-test/foo.yang");
40 public void rpcTest() throws ReactorException {
41 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
42 reactor.addSources(RPC_MODULE, IMPORTED_MODULE, FOO_MODULE);
44 final SchemaContext result = reactor.buildEffective();
45 assertNotNull(result);
47 final Module testModule = result.findModules("baz").iterator().next();
48 assertEquals(1, testModule.getRpcs().size());
50 final RpcDefinition rpc = testModule.getRpcs().iterator().next();
51 assertEquals("get-config", rpc.getQName().getLocalName());
53 final ContainerSchemaNode input = rpc.getInput();
55 assertEquals(2, input.getChildNodes().size());
57 final ContainerSchemaNode container = (ContainerSchemaNode) input.getDataChildByName(
58 QName.create(testModule.getQNameModule(), "source"));
59 assertNotNull(container);
60 AnyXmlSchemaNode anyXml = (AnyXmlSchemaNode) input.getDataChildByName(
61 QName.create(testModule.getQNameModule(), "filter"));
62 assertNotNull(anyXml);
64 final ContainerSchemaNode output = rpc.getOutput();
65 assertNotNull(output);
66 assertEquals(1, output.getChildNodes().size());
68 anyXml = (AnyXmlSchemaNode) output.getDataChildByName(QName.create(testModule.getQNameModule(), "data"));
69 assertNotNull(anyXml);
71 final Module fooModule = result.findModule("foo", QName.parseRevision("2016-09-23")).get();
72 final Set<RpcDefinition> rpcs = fooModule.getRpcs();
73 assertEquals(2, rpcs.size());
75 RpcDefinition fooRpc1 = null;
76 RpcDefinition fooRpc2 = null;
78 for (RpcDefinition rpcDefinition : rpcs) {
79 if ("foo-rpc-1".equals(rpcDefinition.getQName().getLocalName())) {
80 fooRpc1 = rpcDefinition;
81 } else if ("foo-rpc-2".equals(rpcDefinition.getQName().getLocalName())) {
82 fooRpc2 = rpcDefinition;
86 assertFalse(fooRpc1.equals(null));
87 assertFalse(fooRpc1.equals("str"));
88 assertFalse(fooRpc1.equals(fooRpc2));
90 assertNotEquals(fooRpc1.getInput().hashCode(), fooRpc2.getInput().hashCode());
91 assertNotEquals(fooRpc1.getOutput().hashCode(), fooRpc2.getOutput().hashCode());
93 assertTrue(fooRpc1.getInput().equals(fooRpc1.getInput()));
94 assertFalse(fooRpc1.getInput().equals(null));
95 assertFalse(fooRpc1.getInput().equals("str"));
96 assertFalse(fooRpc1.getInput().equals(fooRpc2.getInput()));
98 assertTrue(fooRpc1.getOutput().equals(fooRpc1.getOutput()));
99 assertFalse(fooRpc1.getOutput().equals(null));
100 assertFalse(fooRpc1.getOutput().equals("str"));
101 assertFalse(fooRpc1.getOutput().equals(fooRpc2.getOutput()));
105 public void testImplicitInputAndOutput() throws Exception {
106 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rpc-stmt-test/bar.yang");
107 assertNotNull(schemaContext);
109 final Module barModule = schemaContext.findModule("bar", QName.parseRevision("2016-11-25")).get();
110 final Set<RpcDefinition> rpcs = barModule.getRpcs();
111 assertEquals(1, rpcs.size());
113 final RpcDefinition barRpc = rpcs.iterator().next();
115 final ContainerSchemaNode input = barRpc.getInput();
116 assertNotNull(input);
117 assertEquals(2, input.getChildNodes().size());
118 assertEquals(StatementSource.CONTEXT, ((EffectiveStatement<?, ?>) input).getDeclared().getStatementSource());
120 final ContainerSchemaNode output = barRpc.getOutput();
121 assertNotNull(output);
122 assertEquals(2, output.getChildNodes().size());
123 assertEquals(StatementSource.CONTEXT, ((EffectiveStatement<?, ?>) output).getDeclared().getStatementSource());