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