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