]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2448] add ClientClasses::toElement()
authorAndrei Pavel <andrei@isc.org>
Mon, 4 Jul 2022 08:17:12 +0000 (11:17 +0300)
committerAndrei Pavel <andrei@isc.org>
Thu, 7 Jul 2022 11:48:20 +0000 (11:48 +0000)
src/lib/dhcp/classify.cc
src/lib/dhcp/classify.h
src/lib/dhcp/tests/classify_unittest.cc

index cdd295323cd150a7e0144356f6d980dda181f3fe..7b0be454f1ac26c47db44c9fd0915d098c137ff9 100644 (file)
@@ -5,17 +5,23 @@
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 #include <config.h>
+
+#include <cc/data.h>
 #include <dhcp/classify.h>
 #include <util/strutil.h>
+
 #include <boost/algorithm/string/classification.hpp>
 #include <boost/algorithm/string/constants.hpp>
 #include <boost/algorithm/string/split.hpp>
+
 #include <sstream>
 #include <vector>
 
 namespace isc {
 namespace dhcp {
 
+using namespace isc::data;
+
 ClientClasses::ClientClasses(const std::string& class_names)
     : container_() {
     std::vector<std::string> split_text;
@@ -57,5 +63,14 @@ ClientClasses::toText(const std::string& separator) const {
     return (s.str());
 }
 
+ElementPtr
+ClientClasses::toElement() const {
+    ElementPtr result(Element::createList());
+    for (ClientClass c : container_) {
+        result->add(Element::create(c));
+    }
+    return (result);
+}
+
 } // end of namespace isc::dhcp
 } // end of namespace isc
index c536ba3eb5fa5eebfc5ba07b3890363e1694d3d1..5cf675c81de34ae7236d0c9b09b69e03e2b1c61a 100644 (file)
@@ -7,6 +7,8 @@
 #ifndef CLASSIFY_H
 #define CLASSIFY_H
 
+#include <cc/data.h>
+
 #include <boost/multi_index_container.hpp>
 #include <boost/multi_index/hashed_index.hpp>
 #include <boost/multi_index/identity.hpp>
@@ -149,8 +151,15 @@ namespace dhcp {
         /// @param separator Separator to be used between class names. The
         /// default separator comprises comma sign followed by space
         /// character.
+        ///
+        /// @return the string representation of all classes
         std::string toText(const std::string& separator = ", ") const;
 
+        /// @brief Returns all class names as an ElementPtr of type ListElement
+        ///
+        /// @return the list
+        isc::data::ElementPtr toElement() const;
+
     private:
         /// @brief container part
         ClientClassContainer container_;
index 938379cc88a3c295806d1b232fadc000a5fc0c69..1faffde71b9a9430f71fefc387cc3252029eea0b 100644 (file)
@@ -132,6 +132,26 @@ TEST(ClassifyTest, ClientClassesToText) {
     EXPECT_EQ("alpha.gamma.beta", classes.toText("."));
 }
 
+// Check that the ClientClasses::toElement function returns
+// correct values.
+TEST(ClassifyTest, ClientClassesToElement) {
+    // No classes.
+    ClientClasses classes;
+    EXPECT_TRUE(classes.toElement()->empty());
+
+    // Insert single class name and see that it's there.
+    classes.insert("alpha");
+    EXPECT_EQ("[ \"alpha\" ]", classes.toElement()->str());
+
+    // Insert next class name and see that both classes are present.
+    classes.insert("gamma");
+    EXPECT_EQ("[ \"alpha\", \"gamma\" ]", classes.toElement()->str());
+
+    // Insert third class and make sure they get ordered in insert order.
+    classes.insert("beta");
+    EXPECT_EQ("[ \"alpha\", \"gamma\", \"beta\" ]", classes.toElement()->str());
+}
+
 // Check that selected class can be erased.
 TEST(ClassifyTest, Erase) {
     ClientClasses classes;