Bug 4940 - correctly implement default-request-timeout-millis
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / test / java / org / opendaylight / controller / 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.controller.sal.connect.netconf.sal.tx;
10
11 import static org.mockito.Matchers.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.base.Optional;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20
21 import java.net.InetSocketAddress;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.TimeoutException;
24
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.MockitoAnnotations;
32 import org.mockito.invocation.InvocationOnMock;
33 import org.mockito.stubbing.Answer;
34 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
35 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
36 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
37 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
38 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
39 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
40 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
41 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.powermock.api.mockito.PowerMockito;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
49
50 @PrepareForTest({NetconfBaseOps.class})
51 @RunWith(PowerMockRunner.class)
52 public class ReadOnlyTxTest {
53
54     private static final YangInstanceIdentifier path = YangInstanceIdentifier.create();
55
56     @Mock
57     private DOMRpcService rpc;
58     @Mock
59     private NormalizedNode<?, ?> mockedNode;
60
61     @Before
62     public void setUp() throws DataNormalizationException {
63         MockitoAnnotations.initMocks(this);
64         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult(mockedNode))).when(rpc)
65                 .invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
66         doReturn("node").when(mockedNode).toString();
67     }
68
69     @Test
70     public void testRead() throws Exception {
71         final NetconfBaseOps netconfOps = new NetconfBaseOps(rpc, mock(SchemaContext.class));
72
73         final ReadOnlyTx readOnlyTx = new ReadOnlyTx(netconfOps, new RemoteDeviceId("a", new InetSocketAddress("localhost", 196)), 60000L);
74
75         readOnlyTx.read(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create());
76         verify(rpc).invokeRpc(Mockito.eq(NetconfMessageTransformUtil.toPath(NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME)), any(NormalizedNode.class));
77         readOnlyTx.read(LogicalDatastoreType.OPERATIONAL, path);
78         verify(rpc).invokeRpc(Mockito.eq(NetconfMessageTransformUtil.toPath(NetconfMessageTransformUtil.NETCONF_GET_QNAME)), any(NormalizedNode.class));
79     }
80
81     @SuppressWarnings("unchecked")
82     @Test
83     public void testReadTimeout() throws Exception {
84         final ListenableFuture<DOMRpcResult> future = mock(ListenableFuture.class);
85
86         Mockito.when(future.get(Mockito.anyLong(), any(TimeUnit.class))).then(new Answer<DOMRpcResult>() {
87             @Override
88             public DOMRpcResult answer(InvocationOnMock invocation)
89                     throws Throwable {
90                 throw new TimeoutException("Processing Timeout");
91             }
92         });
93
94         final NetconfBaseOps netconfOps = PowerMockito.mock(NetconfBaseOps.class);
95         Mockito.when(netconfOps.getConfigRunning(any(FutureCallback.class), any(Optional.class))).thenReturn(future);
96
97
98         final ReadOnlyTx readOnlyTx = new ReadOnlyTx(netconfOps, new RemoteDeviceId("a", new InetSocketAddress("localhost", 196)), 100L);
99         Assert.assertNull("Read operation didn't correctly timeout", readOnlyTx.read(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create()));
100         readOnlyTx.close();
101     }
102 }