]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[406-update-cb_cmds-with-get-commands] Added toElement to BackendSelector
authorFrancis Dupont <fdupont@isc.org>
Sat, 26 Jan 2019 22:22:21 +0000 (23:22 +0100)
committerFrancis Dupont <fdupont@isc.org>
Tue, 5 Feb 2019 16:11:11 +0000 (11:11 -0500)
src/lib/database/backend_selector.cc
src/lib/database/backend_selector.h
src/lib/database/tests/Makefile.am
src/lib/database/tests/backend_selector_unittest.cc

index e3ab92d96c63b1bc1d07d0e26416292ec6cc1820..cea5a53c5a963b30683995caa33ca5191cc9a83a 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -109,6 +109,22 @@ BackendSelector::toText() const {
     return (text);
 }
 
+ElementPtr
+BackendSelector::toElement() const {
+    if (backend_type_ == BackendSelector::Type::UNSPEC) {
+        isc_throw(BadValue, "toElement: backend selector type is unspecified");
+    }
+    ElementPtr result = Element::createMap();
+    result->set("type", Element::create(backendTypeToString(backend_type_)));
+    if (!host_.empty()) {
+        result->set("host", Element::create(host_));
+        if (port_ > 0) {
+            result->set("port", Element::create(static_cast<long int>(port_)));
+        }
+    }
+    return (result);
+}
+
 BackendSelector::Type
 BackendSelector::stringToBackendType(const std::string& type) {
     if (type == "mysql") {
index 7d256a037a6e827747c73bb2557a6ade19635578..0628c0e5ae7dc750071969bb6354437012d97c6d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -8,6 +8,7 @@
 #define BACKEND_SELECTOR_H
 
 #include <cc/data.h>
+#include <cc/cfg_to_element.h>
 #include <cstdint>
 #include <string>
 
@@ -51,7 +52,7 @@ namespace db {
 ///
 /// The @c BackendSelector class may be extended in the future to provide
 /// additional backend selection criteria.
-class BackendSelector {
+class BackendSelector : public data::CfgToElement {
 public:
 
     /// @brief Supported database types.
@@ -171,6 +172,14 @@ public:
     /// "type=mysql,host=somehost.example.org,port=1234".
     std::string toText() const;
 
+    /// @brief Unparse a backend selector object.
+    ///
+    /// The caller must check if the type is specified before.
+    ///
+    /// @return A pointer to unparsed backend selector configuration.
+    /// @throw BadValue If the backend selector type is unspecified.
+    virtual data::ElementPtr toElement() const;
+
     /// @brief Converts string to backend type.
     ///
     /// @param type Backend type as string.
@@ -181,7 +190,6 @@ public:
     /// @param type Backend type to be converted.
     static std::string backendTypeToString(const Type& type);
 
-
 private:
 
     /// @brief Checks if the specified selector is valid.
index cdf149b62937b96cb9f24b6846d5a73c1bf07d6e..c05c29ce92d897d753b6de8b9bf65462fe778310 100644 (file)
@@ -30,6 +30,7 @@ libdatabase_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
 libdatabase_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
 
 libdatabase_unittests_LDADD  = $(top_builddir)/src/lib/database/libkea-database.la
+libdatabase_unittests_LDADD += $(top_builddir)/src/lib/testutils/libkea-testutils.la
 libdatabase_unittests_LDADD += $(top_builddir)/src/lib/cc/libkea-cc.la
 libdatabase_unittests_LDADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la
 libdatabase_unittests_LDADD += $(top_builddir)/src/lib/log/libkea-log.la
index 3665d50972f916616754a00569658e9d7af86076..6061e1bd41deb7624f291d895a5113f635f52561 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -7,6 +7,7 @@
 
 #include <config.h>
 #include <database/backend_selector.h>
+#include <testutils/test_to_element.h>
 #include <boost/scoped_ptr.hpp>
 #include <gtest/gtest.h>
 
@@ -169,6 +170,32 @@ TEST(BackendSelectorTest, backendTypeToString) {
               BackendSelector::backendTypeToString(BackendSelector::Type::CQL));
 }
 
+// Tests toElement from backend selectors.
+TEST(BackendSelectorTest, backendToElement) {
+    // Unspecified.
+    boost::scoped_ptr<BackendSelector> sel(new BackendSelector());
+    EXPECT_THROW(sel->toElement(), BadValue);
+
+    // Unspecified type.
+    sel.reset(new BackendSelector("myhost", 1234));
+    EXPECT_THROW(sel->toElement(), BadValue);
+
+    // Type only.
+    EXPECT_NO_THROW(sel.reset(new BackendSelector(BackendSelector::Type::MYSQL)));
+    ElementPtr expected = Element::createMap();
+    expected->set("type", Element::create("mysql"));
+    test::runToElementTest<BackendSelector>(expected, *sel);
+
+    // Add host.
+    expected->set("host", Element::create("myhost"));
+    EXPECT_NO_THROW(sel.reset(new BackendSelector(expected)));
+    test::runToElementTest<BackendSelector>(expected, *sel);
+
+    // Add port.
+    expected->set("port", Element::create(1234L));
+    EXPECT_NO_THROW(sel.reset(new BackendSelector(expected)));
+    test::runToElementTest<BackendSelector>(expected, *sel);
+}
 
 }