From 0043833dc45564b120ff0e0e907747916773998c Mon Sep 17 00:00:00 2001 From: Marcin Siodelski Date: Wed, 9 Jan 2019 15:41:10 +0100 Subject: [PATCH] [#99,!197] Added merging into the ConfigBase class. --- src/lib/process/config_base.cc | 17 +++++ src/lib/process/config_base.h | 13 ++++ src/lib/process/config_ctl_info.cc | 7 ++ src/lib/process/config_ctl_info.h | 9 +++ .../process/tests/config_base_unittests.cc | 66 +++++++++++++++++++ 5 files changed, 112 insertions(+) diff --git a/src/lib/process/config_base.cc b/src/lib/process/config_base.cc index 74b4ca788f..99f03fb2c8 100644 --- a/src/lib/process/config_base.cc +++ b/src/lib/process/config_base.cc @@ -82,6 +82,23 @@ ConfigBase::copy(ConfigBase& other) const { } } +void +ConfigBase::merge(const ConfigBase& other) { + // Merge logging info. + if (!other.logging_info_.empty()) { + logging_info_ = other.logging_info_; + } + + // Merge the config control info + if (other.config_ctl_info_) { + if (config_ctl_info_) { + config_ctl_info_->merge(*other.config_ctl_info_); + } else { + config_ctl_info_ = other.config_ctl_info_; + } + } +} + ElementPtr ConfigBase::toElement() const { ElementPtr result = Element::createMap(); diff --git a/src/lib/process/config_base.h b/src/lib/process/config_base.h index bdf8fbc884..9bb8884bf2 100644 --- a/src/lib/process/config_base.h +++ b/src/lib/process/config_base.h @@ -57,6 +57,19 @@ public: /// @param other the other configuration to compare to bool equals(const ConfigBase& other) const; + /// @brief Merges specified configuration into this configuration. + /// + /// This method merges logging and config control configuration into + /// this configuration. The new logging configuration replaces the + /// existing configuration if the new logging configuration is + /// non-empty. The new config control configuration replaces the + /// existing configuration if the new logging configuration is + /// non-null and non-empty. + /// + /// @param other the other configuration to be merged into this + /// configuration. + virtual void merge(const ConfigBase& other); + /// @brief Converts to Element representation /// /// This creates a Map element with the following content (expressed diff --git a/src/lib/process/config_ctl_info.cc b/src/lib/process/config_ctl_info.cc index 9d84134691..4fb37d26c8 100644 --- a/src/lib/process/config_ctl_info.cc +++ b/src/lib/process/config_ctl_info.cc @@ -90,6 +90,13 @@ ConfigControlInfo::clear() { db_infos_.clear(); } +void +ConfigControlInfo::merge(const ConfigControlInfo& other) { + if (!other.db_infos_.empty()) { + db_infos_ = other.db_infos_; + } +} + ElementPtr ConfigControlInfo::toElement() const { ElementPtr result = Element::createMap(); diff --git a/src/lib/process/config_ctl_info.h b/src/lib/process/config_ctl_info.h index b5c94eff5b..19fd63f129 100644 --- a/src/lib/process/config_ctl_info.h +++ b/src/lib/process/config_ctl_info.h @@ -172,6 +172,15 @@ public: /// @brief Empties the contents of the class, including the database list void clear(); + /// @brief Merges specified configuration into this configuration. + /// + /// If the other configuration is non-empty it completely replaces + /// this configuration. + /// + /// @param other the other configuration to be merged into this + /// configuration. + void merge(const ConfigControlInfo& other); + /// @brief Unparse a configuration object /// /// @return a pointer to unparsed configuration diff --git a/src/lib/process/tests/config_base_unittests.cc b/src/lib/process/tests/config_base_unittests.cc index 0b45bd03cc..2c0302cc7d 100644 --- a/src/lib/process/tests/config_base_unittests.cc +++ b/src/lib/process/tests/config_base_unittests.cc @@ -57,3 +57,69 @@ TEST(ConfigBase, configControlInfoTests) { // They should be equal again. EXPECT_TRUE(base1.equals(base2)); } + +// Verifies that logging information can be merged to another. +TEST(ConfigBase, mergeLoggingInfo) { + // Create first logging info. + LoggingInfo log_info1; + log_info1.name_ = "foo"; + + // Create second logging info. + LoggingInfo log_info2; + log_info2.name_ = "bar"; + + // Create first config base instance. + ConfigBaseImpl base1; + base1.addLoggingInfo(log_info1); + + // Copy the first instance to keep it as reference. + ConfigBaseImpl base1_copy; + base1_copy.copy(base1); + + // Create second config base instance. + ConfigBaseImpl base2; + ASSERT_NO_THROW(base1.merge(base2)); + EXPECT_TRUE(base1.equals(base1_copy)); + + // Set some data for the second config. + base2.addLoggingInfo(log_info2); + + // This time the merge should replace the original config. + ASSERT_NO_THROW(base1.merge(base2)); + EXPECT_TRUE(base1.equals(base2)); +} + +// Verifies that config control can be merged to another. +TEST(ConfigBase, mergeConfigControl) { + // Create first config control info. + ConfigControlInfoPtr ctl_info1(new ConfigControlInfo()); + ctl_info1->addConfigDatabase("type=mysql host=example.com"); + ctl_info1->addConfigDatabase("type=mysql host=example2.com"); + + // Create second config control info. + ConfigControlInfoPtr ctl_info2(new ConfigControlInfo()); + ctl_info2->addConfigDatabase("type=pgsql host=example.com"); + ctl_info2->addConfigDatabase("type=pgsql host=example2.com"); + + // Create first config base instance. + ConfigBaseImpl base1; + base1.setConfigControlInfo(ctl_info1); + + // Copy the first instance to keep it as reference. + ConfigBaseImpl base1_copy; + base1_copy.copy(base1); + + // Create second config base instance. + ConfigBaseImpl base2; + + // Merged base is empty, so the original should be preserved. + ASSERT_NO_THROW(base1.merge(base2)); + EXPECT_TRUE(base1.equals(base1_copy)); + + // Set some data for the second config. + base2.setConfigControlInfo(ctl_info2); + + // This time the merge should replace the original config. + ASSERT_NO_THROW(base1.merge(base2)); + EXPECT_TRUE(base1.equals(base2)); +} -- 2.47.2