Merge "Migrate netconf to mockito ArgumentMatchers"
[netconf.git] / netconf / 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
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
10
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.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
26 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
27 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
28 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
29 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
30 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35
36 public class ReadOnlyTxTest {
37
38     private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.create();
39
40     @Mock
41     private DOMRpcService rpc;
42     @Mock
43     private NormalizedNode<?, ?> mockedNode;
44
45     @Before
46     public void setUp() throws DataNormalizationException {
47         MockitoAnnotations.initMocks(this);
48         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult(mockedNode))).when(rpc)
49                 .invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
50         doReturn("node").when(mockedNode).toString();
51     }
52
53     @Test
54     public void testRead() throws Exception {
55         final NetconfBaseOps netconfOps = new NetconfBaseOps(rpc, mock(SchemaContext.class));
56
57         final ReadOnlyTx readOnlyTx =
58                 new ReadOnlyTx(netconfOps, new RemoteDeviceId("a", new InetSocketAddress("localhost", 196)));
59
60         readOnlyTx.read(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create());
61         verify(rpc).invokeRpc(Mockito.eq(NetconfMessageTransformUtil.toPath(
62                 NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME)), any(NormalizedNode.class));
63         readOnlyTx.read(LogicalDatastoreType.OPERATIONAL, PATH);
64         verify(rpc).invokeRpc(Mockito.eq(NetconfMessageTransformUtil.toPath(
65                 NetconfMessageTransformUtil.NETCONF_GET_QNAME)), any(NormalizedNode.class));
66     }
67
68     @Test
69     public void testExists() throws Exception {
70         final NetconfBaseOps netconfOps = new NetconfBaseOps(rpc, mock(SchemaContext.class));
71
72         final ReadOnlyTx readOnlyTx =
73                 new ReadOnlyTx(netconfOps, new RemoteDeviceId("a", new InetSocketAddress("localhost", 196)));
74
75         readOnlyTx.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create());
76         verify(rpc).invokeRpc(Mockito.eq(NetconfMessageTransformUtil.toPath(
77                 NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME)), any(NormalizedNode.class));
78         readOnlyTx.exists(LogicalDatastoreType.OPERATIONAL, PATH);
79         verify(rpc).invokeRpc(Mockito.eq(NetconfMessageTransformUtil.toPath(
80                 NetconfMessageTransformUtil.NETCONF_GET_QNAME)), any(NormalizedNode.class));
81     }
82
83     @Test
84     public void testIdentifier() throws Exception {
85         final NetconfBaseOps netconfOps = new NetconfBaseOps(rpc, mock(SchemaContext.class));
86         final ReadOnlyTx tx1 =
87                 new ReadOnlyTx(netconfOps, new RemoteDeviceId("a", new InetSocketAddress("localhost", 196)));
88         final ReadOnlyTx tx2 =
89                 new ReadOnlyTx(netconfOps, new RemoteDeviceId("a", new InetSocketAddress("localhost", 196)));
90         Assert.assertNotEquals(tx1.getIdentifier(), tx2.getIdentifier());
91     }
92 }