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