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 "UnifiedDataTest"
16 
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include <string>
20 
21 #include "logger.h"
22 #include "udmf_capi_common.h"
23 #include "unified_data.h"
24 
25 using namespace testing::ext;
26 using namespace OHOS::UDMF;
27 using namespace OHOS;
28 namespace OHOS::Test {
29 using namespace std;
30 
31 class UnifiedDataTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp() override;
36     void TearDown() override;
37 };
38 
SetUpTestCase()39 void UnifiedDataTest::SetUpTestCase()
40 {
41 }
42 
TearDownTestCase()43 void UnifiedDataTest::TearDownTestCase()
44 {
45 }
46 
SetUp()47 void UnifiedDataTest::SetUp()
48 {
49 }
50 
TearDown()51 void UnifiedDataTest::TearDown()
52 {
53 }
54 
55 /**
56 * @tc.name: UnifiedData001
57 * @tc.desc: Normal testcase of UnifiedData
58 * @tc.type: FUNC
59 */
60 HWTEST_F(UnifiedDataTest, UnifiedData001, TestSize.Level1)
61 {
62     LOG_INFO(UDMF_TEST, "UnifiedData001 begin.");
63     std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>();
64     UnifiedData unifiedData(properties);
65     auto duration = std::chrono::system_clock::now().time_since_epoch();
66     EXPECT_EQ(unifiedData.properties_, properties);
67     EXPECT_EQ(unifiedData.properties_->timestamp,
68         std::chrono::duration_cast<std::chrono::milliseconds>(duration).count());
69     LOG_INFO(UDMF_TEST, "UnifiedData001 end.");
70 }
71 
72 /**
73 * @tc.name: GetGroupId001
74 * @tc.desc: Normal testcase of GetGroupId
75 * @tc.type: FUNC
76 */
77 HWTEST_F(UnifiedDataTest, GetGroupId001, TestSize.Level1)
78 {
79     LOG_INFO(UDMF_TEST, "GetGroupId001 begin.");
80     UnifiedData unifiedData;
81     unifiedData.runtime_ = std::make_shared<Runtime>();
82     std::string ret = unifiedData.GetGroupId();
83     EXPECT_EQ(ret, unifiedData.runtime_->key.groupId);
84     LOG_INFO(UDMF_TEST, "GetGroupId001 end.");
85 }
86 
87 /**
88 * @tc.name: GetRuntime001
89 * @tc.desc: Normal testcase of GetRuntime
90 * @tc.type: FUNC
91 */
92 HWTEST_F(UnifiedDataTest, GetRuntime001, TestSize.Level1)
93 {
94     LOG_INFO(UDMF_TEST, "GetRuntime001 begin.");
95     UnifiedData unifiedData;
96     unifiedData.runtime_ = std::make_shared<Runtime>();
97     std::shared_ptr<Runtime> ret = unifiedData.GetRuntime();
98     EXPECT_EQ(ret, unifiedData.runtime_);
99     LOG_INFO(UDMF_TEST, "GetRuntime001 end.");
100 }
101 
102 /**
103 * @tc.name: SetRuntime001
104 * @tc.desc: Normal testcase of SetRuntime
105 * @tc.type: FUNC
106 */
107 HWTEST_F(UnifiedDataTest, SetRuntime001, TestSize.Level1)
108 {
109     LOG_INFO(UDMF_TEST, "SetRuntime001 begin.");
110     UnifiedData unifiedData;
111     Runtime runtime{};
112     unifiedData.SetRuntime(runtime);
113     EXPECT_NE(unifiedData.runtime_, nullptr);
114     LOG_INFO(UDMF_TEST, "SetRuntime001 end.");
115 }
116 
117 /**
118 * @tc.name: AddRecord001
119 * @tc.desc: Abnormal testcase of AddRecord, because record is nullptr
120 * @tc.type: FUNC
121 */
122 HWTEST_F(UnifiedDataTest, AddRecord001, TestSize.Level1)
123 {
124     LOG_INFO(UDMF_TEST, "AddRecord001 begin.");
125     const std::shared_ptr<UnifiedRecord> record = nullptr;
126     UnifiedData unifiedData;
127     unifiedData.AddRecord(record);
128     EXPECT_EQ(unifiedData.records_.size(), 0);
129     LOG_INFO(UDMF_TEST, "AddRecord001 end.");
130 }
131 
132 /**
133 * @tc.name: AddRecords001
134 * @tc.desc: Abnormal testcase of AddRecords, because record is nullptr
135 * @tc.type: FUNC
136 */
137 HWTEST_F(UnifiedDataTest, AddRecords001, TestSize.Level1)
138 {
139     LOG_INFO(UDMF_TEST, "AddRecords001 begin.");
140     const std::vector<std::shared_ptr<UnifiedRecord>> record = {nullptr};
141     UnifiedData unifiedData;
142     unifiedData.AddRecords(record);
143     EXPECT_EQ(unifiedData.records_.size(), 0);
144     LOG_INFO(UDMF_TEST, "AddRecords001 end.");
145 }
146 
147 /**
148 * @tc.name: GetRecordAt001
149 * @tc.desc: Abnormal testcase of GetRecordAt, because the length of records_ is equal to the length of index
150 * @tc.type: FUNC
151 */
152 HWTEST_F(UnifiedDataTest, GetRecordAt001, TestSize.Level1)
153 {
154     LOG_INFO(UDMF_TEST, "GetRecordAt001 begin.");
155     UnifiedData unifiedData;
156     unifiedData.records_ = std::vector<std::shared_ptr<UnifiedRecord>>();
157     std::size_t index = unifiedData.records_.size();
158     std::shared_ptr<UnifiedRecord> ret = unifiedData.GetRecordAt(index);
159     EXPECT_EQ(ret, nullptr);
160     LOG_INFO(UDMF_TEST, "GetRecordAt001 end.");
161 }
162 } // OHOS::Test