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