]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#43,!123] Implemented parseCommandWithArgs function.
authorMarcin Siodelski <marcin@isc.org>
Tue, 13 Nov 2018 16:23:23 +0000 (17:23 +0100)
committerMarcin Siodelski <marcin@isc.org>
Thu, 15 Nov 2018 09:55:35 +0000 (04:55 -0500)
src/lib/cc/command_interpreter.cc
src/lib/cc/command_interpreter.h
src/lib/cc/tests/command_interpreter_unittests.cc

index 742734eb120a63788aba2121c4676aa2d12c17b0..214739d8a82a9fdbcbe0155c56ddf7422afa2274 100644 (file)
@@ -190,6 +190,32 @@ parseCommand(ConstElementPtr& arg, ConstElementPtr command) {
     return (cmd->stringValue());
 }
 
+std::string
+parseCommandWithArgs(ConstElementPtr& arg, ConstElementPtr command) {
+    std::string command_name = parseCommand(arg, command);
+
+    // This function requires arguments within the command.
+    if (!arg) {
+        isc_throw(CtrlChannelError,
+                  "no arguments specified for the '" << command_name
+                  << "' command");
+    }
+
+    // Arguments must be a map.
+    if (arg->getType() != Element::map) {
+        isc_throw(CtrlChannelError, "arguments specified for the '" << command_name
+                  << "' command are not a map");
+    }
+
+    // At least one argument is required.
+    if (arg->size() == 0) {
+        isc_throw(CtrlChannelError, "arguments must not be empty for "
+                  "the '" << command_name << "' command");
+    }
+
+    return (command_name);
+}
+
 ConstElementPtr
 combineCommandsLists(const ConstElementPtr& response1,
                      const ConstElementPtr& response2) {
index 7386fae9c6cbfc81d0cf9acdc7db9401bc7bd384..a1b9690304147bae9355cdd125aed4ddafdb9a4d 100644 (file)
@@ -147,16 +147,34 @@ isc::data::ConstElementPtr createCommand(const std::string& command,
 /// @brief Parses the given command into a string containing the actual
 ///        command and an ElementPtr containing the optional argument.
 ///
-/// @throw Raises a CtrlChannelError if this is not a well-formed command
+/// @throw CtrlChannelError if this is not a well-formed command
 ///
 /// @param arg This value will be set to the ElementPtr pointing to
 ///        the argument, or to an empty Map (ElementPtr) if there was none.
 /// @param command The command message containing the command (as made
 ///        by createCommand()
-/// @return The command name
+/// @return The command name.
 std::string parseCommand(isc::data::ConstElementPtr& arg,
                          isc::data::ConstElementPtr command);
 
+
+/// @brief Parses the given command into a string containing the command
+///        name and an ElementPtr containing the mandatory argument.
+///
+/// This function expects that command arguments are specified and are
+/// a map.
+///
+/// @throw CtrlChannelError if this is not a well-formed command,
+///        arguments are not specified or are not a map.
+///
+/// @param arg Reference to the data element to which command arguments
+///        will be assigned.
+/// @param command The command message containing the command and
+///        the arguments.
+/// @return Command name.
+std::string parseCommandWithArgs(isc::data::ConstElementPtr& arg,
+                                 isc::data::ConstElementPtr command);
+
 /// @brief Combines lists of commands carried in two responses.
 ///
 /// This method is used to combine list of commands returned by the
index 18e1f6ab2f9be7b90fbcb1fb58eddc64f4d317a6..7058e0ed77d4fef2fa23df2a6b95c728109da485 100644 (file)
@@ -183,4 +183,38 @@ TEST(CommandInterpreterTest, parseCommand) {
 
 }
 
+// This test checks whether parseCommandWithArgs function is able to parse
+// various valid and malformed commands.
+TEST(CommandInterpreterTest, parseCommandWithArgs) {
+    ConstElementPtr arg;
+    std::string cmd;
+
+    // Arguments are required.
+    EXPECT_THROW(parseCommandWithArgs(arg, el("{ \"command\": \"my_command\" }")),
+                 CtrlChannelError);
+
+    // Arguments must be a map.
+    EXPECT_THROW(parseCommandWithArgs(arg, el("{ \"command\": \"my_command\", "
+                                              "\"arguments\": [ 1, 2, 3 ] }")),
+                 CtrlChannelError);
+
+    // Arguments must not be empty.
+    EXPECT_THROW(parseCommandWithArgs(arg, el("{ \"command\": \"my_command\", "
+                                              "\"arguments\": { } }")),
+                 CtrlChannelError);
+
+    // Specifying arguments in non empty map should be successful.
+    EXPECT_NO_THROW(
+        cmd = parseCommandWithArgs(arg, el("{ \"command\": \"my_command\", "
+                                           "\"arguments\": { \"arg1\": \"value1\" } }"))
+    );
+    ASSERT_TRUE(arg);
+    ASSERT_EQ(Element::map, arg->getType());
+    auto arg1 = arg->get("arg1");
+    ASSERT_TRUE(arg1);
+    ASSERT_EQ(Element::string, arg1->getType());
+    EXPECT_EQ("value1", arg1->stringValue());
+    EXPECT_EQ("my_command", cmd);
+}
+
 }