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.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 public class RpcStmtTest {
36 public void rpcTest() throws ReactorException {
37 final EffectiveModelContext result = RFC7950Reactors.defaultReactor().newBuild()
38 .addSource(sourceForResource("/model/baz.yang"))
39 .addSource(sourceForResource("/model/bar.yang"))
40 .addSource(sourceForResource("/rpc-stmt-test/foo.yang"))
42 assertNotNull(result);
44 final Module testModule = result.findModules("baz").iterator().next();
45 assertEquals(1, testModule.getRpcs().size());
47 final RpcDefinition rpc = testModule.getRpcs().iterator().next();
48 assertEquals("get-config", rpc.getQName().getLocalName());
50 final InputSchemaNode input = rpc.getInput();
52 assertEquals(2, input.getChildNodes().size());
54 final ContainerSchemaNode container = (ContainerSchemaNode) input.getDataChildByName(
55 QName.create(testModule.getQNameModule(), "source"));
56 assertNotNull(container);
57 AnyxmlSchemaNode anyXml = (AnyxmlSchemaNode) input.getDataChildByName(
58 QName.create(testModule.getQNameModule(), "filter"));
59 assertNotNull(anyXml);
61 final OutputSchemaNode output = rpc.getOutput();
62 assertNotNull(output);
63 assertEquals(1, output.getChildNodes().size());
65 anyXml = (AnyxmlSchemaNode) output.getDataChildByName(QName.create(testModule.getQNameModule(), "data"));
66 assertNotNull(anyXml);
68 final Module fooModule = result.findModule("foo", Revision.of("2016-09-23")).get();
69 final Collection<? extends RpcDefinition> rpcs = fooModule.getRpcs();
70 assertEquals(2, rpcs.size());
72 RpcDefinition fooRpc1 = null;
73 RpcDefinition fooRpc2 = null;
75 for (RpcDefinition rpcDefinition : rpcs) {
76 if ("foo-rpc-1".equals(rpcDefinition.getQName().getLocalName())) {
77 fooRpc1 = rpcDefinition;
78 } else if ("foo-rpc-2".equals(rpcDefinition.getQName().getLocalName())) {
79 fooRpc2 = rpcDefinition;
83 assertFalse(fooRpc1.equals(null));
84 assertFalse(fooRpc1.equals("str"));
85 assertFalse(fooRpc1.equals(fooRpc2));
87 assertNotEquals(fooRpc1.getInput().hashCode(), fooRpc2.getInput().hashCode());
88 assertNotEquals(fooRpc1.getOutput().hashCode(), fooRpc2.getOutput().hashCode());
90 assertTrue(fooRpc1.getInput().equals(fooRpc1.getInput()));
91 assertFalse(fooRpc1.getInput().equals(null));
92 assertFalse(fooRpc1.getInput().equals("str"));
93 assertFalse(fooRpc1.getInput().equals(fooRpc2.getInput()));
95 assertTrue(fooRpc1.getOutput().equals(fooRpc1.getOutput()));
96 assertFalse(fooRpc1.getOutput().equals(null));
97 assertFalse(fooRpc1.getOutput().equals("str"));
98 assertFalse(fooRpc1.getOutput().equals(fooRpc2.getOutput()));
102 public void testImplicitInputAndOutput() throws Exception {
103 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rpc-stmt-test/bar.yang");
104 assertNotNull(schemaContext);
106 final Module barModule = schemaContext.findModule("bar", Revision.of("2016-11-25")).get();
107 final Collection<? extends RpcDefinition> rpcs = barModule.getRpcs();
108 assertEquals(1, rpcs.size());
110 final RpcDefinition barRpc = rpcs.iterator().next();
112 final InputSchemaNode input = barRpc.getInput();
113 assertNotNull(input);
114 assertEquals(2, input.getChildNodes().size());
115 assertEquals(StatementSource.CONTEXT, ((EffectiveStatement<?, ?>) input).getStatementSource());
117 final OutputSchemaNode output = barRpc.getOutput();
118 assertNotNull(output);
119 assertEquals(2, output.getChildNodes().size());
120 assertEquals(StatementSource.CONTEXT, ((EffectiveStatement<?, ?>) output).getStatementSource());