|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 Ekumen, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package org.ros.helpers; |
| 17 | + |
| 18 | + |
| 19 | +import org.apache.commons.logging.Log; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | +import org.ros.RosTest; |
| 23 | +import org.ros.exception.ParameterNotFoundException; |
| 24 | +import org.ros.namespace.GraphName; |
| 25 | +import org.ros.node.AbstractNodeMain; |
| 26 | +import org.ros.node.ConnectedNode; |
| 27 | +import org.ros.node.DefaultNodeListener; |
| 28 | +import org.ros.node.Node; |
| 29 | +import org.ros.node.NodeListener; |
| 30 | +import org.ros.node.parameter.ParameterTree; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.CountDownLatch; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | + |
| 37 | +import static org.junit.Assert.assertEquals; |
| 38 | +import static org.junit.Assert.assertTrue; |
| 39 | +import static org.junit.Assert.fail; |
| 40 | + |
| 41 | + |
| 42 | +/** |
| 43 | + * @author [email protected] (Juan I. Ubeira) |
| 44 | + */ |
| 45 | +public class ParameterLoaderNodeTest extends RosTest { |
| 46 | + |
| 47 | + private ParameterTree parameters; |
| 48 | + private Log log; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setup() throws InterruptedException { |
| 52 | + final CountDownLatch latch = new CountDownLatch(1); |
| 53 | + nodeMainExecutor.execute(new AbstractNodeMain() { |
| 54 | + @Override |
| 55 | + public GraphName getDefaultNodeName() { |
| 56 | + return GraphName.of("node_name"); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onStart(ConnectedNode connectedNode) { |
| 61 | + parameters = connectedNode.getParameterTree(); |
| 62 | + log = connectedNode.getLog(); |
| 63 | + latch.countDown(); |
| 64 | + } |
| 65 | + }, nodeConfiguration); |
| 66 | + assertTrue(latch.await(1, TimeUnit.SECONDS)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testParameterLoad() throws InterruptedException { |
| 71 | + final String namespace = "foo"; |
| 72 | + List<ParameterLoaderNode.Resource> resourceList = new ArrayList<ParameterLoaderNode.Resource>() {{ |
| 73 | + add(new ParameterLoaderNode.Resource(getClass().getResourceAsStream("/parameters.yaml"), "")); |
| 74 | + add(new ParameterLoaderNode.Resource(getClass().getResourceAsStream("/parameters.yaml"), namespace)); |
| 75 | + }}; |
| 76 | + ParameterLoaderNode parameterLoaderNode = new ParameterLoaderNode(resourceList); |
| 77 | + |
| 78 | + final CountDownLatch parameterNodeLatch = new CountDownLatch(1); |
| 79 | + nodeMainExecutor.execute(parameterLoaderNode, nodeConfiguration, new ArrayList<NodeListener>() {{ |
| 80 | + add(new DefaultNodeListener() { |
| 81 | + @Override |
| 82 | + public void onShutdown(Node node) { |
| 83 | + parameterNodeLatch.countDown(); |
| 84 | + } |
| 85 | + }); |
| 86 | + }}); |
| 87 | + |
| 88 | + assertTrue(parameterNodeLatch.await(1, TimeUnit.SECONDS)); |
| 89 | + |
| 90 | + try { |
| 91 | + // Without namespace. |
| 92 | + assertEquals("bar", parameters.getString("/string_param")); |
| 93 | + assertEquals(1823, parameters.getInteger("/int_param")); |
| 94 | + assertEquals(1.74, parameters.getDouble("/double_param"), 0.001); |
| 95 | + assertEquals(false, parameters.getBoolean("/boolean_param")); |
| 96 | + List<?> list = parameters.getList("/list_param"); |
| 97 | + assertEquals("Hello", list.get(0)); |
| 98 | + assertEquals(1, list.get(1)); |
| 99 | + assertEquals(2.3, list.get(2)); |
| 100 | + assertEquals(true, list.get(3)); |
| 101 | + |
| 102 | + // With namespace. |
| 103 | + assertEquals("bar", parameters.getString(namespace + "/string_param")); |
| 104 | + assertEquals(1823, parameters.getInteger(namespace + "/int_param")); |
| 105 | + assertEquals(1.74, parameters.getDouble(namespace + "/double_param"), 0.001); |
| 106 | + assertEquals(false, parameters.getBoolean(namespace + "/boolean_param")); |
| 107 | + list = parameters.getList(namespace + "/list_param"); |
| 108 | + assertEquals("Hello", list.get(0)); |
| 109 | + assertEquals(1, list.get(1)); |
| 110 | + assertEquals(2.3, list.get(2)); |
| 111 | + assertEquals(true, list.get(3)); |
| 112 | + } catch (ParameterNotFoundException e) { |
| 113 | + log.error("Error: " + e.getMessage()); |
| 114 | + fail(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testEmptyYaml() throws InterruptedException { |
| 120 | + List<ParameterLoaderNode.Resource> resourceList = new ArrayList<ParameterLoaderNode.Resource>() {{ |
| 121 | + add(new ParameterLoaderNode.Resource(getClass().getResourceAsStream("/empty.yaml"), "")); |
| 122 | + }}; |
| 123 | + ParameterLoaderNode parameterLoaderNode = new ParameterLoaderNode(resourceList); |
| 124 | + |
| 125 | + final CountDownLatch parameterNodeLatch = new CountDownLatch(1); |
| 126 | + nodeMainExecutor.execute(parameterLoaderNode, nodeConfiguration, new ArrayList<NodeListener>() {{ |
| 127 | + add(new DefaultNodeListener() { |
| 128 | + @Override |
| 129 | + public void onShutdown(Node node) { |
| 130 | + parameterNodeLatch.countDown(); |
| 131 | + } |
| 132 | + }); |
| 133 | + }}); |
| 134 | + |
| 135 | + // No exceptions shall be thrown on node execution, and it should shut down properly. |
| 136 | + assertTrue(parameterNodeLatch.await(1, TimeUnit.SECONDS)); |
| 137 | + } |
| 138 | +} |
0 commit comments