import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.MockitoAnnotations;
-import org.opendaylight.mdsal.dom.api.DOMRpcResult;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.mdsal.dom.api.DOMRpcService;
import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
+import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
-import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
-import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
import org.opendaylight.yangtools.util.concurrent.FluentFutures;
+import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.RpcError;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class KeepaliveSalFacadeTest {
private static final RemoteDeviceId REMOTE_DEVICE_ID =
new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
@Mock
- private RemoteDeviceHandler<NetconfSessionPreferences> underlyingSalFacade;
+ private RemoteDeviceHandler underlyingSalFacade;
private ScheduledExecutorService executorServiceSpy;
private DOMRpcService proxyRpc;
- @Mock
- private ScheduledFuture currentKeepalive;
-
private KeepaliveSalFacade keepaliveSalFacade;
@Before
public void setUp() throws Exception {
executorServiceSpy = Executors.newScheduledThreadPool(1);
- MockitoAnnotations.initMocks(this);
-
doNothing().when(listener).disconnect();
- doReturn("mockedRpc").when(deviceRpc).toString();
- doNothing().when(underlyingSalFacade).onDeviceConnected(
- any(MountPointContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
-
- ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
- executorServiceSpy = Mockito.spy(executorService);
-
- doAnswer(
- invocationOnMock -> {
- invocationOnMock.callRealMethod();
- return currentKeepalive;
- }).when(executorServiceSpy).schedule(Mockito.<Runnable>any(), Mockito.anyLong(), any());
-
- Mockito.when(currentKeepalive.isDone()).thenReturn(true);
+ doNothing().when(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
keepaliveSalFacade =
new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorServiceSpy, 1L, 1L);
@Test
public void testKeepaliveSuccess() throws Exception {
- final DOMRpcResult result = new DefaultDOMRpcResult(Builders.containerBuilder().withNodeIdentifier(
- new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME)).build());
-
- doReturn(FluentFutures.immediateFluentFuture(result))
- .when(deviceRpc).invokeRpc(any(SchemaPath.class), isNull());
+ doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult(Builders.containerBuilder()
+ .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID)
+ .build()))).when(deviceRpc).invokeRpc(any(QName.class), any(ContainerNode.class));
- doReturn(FluentFutures.immediateFluentFuture(result))
- .when(deviceRpc).invokeRpc(any(SchemaPath.class), any(ContainerNode.class));
+ final var services = new RemoteDeviceServices(deviceRpc, null);
+ keepaliveSalFacade.onDeviceConnected(null, null, services);
- keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
+ verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
- verify(underlyingSalFacade).onDeviceConnected(
- isNull(), isNull(), any(DOMRpcService.class), isNull());
-
- verify(deviceRpc, timeout(15000).times(5)).invokeRpc(any(SchemaPath.class), any(ContainerNode.class));
+ verify(deviceRpc, timeout(15000).times(5)).invokeRpc(any(QName.class), any(ContainerNode.class));
}
@Test
public void testKeepaliveRpcFailure() {
doReturn(FluentFutures.immediateFailedFluentFuture(new IllegalStateException("illegal-state")))
- .when(deviceRpc).invokeRpc(any(SchemaPath.class), any(ContainerNode.class));
+ .when(deviceRpc).invokeRpc(any(QName.class), any(ContainerNode.class));
- keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
+ keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
- verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(DOMRpcService.class), isNull());
+ verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
// Should disconnect the session
verify(listener, timeout(15000).times(1)).disconnect();
- verify(deviceRpc, times(1)).invokeRpc(any(SchemaPath.class), any(ContainerNode.class));
+ verify(deviceRpc, times(1)).invokeRpc(any(QName.class), any(ContainerNode.class));
}
@Test
public void testKeepaliveSuccessWithRpcError() {
- final DOMRpcResult rpcSuccessWithError = new DefaultDOMRpcResult(mock(RpcError.class));
+ final var rpcSuccessWithError = new DefaultDOMRpcResult(mock(RpcError.class));
doReturn(FluentFutures.immediateFluentFuture(rpcSuccessWithError))
- .when(deviceRpc).invokeRpc(any(SchemaPath.class), any(ContainerNode.class));
-
- keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
+ .when(deviceRpc).invokeRpc(any(QName.class), any(ContainerNode.class));
- verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(DOMRpcService.class), isNull());
+ keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
+ verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
// Shouldn't disconnect the session
verify(listener, times(0)).disconnect();
- verify(deviceRpc, timeout(15000).times(1)).invokeRpc(any(SchemaPath.class), any(ContainerNode.class));
+ verify(deviceRpc, timeout(15000).times(1)).invokeRpc(any(QName.class), any(ContainerNode.class));
}
@Test
public void testNonKeepaliveRpcFailure() throws Exception {
- doAnswer(
- invocationOnMock -> {
- proxyRpc = (DOMRpcService) invocationOnMock.getArguments()[2];
- return null;
- }).when(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(DOMRpcService.class), isNull());
+ doAnswer(invocation -> proxyRpc = invocation.getArgument(2, RemoteDeviceServices.class).rpcs())
+ .when(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
doReturn(FluentFutures.immediateFailedFluentFuture(new IllegalStateException("illegal-state")))
- .when(deviceRpc).invokeRpc(any(SchemaPath.class), any(ContainerNode.class));
+ .when(deviceRpc).invokeRpc(any(QName.class), any(ContainerNode.class));
keepaliveSalFacade =
new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorServiceSpy, 100L, 1L);
keepaliveSalFacade.setListener(listener);
- keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
+ keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
- proxyRpc.invokeRpc(mock(SchemaPath.class), mock(ContainerNode.class));
+ proxyRpc.invokeRpc(QName.create("foo", "bar"), mock(ContainerNode.class));
verify(listener, times(1)).disconnect();
}