1 /*
2 * Copyright (c) 2021-2022 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 "recovery_test.h"
17
18 #include "database_disaster_recovery.h"
19 #include "test_common.h"
20
21 namespace Contacts {
22 namespace Test {
ClearData()23 void RecoveryTest::ClearData()
24 {
25 OHOS::AbilityRuntime::ContactsDataAbility contactsDataAbility;
26 OHOS::DataShare::DataSharePredicates predicates;
27 OHOS::Uri uriRawContact(ContactsUri::RAW_CONTACT);
28 predicates.NotEqualTo("id", "0");
29 predicates.And();
30 predicates.EqualTo("is_deleted", "0");
31 int code1 = contactsDataAbility.Delete(uriRawContact, predicates);
32 HILOG_INFO("RecoveryTest ClearData code1 is %{public}d", code1);
33 int time = 3000;
34 std::chrono::milliseconds dura(time);
35 std::this_thread::sleep_for(dura);
36 OHOS::DataShare::DataSharePredicates predicates2;
37 OHOS::Uri uriRawContactComplete(ContactsUri::DELETED_RAW_CONTACT);
38 predicates2.NotEqualTo("id", "0");
39 int code = contactsDataAbility.Delete(uriRawContactComplete, predicates2);
40 HILOG_INFO("RecoveryTest ClearData code is %{public}d", code);
41 }
42
RawContactInsert(std::string displayName)43 int64_t RecoveryTest::RawContactInsert(std::string displayName)
44 {
45 OHOS::Uri uriRawContact(ContactsUri::RAW_CONTACT);
46 OHOS::DataShare::DataShareValuesBucket rawContactValues;
47 rawContactValues.Put("display_name", displayName);
48 int64_t code = contactsDataAbility.Insert(uriRawContact, rawContactValues);
49 rawContactValues.Clear();
50 return code;
51 }
52
53 /*
54 * @tc.number recovery_test_100
55 * @tc.name Backup database
56 * @tc.desc Exception use case
57 * @tc.level Level1
58 * @tc.size MediumTest
59 * @tc.type Function
60 */
HWTEST_F(RecoveryTest, recovery_test_100, testing::ext::TestSize.Level1)61 HWTEST_F(RecoveryTest, recovery_test_100, testing::ext::TestSize.Level1)
62 {
63 HILOG_INFO("--- recovery_test_100 is starting! ---");
64 std::shared_ptr<OHOS::Contacts::DataBaseDisasterRecovery> instance =
65 OHOS::Contacts::DataBaseDisasterRecovery::GetInstance();
66 std::shared_ptr<OHOS::Contacts::ContactsDataBase> contactDatabase = OHOS::Contacts::ContactsDataBase::GetInstance();
67 int result = instance->SQLiteCheckDb();
68 HILOG_INFO("--- recovery_test_100 SQLiteCheckDb ret ! --- %{public}d", result);
69 if (result == 0) {
70 instance->BackDatabase();
71 }
72 std::string backupPath = instance->GetBackUpDatabase(contactDatabase->contactStore_);
73 HILOG_INFO("--- recovery_test_100 is end! --- %{private}s ", backupPath.c_str());
74 ClearData();
75 }
76
77 /*
78 * @tc.number recovery_test_200
79 * @tc.name Backup database after insert two person, then recover
80 * @tc.desc Exception use case
81 * @tc.level Level1
82 * @tc.size MediumTest
83 * @tc.type Function
84 */
HWTEST_F(RecoveryTest, recovery_test_200, testing::ext::TestSize.Level1)85 HWTEST_F(RecoveryTest, recovery_test_200, testing::ext::TestSize.Level1)
86 {
87 HILOG_INFO("--- recovery_test_200 is starting! ---");
88 RawContactInsert("liming");
89 RawContactInsert("xiaolilili");
90 // backup
91 OHOS::Uri uriRawContactBackUp(ContactsUri::BACKUP);
92 OHOS::DataShare::DataShareValuesBucket value;
93 OHOS::DataShare::DataSharePredicates predicates;
94 int retCode = contactsDataAbility.Update(uriRawContactBackUp, predicates, value);
95 EXPECT_EQ(0, retCode);
96
97 RawContactInsert("xiaobaibaibai");
98 RawContactInsert("xiaolitiantian");
99 OHOS::Uri uriRawContact(ContactsUri::RAW_CONTACT);
100 std::vector<std::string> columns;
101 predicates.EqualTo("is_deleted", "0");
102 std::shared_ptr<OHOS::DataShare::DataShareResultSet> resultSet =
103 contactsDataAbility.Query(uriRawContact, predicates, columns);
104 int rowCount = 0;
105 resultSet->GetRowCount(rowCount);
106 resultSet->Close();
107 EXPECT_EQ(4, rowCount);
108
109 OHOS::Uri uriRawContactRecover(ContactsUri::RECOVER);
110 int retCodeRecover = contactsDataAbility.Update(uriRawContactRecover, predicates, value);
111 EXPECT_EQ(0, retCodeRecover);
112
113 std::shared_ptr<OHOS::DataShare::DataShareResultSet> resultSetRecover =
114 contactsDataAbility.Query(uriRawContact, predicates, columns);
115 int rowCountRecover = 0;
116 resultSetRecover->GetRowCount(rowCountRecover);
117 resultSetRecover->Close();
118 EXPECT_EQ(2, rowCountRecover);
119 ClearData();
120 }
121 } // namespace Test
122 } // namespace Contacts
123