MasterXmlRpcEndpointImplTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.internal.node.xmlrpc;
00018 
00019 import static org.junit.Assert.assertEquals;
00020 import static org.mockito.Matchers.eq;
00021 import static org.mockito.Mockito.mock;
00022 import static org.mockito.Mockito.when;
00023 
00024 import com.google.common.collect.Lists;
00025 
00026 import org.junit.Test;
00027 import org.mockito.Matchers;
00028 import org.ros.internal.node.response.StatusCode;
00029 import org.ros.internal.node.server.master.MasterServer;
00030 import org.ros.namespace.GraphName;
00031 
00032 import java.net.URI;
00033 import java.util.List;
00034 
00040 public class MasterXmlRpcEndpointImplTest {
00041 
00042   @Test
00043   public void testGetUri() throws Exception {
00044     URI testUri = new URI("http://foo.bar:8080");
00045     MasterServer mockMaster = mock(MasterServer.class);
00046     when(mockMaster.getUri()).thenReturn(testUri);
00047     MasterXmlRpcEndpointImpl master = new MasterXmlRpcEndpointImpl(mockMaster);
00048     List<Object> response = master.getUri("/caller");
00049     assertEquals(StatusCode.SUCCESS.toInt(), response.get(0));
00050     assertEquals(testUri.toString(), response.get(2));
00051   }
00052 
00053   @Test
00054   public void testLookupNodeExisting() throws Exception {
00055     MasterServer mockMaster = mock(MasterServer.class);
00056     final GraphName nodeName = GraphName.of("/foo");
00057     final URI nodeSlaveUri = new URI("http://bar");
00058     when(mockMaster.lookupNode(eq(nodeName))).thenReturn(nodeSlaveUri);
00059     MasterXmlRpcEndpointImpl master = new MasterXmlRpcEndpointImpl(mockMaster);
00060     List<Object> response = master.lookupNode("/caller", nodeName.toString());
00061     assertEquals(StatusCode.SUCCESS.toInt(), response.get(0));
00062     assertEquals(nodeSlaveUri.toString(), response.get(2));
00063   }
00064 
00065   @Test
00066   public void testLookupNodeNotExisting() throws Exception {
00067     MasterServer mockMaster = mock(MasterServer.class);
00068     when(mockMaster.lookupNode(Matchers.<GraphName>any())).thenReturn(null);
00069     MasterXmlRpcEndpointImpl master = new MasterXmlRpcEndpointImpl(mockMaster);
00070     List<Object> response = master.lookupNode("/caller", "/foo");
00071     assertEquals(StatusCode.ERROR.toInt(), response.get(0));
00072     assertEquals("null", response.get(2));
00073   }
00074 
00075   @Test
00076   public void testRegisterPublisherWithNoSubscribers() {
00077     MasterServer mockMaster = mock(MasterServer.class);
00078     when(
00079         mockMaster.registerPublisher(Matchers.<GraphName>any(), Matchers.<URI>any(),
00080             Matchers.<GraphName>any(), Matchers.<String>any())).thenReturn(
00081         Lists.<URI>newArrayList());
00082     MasterXmlRpcEndpointImpl master = new MasterXmlRpcEndpointImpl(mockMaster);
00083     List<Object> response = master.registerPublisher("/caller", "/foo", "/bar", "http://baz");
00084     assertEquals(StatusCode.SUCCESS.toInt(), response.get(0));
00085     assertEquals(Lists.newArrayList(), response.get(2));
00086   }
00087 
00088   @Test
00089   public void testRegisterPublisher() throws Exception {
00090     MasterServer mockMaster = mock(MasterServer.class);
00091     final GraphName nodeName = GraphName.of("/slave");
00092     final URI nodeSlaveUri = new URI("http://api");
00093     final GraphName topicName = GraphName.of("/topic");
00094     final String messageType = "/topicType";
00095     when(
00096         mockMaster
00097             .registerPublisher(eq(nodeName), eq(nodeSlaveUri), eq(topicName), eq(messageType)))
00098         .thenReturn(Lists.<URI>newArrayList(nodeSlaveUri));
00099     MasterXmlRpcEndpointImpl master = new MasterXmlRpcEndpointImpl(mockMaster);
00100     List<Object> response =
00101         master.registerPublisher(nodeName.toString(), topicName.toString(), messageType,
00102             nodeSlaveUri.toString());
00103     assertEquals(StatusCode.SUCCESS.toInt(), response.get(0));
00104     assertEquals(Lists.newArrayList(nodeSlaveUri.toString()), response.get(2));
00105   }
00106 
00107   @Test
00108   public void testRegisterSubscriberWithNoSubscribers() {
00109     MasterServer mockMaster = mock(MasterServer.class);
00110     when(
00111         mockMaster.registerSubscriber(Matchers.<GraphName>any(), Matchers.<URI>any(),
00112             Matchers.<GraphName>any(), Matchers.<String>any())).thenReturn(
00113         Lists.<URI>newArrayList());
00114     MasterXmlRpcEndpointImpl master = new MasterXmlRpcEndpointImpl(mockMaster);
00115     List<Object> response = master.registerSubscriber("/caller", "/foo", "/bar", "http://baz");
00116     assertEquals(StatusCode.SUCCESS.toInt(), response.get(0));
00117     assertEquals(Lists.newArrayList(), response.get(2));
00118   }
00119 
00120   @Test
00121   public void testRegisterSubscriber() throws Exception {
00122     MasterServer mockMaster = mock(MasterServer.class);
00123     final GraphName nodeName = GraphName.of("/slave");
00124     final URI nodeSlaveUri = new URI("http://api");
00125     final GraphName topicName = GraphName.of("/topic");
00126     final String topicMessageType = "/topicType";
00127 
00128     when(
00129         mockMaster.registerSubscriber(eq(nodeName), eq(nodeSlaveUri), eq(topicName),
00130             eq(topicMessageType))).thenReturn(Lists.<URI>newArrayList(nodeSlaveUri));
00131     MasterXmlRpcEndpointImpl master = new MasterXmlRpcEndpointImpl(mockMaster);
00132     List<Object> response =
00133         master.registerSubscriber(nodeName.toString(), topicName.toString(), topicMessageType,
00134             nodeSlaveUri.toString());
00135     assertEquals(StatusCode.SUCCESS.toInt(), response.get(0));
00136     assertEquals(Lists.newArrayList(nodeSlaveUri.toString()), response.get(2));
00137   }
00138 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49