22e451cc28551c0a561180fd3c3b9a1f92ea60d4
[yangtools.git] / parser / 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 package org.opendaylight.yangtools.yang.stmt;
9
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;
16
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.StatementOrigin;
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     @Test
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"))
41                 .buildEffective();
42         assertNotNull(result);
43
44         final Module testModule = result.findModules("baz").iterator().next();
45         assertEquals(1, testModule.getRpcs().size());
46
47         final RpcDefinition rpc = testModule.getRpcs().iterator().next();
48         assertEquals("get-config", rpc.getQName().getLocalName());
49
50         final InputSchemaNode input = rpc.getInput();
51         assertNotNull(input);
52         assertEquals(2, input.getChildNodes().size());
53
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);
60
61         final OutputSchemaNode output = rpc.getOutput();
62         assertNotNull(output);
63         assertEquals(1, output.getChildNodes().size());
64
65         anyXml = (AnyxmlSchemaNode) output.getDataChildByName(QName.create(testModule.getQNameModule(), "data"));
66         assertNotNull(anyXml);
67
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());
71
72         RpcDefinition fooRpc1 = null;
73         RpcDefinition fooRpc2 = null;
74
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;
80             }
81         }
82
83         assertFalse(fooRpc1.equals(null));
84         assertFalse(fooRpc1.equals("str"));
85         assertFalse(fooRpc1.equals(fooRpc2));
86
87         assertNotEquals(fooRpc1.getInput().hashCode(), fooRpc2.getInput().hashCode());
88         assertNotEquals(fooRpc1.getOutput().hashCode(), fooRpc2.getOutput().hashCode());
89
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()));
94
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()));
99     }
100
101     @Test
102     public void testImplicitInputAndOutput() throws Exception {
103         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rpc-stmt-test/bar.yang");
104         assertNotNull(schemaContext);
105
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());
109
110         final RpcDefinition barRpc = rpcs.iterator().next();
111
112         final InputSchemaNode input = barRpc.getInput();
113         assertNotNull(input);
114         assertEquals(2, input.getChildNodes().size());
115         assertEquals(StatementOrigin.CONTEXT, ((EffectiveStatement<?, ?>) input).statementOrigin());
116
117         final OutputSchemaNode output = barRpc.getOutput();
118         assertNotNull(output);
119         assertEquals(2, output.getChildNodes().size());
120         assertEquals(StatementOrigin.CONTEXT, ((EffectiveStatement<?, ?>) output).statementOrigin());
121     }
122 }