Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.internal.node.xmlrpc;
00018
00019 import static org.junit.Assert.assertEquals;
00020 import static org.mockito.Mockito.mock;
00021 import static org.mockito.Mockito.when;
00022
00023 import java.util.List;
00024
00025 import org.junit.Test;
00026 import org.mockito.Matchers;
00027 import org.ros.address.AdvertiseAddress;
00028 import org.ros.internal.node.response.StatusCode;
00029 import org.ros.internal.node.server.ServerException;
00030 import org.ros.internal.node.server.SlaveServer;
00031 import org.ros.internal.node.topic.DefaultPublisher;
00032 import org.ros.internal.transport.ProtocolNames;
00033 import org.ros.internal.transport.tcp.TcpRosProtocolDescription;
00034 import org.ros.namespace.GraphName;
00035
00036 import com.google.common.collect.Lists;
00037 import com.google.common.collect.Sets;
00038
00042 public class SlaveImplTest {
00043
00044 @Test
00045 public void testGetPublicationsEmptyList() {
00046 SlaveServer mockSlave = mock(SlaveServer.class);
00047 when(mockSlave.getPublications()).thenReturn(Lists.<DefaultPublisher<?>>newArrayList());
00048 SlaveXmlRpcEndpointImpl slave = new SlaveXmlRpcEndpointImpl(mockSlave);
00049 List<Object> response = slave.getPublications("/foo");
00050 assertEquals(response.get(0), StatusCode.SUCCESS.toInt());
00051 assertEquals(response.get(2), Lists.newArrayList());
00052 }
00053
00054 @Test
00055 public void testGetPublications() {
00056 SlaveServer mockSlave = mock(SlaveServer.class);
00057 DefaultPublisher<?> mockPublisher = mock(DefaultPublisher.class);
00058 when(mockSlave.getPublications()).thenReturn(Lists.<DefaultPublisher<?>>newArrayList(mockPublisher));
00059 when(mockPublisher.getTopicName()).thenReturn(GraphName.of("/bar"));
00060 when(mockPublisher.getTopicMessageType()).thenReturn("/baz");
00061 when(mockPublisher.getTopicDeclarationAsList()).thenReturn(Lists.newArrayList("/bar", "/baz"));
00062 SlaveXmlRpcEndpointImpl slave = new SlaveXmlRpcEndpointImpl(mockSlave);
00063 List<Object> response = slave.getPublications("/foo");
00064 assertEquals(StatusCode.SUCCESS.toInt(), response.get(0));
00065 List<List<String>> protocols = Lists.newArrayList();
00066 protocols.add(Lists.newArrayList("/bar", "/baz"));
00067 assertEquals(protocols, response.get(2));
00068 }
00069
00070 @Test
00071 public void testRequestTopic() throws ServerException {
00072 SlaveServer mockSlave = mock(SlaveServer.class);
00073 AdvertiseAddress address = AdvertiseAddress.newPrivate();
00074 address.setStaticPort(1234);
00075 TcpRosProtocolDescription protocol = new TcpRosProtocolDescription(address);
00076 when(
00077 mockSlave.requestTopic(Matchers.<String>any(),
00078 Matchers.eq(Sets.newHashSet(ProtocolNames.TCPROS, ProtocolNames.UDPROS)))).thenReturn(
00079 protocol);
00080 SlaveXmlRpcEndpointImpl slave = new SlaveXmlRpcEndpointImpl(mockSlave);
00081 Object[][] protocols = new Object[][] { {ProtocolNames.TCPROS}, {ProtocolNames.UDPROS}};
00082 List<Object> response = slave.requestTopic("/foo", "/bar", protocols);
00083 assertEquals(response.get(0), StatusCode.SUCCESS.toInt());
00084 assertEquals(response.get(2), protocol.toList());
00085 }
00086
00087 @Test
00088 public void testGetPid() {
00089 SlaveServer mockSlave = mock(SlaveServer.class);
00090 AdvertiseAddress address = AdvertiseAddress.newPrivate();
00091 address.setStaticPort(1234);
00092 when(mockSlave.getPid()).thenReturn(1234);
00093 SlaveXmlRpcEndpointImpl slave = new SlaveXmlRpcEndpointImpl(mockSlave);
00094 List<Object> response = slave.getPid("/foo");
00095 assertEquals(response.get(0), StatusCode.SUCCESS.toInt());
00096 assertEquals(response.get(2), 1234);
00097 }
00098
00099 @Test
00100 public void testGetPidNotSupported() {
00101 SlaveServer mockSlave = mock(SlaveServer.class);
00102 AdvertiseAddress address = AdvertiseAddress.newPrivate();
00103 address.setStaticPort(1234);
00104 when(mockSlave.getPid()).thenThrow(new UnsupportedOperationException());
00105 SlaveXmlRpcEndpointImpl slave = new SlaveXmlRpcEndpointImpl(mockSlave);
00106 List<Object> response = slave.getPid("/foo");
00107 assertEquals(response.get(0), StatusCode.FAILURE.toInt());
00108 }
00109
00110 }