Bump MRI upstreams
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDataTreeServiceImplTest.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.ArgumentMatchers.eq;
12 import static org.mockito.ArgumentMatchers.isNull;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME;
21
22 import java.net.InetSocketAddress;
23 import java.util.Collections;
24 import java.util.Optional;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.Mock;
31 import org.mockito.junit.MockitoJUnitRunner;
32 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.mdsal.dom.api.DOMRpcService;
35 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
36 import org.opendaylight.netconf.api.NetconfMessage;
37 import org.opendaylight.netconf.sal.connect.netconf.AbstractTestModelTest;
38 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
39 import org.opendaylight.netconf.sal.connect.netconf.sal.tx.TxTestUtils;
40 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
41 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
42 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.IetfNetconfService;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
45 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
46 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
49 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
50
51 @RunWith(MockitoJUnitRunner.StrictStubs.class)
52 public class NetconfDataTreeServiceImplTest extends AbstractTestModelTest {
53     @Mock
54     private DOMRpcService rpcService;
55     private AbstractNetconfDataTreeService netconService;
56     private NetconfMessageTransformer netconfMessageTransformer;
57     ArgumentCaptor<ContainerNode> captor = ArgumentCaptor.forClass(ContainerNode.class);
58
59     @Before
60     public void setUp() {
61         doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult())).when(rpcService)
62                 .invokeRpc(any(), any());
63         netconService = getNetconService();
64         final EffectiveModelContext model = BindingRuntimeHelpers.createEffectiveModel(IetfNetconfService.class,
65                 NetconfState.class);
66         netconfMessageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(model), true,
67                 BASE_SCHEMAS.getBaseSchema());
68     }
69
70     @Test
71     public void lock() {
72         netconService.lock();
73         verify(rpcService).invokeRpc(eq(NETCONF_LOCK_QNAME), any(ContainerNode.class));
74     }
75
76     @Test
77     public void unlock() {
78         netconService.lock();
79         netconService.unlock();
80         verify(rpcService).invokeRpc(eq(NETCONF_LOCK_QNAME), any(ContainerNode.class));
81         verify(rpcService).invokeRpc(eq(NETCONF_UNLOCK_QNAME), any(ContainerNode.class));
82     }
83
84     @Test
85     public void discardChanges() {
86         doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult())).when(rpcService)
87                 .invokeRpc(any(QName.class), isNull());
88         netconService.discardChanges();
89         verify(rpcService).invokeRpc(eq(NETCONF_DISCARD_CHANGES_QNAME), isNull());
90     }
91
92     @Test
93     public void get() {
94         netconService.get(null);
95         verify(rpcService).invokeRpc(eq(NETCONF_GET_QNAME), any(ContainerNode.class));
96     }
97
98     @Test
99     public void getConfig() {
100         netconService.getConfig(null);
101         verify(rpcService).invokeRpc(eq(NETCONF_GET_CONFIG_QNAME), any(ContainerNode.class));
102     }
103
104     @Test
105     public void merge() {
106         netconService.merge(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
107                 Optional.empty());
108         verify(rpcService).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
109
110         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
111                 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
112         Assert.assertTrue(netconfMessage.toString().contains("operation=\"merge\""));
113     }
114
115     @Test
116     public void replace() {
117         netconService.replace(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
118                 Optional.empty());
119         verify(rpcService).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
120
121         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
122                 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
123         Assert.assertTrue(netconfMessage.toString().contains("operation=\"replace\""));
124     }
125
126     @Test
127     public void create() {
128         netconService.create(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode(),
129                 Optional.empty());
130         verify(rpcService).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
131
132         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
133                 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
134         Assert.assertTrue(netconfMessage.toString().contains("operation=\"create\""));
135     }
136
137     @Test
138     public void delete() {
139         netconService.delete(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId().getParent());
140         verify(rpcService).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
141
142         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
143                 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
144         Assert.assertTrue(netconfMessage.toString().contains("operation=\"delete\""));
145     }
146
147     @Test
148     public void remove() {
149         netconService.remove(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId().getParent());
150         verify(rpcService).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), captor.capture());
151
152         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(
153                 NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, captor.getValue());
154         Assert.assertTrue(netconfMessage.toString().contains("operation=\"remove\""));
155     }
156
157     @Test
158     public void commit() {
159         netconService.commit();
160         verify(rpcService).invokeRpc(eq(NETCONF_COMMIT_QNAME), any(ContainerNode.class));
161     }
162
163     private AbstractNetconfDataTreeService getNetconService() {
164         NetconfSessionPreferences prefs = NetconfSessionPreferences.fromStrings(
165                 Collections.singletonList(NetconfMessageTransformUtil.NETCONF_CANDIDATE_URI.toString()));
166         final RemoteDeviceId id =
167                 new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830));
168         return AbstractNetconfDataTreeService.of(id, new EmptyMountPointContext(SCHEMA_CONTEXT), rpcService, prefs);
169     }
170 }