Commit 818f07b3 authored by Francis Dupont's avatar Francis Dupont

[65-libyang-option-data] Added a fixture template

parent 543bfe26
......@@ -18,6 +18,7 @@ TESTS =
if HAVE_GTEST
TESTS += run_unittests
run_unittests_SOURCES = adaptor_unittests.cc
run_unittests_SOURCES += sysrepo_setup.h
run_unittests_SOURCES += translator_unittests.cc
run_unittests_SOURCES += translator_option_data_unittests.cc
run_unittests_SOURCES += run_unittests.cc
......
// Copyright (C) 2018 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
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef SYSREPO_SETUP_H
#define SYSREPO_SETUP_H
#include <config.h>
#include <yang/translator.h>
#include <gtest/gtest.h>
namespace isc {
namespace yang {
namespace test {
/// @brief Test Fixture temolate for translator tests.
///
/// @tparam Name The name of the translator to test.
/// @tparam Type The type of the translator to test.
template<char const* Name, typename Type>
class GenericTranslatorTest : public ::testing::Test {
public:
/// @brief Constructor.
///
/// Open a sysrepo session and create a translator object.
///
/// #param model The model to use.
GenericTranslatorTest(std::string model) : conn_(), sess_(), t_obj_() {
std::string full_name =
"translator " + std::string(Name) + " unittests";
conn_.reset(new Connection(full_name.c_str()));
sess_.reset(new Session(conn_, SR_DS_CANDIDATE));
EXPECT_NO_THROW(t_obj_.reset(new Type(sess_, model)));
}
/// @brief Destructor.
///
/// Destroy all objects.
virtual ~GenericTranslatorTest() {
t_obj_.reset();
sess_.reset();
conn_.reset();
}
/// @brief Sysrepo connection.
S_Connection conn_;
/// @brief Sysrepo session.
S_Session sess_;
/// @brief Shared pointer to the transaction object.
boost::shared_ptr<Type> t_obj_;
};
} // namespace test
} // namespace yang
} // namespace isc
#endif // SYSREPO_SETUP_H
......@@ -7,8 +7,8 @@
#include <config.h>
#include <yang/translator_option_data.h>
#include <yang/tests/sysrepo_setup.h>
#include <boost/scoped_ptr.hpp>
#include <gtest/gtest.h>
#include <sstream>
......@@ -16,40 +16,77 @@ using namespace std;
using namespace isc;
using namespace isc::data;
using namespace isc::yang;
using namespace isc::yang::test;
namespace {
// Test get empty option data list.
TEST(TranslatorOptionDataListTest, getEmpty) {
// Get a translator option data list object to play with.
S_Connection conn(new Connection("translator option data list unittests"));
S_Session sess(new Session(conn, SR_DS_CANDIDATE));
boost::scoped_ptr<TranslatorOptionDataList> todl_obj;
/// @brief Translator name.
extern char const name[] = "option data list";
/// @brief Test fixture class for @ref TranslatorOptionDataList.
class TranslatorOptionDataListTest : public GenericTranslatorTest<name, TranslatorOptionDataList> {
public:
/// Constructor (passes the model).
///
/// @param model The model.
TranslatorOptionDataListTest(string model) : GenericTranslatorTest<name, TranslatorOptionDataList>(model) { }
/// Destructor (does nothing).
virtual ~TranslatorOptionDataListTest() { }
};
#if 0
/// @brief Test fixture sub-class using the IETF model.
class TranslatorOptionDataListIetf6Test : public TranslatorOptionDataListTest {
public:
/// Constructor.
TranslatorOptionDataListIetf6Test() :
TranslatorOptionDataListTest("ietf-dhcpv6-server") { }
/// Destructor.
~TranslatorOptionDataListIetf6Test() { }
};
#endif
/// @brief Test fixture sub-class using the kea DHcpv4 server model.
class TranslatorOptionDataListKea4Test : public TranslatorOptionDataListTest {
public:
/// Constructor.
TranslatorOptionDataListKea4Test() :
TranslatorOptionDataListTest("kea-dhcp4-server") { }
/// Destructor.
~TranslatorOptionDataListKea4Test() { }
};
// Use the ad hoc model.
const string& model = "kea-dhcp4-server";
EXPECT_NO_THROW(todl_obj.reset(new TranslatorOptionDataList(sess, model)));
/// @brief Test fixture sub-class using the kea DHcpv6 server model.
class TranslatorOptionDataListKea6Test : public TranslatorOptionDataListTest {
public:
/// Constructor.
TranslatorOptionDataListKea6Test() :
TranslatorOptionDataListTest("kea-dhcp6-server") { }
/// Destructor.
~TranslatorOptionDataListKea6Test() { }
};
// Test get empty option data list.
TEST_F(TranslatorOptionDataListKea4Test, getEmpty) {
// Get the option data list and checks it is empty.
const string& xpath = "/kea-dhcp4-server:config/option-data-list";
ConstElementPtr options;
EXPECT_NO_THROW(options = todl_obj->getOptionDataList(xpath));
EXPECT_NO_THROW(options = t_obj_->getOptionDataList(xpath));
ASSERT_TRUE(options);
ASSERT_EQ(Element::list, options->getType());
EXPECT_EQ(0, options->size());
}
// Test get one option data.
TEST(TranslatorOptionDataListTest, get) {
// Get a translator option data list object to play with.
S_Connection conn(new Connection("translator option data list unittests"));
S_Session sess(new Session(conn, SR_DS_CANDIDATE));
boost::scoped_ptr<TranslatorOptionDataList> todl_obj;
// Use the ad hoc model.
const string& model = "kea-dhcp6-server";
EXPECT_NO_THROW(todl_obj.reset(new TranslatorOptionDataList(sess, model)));
TEST_F(TranslatorOptionDataListKea6Test, get) {
// Create the option code 100.
const string& xpath = "/kea-dhcp6-server:config/option-data-list";
const string& xoption = xpath + "/option-data[code='100'][space='dns']";
......@@ -57,21 +94,21 @@ TEST(TranslatorOptionDataListTest, get) {
const string& xdata = xoption + "/data";
const string& xsend = xoption + "/always-send";
S_Val s_false(new Val(false));
ASSERT_NO_THROW(sess->set_item(xformat.c_str(), s_false));
ASSERT_NO_THROW(sess_->set_item(xformat.c_str(), s_false));
S_Val s_data(new Val("12121212"));
ASSERT_NO_THROW(sess->set_item(xdata.c_str(), s_data));
ASSERT_NO_THROW(sess->set_item(xsend.c_str(), s_false));
ASSERT_NO_THROW(sess_->set_item(xdata.c_str(), s_data));
ASSERT_NO_THROW(sess_->set_item(xsend.c_str(), s_false));
// Get the option data.
ConstElementPtr option;
EXPECT_NO_THROW(option = todl_obj->getOptionData(xoption));
EXPECT_NO_THROW(option = t_obj_->getOptionData(xoption));
ASSERT_TRUE(option);
EXPECT_EQ("{ \"always-send\": false, \"code\": 100, \"csv-format\": false, \"data\": \"12121212\", \"space\": \"dns\" }",
option->str());
// Get the option data list.
ConstElementPtr options;
EXPECT_NO_THROW(options = todl_obj->getOptionDataList(xpath));
EXPECT_NO_THROW(options = t_obj_->getOptionDataList(xpath));
ASSERT_TRUE(options);
ASSERT_EQ(Element::list, options->getType());
EXPECT_EQ(1, options->size());
......@@ -79,39 +116,21 @@ TEST(TranslatorOptionDataListTest, get) {
}
// Test set empty option data list.
TEST(TranslatorOptionDataListTest, setEmpty) {
// Get a translator option data list object to play with.
S_Connection conn(new Connection("translator option data list unittests"));
S_Session sess(new Session(conn, SR_DS_CANDIDATE));
boost::scoped_ptr<TranslatorOptionDataList> todl_obj;
// Use the ad hoc model.
const string& model = "kea-dhcp4-server";
EXPECT_NO_THROW(todl_obj.reset(new TranslatorOptionDataList(sess, model)));
TEST_F(TranslatorOptionDataListKea4Test, setEmpty) {
// Set empty list.
const string& xpath = "/kea-dhcp4-server:config/option-data-list";
ConstElementPtr options = Element::createList();
EXPECT_NO_THROW(todl_obj->setOptionDataList(xpath, options));
EXPECT_NO_THROW(t_obj_->setOptionDataList(xpath, options));
// Get it back.
options.reset();
EXPECT_NO_THROW(options = todl_obj->getOptionDataList(xpath));
EXPECT_NO_THROW(options = t_obj_->getOptionDataList(xpath));
ASSERT_TRUE(options);
EXPECT_EQ(0, options->size());
}
// Test set an option data.
TEST(TranslatorOptionDataListTest, set) {
// Get a translator option data list object to play with.
S_Connection conn(new Connection("translator option data list unittests"));
S_Session sess(new Session(conn, SR_DS_CANDIDATE));
boost::scoped_ptr<TranslatorOptionDataList> todl_obj;
// Use the ad hoc model.
const string& model = "kea-dhcp6-server";
EXPECT_NO_THROW(todl_obj.reset(new TranslatorOptionDataList(sess, model)));
TEST_F(TranslatorOptionDataListKea6Test, set) {
// Set one option data.
const string& xpath = "/kea-dhcp6-server:config/option-data-list";
ElementPtr options = Element::createList();
......@@ -122,18 +141,18 @@ TEST(TranslatorOptionDataListTest, set) {
option->set("data", Element::create(string("12121212")));
option->set("always-send", Element::create(false));
options->add(option);
EXPECT_NO_THROW(todl_obj->setOptionDataList(xpath, options));
EXPECT_NO_THROW(t_obj_->setOptionDataList(xpath, options));
// Get it back.
ConstElementPtr got;
EXPECT_NO_THROW(got = todl_obj->getOptionDataList(xpath));
EXPECT_NO_THROW(got = t_obj_->getOptionDataList(xpath));
ASSERT_TRUE(got);
ASSERT_EQ(1, got->size());
EXPECT_TRUE(option->equals(*got->get(0)));
// Check the tree representation.
S_Tree tree;
EXPECT_NO_THROW(tree = sess->get_subtree("/kea-dhcp6-server:config"));
EXPECT_NO_THROW(tree = sess_->get_subtree("/kea-dhcp6-server:config"));
ASSERT_TRUE(tree);
string expected =
"kea-dhcp6-server:config (container)\n"
......
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