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