From c1c334f5395d91e7c107fb2bc9859a715912c390 Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Wed, 26 Jul 2023 16:45:25 +0000 Subject: [PATCH] reafactor func --- .../CDASimAdapter/src/CDASimConnection.cpp | 15 +++++++++------ .../src/include/CDASimConnection.hpp | 11 ++++++----- .../test/TestCARMASimulationConnection.cpp | 13 +++++++++---- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/v2i-hub/CDASimAdapter/src/CDASimConnection.cpp b/src/v2i-hub/CDASimAdapter/src/CDASimConnection.cpp index 7f2ed2d49..b223de4a0 100644 --- a/src/v2i-hub/CDASimAdapter/src/CDASimConnection.cpp +++ b/src/v2i-hub/CDASimAdapter/src/CDASimConnection.cpp @@ -21,7 +21,7 @@ namespace CDASimAdapter{ bool CDASimConnection::connect() { //Read local sensor file and populate the sensors JSON - populate_sensors_with_file(_sensor_json_file_path, _sensors_json_v); + _sensors_json_v = read_json_file(_sensor_json_file_path); if(_sensors_json_v.empty()) { PLOG(logWARNING) << "Sensors JSON is empty!" << std::endl; @@ -224,14 +224,15 @@ namespace CDASimAdapter{ forward_message( msg , message_receiver_publisher ); } - void CDASimConnection::populate_sensors_with_file(const std::string& file_path, Json::Value& sensors_json_v){ + Json::Value CDASimConnection::read_json_file(const std::string& file_path) const{ + Json::Value sensors_json_v; //Read file from disk std::ifstream in_strm; in_strm.open(file_path, std::ifstream::binary); if(!in_strm.is_open()) { PLOG(logERROR) << "File cannot be opened. File path: " << file_path < reader(builder.newCharReader()); JSONCPP_STRING err; @@ -252,6 +254,7 @@ namespace CDASimAdapter{ { PLOG(logERROR) << "Error parsing sensors from string: " << json_str << std::endl; } + return json_v; } Json::Value CDASimConnection::get_sensor_by_id(std::string &sensor_id) diff --git a/src/v2i-hub/CDASimAdapter/src/include/CDASimConnection.hpp b/src/v2i-hub/CDASimAdapter/src/include/CDASimConnection.hpp index 95bb61a1d..b11b32b9b 100644 --- a/src/v2i-hub/CDASimAdapter/src/include/CDASimConnection.hpp +++ b/src/v2i-hub/CDASimAdapter/src/include/CDASimConnection.hpp @@ -144,15 +144,15 @@ namespace CDASimAdapter { /** * @brief Read local file that has the sensor information in JSON format from disk. Populate global sensor json variable with the information. * @param file_path A string of file location in the host machine. - * @param sensors_json_v A reference to the location where the sensors inforation is updated and stored. + * @return A reference to the location where the sensors inforation is updated and stored. */ - void populate_sensors_with_file(const std::string& file_path, Json::Value& sensors_json_v); + Json::Value read_json_file(const std::string& file_path) const; /** * @brief Read local file that has the sensor information in JSON format from disk. Populate global sensor json variable with the information. * @param json_str A JSON string. - * @param json_v A reference to JSON value. + * @return A reference to JSON value. */ - void populate_json_with_string(const std::string &json_str, Json::Value& json_v); + Json::Value string_to_json(const std::string &json_str) const; std::string _simulation_ip; uint _simulation_registration_port; @@ -175,7 +175,8 @@ namespace CDASimAdapter { std::shared_ptr time_sync_listener; FRIEND_TEST(TestCARMASimulationConnection, get_handshake_json); - FRIEND_TEST(TestCARMASimulationConnection, populate_sensors_with_file); + FRIEND_TEST(TestCARMASimulationConnection, read_json_file); + FRIEND_TEST(TestCARMASimulationConnection, string_to_json); }; } diff --git a/src/v2i-hub/CDASimAdapter/test/TestCARMASimulationConnection.cpp b/src/v2i-hub/CDASimAdapter/test/TestCARMASimulationConnection.cpp index b3c7355e2..41468c542 100644 --- a/src/v2i-hub/CDASimAdapter/test/TestCARMASimulationConnection.cpp +++ b/src/v2i-hub/CDASimAdapter/test/TestCARMASimulationConnection.cpp @@ -104,20 +104,25 @@ namespace CDASimAdapter { ASSERT_TRUE(connection->connect()); } - TEST_F(TestCARMASimulationConnection, populate_sensors_with_file) + TEST_F(TestCARMASimulationConnection, read_json_file) { - Json::Value sensorJsonV; - connection->populate_sensors_with_file("Invalid_file_path", sensorJsonV ); + auto sensorJsonV = connection->read_json_file("Invalid_file_path" ); ASSERT_TRUE(sensorJsonV.empty()); std::ifstream in_strm; in_strm.open(sensors_file_path, std::ifstream::binary); if(in_strm.is_open()) { - connection->populate_sensors_with_file(sensors_file_path, sensorJsonV ); + sensorJsonV = connection->read_json_file(sensors_file_path ); ASSERT_FALSE(sensorJsonV.empty()); } } + TEST_F(TestCARMASimulationConnection, string_to_json) + { + auto sensorJsonV = connection->string_to_json("Invalid Json"); + ASSERT_TRUE(sensorJsonV.empty()); + } + TEST_F(TestCARMASimulationConnection, get_sensor_by_id) { Point location;