Bump mdsal to 4.0.0
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceRpcTest.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 java.util.Collection;
16 import java.util.Collections;
17 import java.util.Set;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.ArgumentCaptor;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
25 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
26 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
27 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
28 import org.opendaylight.netconf.api.NetconfMessage;
29 import org.opendaylight.netconf.api.xml.XmlUtil;
30 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
31 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
40 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43
44 public class NetconfDeviceRpcTest {
45
46     @Mock
47     private DOMRpcAvailabilityListener listener;
48     @Mock
49     private RemoteDeviceCommunicator<NetconfMessage> communicator;
50
51     private NetconfDeviceRpc rpc;
52     private SchemaPath path;
53     private DOMRpcResult expectedReply;
54     private SchemaContext schema;
55
56     @Before
57     public void setUp() throws Exception {
58         MockitoAnnotations.initMocks(this);
59         schema = getSchema();
60         NetconfMessageTransformer transformer = new NetconfMessageTransformer(schema, true);
61         final NetconfMessage reply = new NetconfMessage(XmlUtil.readXmlToDocument(
62                 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
63                         + "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"101\">\n"
64                         + "<data>\n"
65                         + "</data>\n"
66                         + "</rpc-reply>"));
67         RpcResult<NetconfMessage> result = RpcResultBuilder.success(reply).build();
68         doReturn(Futures.immediateFuture(result))
69                 .when(communicator).sendRequest(any(NetconfMessage.class), any(QName.class));
70         rpc = new NetconfDeviceRpc(schema, communicator, transformer);
71
72         path = SchemaPath
73                 .create(true, QName.create("urn:ietf:params:xml:ns:netconf:base:1.0", "2011-06-01", "get-config"));
74         expectedReply = transformer.toRpcResult(reply, path);
75     }
76
77     @Test
78     public void testInvokeRpc() throws Exception {
79         NormalizedNode<?, ?> input = createNode("urn:ietf:params:xml:ns:netconf:base:1.0", "2011-06-01", "filter");
80         final DOMRpcResult result = rpc.invokeRpc(path, input).get();
81         Assert.assertEquals(expectedReply, result);
82     }
83
84     @Test
85     public void testRegisterRpcListener() throws Exception {
86         ArgumentCaptor<Collection> argument = ArgumentCaptor.forClass(Collection.class);
87
88         rpc.registerRpcListener(listener);
89
90         verify(listener).onRpcAvailable(argument.capture());
91         final Collection<DOMRpcIdentifier> argValue = argument.getValue();
92         final Set<RpcDefinition> operations = schema.getOperations();
93         Assert.assertEquals(argValue.size(), operations.size());
94         for (RpcDefinition operation : operations) {
95             final DOMRpcIdentifier domRpcIdentifier = DOMRpcIdentifier.create(operation.getPath());
96             Assert.assertTrue(argValue.contains(domRpcIdentifier));
97
98         }
99     }
100
101     private static ContainerNode createNode(final String namespace, final String date, final String localName) {
102         return Builders.containerBuilder().withNodeIdentifier(
103                 new YangInstanceIdentifier.NodeIdentifier(QName.create(namespace, date, localName))).build();
104     }
105
106     private static SchemaContext getSchema() {
107         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
108         moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
109         return moduleInfoBackedContext.tryToCreateSchemaContext().get();
110     }
111 }