-// 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
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") {
-// 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
#define BACKEND_SELECTOR_H
#include <cc/data.h>
+#include <cc/cfg_to_element.h>
#include <cstdint>
#include <string>
///
/// 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.
/// "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.
/// @param type Backend type to be converted.
static std::string backendTypeToString(const Type& type);
-
private:
/// @brief Checks if the specified selector is valid.
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
-// 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
#include <config.h>
#include <database/backend_selector.h>
+#include <testutils/test_to_element.h>
#include <boost/scoped_ptr.hpp>
#include <gtest/gtest.h>
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);
+}
}