2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.stmt;
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;
18 import java.text.ParseException;
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;
33 public class RpcStmtTest {
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");
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);
44 final EffectiveSchemaContext result = reactor.buildEffective();
45 assertNotNull(result);
47 final Module testModule = result.findModuleByName("baz", null);
48 assertNotNull(testModule);
50 assertEquals(1, testModule.getRpcs().size());
52 final RpcDefinition rpc = testModule.getRpcs().iterator().next();
53 assertEquals("get-config", rpc.getQName().getLocalName());
55 final ContainerSchemaNode input = rpc.getInput();
57 assertEquals(2, input.getChildNodes().size());
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);
64 final ContainerSchemaNode output = rpc.getOutput();
65 assertNotNull(output);
66 assertEquals(1, output.getChildNodes().size());
68 anyXml = (AnyXmlSchemaNode) output.getDataChildByName(QName.create(testModule.getQNameModule(), "data"));
69 assertNotNull(anyXml);
71 final Module fooModule = result.findModuleByName("foo", SimpleDateFormatUtil.getRevisionFormat().parse("2016-09-23"));
72 assertNotNull(fooModule);
74 final Set<RpcDefinition> rpcs = fooModule.getRpcs();
75 assertEquals(2, rpcs.size());
77 RpcDefinition fooRpc1 = null;
78 RpcDefinition fooRpc2 = null;
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;
88 assertFalse(fooRpc1.equals(null));
89 assertFalse(fooRpc1.equals("str"));
90 assertFalse(fooRpc1.equals(fooRpc2));
92 assertNotEquals(fooRpc1.getInput().hashCode(), fooRpc2.getInput().hashCode());
93 assertNotEquals(fooRpc1.getOutput().hashCode(), fooRpc2.getOutput().hashCode());
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()));
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()));