Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbourelly999 committed Oct 31, 2023
1 parent 9ccc133 commit 62e8369
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public void initialize(long startTime, long endTime) throws InternalFederateExce
try{
URL xmlRpcServerUrl = new URL(carlaConfig.carlaCDASimAdapterUrl);
carlaXmlRpcClient = new CarlaXmlRpcClient(xmlRpcServerUrl);
carlaXmlRpcClient.initialize();
}
catch (MalformedURLException m)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,25 @@
*/
public class CarlaXmlRpcClient{

boolean isConnected;
private boolean isConnected;
private static final String CREATE_SENSOR = "create_simulated_semantic_lidar_sensor";
private static final String GET_DETECTED_OBJECTS = "get_detected_objects";

private XmlRpcClient client;
private URL xmlRpcServerUrl;
private final Logger log = LoggerFactory.getLogger(this.getClass());


public CarlaXmlRpcClient(URL xmlRpcServerUrl) {
initialize(xmlRpcServerUrl);
this.xmlRpcServerUrl = xmlRpcServerUrl;
}

/**
* This method is used to send a stop message to the python server side to shut down server from there
*/
public void closeConnection()
{
log.info("carla connection server closing");
isConnected = false;
}



/**
* need to getting a URL to connect to from ambassador
* @param xmlRpcServerUrl
*/
public void initialize(URL xmlRpcServerUrl)
public void initialize()
{
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(xmlRpcServerUrl);
Expand Down Expand Up @@ -89,7 +80,7 @@ public DetectedObject[] getDetectedObjects(String infrastructureId ,String senso
if (isConnected) {
Object[] params = new Object[]{infrastructureId, sensorId};
Object result = client.execute(GET_DETECTED_OBJECTS, params);
log.info("Detections from infrastructure {} sensor {} : {}", infrastructureId, sensorId, result);
log.debug("Detections from infrastructure {} sensor {} : {}", infrastructureId, sensorId, result);
String jsonResult = (String)result;
Gson gson = new Gson();
DetectedObject[] parsedMessage = gson.fromJson(jsonResult,
Expand All @@ -102,4 +93,8 @@ public DetectedObject[] getDetectedObjects(String infrastructureId ,String senso
}
}

public void closeConnection() {
log.warn("Closing XML RPC Client connection in CARLA Ambassador!");
isConnected = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.eclipse.mosaic.fed.carla.carlaconnect.CarlaXmlRpcClient;
import org.eclipse.mosaic.lib.util.junit.TestFileRule;
import org.eclipse.mosaic.rti.TIME;
import org.eclipse.mosaic.rti.api.RtiAmbassador;
Expand Down
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));

}
}

0 comments on commit 62e8369

Please sign in to comment.