Move netconf-console to apps/
[netconf.git] / plugins / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / ReadOnlyTxTest.java
1 /*
2  * Copyright (c) 2015 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.tx;
9
10 import static org.junit.Assert.assertNotEquals;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15
16 import com.google.common.util.concurrent.Futures;
17 import java.net.InetSocketAddress;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.junit.MockitoJUnitRunner;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
26 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
27 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
28 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
29 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
30 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33
34 @RunWith(MockitoJUnitRunner.StrictStubs.class)
35 public class ReadOnlyTxTest {
36     @Mock
37     private Rpcs.Normalized rpc;
38     @Mock
39     private ContainerNode mockedNode;
40
41     private NetconfBaseOps netconfOps;
42
43     @Before
44     public void setUp() {
45         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(mockedNode))).when(rpc).invokeNetconf(any(), any());
46         netconfOps = new NetconfBaseOps(rpc, mock(MountPointContext.class));
47     }
48
49     @Test
50     public void testRead() throws Exception {
51         try (var readOnlyTx =
52                 new ReadOnlyTx(netconfOps, new RemoteDeviceId("a", new InetSocketAddress("localhost", 196)))) {
53             readOnlyTx.read(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty());
54             verify(rpc).invokeNetconf(Mockito.eq(NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME),
55                 any(ContainerNode.class));
56             readOnlyTx.read(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.empty());
57             verify(rpc).invokeNetconf(Mockito.eq(NetconfMessageTransformUtil.NETCONF_GET_QNAME), any());
58         }
59     }
60
61     @Test
62     public void testExists() throws Exception {
63         try (var readOnlyTx =
64                 new ReadOnlyTx(netconfOps, new RemoteDeviceId("a", new InetSocketAddress("localhost", 196)))) {
65             readOnlyTx.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty());
66             verify(rpc).invokeNetconf(Mockito.eq(NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME), any());
67             readOnlyTx.exists(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.empty());
68             verify(rpc).invokeNetconf(Mockito.eq(NetconfMessageTransformUtil.NETCONF_GET_QNAME), any());
69         }
70     }
71
72     @Test
73     public void testIdentifier() throws Exception {
74         try (var tx1 = new ReadOnlyTx(netconfOps, new RemoteDeviceId("a", new InetSocketAddress("localhost", 196)))) {
75             try (var tx2 = new ReadOnlyTx(netconfOps, new RemoteDeviceId("a",
76                     new InetSocketAddress("localhost", 196)))) {
77                 assertNotEquals(tx1.getIdentifier(), tx2.getIdentifier());
78             }
79         }
80     }
81 }