-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ccc133
commit 62e8369
Showing
4 changed files
with
269 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
259 changes: 259 additions & 0 deletions
259
...-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
package org.eclipse.mosaic.fed.carla.carlaconnect; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.apache.xmlrpc.XmlRpcException; | ||
import org.apache.xmlrpc.client.XmlRpcClient; | ||
import org.eclipse.mosaic.interactions.detector.DetectorRegistration; | ||
import org.eclipse.mosaic.lib.geo.CartesianPoint; | ||
import org.eclipse.mosaic.lib.math.Vector3d; | ||
import org.eclipse.mosaic.lib.objects.detector.DetectedObject; | ||
import org.eclipse.mosaic.lib.objects.detector.DetectionType; | ||
import org.eclipse.mosaic.lib.objects.detector.Detector; | ||
import org.eclipse.mosaic.lib.objects.detector.DetectorType; | ||
import org.eclipse.mosaic.lib.objects.detector.Orientation; | ||
import org.eclipse.mosaic.lib.objects.detector.Size; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.internal.util.reflection.FieldSetter; | ||
|
||
public class CarlaXmlRpcClientTest { | ||
|
||
private XmlRpcClient mockClient; | ||
|
||
private CarlaXmlRpcClient carlaConnection; | ||
|
||
|
||
@Before | ||
public void setup() throws NoSuchFieldException, MalformedURLException{ | ||
mockClient = mock(XmlRpcClient.class); | ||
URL xmlRpcServerUrl = new URL("http://test_url"); | ||
carlaConnection = new CarlaXmlRpcClient(xmlRpcServerUrl); | ||
carlaConnection.initialize(); | ||
// Set mock after initialize since initialize overwrites member | ||
FieldSetter.setField(carlaConnection, carlaConnection.getClass().getDeclaredField("client"), mockClient); | ||
} | ||
|
||
/** | ||
* Test to see if createSensor runs without exception | ||
* @throws XmlRpcException | ||
*/ | ||
@Test | ||
public void testCreateSensor() throws XmlRpcException { | ||
// Create Detector Registration | ||
Detector detector = new Detector("sensorID1", DetectorType.SEMANTIC_LIDAR, new Orientation( 0.0,0.0,0.0), CartesianPoint.ORIGO); | ||
DetectorRegistration registration = new DetectorRegistration(0, detector, "rsu_2"); | ||
// Create request params | ||
List<Double> location = Arrays.asList(registration.getDetector().getLocation().getX(), registration.getDetector().getLocation().getY(), registration.getDetector().getLocation().getZ()); | ||
List<Double> orientation = Arrays.asList(registration.getDetector().getOrientation().getPitch(), registration.getDetector().getOrientation().getRoll(), registration.getDetector().getOrientation().getYaw()); | ||
Object[] params = new Object[]{registration.getInfrastructureId(), registration.getDetector().getSensorId(), location, orientation}; | ||
// Tell mock to return sensor ID when following method is called with following parameters | ||
when( mockClient.execute("create_simulated_semantic_lidar_sensor", params)).thenReturn(registration.getSenderId()); | ||
// Method has no return so verifying that it is successful is just verifying no exception is thrown | ||
carlaConnection.createSensor(registration); | ||
// Verify following method was called on mock | ||
verify( mockClient, times(1)).execute("create_simulated_semantic_lidar_sensor", params); | ||
} | ||
|
||
@Test | ||
public void testCreateSensorException() throws XmlRpcException { | ||
// Create Detector Registration | ||
Detector detector = new Detector("sensorID1", DetectorType.SEMANTIC_LIDAR, new Orientation( 0.0,0.0,0.0), CartesianPoint.ORIGO); | ||
DetectorRegistration registration = new DetectorRegistration(0, detector, "rsu_2"); | ||
// Create request params | ||
List<Double> location = Arrays.asList(registration.getDetector().getLocation().getX(), registration.getDetector().getLocation().getY(), registration.getDetector().getLocation().getZ()); | ||
List<Double> orientation = Arrays.asList(registration.getDetector().getOrientation().getPitch(), registration.getDetector().getOrientation().getRoll(), registration.getDetector().getOrientation().getYaw()); | ||
Object[] params = new Object[]{registration.getInfrastructureId(), registration.getDetector().getSensorId(), location, orientation}; | ||
// Tell mock to return sensor ID when following method is called with following parameters | ||
when( mockClient.execute("create_simulated_semantic_lidar_sensor", params)).thenThrow(XmlRpcException.class); | ||
try { | ||
// Method has no return so verifying that it is successful is just verifying no exception is thrown | ||
carlaConnection.createSensor(registration); | ||
// Verify following method was called on mock | ||
} | ||
catch( Exception e) { | ||
assertEquals(e.getClass(), XmlRpcException.class); | ||
} | ||
verify( mockClient, times(1)).execute("create_simulated_semantic_lidar_sensor", params); | ||
} | ||
|
||
@Test | ||
public void testGetDetectedObjects() throws XmlRpcException { | ||
// Create return JSON String | ||
String json = "[{" | ||
+ "\"type\":\"CAR\"," | ||
+ "\"confidence\":0.7," | ||
+ "\"sensorId\":\"sensor1\"," | ||
+ "\"projString\":\"projection String2\"," | ||
+ "\"objectId\":\"Object7\"," | ||
+ "\"position\":" | ||
+ "{" | ||
+ "\"x\":-1.1," | ||
+ "\"y\":-2.0," | ||
+ "\"z\":-3.2" | ||
+ "}," | ||
+ "\"positionCovariance\":[[1.0,0.0,0.0],[1.0,0.0,0.0],[1.0,0.0,0.0]]," | ||
+ "\"velocity\":" | ||
+ "{" | ||
+ "\"x\":1," | ||
+ "\"y\":1," | ||
+ "\"z\":1" | ||
+ "}," | ||
+ "\"velocityCovariance\":[[1.0,0.0,0.0],[1.0,0.0,0.0],[1.0,0.0,0.0]]," | ||
+ "\"angularVelocity\":" | ||
+ "{" | ||
+ "\"x\":0.1," | ||
+ "\"y\":0.2," | ||
+ "\"z\":0.3" | ||
+ "}," | ||
+ "\"angularVelocityCovariance\":[[1.0,0.0,0.0],[1.0,0.0,0.0],[1.0,0.0,0.0]]," | ||
+ "\"size\":" | ||
+ "{" | ||
+ "\"length\":2.0," | ||
+ "\"height\":1.0," | ||
+ "\"width\":0.5" | ||
+ "}" | ||
+ "}," | ||
+ "{" | ||
+ "\"type\":\"BUS\"," | ||
+ "\"confidence\":0.5," | ||
+ "\"sensorId\":\"sensor1\"," | ||
+ "\"projString\":\"projection String\"," | ||
+ "\"objectId\":\"Object1\"," | ||
+ "\"position\":" | ||
+ "{" | ||
+ "\"x\":1.1," | ||
+ "\"y\":2.0," | ||
+ "\"z\":3.2" | ||
+ "}," | ||
+ "\"positionCovariance\":[[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]]," | ||
+ "\"velocity\":" | ||
+ "{" | ||
+ "\"x\":0.0," | ||
+ "\"y\":0.0," | ||
+ "\"z\":0.0" | ||
+ "}," | ||
+ "\"velocityCovariance\":[[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]]," | ||
+ "\"angularVelocity\":" | ||
+ "{" | ||
+ "\"x\":0.0," | ||
+ "\"y\":0.0," | ||
+ "\"z\":0.0" | ||
+ "}," | ||
+ "\"angularVelocityCovariance\":[[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]]," | ||
+ "\"size\":" | ||
+ "{" | ||
+ "\"length\":0.0," | ||
+ "\"height\":0.0," | ||
+ "\"width\":0.0" | ||
+ "}" | ||
+ "}" | ||
+ "]"; | ||
// Create request params | ||
Object[] params = new Object[]{"rsu_1", "sensorId_1"}; | ||
// Tell mock to return sensor ID when following method is called with following parameters | ||
when( mockClient.execute("get_detected_objects", params)).thenReturn(json); | ||
// Method has no return so verifying that it is successful is just verifying no exception is thrown | ||
DetectedObject[] detectedObjects = carlaConnection.getDetectedObjects("rsu_1", "sensorId_1"); | ||
// Verify following method was called on mock | ||
verify( mockClient, times(1)).execute("get_detected_objects", params); | ||
// Confirm JSON payload is correctly serialized | ||
assertEquals(2, detectedObjects.length); | ||
// Object 1 is CAR | ||
DetectedObject predictedCar = new DetectedObject( | ||
DetectionType.CAR, | ||
0.7, | ||
"sensor1", | ||
"projection String2", | ||
"Object7", | ||
CartesianPoint.xyz(-1.1, -2, -3.2), | ||
new Vector3d(1, 1, 1), | ||
new Vector3d(.1, .2, .3), | ||
new Size(2, 1, .5)); | ||
Double[][] covarianceMatrix = { {1.0, 0.0, 0.0} , {1.0, 0.0, 0.0} , {1.0, 0.0, 0.0}}; | ||
predictedCar.setPositionCovariance(covarianceMatrix); | ||
predictedCar.setVelocityCovariance(covarianceMatrix); | ||
predictedCar.setAngularVelocityCovariance(covarianceMatrix); | ||
assertEquals(predictedCar, detectedObjects[0]); | ||
|
||
DetectedObject predictedBus = new DetectedObject( | ||
DetectionType.BUS, | ||
0.5, | ||
"sensor1", | ||
"projection String", | ||
"Object1", | ||
CartesianPoint.xyz(1.1, 2, 3.2), | ||
new Vector3d(0, 0, 0), | ||
new Vector3d(), | ||
new Size(0, 0, 0)); | ||
Double[][] bus_covarianceMatrix = { {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0}}; | ||
predictedBus.setPositionCovariance(bus_covarianceMatrix); | ||
predictedBus.setVelocityCovariance(bus_covarianceMatrix); | ||
predictedBus.setAngularVelocityCovariance(bus_covarianceMatrix); | ||
assertEquals(predictedBus, detectedObjects[1]); | ||
|
||
} | ||
|
||
@Test | ||
public void testGetDetectedObjectsException() throws XmlRpcException { | ||
// Create request params | ||
Object[] params = new Object[]{"rsu_1", "sensorId_1"}; | ||
// Tell mock to return sensor ID when following method is called with following parameters | ||
when( mockClient.execute("get_detected_objects", params)).thenThrow(XmlRpcException.class); | ||
try{ | ||
// Method has no return so verifying that it is successful is just verifying no exception is thrown | ||
DetectedObject[] detectedObjects = carlaConnection.getDetectedObjects("rsu_1", "sensorId_1"); | ||
} | ||
catch(Exception e) { | ||
assertEquals(e.getClass(), XmlRpcException.class); | ||
} | ||
// Verify following method was called on mock | ||
verify( mockClient, times(1)).execute("get_detected_objects", params); | ||
} | ||
|
||
@Test | ||
public void testCloseConnection() throws XmlRpcException { | ||
// Create request params | ||
// Create Detector Registration | ||
Detector detector = new Detector("sensorID1", DetectorType.SEMANTIC_LIDAR, new Orientation( 0.0,0.0,0.0), CartesianPoint.ORIGO); | ||
DetectorRegistration registration = new DetectorRegistration(0, detector, "rsu_2"); | ||
List<Double> location = Arrays.asList(registration.getDetector().getLocation().getX(), registration.getDetector().getLocation().getY(), registration.getDetector().getLocation().getZ()); | ||
List<Double> orientation = Arrays.asList(registration.getDetector().getOrientation().getPitch(), registration.getDetector().getOrientation().getRoll(), registration.getDetector().getOrientation().getYaw()); | ||
Object[] params = new Object[]{registration.getInfrastructureId(), registration.getDetector().getSensorId(), location, orientation}; | ||
// Tell mock to return sensor ID when following method is called with following parameters | ||
when( mockClient.execute("create_simulated_semantic_lidar_sensor", params)).thenReturn(registration.getSenderId()); | ||
Object[] get_detected_object_params = new Object[]{registration.getInfrastructureId(), registration.getDetector().getSensorId()}; | ||
// Tell mock to return sensor ID when following method is called with following parameters | ||
when( mockClient.execute("get_detected_objects", get_detected_object_params)).thenReturn(""); | ||
carlaConnection.closeConnection(); | ||
try { | ||
carlaConnection.createSensor(registration); | ||
} | ||
catch( Exception e ) { | ||
assertEquals(XmlRpcException.class, e.getClass()); | ||
assertEquals("XMLRpcClient is not connected to CARLA Adapter!", e.getMessage()); | ||
} | ||
try { | ||
carlaConnection.getDetectedObjects(registration.getInfrastructureId(), registration.getDetector().getSensorId()); | ||
} | ||
catch( Exception e ) { | ||
assertEquals(XmlRpcException.class, e.getClass()); | ||
assertEquals("XMLRpcClient is not connected to CARLA Adapter!", e.getMessage()); | ||
} | ||
// Assert execute is never called on XMLRPC Client after connection is closed. | ||
verify( mockClient, times(0)).execute(any(String.class), any(Object[].class)); | ||
|
||
} | ||
} |