2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
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;
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;
36 public class ReadOnlyTxTest {
38 private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.create();
41 private DOMRpcService rpc;
43 private NormalizedNode<?, ?> mockedNode;
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();
54 public void testRead() throws Exception {
55 final NetconfBaseOps netconfOps = new NetconfBaseOps(rpc, mock(SchemaContext.class));
57 final ReadOnlyTx readOnlyTx =
58 new ReadOnlyTx(netconfOps, new RemoteDeviceId("a", new InetSocketAddress("localhost", 196)));
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));
69 public void testExists() throws Exception {
70 final NetconfBaseOps netconfOps = new NetconfBaseOps(rpc, mock(SchemaContext.class));
72 final ReadOnlyTx readOnlyTx =
73 new ReadOnlyTx(netconfOps, new RemoteDeviceId("a", new InetSocketAddress("localhost", 196)));
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));
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());