Use correct key-arg splitting
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / RpcStmtTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.stmt;
10
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;
17
18 import java.util.Collection;
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.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;
33
34 public class RpcStmtTest {
35
36     @Test
37     public void rpcTest() throws ReactorException {
38         final SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
39                 .addSource(sourceForResource("/model/baz.yang"))
40                 .addSource(sourceForResource("/model/bar.yang"))
41                 .addSource(sourceForResource("/rpc-stmt-test/foo.yang"))
42                 .buildEffective();
43         assertNotNull(result);
44
45         final Module testModule = result.findModules("baz").iterator().next();
46         assertEquals(1, testModule.getRpcs().size());
47
48         final RpcDefinition rpc = testModule.getRpcs().iterator().next();
49         assertEquals("get-config", rpc.getQName().getLocalName());
50
51         final InputSchemaNode input = rpc.getInput();
52         assertNotNull(input);
53         assertEquals(2, input.getChildNodes().size());
54
55         final ContainerSchemaNode container = (ContainerSchemaNode) input.getDataChildByName(
56             QName.create(testModule.getQNameModule(), "source"));
57         assertNotNull(container);
58         AnyxmlSchemaNode anyXml = (AnyxmlSchemaNode) input.getDataChildByName(
59             QName.create(testModule.getQNameModule(), "filter"));
60         assertNotNull(anyXml);
61
62         final OutputSchemaNode output = rpc.getOutput();
63         assertNotNull(output);
64         assertEquals(1, output.getChildNodes().size());
65
66         anyXml = (AnyxmlSchemaNode) output.getDataChildByName(QName.create(testModule.getQNameModule(), "data"));
67         assertNotNull(anyXml);
68
69         final Module fooModule = result.findModule("foo", Revision.of("2016-09-23")).get();
70         final Collection<? extends RpcDefinition> rpcs = fooModule.getRpcs();
71         assertEquals(2, rpcs.size());
72
73         RpcDefinition fooRpc1 = null;
74         RpcDefinition fooRpc2 = null;
75
76         for (RpcDefinition rpcDefinition : rpcs) {
77             if ("foo-rpc-1".equals(rpcDefinition.getQName().getLocalName())) {
78                 fooRpc1 = rpcDefinition;
79             } else if ("foo-rpc-2".equals(rpcDefinition.getQName().getLocalName())) {
80                 fooRpc2 = rpcDefinition;
81             }
82         }
83
84         assertFalse(fooRpc1.equals(null));
85         assertFalse(fooRpc1.equals("str"));
86         assertFalse(fooRpc1.equals(fooRpc2));
87
88         assertNotEquals(fooRpc1.getInput().hashCode(), fooRpc2.getInput().hashCode());
89         assertNotEquals(fooRpc1.getOutput().hashCode(), fooRpc2.getOutput().hashCode());
90
91         assertTrue(fooRpc1.getInput().equals(fooRpc1.getInput()));
92         assertFalse(fooRpc1.getInput().equals(null));
93         assertFalse(fooRpc1.getInput().equals("str"));
94         assertFalse(fooRpc1.getInput().equals(fooRpc2.getInput()));
95
96         assertTrue(fooRpc1.getOutput().equals(fooRpc1.getOutput()));
97         assertFalse(fooRpc1.getOutput().equals(null));
98         assertFalse(fooRpc1.getOutput().equals("str"));
99         assertFalse(fooRpc1.getOutput().equals(fooRpc2.getOutput()));
100     }
101
102     @Test
103     public void testImplicitInputAndOutput() throws Exception {
104         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rpc-stmt-test/bar.yang");
105         assertNotNull(schemaContext);
106
107         final Module barModule = schemaContext.findModule("bar", Revision.of("2016-11-25")).get();
108         final Collection<? extends RpcDefinition> rpcs = barModule.getRpcs();
109         assertEquals(1, rpcs.size());
110
111         final RpcDefinition barRpc = rpcs.iterator().next();
112
113         final InputSchemaNode input = barRpc.getInput();
114         assertNotNull(input);
115         assertEquals(2, input.getChildNodes().size());
116         assertEquals(StatementSource.CONTEXT, ((EffectiveStatement<?, ?>) input).getStatementSource());
117
118         final OutputSchemaNode output = barRpc.getOutput();
119         assertNotNull(output);
120         assertEquals(2, output.getChildNodes().size());
121         assertEquals(StatementSource.CONTEXT, ((EffectiveStatement<?, ?>) output).getStatementSource());
122     }
123 }