From f02ac5b072c5e092869aca8a73d8383fe8ce48d8 Mon Sep 17 00:00:00 2001 From: dev Date: Tue, 11 Jun 2024 11:12:22 -0400 Subject: [PATCH] Address compile warnings and add unit test coverage --- .../CDASimAdapter/src/CDASimAdapter.cpp | 16 +------- .../src/include/CDASimConnection.hpp | 9 ++-- ...onnection.cpp => TestCDASimConnection.cpp} | 41 ++++++++++++------- 3 files changed, 34 insertions(+), 32 deletions(-) rename src/v2i-hub/CDASimAdapter/test/{TestCARMASimulationConnection.cpp => TestCDASimConnection.cpp} (74%) diff --git a/src/v2i-hub/CDASimAdapter/src/CDASimAdapter.cpp b/src/v2i-hub/CDASimAdapter/src/CDASimAdapter.cpp index b33ad80a3..38eb994ab 100644 --- a/src/v2i-hub/CDASimAdapter/src/CDASimAdapter.cpp +++ b/src/v2i-hub/CDASimAdapter/src/CDASimAdapter.cpp @@ -112,24 +112,12 @@ namespace CDASimAdapter{ "\nUsing Registration Port : " << std::to_string( simulation_registration_port) << " Time Sync Port: " << std::to_string( time_sync_port) << " and V2X Port: " << std::to_string(v2x_port) << std::endl; if ( connection ) { - if ( sensor_json_file_path.empty()) { - connection.reset(new CDASimConnection( simulation_ip, infrastructure_id, simulation_registration_port, sim_v2x_port, local_ip, - time_sync_port, simulated_interaction_port, v2x_port, location )); - } - else { - connection.reset(new CDASimConnection( simulation_ip, infrastructure_id, simulation_registration_port, sim_v2x_port, local_ip, + connection.reset(new CDASimConnection( simulation_ip, infrastructure_id, simulation_registration_port, sim_v2x_port, local_ip, time_sync_port, simulated_interaction_port, v2x_port, location, sensor_json_file_path )); - } } else { - if ( sensor_json_file_path.empty()) { - connection = std::make_unique(simulation_ip, infrastructure_id, simulation_registration_port, sim_v2x_port, local_ip, - time_sync_port, simulated_interaction_port, v2x_port, location); - } - else { - connection = std::make_unique(simulation_ip, infrastructure_id, simulation_registration_port, sim_v2x_port, local_ip, + connection = std::make_unique(simulation_ip, infrastructure_id, simulation_registration_port, sim_v2x_port, local_ip, time_sync_port, simulated_interaction_port, v2x_port, location, sensor_json_file_path); - } } } catch (const TmxException &e) { diff --git a/src/v2i-hub/CDASimAdapter/src/include/CDASimConnection.hpp b/src/v2i-hub/CDASimAdapter/src/include/CDASimConnection.hpp index c07c9bdd8..2c8a03bf5 100644 --- a/src/v2i-hub/CDASimAdapter/src/include/CDASimConnection.hpp +++ b/src/v2i-hub/CDASimAdapter/src/include/CDASimConnection.hpp @@ -173,9 +173,12 @@ namespace CDASimAdapter { std::shared_ptr time_sync_listener; std::shared_ptr sensor_detected_object_listener; - FRIEND_TEST(TestCARMASimulationConnection, get_handshake_json); - FRIEND_TEST(TestCARMASimulationConnection, read_json_file); - FRIEND_TEST(TestCARMASimulationConnection, string_to_json); + FRIEND_TEST(TestCDASimConnection, get_handshake_json); + FRIEND_TEST(TestCDASimConnection, get_handshake_json_no_sensor_config); + FRIEND_TEST(TestCDASimConnection, read_json_file); + FRIEND_TEST(TestCDASimConnection, string_to_json); + + }; } diff --git a/src/v2i-hub/CDASimAdapter/test/TestCARMASimulationConnection.cpp b/src/v2i-hub/CDASimAdapter/test/TestCDASimConnection.cpp similarity index 74% rename from src/v2i-hub/CDASimAdapter/test/TestCARMASimulationConnection.cpp rename to src/v2i-hub/CDASimAdapter/test/TestCDASimConnection.cpp index 2e992933e..c1ec10d4d 100644 --- a/src/v2i-hub/CDASimAdapter/test/TestCARMASimulationConnection.cpp +++ b/src/v2i-hub/CDASimAdapter/test/TestCDASimConnection.cpp @@ -19,7 +19,7 @@ using namespace tmx::utils; namespace CDASimAdapter { - class TestCARMASimulationConnection : public ::testing::Test { + class TestCDASimConnection : public ::testing::Test { protected: void SetUp() override { // Initialize CARMA Simulation connection with (0,0,0) location. @@ -35,11 +35,11 @@ namespace CDASimAdapter { }; - TEST_F( TestCARMASimulationConnection, initialize) { + TEST_F( TestCDASimConnection, initialize) { ASSERT_FALSE(connection->is_connected()); } - TEST_F( TestCARMASimulationConnection, forward_message) { + TEST_F( TestCDASimConnection, forward_message) { std::shared_ptr client = std::make_shared(); std::string test_message = "message"; EXPECT_CALL( *client, Send(test_message) ).Times(2).WillOnce(testing::DoAll(Return(-1))).WillRepeatedly(testing::DoAll(Return(test_message.size()))); @@ -47,7 +47,7 @@ namespace CDASimAdapter { connection->forward_message(test_message, client); } - TEST_F( TestCARMASimulationConnection, forward_message_invalid ) { + TEST_F( TestCDASimConnection, forward_message_invalid ) { std::shared_ptr client = std::make_shared(); std::string test_message = ""; // ASSERT that we never call Send message. @@ -59,11 +59,11 @@ namespace CDASimAdapter { connection->forward_message(test_message, client); } - TEST_F( TestCARMASimulationConnection, consume_msg){ + TEST_F( TestCDASimConnection, consume_msg){ std::shared_ptr server = std::make_shared(); char *msg_data = new char(); - char *test_string = "Test Message"; + char test_string[] = "Test Message"; EXPECT_CALL( *server, TimedReceive(_, _, _) ).Times(2). WillOnce(testing::DoAll(Return(-1))). WillRepeatedly( testing::DoAll( SetArrayArgument<0>(test_string, test_string + strlen(test_string) + 1),Return(10))); @@ -78,11 +78,11 @@ namespace CDASimAdapter { } - TEST_F( TestCARMASimulationConnection, setup_upd_connection) { + TEST_F( TestCDASimConnection, setup_upd_connection) { ASSERT_TRUE(connection->setup_udp_connection("127.0.0.1", "127.0.0.1", 4567, 4568, 4569, 4570)); } - TEST_F( TestCARMASimulationConnection, get_handshake_json) { + TEST_F( TestCDASimConnection, get_handshake_json) { Point location; location.X = 1000; location.Y = 38.955; @@ -91,23 +91,34 @@ namespace CDASimAdapter { in_strm.open(sensors_file_path, std::ifstream::binary); if(in_strm.is_open()) { - ASSERT_EQ(connection->get_handshake_json("4566", "127.0.0.1", 4567, 4568, 4569, location), + EXPECT_EQ(connection->get_handshake_json("4566", "127.0.0.1", 4567, 4568, 4569, location), "{\n \"infrastructureId\" : \"4566\",\n \"location\" : {\n \"x\" : 1000.0,\n \"y\" : 38.954999999999998,\n \"z\" : -77.149000000000001\n },\n \"rxMessageIpAddress\" : \"127.0.0.1\",\n \"rxMessagePort\" : 4569,\n \"sensors\" : [\n {\n \"location\" : {\n \"x\" : 0.0,\n \"y\" : 0.0,\n \"z\" : 0.0\n },\n \"orientation\" : {\n \"pitch\" : 0.0,\n \"roll\" : 0.0,\n \"yaw\" : 0.0\n },\n \"sensorId\" : \"SomeID\",\n \"type\" : \"SemanticLidar\"\n },\n {\n \"location\" : {\n \"x\" : 1.0,\n \"y\" : 2.0,\n \"z\" : 0.0\n },\n \"orientation\" : {\n \"pitch\" : 0.0,\n \"roll\" : 0.0,\n \"yaw\" : 23.0\n },\n \"sensorId\" : \"SomeID2\",\n \"type\" : \"SemanticLidar\"\n }\n ],\n \"simulatedInteractionPort\" : 4568,\n \"timeSyncPort\" : 4567\n}\n"); } } - TEST_F( TestCARMASimulationConnection, carma_simulation_handshake) { + TEST_F( TestCDASimConnection, get_handshake_json_no_sensor_config) { + Point location; + location.X = 1000; + location.Y = 38.955; + location.Z = -77.149; + connection.reset(new CDASimConnection("127.0.0.1", "1212", 4567, 4678, "127.0.0.1", 1213, 1214, 1215, location)); + // Test method when sensors_file_path is empty + EXPECT_EQ(connection->get_handshake_json("4566", "127.0.0.1", 4567, 4568, 4569, location), + "{\n \"infrastructureId\" : \"4566\",\n \"location\" : {\n \"x\" : 1000.0,\n \"y\" : 38.954999999999998,\n \"z\" : -77.149000000000001\n },\n \"rxMessageIpAddress\" : \"127.0.0.1\",\n \"rxMessagePort\" : 4569,\n \"simulatedInteractionPort\" : 4568,\n \"timeSyncPort\" : 4567\n}\n"); + } + + TEST_F( TestCDASimConnection, carma_simulation_handshake) { Point location; // UDP creation error - ASSERT_FALSE(connection->carma_simulation_handshake("", "45", NULL, + EXPECT_FALSE(connection->carma_simulation_handshake("", "45", 0, "", 45, 45, 45, location)); } - TEST_F(TestCARMASimulationConnection, connect) { - ASSERT_TRUE(connection->connect()); + TEST_F(TestCDASimConnection, connect) { + EXPECT_TRUE(connection->connect()); } - TEST_F(TestCARMASimulationConnection, read_json_file) + TEST_F(TestCDASimConnection, read_json_file) { auto sensorJsonV = connection->read_json_file("Invalid_file_path" ); ASSERT_TRUE(sensorJsonV.empty()); @@ -120,7 +131,7 @@ namespace CDASimAdapter { } } - TEST_F(TestCARMASimulationConnection, string_to_json) + TEST_F(TestCDASimConnection, string_to_json) { auto sensorJsonV = connection->string_to_json("Invalid Json"); ASSERT_TRUE(sensorJsonV.empty());