Bump MRI upstreams
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / SchemalessNetconfDeviceRpcTest.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.netconf.sal.connect.netconf.sal;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.verify;
13
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.net.InetSocketAddress;
17 import javax.xml.transform.dom.DOMSource;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.ArgumentCaptor;
22 import org.mockito.Mock;
23 import org.mockito.junit.MockitoJUnitRunner;
24 import org.opendaylight.netconf.api.NetconfMessage;
25 import org.opendaylight.netconf.api.xml.XmlUtil;
26 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
27 import org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest;
28 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseRpcSchemalessTransformer;
29 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.SchemalessMessageTransformer;
30 import org.opendaylight.netconf.sal.connect.util.MessageCounter;
31 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.AnyxmlNode;
36 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @RunWith(MockitoJUnitRunner.StrictStubs.class)
41 public class SchemalessNetconfDeviceRpcTest extends AbstractBaseSchemasTest {
42
43     private static final Logger LOG = LoggerFactory.getLogger(SchemalessNetconfDeviceRpcTest.class);
44
45     @Mock
46     private RemoteDeviceCommunicator<NetconfMessage> listener;
47
48     private SchemalessNetconfDeviceRpc deviceRpc;
49
50     @Before
51     public void setUp() throws Exception {
52         RpcResult<NetconfMessage> msg = null;
53         ListenableFuture<RpcResult<NetconfMessage>> future = Futures.immediateFuture(msg);
54         doReturn(future).when(listener).sendRequest(any(), any());
55         final MessageCounter counter = new MessageCounter();
56         deviceRpc = new SchemalessNetconfDeviceRpc(
57                 new RemoteDeviceId("device1", InetSocketAddress.createUnresolved("0.0.0.0", 17830)), listener,
58                 new BaseRpcSchemalessTransformer(BASE_SCHEMAS, counter), new SchemalessMessageTransformer(counter));
59
60     }
61
62     @Test
63     public void testInvokeRpc() throws Exception {
64         final QName qName = QName.create("urn:ietf:params:xml:ns:netconf:base:1.0", "2011-06-01", "get-config");
65         DOMSource src = new DOMSource(XmlUtil.readXmlToDocument("<get-config xmlns=\"dd\">\n"
66                 + "    <source>\n"
67                 + "      <running/>\n"
68                 + "    </source>\n"
69                 + "    <filter type=\"subtree\">\n"
70                 + "      <mainroot xmlns=\"urn:dummy:mod-0\">\n"
71                 + "        <maincontent/>\n"
72                 + "<choiceList></choiceList>\n"
73                 + "      </mainroot>\n"
74                 + "    </filter>\n"
75                 + "  </get-config>"));
76         AnyxmlNode<?> input = Builders.anyXmlBuilder()
77                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(qName))
78                 .withValue(src)
79                 .build();
80
81         deviceRpc.invokeRpc(qName, input);
82         ArgumentCaptor<NetconfMessage> msgCaptor = ArgumentCaptor.forClass(NetconfMessage.class);
83         ArgumentCaptor<QName> qnameCaptor = ArgumentCaptor.forClass(QName.class);
84         verify(listener).sendRequest(msgCaptor.capture(), qnameCaptor.capture());
85         LOG.info(XmlUtil.toString(msgCaptor.getValue().getDocument()));
86     }
87 }