• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include <string>
18 #include <vector>
19 #include <iostream>
20 
21 #include "distributed_device_profile_constants.h"
22 #include "distributed_device_profile_log.h"
23 #include "distributed_device_profile_errors.h"
24 
25 #include "device_profile_dao.h"
26 #include "device_profile.h"
27 #include "device_profile_filter_options.h"
28 
29 using namespace testing::ext;
30 namespace OHOS {
31 namespace DistributedDeviceProfile {
32 using namespace std;
33 namespace {
34 const std::string TAG = "DeviceProfileDaoTest";
35 }
36 class DeviceProfileDaoTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp();
41     void TearDown();
42 };
43 
SetUpTestCase()44 void DeviceProfileDaoTest::SetUpTestCase()
45 {}
46 
TearDownTestCase()47 void DeviceProfileDaoTest::TearDownTestCase()
48 {}
49 
SetUp()50 void DeviceProfileDaoTest::SetUp()
51 {}
52 
TearDown()53 void DeviceProfileDaoTest::TearDown()
54 {}
55 
56 /*
57  * @tc.name: Init001
58  * @tc.desc: Init
59  * @tc.type: FUNC
60  * @tc.require:
61  */
62 HWTEST_F(DeviceProfileDaoTest, Init001, TestSize.Level1)
63 {
64     int32_t result = DeviceProfileDao::GetInstance().Init();
65     EXPECT_EQ(result, DP_SUCCESS);
66     DeviceProfileDao::GetInstance().UnInit();
67 }
68 
69 /*
70  * @tc.name: Init001
71  * @tc.desc: Init
72  * @tc.type: FUNC
73  * @tc.require:
74  */
75 HWTEST_F(DeviceProfileDaoTest, UnInit001, TestSize.Level1)
76 {
77     int32_t result = DeviceProfileDao::GetInstance().UnInit();
78     EXPECT_EQ(result, DP_SUCCESS);
79 }
80 
81 /*
82  * @tc.name: Init002
83  * @tc.desc: Init
84  * @tc.type: FUNC
85  * @tc.require:
86  */
87 HWTEST_F(DeviceProfileDaoTest, UnInit002, TestSize.Level1)
88 {
89     DeviceProfileDao::GetInstance().Init();
90     int32_t result = DeviceProfileDao::GetInstance().UnInit();
91     EXPECT_EQ(result, DP_SUCCESS);
92 }
93 
94 /*
95  * @tc.name: UnInit002
96  * @tc.desc: UnInit
97  * @tc.type: FUNC
98  * @tc.require:
99  */
100 HWTEST_F(DeviceProfileDaoTest, UnInit003, TestSize.Level1)
101 {
102     int32_t result = DeviceProfileDao::GetInstance().UnInit();
103     EXPECT_EQ(result, DP_SUCCESS);
104 }
105 
106 /*
107  * @tc.name: PutDeviceProfile001
108  * @tc.desc: PutDeviceProfile
109  * @tc.type: FUNC
110  * @tc.require:
111  */
112 HWTEST_F(DeviceProfileDaoTest, PutDeviceProfile001, TestSize.Level1)
113 {
114     DeviceProfileDao::GetInstance().Init();
115     DeviceProfile deviceProfile;
116     DeviceProfileDao::GetInstance().PutDeviceProfile(deviceProfile);
117     DeviceProfileDao::GetInstance().UnInit();
118     int32_t result = DeviceProfileDao::GetInstance().PutDeviceProfile(deviceProfile);
119     DeviceProfileDao::GetInstance().Init();
120     deviceProfile.SetDeviceId("deviceId");
121     deviceProfile.SetUserId(1);
122     DeviceProfileDao::GetInstance().PutDeviceProfile(deviceProfile);
123     DeviceProfileDao::GetInstance().UnInit();
124     EXPECT_EQ(result, DP_PUT_TRUST_DEVICE_PROFILE_FAIL);
125 }
126 
127 /*
128  * @tc.name: GetDeviceProfiles003
129  * @tc.desc: GetDeviceProfiles
130  * @tc.type: FUNC
131  * @tc.require:
132  */
133 HWTEST_F(DeviceProfileDaoTest, PutDeviceProfile003, TestSize.Level1)
134 {
135     DeviceProfileDao::GetInstance().Init();
136     DeviceProfile deviceProfile;
137     deviceProfile.SetDeviceId("deviceId");
138     deviceProfile.SetUserId(1);
139     int ret = DeviceProfileDao::GetInstance().PutDeviceProfile(deviceProfile);
140     EXPECT_EQ(ret, DP_PUT_TRUST_DEVICE_PROFILE_FAIL);
141     DeviceProfileDao::GetInstance().UnInit();
142 }
143 
144 /*
145  * @tc.name: GetDeviceProfiles001
146  * @tc.desc: GetDeviceProfiles
147  * @tc.type: FUNC
148  * @tc.require:
149  */
150 HWTEST_F(DeviceProfileDaoTest, GetDeviceProfiles001, TestSize.Level1)
151 {
152     DeviceProfileFilterOptions filterOptions;
153     std::vector<DeviceProfile> deviceProfiles;
154     int32_t result = DeviceProfileDao::GetInstance().GetDeviceProfiles(filterOptions, deviceProfiles);
155     EXPECT_EQ(result, DP_GET_RESULTSET_FAIL);
156 }
157 
158 /*
159  * @tc.name: GetDeviceProfiles002
160  * @tc.desc: GetDeviceProfiles
161  * @tc.type: FUNC
162  * @tc.require:
163  */
164 HWTEST_F(DeviceProfileDaoTest, GetDeviceProfiles002, TestSize.Level1)
165 {
166     DeviceProfileDao::GetInstance().Init();
167     DeviceProfileFilterOptions filterOptions;
168     filterOptions.SetUserId(5);
169     filterOptions.SetDeviceIds({"deviceId"});
170     std::vector<DeviceProfile> deviceProfiles;
171     int32_t result = DeviceProfileDao::GetInstance().GetDeviceProfiles(filterOptions, deviceProfiles);
172     EXPECT_EQ(result, DP_NOT_FIND_DATA);
173     DeviceProfileDao::GetInstance().UnInit();
174 }
175 
176 /*
177  * @tc.name: GetDeviceProfiles003
178  * @tc.desc: GetDeviceProfiles
179  * @tc.type: FUNC
180  * @tc.require:
181  */
182 HWTEST_F(DeviceProfileDaoTest, GetDeviceProfiles003, TestSize.Level1)
183 {
184     DeviceProfileDao::GetInstance().Init();
185     DeviceProfile deviceProfile;
186     deviceProfile.SetDeviceId("deviceId");
187     deviceProfile.SetUserId(1);
188     DeviceProfileDao::GetInstance().PutDeviceProfile(deviceProfile);
189     DeviceProfileFilterOptions filterOptions;
190     filterOptions.SetUserId(1);
191     filterOptions.SetDeviceIds({"deviceId"});
192     std::vector<DeviceProfile> deviceProfiles;
193     int32_t result = DeviceProfileDao::GetInstance().GetDeviceProfiles(filterOptions, deviceProfiles);
194     EXPECT_EQ(result, DP_SUCCESS);
195     DeviceProfileDao::GetInstance().UnInit();
196 }
197 
198 /*
199  * @tc.name: DeleteDeviceProfile001
200  * @tc.desc: DeleteDeviceProfile
201  * @tc.type: FUNC
202  * @tc.require:
203  */
204 HWTEST_F(DeviceProfileDaoTest, DeleteDeviceProfile001, TestSize.Level1)
205 {
206     DeviceProfileDao::GetInstance().Init();
207     DeviceProfile deviceProfile;
208     deviceProfile.SetDeviceId("deviceId");
209     deviceProfile.SetUserId(1);
210     DeviceProfileDao::GetInstance().PutDeviceProfile(deviceProfile);
211     int32_t result = DeviceProfileDao::GetInstance().DeleteDeviceProfile(deviceProfile);
212     EXPECT_EQ(result, DP_SUCCESS);
213     DeviceProfileDao::GetInstance().UnInit();
214 }
215 
216 /*
217  * @tc.name: DeleteDeviceProfile002
218  * @tc.desc: DeleteDeviceProfile
219  * @tc.type: FUNC
220  * @tc.require:
221  */
222 HWTEST_F(DeviceProfileDaoTest, DeleteDeviceProfile002, TestSize.Level1)
223 {
224     DeviceProfileDao::GetInstance().Init();
225     DeviceProfile deviceProfile;
226     deviceProfile.SetDeviceId("deviceId");
227     deviceProfile.SetUserId(1);
228     DeviceProfileDao::GetInstance().PutDeviceProfile(deviceProfile);
229     DeviceProfileDao::GetInstance().UnInit();
230     int32_t result = DeviceProfileDao::GetInstance().DeleteDeviceProfile(deviceProfile);
231     EXPECT_EQ(result, DP_DELETE_TRUST_DEVICE_PROFILE_FAIL);
232 }
233 
234 /*
235  * @tc.name: UpdateDeviceProfile001
236  * @tc.desc: UpdateDeviceProfile
237  * @tc.type: FUNC
238  * @tc.require:
239  */
240 HWTEST_F(DeviceProfileDaoTest, UpdateDeviceProfile001, TestSize.Level1)
241 {
242     DeviceProfileDao::GetInstance().Init();
243     DeviceProfile deviceProfile;
244     deviceProfile.SetDeviceId("deviceId");
245     deviceProfile.SetUserId(1);
246     DeviceProfileDao::GetInstance().PutDeviceProfile(deviceProfile);
247     deviceProfile.SetSubProductId("SubProductId");
248     int32_t result = DeviceProfileDao::GetInstance().UpdateDeviceProfile(deviceProfile);
249     EXPECT_EQ(result, DP_SUCCESS);
250     DeviceProfileDao::GetInstance().UnInit();
251 }
252 
253 /*
254  * @tc.name: UpdateDeviceProfile001
255  * @tc.desc: UpdateDeviceProfile
256  * @tc.type: FUNC
257  * @tc.require:
258  */
259 HWTEST_F(DeviceProfileDaoTest, UpdateDeviceProfile002, TestSize.Level1)
260 {
261     DeviceProfileDao::GetInstance().Init();
262     DeviceProfile deviceProfile;
263     deviceProfile.SetDeviceId("deviceId");
264     deviceProfile.SetUserId(1);
265     DeviceProfileDao::GetInstance().PutDeviceProfile(deviceProfile);
266     DeviceProfileDao::GetInstance().UnInit();
267     deviceProfile.SetSubProductId("SubProductId");
268     int32_t result = DeviceProfileDao::GetInstance().UpdateDeviceProfile(deviceProfile);
269     EXPECT_EQ(result, DP_UPDATE_TRUST_DEVICE_PROFILE_FAIL);
270 }
271 
272 /*
273  * @tc.name: CreateTable001
274  * @tc.desc: CreateTable
275  * @tc.type: FUNC
276  * @tc.require:
277  */
278 HWTEST_F(DeviceProfileDaoTest, CreateTable001, TestSize.Level1)
279 {
280     DeviceProfileDao::GetInstance().Init();
281     int32_t result = DeviceProfileDao::GetInstance().CreateTable();
282     EXPECT_EQ(result, DP_SUCCESS);
283     DeviceProfileDao::GetInstance().UnInit();
284 }
285 
286 /*
287  * @tc.name: CreateTable002
288  * @tc.desc: CreateTable
289  * @tc.type: FUNC
290  * @tc.require:
291  */
292 HWTEST_F(DeviceProfileDaoTest, CreateTable002, TestSize.Level1)
293 {
294     DeviceProfileDao::GetInstance().Init();
295     ProfileDataRdbAdapter::GetInstance().UnInit();
296     int32_t result = DeviceProfileDao::GetInstance().CreateTable();
297     EXPECT_EQ(result, DP_CREATE_TABLE_FAIL);
298     DeviceProfileDao::GetInstance().UnInit();
299 }
300 
301 /*
302  * @tc.name: CreateIndex001
303  * @tc.desc: CreateIndex
304  * @tc.type: FUNC
305  * @tc.require:
306  */
307 HWTEST_F(DeviceProfileDaoTest, CreateIndex001, TestSize.Level1)
308 {
309     DeviceProfileDao::GetInstance().Init();
310     ProfileDataRdbAdapter::GetInstance().UnInit();
311     int32_t result = DeviceProfileDao::GetInstance().CreateIndex();
312     EXPECT_EQ(result, DP_CREATE_UNIQUE_INDEX_FAIL);
313     DeviceProfileDao::GetInstance().UnInit();
314 }
315 
316 /*
317  * @tc.name: CreateIndex002
318  * @tc.desc: CreateIndex
319  * @tc.type: FUNC
320  * @tc.require:
321  */
322 HWTEST_F(DeviceProfileDaoTest, CreateIndex002, TestSize.Level1)
323 {
324     DeviceProfileDao::GetInstance().Init();
325     int32_t result = DeviceProfileDao::GetInstance().CreateIndex();
326     EXPECT_EQ(result, DP_SUCCESS);
327     DeviceProfileDao::GetInstance().UnInit();
328 }
329 
330 /*
331  * @tc.name: CreateQuerySqlAndCondition001
332  * @tc.desc: CreateQuerySqlAndCondition
333  * @tc.type: FUNC
334  * @tc.require:
335  */
336 HWTEST_F(DeviceProfileDaoTest, CreateQuerySqlAndCondition001, TestSize.Level1)
337 {
338     DeviceProfileFilterOptions filterOptions;
339     std::string sql = "";
340     std::vector<ValueObject> condition;
341     DeviceProfileDao::GetInstance().CreateQuerySqlAndCondition(filterOptions, sql, condition);
342     EXPECT_NE(sql, "");
343     DeviceProfileDao::GetInstance().UnInit();
344 }
345 
346 /*
347  * @tc.name: CreateQuerySqlAndCondition001
348  * @tc.desc: CreateQuerySqlAndCondition
349  * @tc.type: FUNC
350  * @tc.require:
351  */
352 HWTEST_F(DeviceProfileDaoTest, CreateQuerySqlAndCondition002, TestSize.Level1)
353 {
354     DeviceProfileFilterOptions filterOptions;
355     std::vector<int32_t> deviceProfileIds;
356     deviceProfileIds.emplace_back(3);
357     filterOptions.SetDeviceProfileIds(deviceProfileIds);
358     std::vector<std::string> deviceIds;
359     deviceIds.emplace_back("deviceId1");
360     filterOptions.SetDeviceIds(deviceIds);
361     filterOptions.SetUserId(1);
362     filterOptions.SetAccountId("AccountId");
363     std::vector<std::string> wiseDeviceIds;
364     wiseDeviceIds.emplace_back("WiseDeviceIds");
365     filterOptions.SetWiseDeviceIds(wiseDeviceIds);
366     std::string sql = "";
367     std::vector<ValueObject> condition;
368     DeviceProfileDao::GetInstance().CreateQuerySqlAndCondition(filterOptions, sql, condition);
369     EXPECT_NE(sql, "");
370     DeviceProfileDao::GetInstance().UnInit();
371 }
372 }  // namespace DistributedDeviceProfile
373 }  // namespace OHOS
374