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 #define LOG_TAG "SystemDefinedRecordTest"
16 
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include <string>
20 
21 #include "logger.h"
22 #include "system_defined_record.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::UDMF;
26 using namespace OHOS;
27 namespace OHOS::Test {
28 using namespace std;
29 
30 class SystemDefinedRecordTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp() override;
35     void TearDown() override;
36 };
37 
SetUpTestCase()38 void SystemDefinedRecordTest::SetUpTestCase()
39 {
40 }
41 
TearDownTestCase()42 void SystemDefinedRecordTest::TearDownTestCase()
43 {
44 }
45 
SetUp()46 void SystemDefinedRecordTest::SetUp()
47 {
48 }
49 
TearDown()50 void SystemDefinedRecordTest::TearDown()
51 {
52 }
53 
54 /**
55 * @tc.name: AddProperty001
56 * @tc.desc: Normal testcase of AddProperty
57 * @tc.type: FUNC
58 */
59 HWTEST_F(SystemDefinedRecordTest, AddProperty001, TestSize.Level1)
60 {
61     LOG_INFO(UDMF_TEST, "AddProperty001 begin.");
62     const std::string property = "first";
63     UDVariant value = 0;
64     SystemDefinedRecord systemDefinedRecord;
65     systemDefinedRecord.value_ = std::make_shared<Object>();
66     systemDefinedRecord.AddProperty(property, value);
67     auto object = std::get<std::shared_ptr<Object>>(systemDefinedRecord.value_);
68     auto details = std::get<std::shared_ptr<Object>>(object->value_[SystemDefinedRecord::DETAILS]);
69     systemDefinedRecord.InitObject();
70     EXPECT_EQ(systemDefinedRecord.details_.size(), 1);
71     EXPECT_NE(details->value_.size(), 0);
72     LOG_INFO(UDMF_TEST, "AddProperty001 end.");
73 }
74 
75 /**
76 * @tc.name: AddProperty002
77 * @tc.desc: Normal testcase of AddProperty
78 * @tc.type: FUNC
79 */
80 HWTEST_F(SystemDefinedRecordTest, AddProperty002, TestSize.Level1)
81 {
82     LOG_INFO(UDMF_TEST, "AddProperty002 begin.");
83     UDVariant value = 0;
84     SystemDefinedRecord systemDefinedRecord;
85     systemDefinedRecord.details_.insert({ "first", "second" });
86     const std::string property = "first";
87     systemDefinedRecord.value_ = std::make_shared<Object>();
88     systemDefinedRecord.AddProperty(property, value);
89     auto object = std::get<std::shared_ptr<Object>>(systemDefinedRecord.value_);
90     auto details = std::get<std::shared_ptr<Object>>(object->value_[SystemDefinedRecord::DETAILS]);
91     EXPECT_EQ(systemDefinedRecord.details_[property], value);
92     EXPECT_NE(details->value_.size(), 0);
93     LOG_INFO(UDMF_TEST, "AddProperty002 end.");
94 }
95 
96 /**
97 * @tc.name: GetPropertyByName001
98 * @tc.desc: Normal testcase of GetPropertyByName
99 * @tc.type: FUNC
100 */
101 HWTEST_F(SystemDefinedRecordTest, GetPropertyByName001, TestSize.Level1)
102 {
103     LOG_INFO(UDMF_TEST, "GetPropertyByName001 begin.");
104     const std::string property = "first";
105     SystemDefinedRecord systemDefinedRecord;
106     systemDefinedRecord.details_.insert({ "first", "second" });
107     const UDVariant ret = systemDefinedRecord.GetPropertyByName(property);
108     EXPECT_EQ(std::get<std::string>(ret), "second");
109     LOG_INFO(UDMF_TEST, "GetPropertyByName001 end.");
110 }
111 
112 /**
113 * @tc.name: InitObject001
114 * @tc.desc: Normal testcase of InitObject
115 * @tc.type: FUNC
116 */
117 HWTEST_F(SystemDefinedRecordTest, InitObject001, TestSize.Level1)
118 {
119     LOG_INFO(UDMF_TEST, "InitObject001 begin.");
120     SystemDefinedRecord systemDefinedRecord;
121     systemDefinedRecord.details_.insert({ "first", "second" });
122     systemDefinedRecord.InitObject();
123     auto object = std::get<std::shared_ptr<Object>>(systemDefinedRecord.value_);
124     auto details = std::get<std::shared_ptr<Object>>(object->value_[SystemDefinedRecord::DETAILS]);
125     EXPECT_NE(details->value_.size(), 0);
126     LOG_INFO(UDMF_TEST, "InitObject001 end.");
127 }
128 } // OHOS::Test