std::map<std::string, common::ParameterType> kTypeMap = {
{"integer", common::ParameterType::IntType},
{"string", common::ParameterType::StringType},
- {"double", common::ParameterType::DoubleType},
- {"bool", common::ParameterType::BoolType},
- {"json", common::ParameterType::JsonType},
+ {"number", common::ParameterType::NumberType},
+ {"boolean", common::ParameterType::BoolType},
+ {"object", common::ParameterType::ObjectType},
+ {"array", common::ParameterType::ArrayType}
};
common::ParameterType ConvertStringToParameterType(const std::string& type) {
}
common::ActionType GetActionTypeFromString(const std::string& type) {
- if (type.compare("app_control") == 0)
+ if (type.compare("appControl") == 0)
return common::ActionType::AppControl;
else if (type.compare("plugin") == 0)
return common::ActionType::Plugin;
try {
SafeJson root(json_str);
action_id_ = root.get<std::string>("name");
- appid_ = root.get<std::string>("appId"); // TODO: Change to details::appId
try {
category_ = root.get<std::string>("category");
} catch (...) {
returns_.emplace_back(name, type, "", description, false);
}
- for (auto const& it : root.array("required-privileges"))
+ for (auto const& it : root.array("requiredprivileges"))
privileges_.push_back(it->GetString());
+
+ // details
+ switch (type_) {
+ case ActionType::AppControl:
+ appid_ = root.get<std::string>("details.appid");
+ break;
+ case ActionType::Plugin:
+ plugin_path_ = root.get<std::string>("details.pluginPath");
+ break;
+ default:
+ break;
+ }
} catch (const std::runtime_error& e) {
// TODO: how to handle invalid json case?
LOG(ERROR) << "Failed to parse json string: " << e.what();
appid_ = std::move(appid);
}
+const std::string& ActionSchema::GetPluginPath() const {
+ return plugin_path_;
+}
+
+void ActionSchema::SetPluginPath(std::string plugin_path) {
+ plugin_path_ = std::move(plugin_path);
+}
+
const std::string& ActionSchema::GetCategory() const {
return category_;
}
-#include <gtest/gtest.h>
#include "common/action_schema.h"
+#include <gtest/gtest.h>
#include <sstream>
using namespace common;
class ActionSchemaTest : public ::testing::Test {
-protected:
- void SetUp() override {
- valid_json_ = R"(
+ protected:
+ void SetUp() override {
+ valid_appcontrol_action_json_ = R"(
{
"name": "test_action",
- "appId": "com.example.app",
"category": "test_category",
"desc": "Test action description",
- "type": "app_control",
+ "type": "appControl",
"params": {
"param1": {
"type": "integer",
},
"returns": {
"return1": {
- "type": "double",
+ "type": "number",
"desc": "Return 1"
}
},
- "required-privileges": ["priv1", "priv2"]
+ "requiredprivileges": ["priv1", "priv2"],
+ "details": {
+ "appid": "com.example.app"
+ }
})";
- invalid_json_ = R"( { "invalid": "json" })";
- }
+ valid_plugin_action_json_ = R"(
+ {
+ "name": "test_plugin_action",
+ "category": "test_category",
+ "desc": "Test action description",
+ "type": "plugin",
+ "params": {
+ "param1": {
+ "type": "boolean",
+ "desc": "Param 1"
+ },
+ "param2": {
+ "type": "number",
+ "desc": "Param 2"
+ }
+ },
+ "returns": {
+ "return1": {
+ "type": "number",
+ "desc": "Return 1"
+ }
+ },
+ "requiredprivileges": ["priv1", "priv2"],
+ "details": {
+ "pluginPath": "/path/to/plugin"
+ }
+ })";
+ invalid_json_ = R"( { "invalid": "json" })";
+ }
- std::string valid_json_;
- std::string invalid_json_;
+ std::string valid_appcontrol_action_json_;
+ std::string valid_plugin_action_json_;
+ std::string invalid_json_;
};
-TEST_F(ActionSchemaTest, ValidJsonParsing) {
- ActionSchema schema(valid_json_);
- EXPECT_EQ(schema.GetActionId(), "test_action");
- EXPECT_EQ(schema.GetAppId(), "com.example.app");
- EXPECT_EQ(schema.GetCategory(), "test_category");
- EXPECT_EQ(schema.GetDescription(), "Test action description");
- EXPECT_EQ(schema.GetType(), ActionType::AppControl);
-
- const auto& params = schema.GetParameters();
- EXPECT_EQ(params.size(), 2);
- EXPECT_EQ(params[0].GetName(), "param1");
- EXPECT_EQ(params[0].GetType(), ParameterType::IntType);
- EXPECT_EQ(params[1].GetName(), "param2");
- EXPECT_EQ(params[1].GetType(), ParameterType::StringType);
-
- const auto& returns = schema.GetReturns();
- EXPECT_EQ(returns.size(), 1);
- EXPECT_EQ(returns[0].GetType(), ParameterType::DoubleType);
-
- const auto& privileges = schema.GetPrivileges();
- EXPECT_EQ(privileges.size(), 2);
- EXPECT_EQ(privileges[0], "priv1");
- EXPECT_EQ(privileges[1], "priv2");
+TEST_F(ActionSchemaTest, ValidAppControlActionJsonParsing) {
+ ActionSchema schema(valid_appcontrol_action_json_);
+ EXPECT_EQ(schema.GetActionId(), "test_action");
+ EXPECT_EQ(schema.GetAppId(), "com.example.app");
+ EXPECT_EQ(schema.GetCategory(), "test_category");
+ EXPECT_EQ(schema.GetDescription(), "Test action description");
+ EXPECT_EQ(schema.GetType(), ActionType::AppControl);
+
+ const auto& params = schema.GetParameters();
+ EXPECT_EQ(params.size(), 2);
+ EXPECT_EQ(params[0].GetName(), "param1");
+ EXPECT_EQ(params[0].GetType(), ParameterType::IntType);
+ EXPECT_EQ(params[1].GetName(), "param2");
+ EXPECT_EQ(params[1].GetType(), ParameterType::StringType);
+
+ const auto& returns = schema.GetReturns();
+ EXPECT_EQ(returns.size(), 1);
+ EXPECT_EQ(returns[0].GetType(), ParameterType::NumberType);
+
+ const auto& privileges = schema.GetPrivileges();
+ EXPECT_EQ(privileges.size(), 2);
+ EXPECT_EQ(privileges[0], "priv1");
+ EXPECT_EQ(privileges[1], "priv2");
+}
+
+TEST_F(ActionSchemaTest, ValidPluginActionJsonParsing) {
+ ActionSchema schema(valid_plugin_action_json_);
+ EXPECT_EQ(schema.GetActionId(), "test_plugin_action");
+ EXPECT_EQ(schema.GetPluginPath(), "/path/to/plugin");
+ EXPECT_EQ(schema.GetCategory(), "test_category");
+ EXPECT_EQ(schema.GetDescription(), "Test action description");
+ EXPECT_EQ(schema.GetType(), ActionType::Plugin);
+
+ const auto& params = schema.GetParameters();
+ EXPECT_EQ(params.size(), 2);
+ EXPECT_EQ(params[0].GetName(), "param1");
+ EXPECT_EQ(params[0].GetType(), ParameterType::BoolType);
+ EXPECT_EQ(params[1].GetName(), "param2");
+ EXPECT_EQ(params[1].GetType(), ParameterType::NumberType);
+
+ const auto& returns = schema.GetReturns();
+ EXPECT_EQ(returns.size(), 1);
+ EXPECT_EQ(returns[0].GetType(), ParameterType::NumberType);
+
+ const auto& privileges = schema.GetPrivileges();
+ EXPECT_EQ(privileges.size(), 2);
+ EXPECT_EQ(privileges[0], "priv1");
+ EXPECT_EQ(privileges[1], "priv2");
}
TEST_F(ActionSchemaTest, InvalidJsonParsing) {
- ActionSchema invalid_schema(invalid_json_);
- EXPECT_EQ(invalid_schema.GetActionId().empty(), true);
- EXPECT_EQ(invalid_schema.GetAppId().empty(), true);
- EXPECT_EQ(invalid_schema.GetCategory().empty(), true);
- EXPECT_EQ(invalid_schema.GetDescription().empty(), true);
- EXPECT_EQ(invalid_schema.GetType(), ActionType::InvalidType);
+ ActionSchema invalid_schema(invalid_json_);
+ EXPECT_EQ(invalid_schema.GetActionId().empty(), true);
+ EXPECT_EQ(invalid_schema.GetAppId().empty(), true);
+ EXPECT_EQ(invalid_schema.GetCategory().empty(), true);
+ EXPECT_EQ(invalid_schema.GetDescription().empty(), true);
+ EXPECT_EQ(invalid_schema.GetType(), ActionType::InvalidType);
}
TEST_F(ActionSchemaTest, SettersAndGetters) {
- ActionSchema schema;
- schema.SetActionId("new_id");
- schema.SetAppId("new_package");
- schema.SetCategory("new_category");
- schema.SetDescription("new_desc");
- schema.SetType(ActionType::Plugin);
-
- EXPECT_EQ(schema.GetActionId(), "new_id");
- EXPECT_EQ(schema.GetAppId(), "new_package");
- EXPECT_EQ(schema.GetCategory(), "new_category");
- EXPECT_EQ(schema.GetDescription(), "new_desc");
- EXPECT_EQ(schema.GetType(), ActionType::Plugin);
+ ActionSchema schema;
+ schema.SetActionId("new_id");
+ schema.SetAppId("new_package");
+ schema.SetCategory("new_category");
+ schema.SetDescription("new_desc");
+ schema.SetType(ActionType::Plugin);
+
+ EXPECT_EQ(schema.GetActionId(), "new_id");
+ EXPECT_EQ(schema.GetAppId(), "new_package");
+ EXPECT_EQ(schema.GetCategory(), "new_category");
+ EXPECT_EQ(schema.GetDescription(), "new_desc");
+ EXPECT_EQ(schema.GetType(), ActionType::Plugin);
}
TEST_F(ActionSchemaTest, ParameterTypeConversion) {
- ActionSchema schema(valid_json_);
- const auto& params = schema.GetParameters();
- EXPECT_EQ(params[0].GetType(), ParameterType::IntType);
- EXPECT_EQ(params[1].GetType(), ParameterType::StringType);
+ ActionSchema schema(valid_appcontrol_action_json_);
+ const auto& params = schema.GetParameters();
+ EXPECT_EQ(params[0].GetType(), ParameterType::IntType);
+ EXPECT_EQ(params[1].GetType(), ParameterType::StringType);
- const auto& returns = schema.GetReturns();
- EXPECT_EQ(returns[0].GetType(), ParameterType::DoubleType);
+ const auto& returns = schema.GetReturns();
+ EXPECT_EQ(returns[0].GetType(), ParameterType::NumberType);
}
TEST_F(ActionSchemaTest, DefaultConstructor) {
- ActionSchema default_schema;
- EXPECT_TRUE(default_schema.GetActionId().empty());
- EXPECT_TRUE(default_schema.GetAppId().empty());
- EXPECT_TRUE(default_schema.GetCategory().empty());
- EXPECT_TRUE(default_schema.GetDescription().empty());
- EXPECT_EQ(default_schema.GetType(), ActionType::InvalidType);
- EXPECT_TRUE(default_schema.GetParameters().empty());
- EXPECT_TRUE(default_schema.GetReturns().empty());
- EXPECT_TRUE(default_schema.GetPrivileges().empty());
+ ActionSchema default_schema;
+ EXPECT_TRUE(default_schema.GetActionId().empty());
+ EXPECT_TRUE(default_schema.GetAppId().empty());
+ EXPECT_TRUE(default_schema.GetCategory().empty());
+ EXPECT_TRUE(default_schema.GetDescription().empty());
+ EXPECT_EQ(default_schema.GetType(), ActionType::InvalidType);
+ EXPECT_TRUE(default_schema.GetParameters().empty());
+ EXPECT_TRUE(default_schema.GetReturns().empty());
+ EXPECT_TRUE(default_schema.GetPrivileges().empty());
}