diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h index 4494f01e9c1e5d8031e924a67ff0428a36303b34..a6b274ef914574a602b48f55ae541de7c9129418 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h @@ -34,14 +34,15 @@ public: static bool CheckProjectionField(JsonObject &node); static int ParseNode(JsonObject &Node, std::vector singlePath, std::vector> &resultPath, bool isFirstFloor); static std::vector> ParsePath(const JsonObject &node); - static std::vector GetLeafValue(JsonObject &node); - + static std::vector GetLeafValue(const JsonObject &node); + static bool isValueEqual(const ValueObject &srcValue, const ValueObject &targetValue); static int Append(const JsonObject &src, const JsonObject &add); - + static bool isJsonNodeMatch(const JsonObject &src, const JsonObject &target, int &externErrCode); private: static bool CheckNode(JsonObject &Node, std::set filedSet, bool &errFlag); static bool CheckProjectionNode(JsonObject &Node, std::set filedSet, bool &errFlag, bool isFirstFloor); - static int CheckLeafNode(JsonObject &Node, std::vector &leafValue); + static int CheckLeafNode(const JsonObject &Node, std::vector &leafValue); + static bool isArrayMathch(const JsonObject &src, const JsonObject &target, int &flag); }; } // DocumentDB #endif // JSON_COMMON_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp index 47a75e9c19df9005c94aa216b17228b2be4fd734..b7563c0d2c9a53203f2dc0b388f4af6d882011df 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp @@ -14,7 +14,6 @@ */ #include #include - #include "json_common.h" #include "doc_errno.h" #include "log_print.h" @@ -37,7 +36,7 @@ ValueObject JsonCommon::GetValueByFiled(JsonObject &node, const std::string& fil return ValueObject(); } -int JsonCommon::CheckLeafNode(JsonObject &node, std::vector &leafValue) +int JsonCommon::CheckLeafNode(const JsonObject &node, std::vector &leafValue) { if (node.GetChild().IsNull()) { auto itemValue = node.GetItemValue(); @@ -54,7 +53,7 @@ int JsonCommon::CheckLeafNode(JsonObject &node, std::vector &leafVa return E_OK; } -std::vector JsonCommon::GetLeafValue(JsonObject &node) +std::vector JsonCommon::GetLeafValue(const JsonObject &node) { std::vector leafValue; CheckLeafNode(node, leafValue); @@ -237,6 +236,27 @@ JsonFieldPath ExpendPath(const JsonFieldPath &path, bool &isCollapse) return splitPath; } +JsonFieldPath ExpendPathForField(const JsonFieldPath &path, bool &isCollapse) +{ + + JsonFieldPath splitPath; + const std::string &str = path[0]; + size_t start = 0; + size_t end = 0; + while ((end = str.find('.', start)) != std::string::npos) { + splitPath.push_back(str.substr(start, end - start)); + start = end + 1; + } + if (start < str.length()) { + splitPath.push_back(str.substr(start)); + } + isCollapse = (splitPath.size() > 1); + for (int i = 1; i < path.size(); i++) { + splitPath.emplace_back(path[i]); + } + return splitPath; +} + void JsonObjectIterator(const JsonObject &obj, JsonFieldPath path, std::function foo) { @@ -251,6 +271,22 @@ void JsonObjectIterator(const JsonObject &obj, JsonFieldPath path, } return; } + +void JsonObjectIterator(const JsonObject &obj, JsonFieldPath path, + std::function foo) +{ + JsonObject child = obj.GetChild(); + while(!child.IsNull()) { + bool isCollapse = false; + JsonFieldPath childPath = path; + childPath.push_back(child.GetItemFiled()); + if (foo != nullptr && foo(childPath, child)) { + JsonObjectIterator(child, childPath, foo); + } + child = child.GetNext(); + } + return; +} } int JsonCommon::Append(const JsonObject &src, const JsonObject &add) @@ -308,4 +344,141 @@ int JsonCommon::Append(const JsonObject &src, const JsonObject &add) }); return externErrCode; } +bool JsonCommon::isValueEqual(const ValueObject &srcValue, const ValueObject &targetValue) +{ + if (srcValue.GetValueType() == targetValue.GetValueType()) { + switch (srcValue.GetValueType()) { + case ValueObject::ValueType::VALUE_NULL: + return true; + break; + case ValueObject::ValueType::VALUE_BOOL: + if (srcValue.GetBoolValue() == targetValue.GetBoolValue()) { + return true; + } + return false; + break; + case ValueObject::ValueType::VALUE_NUMBER: + if (srcValue.GetDoubleValue() == targetValue.GetDoubleValue()) { + return true; + } + return false; + break; + case ValueObject::ValueType::VALUE_STRING: + if (srcValue.GetStringValue() == targetValue.GetStringValue()) { + return true; + } + return false; + break; + } + } + return false; +} + +bool JsonCommon::isArrayMathch(const JsonObject &src, const JsonObject &target, int &flag) { + JsonObject srcChild = src.GetChild(); + JsonObject targetObj = target; + bool isMatch = false; + int errCode = 0; + while (!srcChild.IsNull()) { + if (srcChild.GetType() == JsonObject::Type::JSON_OBJECT && target.GetType() == JsonObject::Type::JSON_OBJECT + && (isJsonNodeMatch(srcChild, target, errCode))) { + isMatch = true; + flag = 1; + break; + } + srcChild = srcChild.GetNext(); + } + return isMatch; +} + +bool JsonCommon::isJsonNodeMatch(const JsonObject &src, const JsonObject &target, int &externErrCode) +{ + externErrCode = E_OK; + int isMatchFlag = true; + JsonObjectIterator(target, {}, + [&src, &isMatchFlag, &externErrCode](JsonFieldPath &path, const JsonObject &item) { + int flag = 0; + bool isCollapse = false; + if (isMatchFlag == false) { + return false; + } + JsonFieldPath itemPath = ExpendPath(path, isCollapse); + int errCode = 0; + if (src.IsFieldExistsIncludeArray(itemPath)) { + JsonObject srcItem = src.FindItemIncludeArray(itemPath, errCode); + auto GranpaPath = itemPath; + auto lastFiledName = GranpaPath.back(); + GranpaPath.pop_back(); + JsonObject GranpaItem = src.FindItemIncludeArray(GranpaPath, errCode); + if (GranpaItem.GetType() == JsonObject::Type::JSON_ARRAY && isCollapse) { + JsonObject FatherItem = GranpaItem.GetChild(); + while (!FatherItem.IsNull()) { + bool isEqual = (FatherItem.GetObjectItem(lastFiledName, errCode).Print() == item.Print()); + if (isEqual) { + GLOGE("Filter value is equal with src"); + isMatchFlag = isEqual; + flag = 1; + } + FatherItem = FatherItem.GetNext(); + } + } + if (errCode != E_OK) { + externErrCode = (externErrCode == E_OK ? errCode : externErrCode); + GLOGE("Find item in source json object failed. %d", errCode); + return false; + } + if (srcItem.GetType() == JsonObject::Type::JSON_LEAF && item.GetType() == JsonObject::Type::JSON_LEAF && !flag) { + bool isEqual = isValueEqual(srcItem.GetItemValue(), item.GetItemValue()); + if (!isEqual) { + GLOGE("Filter value is No equal with src"); + isMatchFlag = isEqual; + } + flag = isMatchFlag; + return false; // Both leaf node, no need iterate + } else if (srcItem.GetType() != item.GetType()) { + if (srcItem.GetType() == JsonObject::Type::JSON_ARRAY) { + GLOGE("Check if there has an object in array"); + bool isEqual = isArrayMathch(srcItem, item, flag); + if (!isEqual) { + isMatchFlag = isEqual; + } + return true; + } + GLOGE("valueType is different,"); + isMatchFlag = false; + return false; // Different node types, overwrite directly, skip child node + } + return true; // Both array or object + } else { + if (isCollapse) { + GLOGE("Match failed, path not exist."); + isMatchFlag = false; + return false; + } + GLOGE("Not match anything"); + if (flag == 0) { + isMatchFlag = false; + } + std::vector ItemLeafValue = GetLeafValue(item); + int isNULLFlag = true; + for (auto ValueItem : ItemLeafValue) { + if (ValueItem.GetValueType() != ValueObject::ValueType::VALUE_NULL) { + GLOGE("leaf value is null"); + isNULLFlag = false; + } + } + if (isNULLFlag == true) { + isMatchFlag = true; // + } + return false; // Source path not exist, if leaf value is null, isMatchFlag become true, else it will become false. + } + }); + return isMatchFlag; +} + + + + + + } // namespace DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/projection_tree.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/projection_tree.h index 15920bd41d178faff1529555bbba0bede7e6e581..d317dd0227391649821a81415a4cce7a3b692323 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/projection_tree.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/projection_tree.h @@ -1,3 +1,17 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #ifndef PROJECTION_TREE_H #define PROJECTION_TREE_H diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/projection_tree.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/projection_tree.cpp index edf6f3568215d97e258f18658f1b67842f093143..ce4f7cfca91599de68f8079620a61863bed4059e 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/projection_tree.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/projection_tree.cpp @@ -1,3 +1,17 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ #include #include "projection_tree.h" diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h index 12510f1dc5c85f1784a0c0a5c8d29f9651240a6e..1ffe19ff20b924b69b4ba334eda541e65ae7bd0f 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h @@ -83,7 +83,9 @@ public: std::string GetItemFiled(int &errCode) const; bool IsFieldExists(const JsonFieldPath &jsonPath) const; + bool IsFieldExistsIncludeArray(const JsonFieldPath &jsonPath) const; JsonObject FindItem(const JsonFieldPath &jsonPath, int &errCode) const; + JsonObject FindItemIncludeArray(const JsonFieldPath &jsonPath, int &errCode) const; ValueObject GetObjectByPath(const JsonFieldPath &jsonPath, int &errCode) const; int DeleteItemOnTarget(const JsonFieldPath &path); int DeleteItemDeeplyOnTarget(const JsonFieldPath &path); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp index 44bd987950952ac294f0b29966b1ac2cd82c337f..77080bc0e487e5df24561746a7e37ccc337aea27 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp @@ -412,6 +412,33 @@ cJSON *GetChild(cJSON *cjson, const std::string &field, bool caseSens) return nullptr; } + +cJSON *GetChildIncludeArray(cJSON *cjson, const std::string &field, bool caseSens) +{ + if (cjson->type == cJSON_Object) { + if (caseSens) { + return cJSON_GetObjectItemCaseSensitive(cjson, field.c_str()); + } else { + return cJSON_GetObjectItem(cjson, field.c_str()); + } + } else if (cjson->type == cJSON_Array) { + if (!IsNumber(field)) { + cjson = cjson->child; + while (cjson != nullptr) { + auto resultItem = GetChild(cjson, field, caseSens); + if (resultItem != nullptr) { + return resultItem; + } + cjson = cjson->next; + } + return nullptr; + } + return cJSON_GetArrayItem(cjson, std::stoi(field)); + } + GLOGW("Invalid json field type, expect object or array."); + return nullptr; +} + cJSON *MoveToPath(cJSON *cjson, const JsonFieldPath &jsonPath, bool caseSens) { for (const auto &field : jsonPath) { @@ -423,11 +450,27 @@ cJSON *MoveToPath(cJSON *cjson, const JsonFieldPath &jsonPath, bool caseSens) return cjson; } +cJSON *MoveToPathIncludeArray(cJSON *cjson, const JsonFieldPath &jsonPath, bool caseSens) +{ + for (const auto &field : jsonPath) { + cjson = GetChildIncludeArray(cjson, field, caseSens); + if (cjson == nullptr) { + break; + } + } + return cjson; +} + bool JsonObject::IsFieldExists(const JsonFieldPath &jsonPath) const { return (MoveToPath(cjson_, jsonPath, caseSensitive_) != nullptr); } +bool JsonObject::IsFieldExistsIncludeArray(const JsonFieldPath &jsonPath) const +{ + return (MoveToPathIncludeArray(cjson_, jsonPath, caseSensitive_) != nullptr); +} + JsonObject JsonObject::FindItem(const JsonFieldPath &jsonPath, int &errCode) const { if (jsonPath.empty()) { @@ -452,6 +495,29 @@ JsonObject JsonObject::FindItem(const JsonFieldPath &jsonPath, int &errCode) con return item; } +JsonObject JsonObject::FindItemIncludeArray(const JsonFieldPath &jsonPath, int &errCode) const +{ + if (jsonPath.empty()) { + JsonObject curr = JsonObject(); + curr.cjson_ = cjson_; + curr.caseSensitive_ = caseSensitive_; + curr.isOwner_ = false; + GLOGW("Path empty, return current object"); + return curr; + } + + cJSON *findItem = MoveToPathIncludeArray(cjson_, jsonPath, caseSensitive_); + if (findItem == nullptr) { + GLOGE("Find item failed. json field path not found."); + errCode = -E_JSON_PATH_NOT_EXISTS; + return {}; + } + JsonObject item; + item.caseSensitive_ = caseSensitive_; + item.cjson_ = findItem; + return item; +} + ValueObject JsonObject::GetObjectByPath(const JsonFieldPath &jsonPath, int &errCode) const { JsonObject objGot = FindItem(jsonPath, errCode); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp index 11717fe75652d63513e1ad8b579825b993cdb35b..7c0b9492625d4ddc08e36259b46ea06cd167c773 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/oh_adapter/documentdb_json_common_test.cpp @@ -327,4 +327,201 @@ HWTEST_F(DocumentDBJsonCommonTest, JsonObjectAppendTest015, TestSize.Level0) JsonObject itemCase = src.FindItem({"name", "first", "0"}, errCode); EXPECT_EQ(errCode, E_OK); EXPECT_EQ(itemCase.GetItemValue().GetStringValue(), "GG"); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest001, TestSize.Level0) +{ + std::string document = R""({"name":{"first": {"job" : "it"}}, "t1" : {"second":"Lang"}})""; + std::string filter = R""({"name":{"first": {"job" : "it"}}, "t1" : {"second":"Lang"}})""; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); + + std::string document2 = R""({"name":{"first": {"job" : "it"}, "t1" : {"second":"Lang"}}})""; + std::string filter2 = R""({"name":{"first": {"job" : "NoEqual"}}, "t1" : {"second":"Lang"}})""; + int errCode2 = E_OK; + JsonObject srcObj2 = JsonObject::Parse(document2, errCode2); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj2 = JsonObject::Parse(filter2, errCode2); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj2, filterObj2, errCode), false); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest002, TestSize.Level0) +{ + std::string document = R""({"item": [{"gender":"girl"}, "GG"], "instock": [{"warehouse":"A", "qty":5}, {"warehouse":"C", "qty":15}]})""; + std::string filter = R""({"instock": {"warehouse":"A", "qty":5}})"";; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest003, TestSize.Level0) +{ + std::string document = R""({"item": [{"gender":"girl"}, "GG"], "instock": [{"warehouse":"A", "qty":5}, {"warehouse":"C", "qty":15}]})""; + std::string filter = R""({"item": "GG"})"";; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), false); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest004, TestSize.Level0) +{ + std::string document = R""({"item": [{"gender":"girl"}, "GG"], "instock": [{"warehouse":"A", "qty":5}, {"warehouse":"C", "qty":15}]})""; + std::string filter = R""({"item": "GG"})"";; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), false); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest005, TestSize.Level0) +{ + std::string document = R""({"item": ["GG", "AA"], "instock": [{"warehouse":"A", "qty":5}, {"warehouse":"C", "qty":15}]})""; + std::string filter = R""({"item": ["GG", "AA"]})"";; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest006, TestSize.Level0) +{ + std::string document = R""({"item": ["GG", {"gender":"girl"}], "instock": [{"warehouse":"A", "qty":5}, {"warehouse":"C", "qty":15}]})""; + std::string filter = R""({"item.0": "GG"})"";; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest007, TestSize.Level0) +{ + std::string document = R""({"item": ["GG", {"gender":"girl"}], "instock": [{"warehouse":"A", "qty":5}, {"warehouse":"C", "qty":15}]})""; + std::string filter = R""({"item": ["GG", {"gender":"girl"}]})"";; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest008, TestSize.Level0) +{ + std::string document = R""({"item": ["GG", {"gender":"girl", "hobby" : "IT"}], "instock": [{"warehouse":"A", "qty":5}, {"warehouse":"C", "qty":15}]})""; + std::string filter = R""({"item": {"gender":"girl"}})"";; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest009, TestSize.Level0) +{ + std::string document = R""({"item": ["GG", {"gender":"girl", "hobby" : "IT"}], "instock": [{"qty" : 16, "warehouse":"A"}, + {"warehouse":"C", "qty":15}]})""; + std::string filter = R""({"instock.warehouse": "A"})"";; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest010, TestSize.Level0) +{ + std::string document = R""({"item": ["GG", {"gender":"girl", "hobby" : "IT"}], "instock": [{"warehouse":"A"}, + {"warehouse":"C", "qty":15}]})""; + std::string filter = R""({"instock.warehouse": "C"})""; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest011, TestSize.Level0) +{ + std::string document = R""({"item" : "journal", "instock" : [{"warehose" : "A", "qty" : 5}, {"warehose" : "C", "qty" : 15}]})""; + std::string filter = R""({"instock" : {"warehose" : "A", "qty" : 5}})""; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest012, TestSize.Level0) +{ + std::string document = R""({"item" : "journal", "instock" : [{"warehose" : "A", "qty" : 5}, {"warehose" : "C", "qty" : 15}]})""; + std::string filter = R""({"instock" : {"warehose" : "A", "bad" : "2" ,"qty" : 5}})""; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), false); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest013, TestSize.Level0) +{ + std::string document = R""({"item" : "journal", "instock" : [{"warehose" : "A", "qty" : 5}, {"warehose" : "C", "qty" : 15}]})""; + std::string filter = R""({"instock.qty" : 15})""; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest014, TestSize.Level0) +{ + std::string document = R""({"item" : "journal", "instock" : [{"warehose" : "A", "qty" : 5}, {"warehose" : "C", "qty" : 15}]})""; + std::string filter = R""({"instock.1.qty" : 15})""; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest015, TestSize.Level0) +{ + std::string document = R""({"item" : "journal", "qty" : 25, "tags" : ["blank", "red"], "dim_cm" : [14, 21]})""; + std::string filter = R""({"tags" : ["blank", "red"]})""; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest016, TestSize.Level0) +{ + std::string document = R""({"item" : "journal", "qty" : 25, "tags" : {"value" : null}, "dim_cm" : [14, 21]})""; + std::string filter = R""({"tags" : {"value" : null}})""; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); +} + +HWTEST_F(DocumentDBJsonCommonTest, JsonObjectisFilterCheckTest017, TestSize.Level0) +{ + std::string document = R""({"item" : "journal", "qty" : 25, "dim_cm" : [14, 21]})""; + std::string filter = R""({"tags" : {"value" : null}})""; + int errCode = E_OK; + JsonObject srcObj = JsonObject::Parse(document, errCode); + EXPECT_EQ(errCode, E_OK); + JsonObject filterObj = JsonObject::Parse(filter, errCode); + EXPECT_EQ(JsonCommon::isJsonNodeMatch(srcObj, filterObj, errCode), true); } \ No newline at end of file