From: Changgyu Choi Date: Sun, 27 Apr 2025 08:43:59 +0000 (+0900) Subject: Apply modified action schema X-Git-Tag: accepted/tizen/unified/20250514.114137~16^2~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=918d913f48414ade41f31d3d889b0da7e828ae3b;p=platform%2Fcore%2Fappfw%2Ftizen-action.git Apply modified action schema Signed-off-by: Changgyu Choi --- diff --git a/src/action/action_request_handler.cc b/src/action/action_request_handler.cc index 71249e0..a5b8bab 100755 --- a/src/action/action_request_handler.cc +++ b/src/action/action_request_handler.cc @@ -92,9 +92,9 @@ bool CheckPrivileges(pid_t pid, uid_t uid, // return common::ParameterType::IntType; // else if (type.compare("string") == 0) // return common::ParameterType::StringType; -// else if (type.compare("double") == 0) -// return common::ParameterType::DoubleType; -// else if (type.compare("bool") == 0) +// else if (type.compare("number") == 0) +// return common::ParameterType::NumberType; +// else if (type.compare("boolean") == 0) // return common::ParameterType::BoolType; // return common::ParameterType::InvalidType; @@ -107,10 +107,10 @@ std::string GetStringFromParameterType(common::ParameterType type) { return "integer"; else if (type == common::ParameterType::StringType) return "string"; - else if (type == common::ParameterType::DoubleType) - return "double"; + else if (type == common::ParameterType::NumberType) + return "number"; else if (type == common::ParameterType::BoolType) - return "bool"; + return "boolean"; return ""; } @@ -122,9 +122,6 @@ void FillActionModelInfo(common::ActionModel& model, case common::ActionType::AppControl: model.SetExtraInfo(schema.GetAppId()); break; - case common::ActionType::Plugin: - model.SetExtraInfo("plugin-path"); // TODO: Set plugin path - break; default: break; } diff --git a/src/action/utils/action_model_converter.cc b/src/action/utils/action_model_converter.cc index 27c39b3..41dbe0e 100644 --- a/src/action/utils/action_model_converter.cc +++ b/src/action/utils/action_model_converter.cc @@ -18,7 +18,7 @@ plugin_proxy::ActionModel ActionModelConverter::ConvertToAction( std::string ActionModelConverter::ActionTypeToString(common::ActionType type) { switch (type) { case common::ActionType::AppControl: - return "app_control"; + return "appControl"; case common::ActionType::Plugin: return "plugin"; default: @@ -33,10 +33,14 @@ std::string ActionModelConverter::ActionParameterTypeToString( return "integer"; case common::ParameterType::StringType: return "string"; - case common::ParameterType::DoubleType: - return "double"; + case common::ParameterType::NumberType: + return "number"; case common::ParameterType::BoolType: - return "bool"; + return "boolean"; + case common::ParameterType::ObjectType: + return "object"; + case common::ParameterType::ArrayType: + return "array"; default: return "InvalidType"; } diff --git a/src/action/utils/action_validator.cc b/src/action/utils/action_validator.cc index eb93c8d..2b91b52 100644 --- a/src/action/utils/action_validator.cc +++ b/src/action/utils/action_validator.cc @@ -52,13 +52,13 @@ bool ActionValidator::CheckModelValid(const std::string& schemajson, int value = model.get({"params", key}); } else if (type == "string") { std::string value = model.get({"params", key}); - } else if (type == "bool") { // TODO: change to boolean + } else if (type == "boolean") { bool value = model.get({"params", key}); } else if (type == "array") { auto value = model.array({"params", key}); } else if (type == "object") { auto value = model.members({"params", key}); - } else if (type == "double") { // TODO: change to number + } else if (type == "number") { double value = model.get({"params", key}); } else { LOG(ERROR) << "Unknown type: " << type; diff --git a/src/common/action_parameter.h b/src/common/action_parameter.h index 85b0f21..1d0a9ca 100644 --- a/src/common/action_parameter.h +++ b/src/common/action_parameter.h @@ -24,9 +24,10 @@ namespace common { enum class ParameterType { IntType = 0, StringType = 1, - DoubleType = 2, + NumberType = 2, BoolType = 3, - JsonType = 4, + ObjectType = 4, + ArrayType = 5, InvalidType = 100 }; diff --git a/src/common/action_schema.cc b/src/common/action_schema.cc index 5895579..cfab450 100644 --- a/src/common/action_schema.cc +++ b/src/common/action_schema.cc @@ -27,9 +27,10 @@ namespace { std::map 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) { @@ -42,7 +43,7 @@ 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; @@ -59,7 +60,6 @@ ActionSchema::ActionSchema(const std::string& json_str) { try { SafeJson root(json_str); action_id_ = root.get("name"); - appid_ = root.get("appId"); // TODO: Change to details::appId try { category_ = root.get("category"); } catch (...) { @@ -90,8 +90,20 @@ ActionSchema::ActionSchema(const std::string& json_str) { 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("details.appid"); + break; + case ActionType::Plugin: + plugin_path_ = root.get("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(); @@ -128,6 +140,14 @@ void ActionSchema::SetAppId(std::string appid) { 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_; } diff --git a/src/common/action_schema.h b/src/common/action_schema.h index ea26e1b..d06429a 100644 --- a/src/common/action_schema.h +++ b/src/common/action_schema.h @@ -40,6 +40,9 @@ class ActionSchema { const std::string& GetAppId() const; void SetAppId(std::string app_id); + const std::string& GetPluginPath() const; + void SetPluginPath(std::string plugin_path); + const std::string& GetCategory() const; void SetCategory(std::string category); @@ -61,6 +64,7 @@ class ActionSchema { private: std::string action_id_; std::string appid_; + std::string plugin_path_; std::string category_; ActionType type_ = ActionType::InvalidType; std::string description_; diff --git a/src/pkgmgr_plugin_parser/json_action_schema_parser.cc b/src/pkgmgr_plugin_parser/json_action_schema_parser.cc index edb285a..34464c0 100644 --- a/src/pkgmgr_plugin_parser/json_action_schema_parser.cc +++ b/src/pkgmgr_plugin_parser/json_action_schema_parser.cc @@ -36,7 +36,7 @@ ActionSchema JsonActionSchemaParser::Parse(const std::string& pkgid, common::SafeJson root(json); std::string name = root.get("name"); std::string category = root.get("category"); - const auto priv_array = root.array("required-privileges"); + const auto priv_array = root.array("requiredprivileges"); std::vector privs; privs.reserve(priv_array.size()); std::transform(priv_array.begin(), priv_array.end(), diff --git a/test/unit_tests/action_model_converter_test.cc b/test/unit_tests/action_model_converter_test.cc index f9415f3..38c18d7 100644 --- a/test/unit_tests/action_model_converter_test.cc +++ b/test/unit_tests/action_model_converter_test.cc @@ -23,7 +23,7 @@ TEST(ActionModelConverterTest, ConvertToAction) { TEST(ActionModelConverterTest, ActionTypeToString) { EXPECT_EQ( ActionModelConverter().ActionTypeToString(common::ActionType::AppControl), - "app_control"); + "appControl"); EXPECT_EQ( ActionModelConverter().ActionTypeToString(common::ActionType::Plugin), "plugin"); @@ -40,11 +40,11 @@ TEST(ActionModelConverterTest, ParameterTypeToString) { common::ParameterType::StringType), "string"); EXPECT_EQ(ActionModelConverter().ActionParameterTypeToString( - common::ParameterType::DoubleType), - "double"); + common::ParameterType::NumberType), + "number"); EXPECT_EQ(ActionModelConverter().ActionParameterTypeToString( common::ParameterType::BoolType), - "bool"); + "boolean"); EXPECT_EQ(ActionModelConverter().ActionParameterTypeToString( static_cast(999)), "InvalidType"); diff --git a/test/unit_tests/action_schema_test.cc b/test/unit_tests/action_schema_test.cc index 82ef9e0..f9349d2 100644 --- a/test/unit_tests/action_schema_test.cc +++ b/test/unit_tests/action_schema_test.cc @@ -1,19 +1,18 @@ -#include #include "common/action_schema.h" +#include #include 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", @@ -26,86 +25,142 @@ protected: }, "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()); }