Commit a69c0def authored by Francis Dupont's avatar Francis Dupont

[406-update-cb_cmds-with-get-commands] Added toElement to BackendSelector

parent b8c0bac0
// 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") {
......
// 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.
......
......@@ -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
......
// 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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment