Update StmtTestUtils
[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.text.ParseException;
19 import java.util.Set;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
23 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
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, ParseException {
41         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
42         reactor.addSources(RPC_MODULE, IMPORTED_MODULE, FOO_MODULE);
43
44         final EffectiveSchemaContext result = reactor.buildEffective();
45         assertNotNull(result);
46
47         final Module testModule = result.findModuleByName("baz", null);
48         assertNotNull(testModule);
49
50         assertEquals(1, testModule.getRpcs().size());
51
52         final RpcDefinition rpc = testModule.getRpcs().iterator().next();
53         assertEquals("get-config", rpc.getQName().getLocalName());
54
55         final ContainerSchemaNode input = rpc.getInput();
56         assertNotNull(input);
57         assertEquals(2, input.getChildNodes().size());
58
59         final ContainerSchemaNode container = (ContainerSchemaNode) input.getDataChildByName(QName.create(testModule.getQNameModule(), "source"));
60         assertNotNull(container);
61         AnyXmlSchemaNode anyXml = (AnyXmlSchemaNode) input.getDataChildByName(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.findModuleByName("foo", SimpleDateFormatUtil.getRevisionFormat().parse("2016-09-23"));
72         assertNotNull(fooModule);
73
74         final Set<RpcDefinition> rpcs = fooModule.getRpcs();
75         assertEquals(2, rpcs.size());
76
77         RpcDefinition fooRpc1 = null;
78         RpcDefinition fooRpc2 = null;
79
80         for (RpcDefinition rpcDefinition : rpcs) {
81             if ("foo-rpc-1".equals(rpcDefinition.getQName().getLocalName())) {
82                 fooRpc1 = rpcDefinition;
83             } else if ("foo-rpc-2".equals(rpcDefinition.getQName().getLocalName())) {
84                 fooRpc2 = rpcDefinition;
85             }
86         }
87
88         assertFalse(fooRpc1.equals(null));
89         assertFalse(fooRpc1.equals("str"));
90         assertFalse(fooRpc1.equals(fooRpc2));
91
92         assertNotEquals(fooRpc1.getInput().hashCode(), fooRpc2.getInput().hashCode());
93         assertNotEquals(fooRpc1.getOutput().hashCode(), fooRpc2.getOutput().hashCode());
94
95         assertTrue(fooRpc1.getInput().equals(fooRpc1.getInput()));
96         assertFalse(fooRpc1.getInput().equals(null));
97         assertFalse(fooRpc1.getInput().equals("str"));
98         assertFalse(fooRpc1.getInput().equals(fooRpc2.getInput()));
99
100         assertTrue(fooRpc1.getOutput().equals(fooRpc1.getOutput()));
101         assertFalse(fooRpc1.getOutput().equals(null));
102         assertFalse(fooRpc1.getOutput().equals("str"));
103         assertFalse(fooRpc1.getOutput().equals(fooRpc2.getOutput()));
104     }
105 }