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
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
17 import java.util.Collection;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.Revision;
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.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.InputSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.OutputSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.StatementOrigin;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
33 public class RpcStmtTest extends AbstractYangTest {
35 public void rpcTest() throws ReactorException {
36 final EffectiveModelContext result = RFC7950Reactors.defaultReactor().newBuild()
37 .addSource(sourceForResource("/model/baz.yang"))
38 .addSource(sourceForResource("/model/bar.yang"))
39 .addSource(sourceForResource("/rpc-stmt-test/foo.yang"))
41 assertNotNull(result);
43 final Module testModule = result.findModules("baz").iterator().next();
44 assertEquals(1, testModule.getRpcs().size());
46 final RpcDefinition rpc = testModule.getRpcs().iterator().next();
47 assertEquals("get-config", rpc.getQName().getLocalName());
49 final InputSchemaNode input = rpc.getInput();
51 assertEquals(2, input.getChildNodes().size());
53 final ContainerSchemaNode container = (ContainerSchemaNode) input.getDataChildByName(
54 QName.create(testModule.getQNameModule(), "source"));
55 assertNotNull(container);
56 AnyxmlSchemaNode anyXml = (AnyxmlSchemaNode) input.getDataChildByName(
57 QName.create(testModule.getQNameModule(), "filter"));
58 assertNotNull(anyXml);
60 final OutputSchemaNode output = rpc.getOutput();
61 assertNotNull(output);
62 assertEquals(1, output.getChildNodes().size());
64 anyXml = (AnyxmlSchemaNode) output.getDataChildByName(QName.create(testModule.getQNameModule(), "data"));
65 assertNotNull(anyXml);
67 final Module fooModule = result.findModule("foo", Revision.of("2016-09-23")).get();
68 final Collection<? extends RpcDefinition> rpcs = fooModule.getRpcs();
69 assertEquals(2, rpcs.size());
71 RpcDefinition fooRpc1 = null;
72 RpcDefinition fooRpc2 = null;
74 for (RpcDefinition rpcDefinition : rpcs) {
75 if ("foo-rpc-1".equals(rpcDefinition.getQName().getLocalName())) {
76 fooRpc1 = rpcDefinition;
77 } else if ("foo-rpc-2".equals(rpcDefinition.getQName().getLocalName())) {
78 fooRpc2 = rpcDefinition;
82 assertFalse(fooRpc1.equals(null));
83 assertFalse(fooRpc1.equals("str"));
84 assertFalse(fooRpc1.equals(fooRpc2));
86 assertNotEquals(fooRpc1.getInput().hashCode(), fooRpc2.getInput().hashCode());
87 assertNotEquals(fooRpc1.getOutput().hashCode(), fooRpc2.getOutput().hashCode());
89 assertTrue(fooRpc1.getInput().equals(fooRpc1.getInput()));
90 assertFalse(fooRpc1.getInput().equals(null));
91 assertFalse(fooRpc1.getInput().equals("str"));
92 assertFalse(fooRpc1.getInput().equals(fooRpc2.getInput()));
94 assertTrue(fooRpc1.getOutput().equals(fooRpc1.getOutput()));
95 assertFalse(fooRpc1.getOutput().equals(null));
96 assertFalse(fooRpc1.getOutput().equals("str"));
97 assertFalse(fooRpc1.getOutput().equals(fooRpc2.getOutput()));
101 public void testImplicitInputAndOutput() {
102 final var context = assertEffectiveModel("/rpc-stmt-test/bar.yang");
104 final Module barModule = context.findModule("bar", Revision.of("2016-11-25")).get();
105 final Collection<? extends RpcDefinition> rpcs = barModule.getRpcs();
106 assertEquals(1, rpcs.size());
108 final RpcDefinition barRpc = rpcs.iterator().next();
110 final InputSchemaNode input = barRpc.getInput();
111 assertNotNull(input);
112 assertEquals(2, input.getChildNodes().size());
113 assertEquals(StatementOrigin.CONTEXT, ((EffectiveStatement<?, ?>) input).statementOrigin());
115 final OutputSchemaNode output = barRpc.getOutput();
116 assertNotNull(output);
117 assertEquals(2, output.getChildNodes().size());
118 assertEquals(StatementOrigin.CONTEXT, ((EffectiveStatement<?, ?>) output).statementOrigin());