Skip to content
Snippets Groups Projects
Unverified Commit b7edf476 authored by Juan Ignacio Ubeira's avatar Juan Ignacio Ubeira Committed by GitHub
Browse files

Merge pull request #284 from jubeira/feature/rosjava_helpers_test

Imrpoving ParameterLoaderNode error checking
parents f2134b28 9ab275ac
No related branches found
No related tags found
No related merge requests found
apply plugin: 'java'
dependencies { dependencies {
compile project(':rosjava') compile project(':rosjava')
compile 'org.yaml:snakeyaml:[1.17, 1.18)' compile 'org.yaml:snakeyaml:[1.17, 1.18)'
testCompile 'junit:junit:4.8.2'
testCompile project(':rosjava').sourceSets.test.output
} }
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package org.ros.helpers; package org.ros.helpers;
import com.google.common.base.Preconditions;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.ros.namespace.GraphName; import org.ros.namespace.GraphName;
import org.ros.node.AbstractNodeMain; import org.ros.node.AbstractNodeMain;
...@@ -47,36 +49,39 @@ public class ParameterLoaderNode extends AbstractNodeMain { ...@@ -47,36 +49,39 @@ public class ParameterLoaderNode extends AbstractNodeMain {
* Default constructor * Default constructor
* @param resources Array of resources with their respective namespace to load. * @param resources Array of resources with their respective namespace to load.
*/ */
public ParameterLoaderNode(ArrayList<Resource> resources) { public ParameterLoaderNode(List<Resource> resources) {
Preconditions.checkNotNull(resources);
for (Resource r : resources) { for (Resource r : resources) {
Preconditions.checkNotNull(r.inputStream);
addSingleYmlInput(r.inputStream, r.namespace == null ? "" : r.namespace); addSingleYmlInput(r.inputStream, r.namespace == null ? "" : r.namespace);
} }
} }
private void addSingleYmlInput(InputStream ymlInputStream, String namespace) { private void addSingleYmlInput(InputStream ymlInputStream, String namespace) {
this.params.add(new LoadedResource((new Yaml()).load(ymlInputStream), namespace)); Object loadedYaml = new Yaml().load(ymlInputStream);
if (loadedYaml != null && loadedYaml instanceof Map<?, ?>) {
this.params.add(new LoadedResource(Map.class.cast(loadedYaml), namespace));
}
} }
@SuppressWarnings("unchecked") private void addParams(ParameterTree parameterTree, String namespace, Map<?, ?> params) {
private void addParams(ParameterTree parameterTree, String namespace, Map<String, Object> params) { for (Map.Entry<?, ?> e : params.entrySet()) {
for (Map.Entry<String, Object> e : params.entrySet()) { String fullKeyName = namespace + "/" + e.getKey().toString();
String fullKeyName = namespace + "/" + e.getKey();
if (log != null) { if (log != null) {
log.info("Loading parameter " + fullKeyName + " \nValue = " + e.getValue()); log.debug("Loading parameter " + fullKeyName + " \nValue = " + e.getValue());
} }
if (e.getValue() instanceof String) { if (e.getValue() instanceof String) {
parameterTree.set(fullKeyName, (String)e.getValue()); parameterTree.set(fullKeyName, String.class.cast(e.getValue()));
} else if (e.getValue() instanceof Integer) { } else if (e.getValue() instanceof Integer) {
parameterTree.set(fullKeyName, (Integer)e.getValue()); parameterTree.set(fullKeyName, Integer.class.cast(e.getValue()));
} else if (e.getValue() instanceof Double) { } else if (e.getValue() instanceof Double) {
parameterTree.set(fullKeyName, (Double)e.getValue()); parameterTree.set(fullKeyName, Double.class.cast(e.getValue()));
} else if (e.getValue() instanceof Map) { } else if (e.getValue() instanceof Map) {
parameterTree.set(fullKeyName, (Map)e.getValue()); parameterTree.set(fullKeyName, Map.class.cast(e.getValue()));
} else if (e.getValue() instanceof Boolean) { } else if (e.getValue() instanceof Boolean) {
parameterTree.set(fullKeyName, (Boolean)e.getValue()); parameterTree.set(fullKeyName, Boolean.class.cast(e.getValue()));
} else if (e.getValue() instanceof List) { } else if (e.getValue() instanceof List) {
parameterTree.set(fullKeyName, (List)e.getValue()); parameterTree.set(fullKeyName, List.class.cast(e.getValue()));
} else if (log != null) { } else if (log != null) {
log.debug("I don't know what type parameter " + fullKeyName + " is. Value = " + e.getValue()); log.debug("I don't know what type parameter " + fullKeyName + " is. Value = " + e.getValue());
log.debug("Class name is: " + e.getValue().getClass().getName()); log.debug("Class name is: " + e.getValue().getClass().getName());
...@@ -95,7 +100,6 @@ public class ParameterLoaderNode extends AbstractNodeMain { ...@@ -95,7 +100,6 @@ public class ParameterLoaderNode extends AbstractNodeMain {
@Override @Override
public void onStart(ConnectedNode connectedNode) { public void onStart(ConnectedNode connectedNode) {
if (params != null) {
ParameterTree parameterTree = connectedNode.getParameterTree(); ParameterTree parameterTree = connectedNode.getParameterTree();
log = connectedNode.getLog(); log = connectedNode.getLog();
...@@ -107,7 +111,6 @@ public class ParameterLoaderNode extends AbstractNodeMain { ...@@ -107,7 +111,6 @@ public class ParameterLoaderNode extends AbstractNodeMain {
connectedNode.shutdown(); connectedNode.shutdown();
} }
}
/** /**
* Resource to load to Parameter Server, consisting of an InputStream and its corresponding namespace. * Resource to load to Parameter Server, consisting of an InputStream and its corresponding namespace.
...@@ -128,11 +131,11 @@ public class ParameterLoaderNode extends AbstractNodeMain { ...@@ -128,11 +131,11 @@ public class ParameterLoaderNode extends AbstractNodeMain {
* keep the code simple. * keep the code simple.
*/ */
private class LoadedResource { private class LoadedResource {
public Map<String, Object> resource; private Map<?, ?> resource;
public String namespace; private String namespace;
LoadedResource(Object resource, String namespace) { LoadedResource(Map resource, String namespace) {
this.resource = (Map<String, Object>) resource; this.resource = resource;
this.namespace = namespace; this.namespace = namespace;
} }
} }
......
/*
* Copyright (C) 2018 Ekumen, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.ros.helpers;
import org.apache.commons.logging.Log;
import org.junit.Before;
import org.junit.Test;
import org.ros.RosTest;
import org.ros.exception.ParameterNotFoundException;
import org.ros.namespace.GraphName;
import org.ros.node.AbstractNodeMain;
import org.ros.node.ConnectedNode;
import org.ros.node.DefaultNodeListener;
import org.ros.node.Node;
import org.ros.node.NodeListener;
import org.ros.node.parameter.ParameterTree;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author jubeira@ekumenlabs.com (Juan I. Ubeira)
*/
public class ParameterLoaderNodeTest extends RosTest {
private ParameterTree parameters;
private Log log;
@Before
public void setup() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
nodeMainExecutor.execute(new AbstractNodeMain() {
@Override
public GraphName getDefaultNodeName() {
return GraphName.of("node_name");
}
@Override
public void onStart(ConnectedNode connectedNode) {
parameters = connectedNode.getParameterTree();
log = connectedNode.getLog();
latch.countDown();
}
}, nodeConfiguration);
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
@Test
public void testParameterLoad() throws InterruptedException {
final String namespace = "foo";
List<ParameterLoaderNode.Resource> resourceList = new ArrayList<ParameterLoaderNode.Resource>() {{
add(new ParameterLoaderNode.Resource(getClass().getResourceAsStream("/parameters.yaml"), ""));
add(new ParameterLoaderNode.Resource(getClass().getResourceAsStream("/parameters.yaml"), namespace));
}};
ParameterLoaderNode parameterLoaderNode = new ParameterLoaderNode(resourceList);
final CountDownLatch parameterNodeLatch = new CountDownLatch(1);
nodeMainExecutor.execute(parameterLoaderNode, nodeConfiguration, new ArrayList<NodeListener>() {{
add(new DefaultNodeListener() {
@Override
public void onShutdown(Node node) {
parameterNodeLatch.countDown();
}
});
}});
assertTrue(parameterNodeLatch.await(1, TimeUnit.SECONDS));
try {
// Without namespace.
assertEquals("bar", parameters.getString("/string_param"));
assertEquals(1823, parameters.getInteger("/int_param"));
assertEquals(1.74, parameters.getDouble("/double_param"), 0.001);
assertEquals(false, parameters.getBoolean("/boolean_param"));
List<?> list = parameters.getList("/list_param");
assertEquals("Hello", list.get(0));
assertEquals(1, list.get(1));
assertEquals(2.3, list.get(2));
assertEquals(true, list.get(3));
// With namespace.
assertEquals("bar", parameters.getString(namespace + "/string_param"));
assertEquals(1823, parameters.getInteger(namespace + "/int_param"));
assertEquals(1.74, parameters.getDouble(namespace + "/double_param"), 0.001);
assertEquals(false, parameters.getBoolean(namespace + "/boolean_param"));
list = parameters.getList(namespace + "/list_param");
assertEquals("Hello", list.get(0));
assertEquals(1, list.get(1));
assertEquals(2.3, list.get(2));
assertEquals(true, list.get(3));
} catch (ParameterNotFoundException e) {
log.error("Error: " + e.getMessage());
fail();
}
}
@Test
public void testEmptyYaml() throws InterruptedException {
List<ParameterLoaderNode.Resource> resourceList = new ArrayList<ParameterLoaderNode.Resource>() {{
add(new ParameterLoaderNode.Resource(getClass().getResourceAsStream("/empty.yaml"), ""));
}};
ParameterLoaderNode parameterLoaderNode = new ParameterLoaderNode(resourceList);
final CountDownLatch parameterNodeLatch = new CountDownLatch(1);
nodeMainExecutor.execute(parameterLoaderNode, nodeConfiguration, new ArrayList<NodeListener>() {{
add(new DefaultNodeListener() {
@Override
public void onShutdown(Node node) {
parameterNodeLatch.countDown();
}
});
}});
// No exceptions shall be thrown on node execution, and it should shut down properly.
assertTrue(parameterNodeLatch.await(1, TimeUnit.SECONDS));
}
}
string_param: "bar"
int_param: 1823
double_param: 1.74
boolean_param: false
list_param:
- "Hello"
- 1
- 2.3
- true
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment