translator_option_data_unittests.cc 5 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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/.

#include <config.h>

#include <yang/translator_option_data.h>
10
#include <yang/tests/sysrepo_setup.h>
11 12 13 14 15 16 17 18

#include <gtest/gtest.h>
#include <sstream>

using namespace std;
using namespace isc;
using namespace isc::data;
using namespace isc::yang;
19
using namespace isc::yang::test;
20 21 22

namespace {

23
/// @brief Translator name.
24
extern char const option_data_list[] = "option data list";
25 26

/// @brief Test fixture class for @ref TranslatorOptionDataList.
27 28
class TranslatorOptionDataListTest :
    public GenericTranslatorTest<option_data_list, TranslatorOptionDataList> {
29 30
public:

31 32
    /// Constructor.
    TranslatorOptionDataListTest() { }
33 34 35 36 37

    /// Destructor (does nothing).
    virtual ~TranslatorOptionDataListTest() { }
};

38 39
// This test verifies that an empty option data list can be properly
// translated from YANG to JSON.
40 41 42
TEST_F(TranslatorOptionDataListTest, getEmpty) {
    useModel("kea-dhcp4-server");

43
    // Get the option data list and checks it is empty.
44
    const string& xpath = "/kea-dhcp4-server:config/option-data-list";
45
    ConstElementPtr options;
46
    EXPECT_NO_THROW(options = t_obj_->getOptionDataList(xpath));
47 48 49 50 51
    ASSERT_TRUE(options);
    ASSERT_EQ(Element::list, options->getType());
    EXPECT_EQ(0, options->size());
}

52 53
// This test verifies that one option data can be properly translated
// from YANG to JSON.
54 55 56
TEST_F(TranslatorOptionDataListTest, get) {
    useModel("kea-dhcp6-server");

57
    // Create the option code 100.
58
    const string& xpath = "/kea-dhcp6-server:config/option-data-list";
59 60 61 62 63
    const string& xoption = xpath + "/option-data[code='100'][space='dns']";
    const string& xformat = xoption + "/csv-format";
    const string& xdata = xoption + "/data";
    const string& xsend = xoption + "/always-send";
    S_Val s_false(new Val(false));
64
    ASSERT_NO_THROW(sess_->set_item(xformat.c_str(), s_false));
65
    S_Val s_data(new Val("12121212"));
66 67
    ASSERT_NO_THROW(sess_->set_item(xdata.c_str(), s_data));
    ASSERT_NO_THROW(sess_->set_item(xsend.c_str(), s_false));
68 69 70
    
    // Get the option data.
    ConstElementPtr option;
71
    EXPECT_NO_THROW(option = t_obj_->getOptionData(xoption));
72 73 74 75 76 77
    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;
78
    EXPECT_NO_THROW(options = t_obj_->getOptionDataList(xpath));
79 80 81 82 83 84
    ASSERT_TRUE(options);
    ASSERT_EQ(Element::list, options->getType());
    EXPECT_EQ(1, options->size());
    EXPECT_TRUE(option->equals(*options->get(0)));
}

85 86
// This test verifies that an empty option data list can be properly
// translated from JSON to YANG.
87 88 89
TEST_F(TranslatorOptionDataListTest, setEmpty) {
    useModel("kea-dhcp4-server");

90
    // Set empty list.
91
    const string& xpath = "/kea-dhcp4-server:config/option-data-list";
92
    ConstElementPtr options = Element::createList();
93
    EXPECT_NO_THROW(t_obj_->setOptionDataList(xpath, options));
94 95 96

    // Get it back.
    options.reset();
97
    EXPECT_NO_THROW(options = t_obj_->getOptionDataList(xpath));
98 99 100 101
    ASSERT_TRUE(options);
    EXPECT_EQ(0, options->size());
}

102 103
// This test verifies that one option data can be properly translated
// from JSON to YANG.
104 105 106
TEST_F(TranslatorOptionDataListTest, set) {
    useModel("kea-dhcp6-server");

107
    // Set one option data.
108
    const string& xpath = "/kea-dhcp6-server:config/option-data-list";
109 110 111 112 113 114 115 116
    ElementPtr options = Element::createList();
    ElementPtr option = Element::createMap();
    option->set("code", Element::create(100));
    option->set("space", Element::create(string("dns")));
    option->set("csv-format", Element::create(false));
    option->set("data", Element::create(string("12121212")));
    option->set("always-send", Element::create(false));
    options->add(option);
117
    EXPECT_NO_THROW(t_obj_->setOptionDataList(xpath, options));
118 119 120

    // Get it back.
    ConstElementPtr got;
121
    EXPECT_NO_THROW(got = t_obj_->getOptionDataList(xpath));
122 123 124 125 126 127
    ASSERT_TRUE(got);
    ASSERT_EQ(1, got->size());
    EXPECT_TRUE(option->equals(*got->get(0)));

    // Check the tree representation.
    S_Tree tree;
128
    EXPECT_NO_THROW(tree = sess_->get_subtree("/kea-dhcp6-server:config"));
129 130
    ASSERT_TRUE(tree);
    string expected =
131
        "kea-dhcp6-server:config (container)\n"
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        " |\n"
        " -- option-data-list (container)\n"
        "     |\n"
        "     -- option-data (list instance)\n"
        "         |\n"
        "         -- code = 100\n"
        "         |\n"
        "         -- space = dns\n"
        "         |\n"
        "         -- data = 12121212\n"
        "         |\n"
        "         -- csv-format = false\n"
        "         |\n"
        "         -- always-send = false\n";
    EXPECT_EQ(expected, tree->to_string(100));
}

}; // end of anonymous namespace