diff --git a/src/common/dbpa_interface.h b/src/common/dbpa_interface.h index 3c2b261..726633c 100644 --- a/src/common/dbpa_interface.h +++ b/src/common/dbpa_interface.h @@ -101,7 +101,7 @@ class DBPS_EXPORT DataBatchProtectionAgentInterface { // (e.g., as a serialized map/JSON field). virtual void init( std::string column_name, - std::map connection_config, + std::map configuration_map, std::string app_context, std::string column_key_id, Type::type datatype, @@ -110,7 +110,7 @@ class DBPS_EXPORT DataBatchProtectionAgentInterface { std::optional> column_encryption_metadata) { column_name_ = std::move(column_name); - connection_config_ = std::move(connection_config); + configuration_map_ = std::move(configuration_map); app_context_ = std::move(app_context); column_key_id_ = std::move(column_key_id); datatype_ = datatype; @@ -150,7 +150,7 @@ class DBPS_EXPORT DataBatchProtectionAgentInterface { protected: std::string column_name_; - std::map connection_config_; + std::map configuration_map_; std::string app_context_; // includes user_id std::optional> column_encryption_metadata_; diff --git a/src/common/dbpa_local.cpp b/src/common/dbpa_local.cpp index 289f07d..1413678 100644 --- a/src/common/dbpa_local.cpp +++ b/src/common/dbpa_local.cpp @@ -111,7 +111,7 @@ const std::map& LocalDecryptionResult::error_fields() void LocalDataBatchProtectionAgent::init( std::string column_name, - std::map connection_config, + std::map configuration_map, std::string app_context, std::string column_key_id, Type::type datatype, @@ -126,7 +126,7 @@ void LocalDataBatchProtectionAgent::init( // Call the base class init to store the configuration DataBatchProtectionAgentInterface::init( std::move(column_name), - std::move(connection_config), + std::move(configuration_map), std::move(app_context), std::move(column_key_id), datatype, diff --git a/src/common/dbpa_local.h b/src/common/dbpa_local.h index 8fea72f..7f18093 100644 --- a/src/common/dbpa_local.h +++ b/src/common/dbpa_local.h @@ -104,7 +104,7 @@ class DBPS_EXPORT LocalDataBatchProtectionAgent : public DataBatchProtectionAgen // DataBatchProtectionAgentInterface implementation void init( std::string column_name, - std::map connection_config, + std::map configuration_map, std::string app_context, std::string column_key_id, Type::type datatype, diff --git a/src/common/dbpa_local_test.cpp b/src/common/dbpa_local_test.cpp index aca7778..04345e7 100644 --- a/src/common/dbpa_local_test.cpp +++ b/src/common/dbpa_local_test.cpp @@ -41,10 +41,10 @@ class LocalDataBatchProtectionAgentTest : public ::testing::Test { TEST_F(LocalDataBatchProtectionAgentTest, SuccessfulEncryption) { LocalDataBatchProtectionAgent agent; - std::map connection_config; + std::map configuration_map = {}; std::string app_context = R"({"user_id": "test_user"})"; - EXPECT_NO_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_NO_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::UNDEFINED, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt)); std::vector test_data = {1, 2, 3, 4}; @@ -59,10 +59,10 @@ TEST_F(LocalDataBatchProtectionAgentTest, SuccessfulEncryption) { TEST_F(LocalDataBatchProtectionAgentTest, SuccessfulEncryptionCompressedDictionary) { LocalDataBatchProtectionAgent agent; - std::map connection_config; + std::map configuration_map = {}; std::string app_context = R"({"user_id": "test_user"})"; - EXPECT_NO_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_NO_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::BYTE_ARRAY, std::nullopt, CompressionCodec::GZIP, std::nullopt)); // GZIP compressed data for strings "apple" and "banana" @@ -84,10 +84,10 @@ TEST_F(LocalDataBatchProtectionAgentTest, SuccessfulEncryptionCompressedDictiona TEST_F(LocalDataBatchProtectionAgentTest, SuccessfulDecryption) { LocalDataBatchProtectionAgent agent; - std::map connection_config; + std::map configuration_map = {}; std::string app_context = R"({"user_id": "test_user"})"; - EXPECT_NO_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_NO_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::UNDEFINED, std::nullopt, CompressionCodec::UNCOMPRESSED, DBPS_ENCRYPTION_METADATA)); std::vector test_data = {1, 2, 3, 4}; @@ -103,10 +103,10 @@ TEST_F(LocalDataBatchProtectionAgentTest, SuccessfulDecryption) { TEST_F(LocalDataBatchProtectionAgentTest, RoundTripEncryptDecrypt) { LocalDataBatchProtectionAgent encrypt_agent; - std::map connection_config; + std::map configuration_map = {}; std::string app_context = R"({"user_id": "test_user"})"; - EXPECT_NO_THROW(encrypt_agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_NO_THROW(encrypt_agent.init("test_column", configuration_map, app_context, "test_key", Type::UNDEFINED, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt)); // Original data to encrypt @@ -132,7 +132,7 @@ TEST_F(LocalDataBatchProtectionAgentTest, RoundTripEncryptDecrypt) { // Create a new agent for decryption with the encryption_metadata from the encryption result LocalDataBatchProtectionAgent decrypt_agent; - EXPECT_NO_THROW(decrypt_agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_NO_THROW(decrypt_agent.init("test_column", configuration_map, app_context, "test_key", Type::UNDEFINED, std::nullopt, CompressionCodec::UNCOMPRESSED, encryption_metadata)); // Decrypt the ciphertext @@ -183,10 +183,10 @@ TEST_F(LocalDataBatchProtectionAgentTest, DecryptWithoutInit) { TEST_F(LocalDataBatchProtectionAgentTest, MissingUserId) { LocalDataBatchProtectionAgent agent; - std::map connection_config; + std::map configuration_map = {}; std::string app_context = R"({"role": "admin"})"; - EXPECT_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::BYTE_ARRAY, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt), DBPSException); } @@ -194,10 +194,10 @@ TEST_F(LocalDataBatchProtectionAgentTest, MissingUserId) { TEST_F(LocalDataBatchProtectionAgentTest, MissingPageEncoding) { LocalDataBatchProtectionAgent agent; - std::map connection_config; + std::map configuration_map = {}; std::string app_context = R"({"user_id": "test_user"})"; - EXPECT_NO_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_NO_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::BYTE_ARRAY, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt)); std::vector test_data = {1, 2, 3, 4}; diff --git a/src/common/dbpa_remote.cpp b/src/common/dbpa_remote.cpp index 9f488a1..4839355 100644 --- a/src/common/dbpa_remote.cpp +++ b/src/common/dbpa_remote.cpp @@ -19,6 +19,7 @@ #include "../client/dbps_api_client.h" #include "../client/httplib_pool_registry.h" #include "../client/httplib_pooled_client.h" +#include "dbpa_utils.h" #include "enum_utils.h" #include @@ -167,7 +168,7 @@ RemoteDataBatchProtectionAgent::RemoteDataBatchProtectionAgent(std::shared_ptr connection_config, + std::map configuration_map, std::string app_context, std::string column_key_id, Type::type datatype, @@ -182,7 +183,7 @@ void RemoteDataBatchProtectionAgent::init( // Call the base class init to store the configuration DataBatchProtectionAgentInterface::init( std::move(column_name), - std::move(connection_config), + std::move(configuration_map), std::move(app_context), std::move(column_key_id), datatype, @@ -212,7 +213,7 @@ void RemoteDataBatchProtectionAgent::init( // The API client constructor does not attempt a HTTP connection with the server. The first Get/Post call creates the HTTP connection. if (!api_client_) { // given that there is no API client, instantiate a new one with a new HTTP client. - auto http_client = InstantiateHttpClient(); //uses connection_config_ + auto http_client = InstantiateHttpClient(); //uses configuration_map_ api_client_ = std::make_unique(http_client); } else { std::cerr << "INFO: RemoteDataBatchProtectionAgent::init() - Using existing API client" << std::endl; @@ -366,24 +367,24 @@ void RemoteDataBatchProtectionAgent::UpdateEncryptionMetadata(std::optional RemoteDataBatchProtectionAgent::LoadConnectionConfigFile(const std::map& connection_config) const { - const std::string error_trace = "ERROR: RemoteDataBatchProtectionAgent::LoadConnectionConfigFile() - "; - auto config_file_it = connection_config.find(k_connection_config_key_); - if (config_file_it == connection_config.end()) { +std::optional RemoteDataBatchProtectionAgent::LoadConfigFile(const std::string& config_file_key) const { + const std::string error_trace = "ERROR: RemoteDataBatchProtectionAgent::LoadConfigFile() - "; + auto config_file_it = configuration_map_.find(config_file_key); + if (config_file_it == configuration_map_.end()) { std::cerr << error_trace - << "connection_config does not provide " << k_connection_config_key_ << std::endl; + << "configuration_map does not provide " << config_file_key << std::endl; return std::nullopt; } auto config_file_path = config_file_it->second; if (!std::filesystem::exists(config_file_path)) { - std::cerr << error_trace << k_connection_config_key_ << " [" << config_file_path + std::cerr << error_trace << config_file_key << " [" << config_file_path << "] does not exist" << std::endl; return std::nullopt; } std::ifstream config_file(config_file_path); if (!config_file.is_open()) { - std::cerr << error_trace << k_connection_config_key_ << " [" << config_file_path + std::cerr << error_trace << config_file_key << " [" << config_file_path << "] could not be opened" << std::endl; return std::nullopt; } @@ -393,7 +394,7 @@ std::optional RemoteDataBatchProtectionAgent::LoadConnectionConf config_file.close(); if (config_file_contents.empty()) { - std::cerr << error_trace << k_connection_config_key_ << " [" << config_file_path + std::cerr << error_trace << config_file_key << " [" << config_file_path << "] is empty" << std::endl; return std::nullopt; } @@ -402,19 +403,19 @@ std::optional RemoteDataBatchProtectionAgent::LoadConnectionConf auto json = nlohmann::json::parse(config_file_contents); return json; } catch (const nlohmann::json::exception& e) { - std::cerr << error_trace << k_connection_config_key_ << " [" << config_file_path + std::cerr << error_trace << config_file_key << " [" << config_file_path << "] is not a valid JSON file: " << e.what() << std::endl; return std::nullopt; } } std::shared_ptr RemoteDataBatchProtectionAgent::InstantiateHttpClient() { - auto config_json_opt = LoadConnectionConfigFile(connection_config_); + auto config_json_opt = LoadConfigFile(k_connection_config_key_); auto server_url_opt = config_json_opt ? ExtractServerUrl(*config_json_opt) : std::nullopt; if (!server_url_opt || server_url_opt->empty()) { - std::cerr << "ERROR: RemoteDataBatchProtectionAgent::InstantiateHttpClient() - No server_url provided in connection_config." << std::endl; + std::cerr << "ERROR: RemoteDataBatchProtectionAgent::InstantiateHttpClient() - No server_url provided in " << k_connection_config_key_ << "." << std::endl; initialized_ = "Agent not properly initialized - server_url missing"; - throw DBPSException("No server_url provided in connection_config"); + throw DBPSException("No server_url provided in " + k_connection_config_key_); } server_url_ = *server_url_opt; std::cerr << "INFO: RemoteDataBatchProtectionAgent::init() - server_url extracted: [" << server_url_ << "]" << std::endl; @@ -475,7 +476,7 @@ static long long get_int_or_default(const nlohmann::json& config_json, const cha return val.get(); } -//Extract pool config from connection_config. Expected format is a set of key-values pairs which are top level in the file +// Extract pool config from connection_config json. Expected format is a set of key-values pairs which are top level in the file // there is no nesting HttplibPoolRegistry::PoolConfig RemoteDataBatchProtectionAgent::ExtractPoolConfig(const nlohmann::json& config_json) { HttplibPoolRegistry::PoolConfig pool_config; @@ -511,35 +512,3 @@ std::size_t RemoteDataBatchProtectionAgent::ExtractNumWorkerThreads(const nlohma return static_cast( get_int_or_default(config_json, "connection_pool.num_worker_threads", 0)); } - -std::optional RemoteDataBatchProtectionAgent::ExtractUserId(const std::string& app_context) const { - try { - auto json = nlohmann::json::parse(app_context); - if (json.contains("user_id") && json["user_id"].is_string()) { - std::string user_id = json["user_id"]; - if (!user_id.empty()) { - return user_id; - } - } - } catch (const nlohmann::json::exception& e) { - std::cerr << "ERROR: RemoteDataBatchProtectionAgent::ExtractUserId() - Failed to parse app_context JSON: " << e.what() << std::endl; - } - return std::nullopt; -} - -std::optional RemoteDataBatchProtectionAgent::ExtractPageEncoding(const std::map& encoding_attributes) const { - auto it = encoding_attributes.find("page_encoding"); - if (it != encoding_attributes.end()) { - const std::string& encoding_str = it->second; - auto format_opt = to_format_enum(encoding_str); - if (format_opt.has_value()) { - return format_opt.value(); - } else { - std::cerr << "ERROR: RemoteDataBatchProtectionAgent::ExtractPageEncoding() - Unknown page_encoding: " << encoding_str << std::endl; - return std::nullopt; - } - } - // Return nullopt if page_encoding not found - std::cerr << "ERROR: RemoteDataBatchProtectionAgent::ExtractPageEncoding() - page_encoding not found." << std::endl; - return std::nullopt; -} diff --git a/src/common/dbpa_remote.h b/src/common/dbpa_remote.h index 71c159c..5dbae08 100644 --- a/src/common/dbpa_remote.h +++ b/src/common/dbpa_remote.h @@ -98,7 +98,7 @@ class DBPS_EXPORT RemoteDecryptionResult : public DecryptionResult { */ class DBPS_EXPORT RemoteDataBatchProtectionAgent : public DataBatchProtectionAgentInterface { public: - // Constructor (default). Creates API_client during init() using server_url from connection_config + // Constructor (default). Creates API_client during init() using server_url from the connection_config file. RemoteDataBatchProtectionAgent() = default; // Constructor with HTTP client passed. Creates API_client immediately on the contructor. @@ -107,7 +107,7 @@ class DBPS_EXPORT RemoteDataBatchProtectionAgent : public DataBatchProtectionAge // DataBatchProtectionAgentInterface implementation void init( std::string column_name, - std::map connection_config, + std::map configuration_map, std::string app_context, std::string column_key_id, Type::type datatype, @@ -135,29 +135,26 @@ class DBPS_EXPORT RemoteDataBatchProtectionAgent : public DataBatchProtectionAge std::optional initialized_; std::string server_url_; std::string user_id_; - std::string k_connection_config_key_ = "connection_config_file_path"; + inline static const std::string k_connection_config_key_ = "connection_config_file_path"; + + // Helper methods for configuration parsing - // Extract pool config from connection_config - // assumes all values in connection_config are optional, and will use default values if any not present. + // Load and parse the config file in the configuration_map. + std::optional LoadConfigFile(const std::string& config_file_key) const; + + // Extract pool config from the connection_config json. Values are optional, and will use default values if not present. HttplibPoolRegistry::PoolConfig ExtractPoolConfig(const nlohmann::json& config_json); + // Extract number of worker threads for pooled client; defaults to 0 (auto) std::size_t ExtractNumWorkerThreads(const nlohmann::json& config_json) const; -private: - // Helper methods for configuration parsing - - // Load and parse the connection config file specified in connection_config - std::optional LoadConnectionConfigFile(const std::map& connection_config) const; + // Extract server_url from parsed JSON config such as {"server_url": "http://localhost:8080"} + std::optional ExtractServerUrl(const nlohmann::json& config_json) const; +private: // Instantiate a new HTTP client using the connection config file std::shared_ptr InstantiateHttpClient(); - // Extract server_url from parsed JSON config such as {"server_url": "http://localhost:8080"} - std::optional ExtractServerUrl(const nlohmann::json& config_json) const; - - std::optional ExtractUserId(const std::string& app_context) const; - std::optional ExtractPageEncoding(const std::map& encoding_attributes) const; - // Client instance std::unique_ptr api_client_; }; diff --git a/src/common/dbpa_remote_test.cpp b/src/common/dbpa_remote_test.cpp index 639b271..9cf5f31 100644 --- a/src/common/dbpa_remote_test.cpp +++ b/src/common/dbpa_remote_test.cpp @@ -43,13 +43,20 @@ class TestableRemoteDataBatchProtectionAgent : public RemoteDataBatchProtectionA const std::string& get_column_key_id() const { return column_key_id_; } Type::type get_datatype() const { return datatype_; } CompressionCodec::type get_compression_type() const { return compression_type_; } - const std::map& get_connection_config() const { return connection_config_; } + const std::map& get_configuration_map() const { return configuration_map_; } const std::string& get_app_context() const { return app_context_; } // Expose private members for testing const std::string& get_server_url() const { return server_url_; } const std::string& get_user_id() const { return user_id_; } const std::optional& get_initialized() const { return initialized_; } + + // Expose connection config key for testing + using RemoteDataBatchProtectionAgent::k_connection_config_key_; + + // Expose protected helper methods as public for testing + using RemoteDataBatchProtectionAgent::ExtractPoolConfig; + using RemoteDataBatchProtectionAgent::ExtractNumWorkerThreads; }; // Mock HTTP client for testing @@ -125,22 +132,22 @@ class RemoteDataBatchProtectionAgentTest : public ::testing::Test { } } - std::map GetConnectionConfig( + std::map GetConfigurationMap( const std::string& file_contents, const std::string& file_name) { std::string tmp_file_path = std::filesystem::temp_directory_path() / file_name; if (!std::filesystem::exists(tmp_file_path)) { CreateTemporaryConnectionConfigFile(file_contents, file_name); } - return {{"connection_config_file_path", tmp_file_path}}; + return {{TestableRemoteDataBatchProtectionAgent::k_connection_config_key_, tmp_file_path}}; } - void TestConnectionConfigFailures(const std::map& connection_config) { + void TestConnectionConfigFailures(const std::map& configuration_map) { auto agent = TestableRemoteDataBatchProtectionAgent(); std::string app_context = "{\"user_id\": \"test_user\"}"; // init() should throw DBPSException for missing server URL - EXPECT_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::BYTE_ARRAY, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt), DBPSException); @@ -167,26 +174,19 @@ class RemoteDataBatchProtectionAgentTest : public ::testing::Test { std::vector tmp_test_data_dir_; }; -// Expose ExtractPoolConfig via the test subclass -class ExtractPoolConfigExposer : public TestableRemoteDataBatchProtectionAgent { -public: - using TestableRemoteDataBatchProtectionAgent::ExtractPoolConfig; - using TestableRemoteDataBatchProtectionAgent::ExtractNumWorkerThreads; -}; - // Test basic initialization with valid configuration TEST_F(RemoteDataBatchProtectionAgentTest, LoadsConfigFromFileAndFailsHealthCheck) { // Use default constructor to ensure config is loaded from file and HTTP client is created internally auto agent = TestableRemoteDataBatchProtectionAgent(); // Create a (temp) config file with a localhost URL to avoid external network - auto connection_config = GetConnectionConfig( + auto configuration_map = GetConfigurationMap( "{\"server_url\": \"http://localhost:8080\"}", "test_connection_config.json"); std::string app_context = "{\"user_id\": \"test_user\"}"; // init() should throw because health check will fail (we're using a real client with no server) // but it must parse config and extract values first - EXPECT_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::BYTE_ARRAY, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt), DBPSException); @@ -198,10 +198,10 @@ TEST_F(RemoteDataBatchProtectionAgentTest, LoadsConfigFromFileAndFailsHealthChec EXPECT_EQ(agent.get_app_context(), app_context); // Config path should be present and file exists - EXPECT_TRUE(agent.get_connection_config().find("connection_config_file_path") - != agent.get_connection_config().end()); + EXPECT_TRUE(agent.get_configuration_map().find(TestableRemoteDataBatchProtectionAgent::k_connection_config_key_) + != agent.get_configuration_map().end()); EXPECT_TRUE(std::filesystem::exists( - agent.get_connection_config().at("connection_config_file_path"))); + agent.get_configuration_map().at(TestableRemoteDataBatchProtectionAgent::k_connection_config_key_))); // Values extracted before health check EXPECT_EQ(agent.get_server_url(), "http://localhost:8080"); @@ -214,7 +214,7 @@ TEST_F(RemoteDataBatchProtectionAgentTest, LoadsConfigFromFileAndFailsHealthChec // Verify default pool configuration values are applied when not provided TEST_F(RemoteDataBatchProtectionAgentTest, PoolConfigDefaultsAreApplied) { - ExtractPoolConfigExposer agent; + TestableRemoteDataBatchProtectionAgent agent; const nlohmann::json json = nlohmann::json::parse("{\"server_url\": \"http://localhost:8080\"}"); auto cfg = agent.ExtractPoolConfig(json); EXPECT_EQ(cfg.max_pool_size, HttplibPoolRegistry::kDefaultMaxPoolSize); @@ -227,7 +227,7 @@ TEST_F(RemoteDataBatchProtectionAgentTest, PoolConfigDefaultsAreApplied) { // Verify custom pool configuration values from JSON are applied TEST_F(RemoteDataBatchProtectionAgentTest, PoolConfigCustomValuesAreApplied) { - ExtractPoolConfigExposer agent; + TestableRemoteDataBatchProtectionAgent agent; const std::string json_str = "{\n" " \"server_url\": \"http://localhost:8080\",\n" @@ -250,7 +250,7 @@ TEST_F(RemoteDataBatchProtectionAgentTest, PoolConfigCustomValuesAreApplied) { // Verify a mix of provided and missing values results in custom + defaults accordingly TEST_F(RemoteDataBatchProtectionAgentTest, PoolConfigMixedDefaultsAndCustom) { - ExtractPoolConfigExposer agent; + TestableRemoteDataBatchProtectionAgent agent; const std::string json_str = "{\n" " \"server_url\": \"http://localhost:8080\",\n" @@ -274,7 +274,7 @@ TEST_F(RemoteDataBatchProtectionAgentTest, PoolConfigMixedDefaultsAndCustom) { // Verify malformed (wrong-typed) values throw TEST_F(RemoteDataBatchProtectionAgentTest, PoolConfigMalformedValuesThrow) { - ExtractPoolConfigExposer agent; + TestableRemoteDataBatchProtectionAgent agent; const std::string json_str = "{\n" " \"server_url\": \"http://localhost:8080\",\n" @@ -292,14 +292,14 @@ TEST_F(RemoteDataBatchProtectionAgentTest, PoolConfigMalformedValuesThrow) { // Verify num_worker_threads extraction (default/custom/malformed) TEST_F(RemoteDataBatchProtectionAgentTest, NumWorkerThreadsDefaultIsZero) { - ExtractPoolConfigExposer agent; + TestableRemoteDataBatchProtectionAgent agent; const nlohmann::json json = nlohmann::json::parse("{\"server_url\": \"http://localhost:8080\"}"); auto n = agent.ExtractNumWorkerThreads(json); EXPECT_EQ(n, 0u); } TEST_F(RemoteDataBatchProtectionAgentTest, NumWorkerThreadsCustomValue) { - ExtractPoolConfigExposer agent; + TestableRemoteDataBatchProtectionAgent agent; const nlohmann::json json = nlohmann::json::parse( "{\"server_url\": \"http://localhost:8080\", \"connection_pool.num_worker_threads\": 7}"); auto n = agent.ExtractNumWorkerThreads(json); @@ -307,7 +307,7 @@ TEST_F(RemoteDataBatchProtectionAgentTest, NumWorkerThreadsCustomValue) { } TEST_F(RemoteDataBatchProtectionAgentTest, NumWorkerThreadsMalformedThrows) { - ExtractPoolConfigExposer agent; + TestableRemoteDataBatchProtectionAgent agent; const nlohmann::json json = nlohmann::json::parse( "{\"server_url\": \"http://localhost:8080\", \"connection_pool.num_worker_threads\": \"threads\"}"); EXPECT_THROW(agent.ExtractNumWorkerThreads(json), DBPSException); @@ -331,27 +331,27 @@ TEST_F(RemoteDataBatchProtectionAgentTest, DecryptWithoutInit) { // Test initialization with bad connection configurations TEST_F(RemoteDataBatchProtectionAgentTest, EmptyConnectionConfig) { - TestConnectionConfigFailures(GetConnectionConfig("", "empty_connection_config.json")); + TestConnectionConfigFailures(GetConfigurationMap("", "empty_connection_config.json")); } TEST_F(RemoteDataBatchProtectionAgentTest, NonExistingConnectionConfigFile) { - TestConnectionConfigFailures({{"connection_config_file_path", "foo"}}); + TestConnectionConfigFailures({{TestableRemoteDataBatchProtectionAgent::k_connection_config_key_, "foo"}}); } TEST_F(RemoteDataBatchProtectionAgentTest, BadJsonConfigFile) { - TestConnectionConfigFailures(GetConnectionConfig("foo", "bad_json_connection_config.json")); + TestConnectionConfigFailures(GetConfigurationMap("foo", "bad_json_connection_config.json")); } // Test initialization with missing user ID TEST_F(RemoteDataBatchProtectionAgentTest, MissingUserId) { auto agent = TestableRemoteDataBatchProtectionAgent(); - auto connection_config = GetConnectionConfig( + auto configuration_map = GetConfigurationMap( "{\"server_url\": \"http://localhost:8080\"}", "test_connection_config.json"); std::string app_context = "{}"; // No user_id // init() should throw DBPSException for missing user ID - EXPECT_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::BYTE_ARRAY, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt), DBPSException); @@ -380,12 +380,12 @@ TEST_F(RemoteDataBatchProtectionAgentTest, HealthCheckFailure) { auto agent = TestableRemoteDataBatchProtectionAgent(std::move(mock_client_)); - auto connection_config = GetConnectionConfig( + auto configuration_map = GetConfigurationMap( "{\"server_url\": \"http://localhost:8080\"}", "test_connection_config.json"); std::string app_context = "{\"user_id\": \"test_user\"}"; // init() should throw DBPSException for health check failure - EXPECT_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::BYTE_ARRAY, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt), DBPSException); @@ -424,12 +424,12 @@ TEST_F(RemoteDataBatchProtectionAgentTest, SuccessfulEncryption) { auto agent = TestableRemoteDataBatchProtectionAgent(std::move(mock_client_)); - auto connection_config = GetConnectionConfig( + auto configuration_map = GetConfigurationMap( "{\"server_url\": \"http://localhost:8080\"}", "test_connection_config.json"); std::string app_context = "{\"user_id\": \"test_user\"}"; // init() should not throw an exception for valid configuration - EXPECT_NO_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_NO_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::BYTE_ARRAY, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt)); std::vector test_data = {1, 2, 3, 4}; @@ -466,12 +466,12 @@ TEST_F(RemoteDataBatchProtectionAgentTest, SuccessfulDecryption) { auto agent = TestableRemoteDataBatchProtectionAgent(std::move(mock_client_)); - auto connection_config = GetConnectionConfig( + auto configuration_map = GetConfigurationMap( "{\"server_url\": \"http://localhost:8080\"}", "test_connection_config.json"); std::string app_context = "{\"user_id\": \"test_user\"}"; // init() should not throw an exception for valid configuration - EXPECT_NO_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_NO_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::BYTE_ARRAY, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt)); std::vector test_data = {1, 2, 3, 4}; @@ -536,12 +536,12 @@ TEST_F(RemoteDataBatchProtectionAgentTest, DecryptionFieldMismatch) { auto agent = TestableRemoteDataBatchProtectionAgent(std::move(mock_client)); - auto connection_config = GetConnectionConfig( + auto configuration_map = GetConfigurationMap( "{\"server_url\": \"http://localhost:8080\"}", "test_connection_config.json"); std::string app_context = "{\"user_id\": \"test_user\"}"; // init() should not throw an exception for valid configuration - EXPECT_NO_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_NO_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::BYTE_ARRAY, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt)); std::vector test_data = {1, 2, 3, 4}; @@ -579,12 +579,12 @@ TEST_F(RemoteDataBatchProtectionAgentTest, EncryptionFieldMismatch) { auto agent = TestableRemoteDataBatchProtectionAgent(std::move(mock_client_)); - auto connection_config = GetConnectionConfig( + auto configuration_map = GetConfigurationMap( "{\"server_url\": \"http://localhost:8080\"}", "test_connection_config.json"); std::string app_context = "{\"user_id\": \"test_user\"}"; // init() should not throw an exception for valid configuration - EXPECT_NO_THROW(agent.init("test_column", connection_config, app_context, "test_key", + EXPECT_NO_THROW(agent.init("test_column", configuration_map, app_context, "test_key", Type::BYTE_ARRAY, std::nullopt, CompressionCodec::UNCOMPRESSED, std::nullopt)); std::vector test_data = {1, 2, 3, 4}; diff --git a/src/common/dbpa_utils.h b/src/common/dbpa_utils.h index adca3d9..3d0bff9 100644 --- a/src/common/dbpa_utils.h +++ b/src/common/dbpa_utils.h @@ -25,8 +25,6 @@ #include "enum_utils.h" #include -// TODO: Define all constant/literal (e.g. "server_url", "user_id", "page_encoding") as constants here. - namespace dbps::external { /** @@ -34,22 +32,6 @@ namespace dbps::external { * These functions are used by both RemoteDataBatchProtectionAgent and LocalDataBatchProtectionAgent */ -/** - * Extract server_url from connection_config map - * - * @param connection_config Map of connection configuration key-value pairs - * @return server_url value if found, std::nullopt otherwise - * - * Expected key: "server_url" with value as the server URL string - */ -inline std::optional ExtractServerUrl(const std::map& connection_config) { - auto it = connection_config.find("server_url"); - if (it != connection_config.end()) { - return it->second; - } - return std::nullopt; -} - /** * Extract user_id from app_context JSON string * diff --git a/src/common/dbpa_utils_test.cpp b/src/common/dbpa_utils_test.cpp index 1e768f6..4f08e9d 100644 --- a/src/common/dbpa_utils_test.cpp +++ b/src/common/dbpa_utils_test.cpp @@ -22,53 +22,6 @@ using namespace dbps::external; -// Test ExtractServerUrl function -TEST(DBPAUtilsTest, ExtractServerUrl_ValidUrl) { - std::map connection_config = {{"server_url", "http://localhost:8080"}}; - auto result = ExtractServerUrl(connection_config); - - ASSERT_TRUE(result.has_value()); - EXPECT_EQ(result.value(), "http://localhost:8080"); -} - -TEST(DBPAUtilsTest, ExtractServerUrl_ValidUrlWithOtherFields) { - std::map connection_config = { - {"server_url", "https://example.com:443"}, - // timeout and retry_count are for testing purposes only - {"timeout", "30"}, - {"retry_count", "3"} - }; - auto result = ExtractServerUrl(connection_config); - - ASSERT_TRUE(result.has_value()); - EXPECT_EQ(result.value(), "https://example.com:443"); -} - -TEST(DBPAUtilsTest, ExtractServerUrl_MissingServerUrl) { - std::map connection_config = { - {"timeout", "30"}, - {"retry_count", "3"} - }; - auto result = ExtractServerUrl(connection_config); - - EXPECT_FALSE(result.has_value()); -} - -TEST(DBPAUtilsTest, ExtractServerUrl_EmptyMap) { - std::map connection_config; - auto result = ExtractServerUrl(connection_config); - - EXPECT_FALSE(result.has_value()); -} - -TEST(DBPAUtilsTest, ExtractServerUrl_EmptyValue) { - std::map connection_config = {{"server_url", ""}}; - auto result = ExtractServerUrl(connection_config); - - ASSERT_TRUE(result.has_value()); - EXPECT_EQ(result.value(), ""); -} - // Test ExtractUserId function TEST(DBPAUtilsTest, ExtractUserId_ValidJson) { std::string app_context = R"({"user_id": "test_user_123"})";