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