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) {
/// @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
}
+// 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);
+}
+
}