• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 
18 #include "cjson_serializer.h"
19 
20 using namespace testing::ext;
21 
22 namespace OHOS {
23 namespace EDM {
24 namespace TEST {
25 const std::string TEST_POLICY_DATA = "{\"test_app_id\": \"{\\\"test_policy_name\\\":\\\"test_policy_value\\\"}\"}";
26 class CjsonSerializerTest : public testing::Test {};
27 /**
28  * @tc.name: TestWritePolicy
29  * @tc.desc: Test CjsonSerializer::WritePolicy func.
30  * and policies is empty.
31  * @tc.type: FUNC
32  */
33 HWTEST_F(CjsonSerializerTest, TestWritePolicy, TestSize.Level1)
34 {
35     auto serializer =  CjsonSerializer::GetInstance();
36     cJSON* jsonPolicy;
37     MessageParcel reply;
38     serializer->Deserialize(TEST_POLICY_DATA, jsonPolicy);
39     ASSERT_TRUE(serializer->WritePolicy(reply, jsonPolicy));
40 
41     cJSON* jsonPolicy2;
42     std::string strPolicy;
43     ASSERT_TRUE(serializer->GetPolicy(reply, jsonPolicy2));
44     ASSERT_TRUE(serializer->Serialize(jsonPolicy2, strPolicy));
45 
46     std::string jsonString = "";
47     cJSON* root = cJSON_Parse(TEST_POLICY_DATA.c_str());
48     char* cJsonStr = cJSON_Print(root);
49     if (cJsonStr != nullptr) {
50         jsonString = std::string(cJsonStr);
51         cJSON_free(cJsonStr);
52     }
53     cJSON_Delete(root);
54 
55     ASSERT_TRUE(jsonString == strPolicy);
56 
57     std::vector<cJSON*> jsons{jsonPolicy};
58     ASSERT_TRUE(serializer->MergePolicy(jsons, jsonPolicy2));
59 }
60 } // namespace TEST
61 } // namespace EDM
62 } // namespace OHOS
63