Further rework of base schemas
[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.opendaylight.yangtools.yang.model.api.SchemaPath;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class SchemalessNetconfDeviceRpcTest extends AbstractBaseSchemasTest {
41
42     private static final Logger LOG = LoggerFactory.getLogger(SchemalessNetconfDeviceRpcTest.class);
43
44     @Mock
45     private RemoteDeviceCommunicator<NetconfMessage> listener;
46
47     private SchemalessNetconfDeviceRpc deviceRpc;
48
49     @Before
50     public void setUp() throws Exception {
51         MockitoAnnotations.initMocks(this);
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         SchemaPath type = SchemaPath.create(true, qName);
66         DOMSource src = new DOMSource(XmlUtil.readXmlToDocument("<get-config xmlns=\"dd\">\n"
67                 + "    <source>\n"
68                 + "      <running/>\n"
69                 + "    </source>\n"
70                 + "    <filter type=\"subtree\">\n"
71                 + "      <mainroot xmlns=\"urn:dummy:mod-0\">\n"
72                 + "        <maincontent/>\n"
73                 + "<choiceList></choiceList>\n"
74                 + "      </mainroot>\n"
75                 + "    </filter>\n"
76                 + "  </get-config>"));
77         NormalizedNode<?, ?> input = Builders.anyXmlBuilder()
78                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(qName))
79                 .withValue(src)
80                 .build();
81
82         deviceRpc.invokeRpc(type, input);
83         ArgumentCaptor<NetconfMessage> msgCaptor = ArgumentCaptor.forClass(NetconfMessage.class);
84         ArgumentCaptor<QName> qnameCaptor = ArgumentCaptor.forClass(QName.class);
85         verify(listener).sendRequest(msgCaptor.capture(), qnameCaptor.capture());
86         LOG.info(XmlUtil.toString(msgCaptor.getValue().getDocument()));
87     }
88 }