• 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 #define LOG_TAG "RdbCloudTest"
16 
17 #include "rdb_cloud.h"
18 
19 #include "gtest/gtest.h"
20 #include "log_print.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::DistributedData;
24 using namespace OHOS::DistributedRdb;
25 using DBVBucket = DistributedDB::VBucket;
26 using DBStatus = DistributedDB::DBStatus;
27 std::vector<DBVBucket> g_DBVBucket = { { { "#gid", { "0000000" } }, { "#flag", { true } },
28     { "#value", { int64_t(100) } }, { "#float", { double(100) } }, { "#_type", { int64_t(1) } },
29     { "#_query", { Bytes({ 1, 2, 3, 4 }) } } } };
30 namespace OHOS::Test {
31 namespace DistributedRDBTest {
32 class RdbCloudTest : public testing::Test {
33 public:
SetUpTestCase(void)34     static void SetUpTestCase(void){};
TearDownTestCase(void)35     static void TearDownTestCase(void){};
SetUp()36     void SetUp(){};
TearDown()37     void TearDown(){};
38 };
39 
40 /**
41 * @tc.name: RdbCloudTest001
42 * @tc.desc: RdbCloud BatchInsert BatchUpdate BatchDelete test.
43 * @tc.type: FUNC
44 * @tc.require:
45 * @tc.author: SQL
46 */
47 HWTEST_F(RdbCloudTest, RdbCloudTest001, TestSize.Level1)
48 {
49     BindAssets bindAssets;
50     Bytes bytes;
51     std::shared_ptr<CloudDB> cloudDB = std::make_shared<CloudDB>();
52     RdbCloud rdbCloud(cloudDB, &bindAssets);
53     std::string tableName = "testTable";
54     auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBucket), g_DBVBucket);
55     EXPECT_EQ(result, DBStatus::CLOUD_ERROR);
56     result = rdbCloud.BatchUpdate(tableName, std::move(g_DBVBucket), g_DBVBucket);
57     EXPECT_EQ(result, DBStatus::CLOUD_ERROR);
58     result = rdbCloud.BatchDelete(tableName, g_DBVBucket);
59     EXPECT_EQ(result, DBStatus::CLOUD_ERROR);
60 }
61 
62 /**
63 * @tc.name: RdbCloudTest002
64 * @tc.desc: RdbCloud Query PreSharing HeartBeat Close test.
65 * @tc.type: FUNC
66 * @tc.require:
67 * @tc.author: SQL
68 */
69 HWTEST_F(RdbCloudTest, RdbCloudTest002, TestSize.Level1)
70 {
71     BindAssets bindAssets;
72     std::shared_ptr<CloudDB> cloudDB = std::make_shared<CloudDB>();
73     RdbCloud rdbCloud(cloudDB, &bindAssets);
74     std::string tableName = "testTable";
75     rdbCloud.Lock();
76     std::string traceId = "id";
77     rdbCloud.SetPrepareTraceId(traceId);
78     auto result = rdbCloud.BatchInsert(tableName, std::move(g_DBVBucket), g_DBVBucket);
79     EXPECT_EQ(result, DBStatus::CLOUD_ERROR);
80     DBVBucket extends = {
81         {"#gid", {"0000000"}}, {"#flag", {true}}, {"#value", {int64_t(100)}}, {"#float", {double(100)}},
82         {"#_type", {int64_t(1)}}, {"#_query", {Bytes({ 1, 2, 3, 4 })}}
83     };
84     result = rdbCloud.Query(tableName, extends, g_DBVBucket);
85     EXPECT_EQ(result, DBStatus::CLOUD_ERROR);
86     std::vector<VBucket> vBuckets = {
87         {{"#gid", {"0000000"}}, {"#flag", {true}}, {"#value", {int64_t(100)}}, {"#float", {double(100)}},
88         {"#_type", {int64_t(1)}}, {"#_query", {Bytes({ 1, 2, 3, 4 })}}}
89     };
90     rdbCloud.PreSharing(tableName, vBuckets);
91     result = rdbCloud.HeartBeat();
92     EXPECT_EQ(result, DBStatus::CLOUD_ERROR);
93     rdbCloud.UnLock();
94     rdbCloud.GetEmptyCursor(tableName);
95     result = rdbCloud.Close();
96     EXPECT_EQ(result, DBStatus::CLOUD_ERROR);
97 }
98 
99 /**
100 * @tc.name: RdbCloudTest003
101 * @tc.desc: RdbCloud Query error test.
102 * @tc.type: FUNC
103 * @tc.require:
104 * @tc.author: SQL
105 */
106 HWTEST_F(RdbCloudTest, RdbCloudTest003, TestSize.Level1)
107 {
108     BindAssets bindAssets;
109     bindAssets.bindAssets = nullptr;
110     std::shared_ptr<CloudDB> cloudDB = std::make_shared<CloudDB>();
111     RdbCloud rdbCloud(cloudDB, &bindAssets);
112     std::string tableName = "testTable";
113     DBVBucket extends = {
114         {"#gid", {"0000000"}}, {"#flag", {true}}, {"#value", {int64_t(100)}}, {"#float", {double(100)}},
115         {"#_type", {int64_t(1)}}
116     };
117     std::vector<DBVBucket> data = {
118         {{"#gid", {"0000000"}}, {"#flag", {true}}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}}
119     };
120     auto result = rdbCloud.Query(tableName, extends, data);
121     EXPECT_EQ(result, DBStatus::CLOUD_ERROR);
122 
123     extends = {
124         {"#gid", {"0000000"}}, {"#flag", {true}}, {"#value", {int64_t(100)}}, {"#float", {double(100)}},
125         {"#_query", {Bytes({ 1, 2, 3, 4 })}}
126     };
127     result = rdbCloud.Query(tableName, extends, data);
128     EXPECT_EQ(result, DBStatus::CLOUD_ERROR);
129 
130     extends = {
131         {"#gid", {"0000000"}}, {"#flag", {true}}, {"#value", {int64_t(100)}}, {"#float", {double(100)}},
132         {"#_type", {int64_t(0)}}
133     };
134     result = rdbCloud.Query(tableName, extends, data);
135     EXPECT_EQ(result, DBStatus::CLOUD_ERROR);
136 }
137 
138 /**
139 * @tc.name: RdbCloudTest004
140 * @tc.desc: RdbCloud UnLockCloudDB LockCloudDB InnerUnLock test.
141 * @tc.type: FUNC
142 * @tc.require:
143 * @tc.author: SQL
144 */
145 HWTEST_F(RdbCloudTest, RdbCloudTest004, TestSize.Level1)
146 {
147     BindAssets bindAssets;
148     std::shared_ptr<CloudDB> cloudDB = std::make_shared<CloudDB>();
149     RdbCloud rdbCloud(cloudDB, &bindAssets);
150 
151     auto err = rdbCloud.UnLockCloudDB(OHOS::DistributedRdb::RdbCloud::FLAG::SYSTEM_ABILITY);
152     EXPECT_EQ(err, GeneralError::E_NOT_SUPPORT);
153 
154     auto result = rdbCloud.LockCloudDB(OHOS::DistributedRdb::RdbCloud::FLAG::SYSTEM_ABILITY);
155     EXPECT_EQ(result.first, GeneralError::E_NOT_SUPPORT);
156 
157     rdbCloud.flag_ = 1;
158     err = rdbCloud.InnerUnLock(static_cast<OHOS::DistributedRdb::RdbCloud::FLAG>(0));
159     EXPECT_EQ(err, GeneralError::E_OK);
160 }
161 
162 /**
163 * @tc.name: ConvertStatus
164 * @tc.desc: RdbCloud ConvertStatus function test.
165 * @tc.type: FUNC
166 * @tc.require:
167 * @tc.author: SQL
168 */
169 HWTEST_F(RdbCloudTest, ConvertStatus, TestSize.Level1)
170 {
171     BindAssets bindAssets;
172     std::shared_ptr<CloudDB> cloudDB = std::make_shared<CloudDB>();
173     RdbCloud rdbCloud(cloudDB, &bindAssets);
174     auto result = rdbCloud.ConvertStatus(GeneralError::E_OK);
175     EXPECT_EQ(result, DBStatus::OK);
176     result = rdbCloud.ConvertStatus(GeneralError::E_NETWORK_ERROR);
177     EXPECT_EQ(result, DBStatus::CLOUD_NETWORK_ERROR);
178     result = rdbCloud.ConvertStatus(GeneralError::E_LOCKED_BY_OTHERS);
179     EXPECT_EQ(result, DBStatus::CLOUD_LOCK_ERROR);
180     result = rdbCloud.ConvertStatus(GeneralError::E_RECODE_LIMIT_EXCEEDED);
181     EXPECT_EQ(result, DBStatus::CLOUD_FULL_RECORDS);
182     result = rdbCloud.ConvertStatus(GeneralError::E_NO_SPACE_FOR_ASSET);
183     EXPECT_EQ(result, DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT);
184     result = rdbCloud.ConvertStatus(GeneralError::E_RECORD_EXIST_CONFLICT);
185     EXPECT_EQ(result, DBStatus::CLOUD_RECORD_EXIST_CONFLICT);
186     result = rdbCloud.ConvertStatus(GeneralError::E_VERSION_CONFLICT);
187     EXPECT_EQ(result, DBStatus::CLOUD_VERSION_CONFLICT);
188     result = rdbCloud.ConvertStatus(GeneralError::E_RECORD_NOT_FOUND);
189     EXPECT_EQ(result, DBStatus::CLOUD_RECORD_NOT_FOUND);
190     result = rdbCloud.ConvertStatus(GeneralError::E_RECORD_ALREADY_EXISTED);
191     EXPECT_EQ(result, DBStatus::CLOUD_RECORD_ALREADY_EXISTED);
192     result = rdbCloud.ConvertStatus(GeneralError::E_FILE_NOT_EXIST);
193     EXPECT_EQ(result, DBStatus::LOCAL_ASSET_NOT_FOUND);
194     result = rdbCloud.ConvertStatus(GeneralError::E_TIME_OUT);
195     EXPECT_EQ(result, DBStatus::TIME_OUT);
196     result = rdbCloud.ConvertStatus(GeneralError::E_CLOUD_DISABLED);
197     EXPECT_EQ(result, DBStatus::CLOUD_DISABLED);
198     result = rdbCloud.ConvertStatus(GeneralError::E_SKIP_ASSET);
199     EXPECT_EQ(result, DBStatus::SKIP_ASSET);
200 }
201 
202 /**
203 * @tc.name: ConvertQuery
204 * @tc.desc: RdbCloud ConvertQuery function test.
205 * @tc.type: FUNC
206 * @tc.require:
207 * @tc.author: SQL
208 */
209 HWTEST_F(RdbCloudTest, ConvertQuery, TestSize.Level1)
210 {
211     RdbCloud::DBQueryNodes nodes;
212     DistributedDB::QueryNode node = { DistributedDB::QueryNodeType::IN, "",  {int64_t(1)} };
213     nodes.push_back(node);
214     node = { DistributedDB::QueryNodeType::OR, "",  {int64_t(1)} };
215     nodes.push_back(node);
216     node = { DistributedDB::QueryNodeType::AND, "",  {int64_t(1)} };
217     nodes.push_back(node);
218     node = { DistributedDB::QueryNodeType::EQUAL_TO, "",  {int64_t(1)} };
219     nodes.push_back(node);
220     node = { DistributedDB::QueryNodeType::BEGIN_GROUP, "",  {int64_t(1)} };
221     nodes.push_back(node);
222     node = { DistributedDB::QueryNodeType::END_GROUP, "",  {int64_t(1)} };
223     nodes.push_back(node);
224 
225     auto result = RdbCloud::ConvertQuery(std::move(nodes));
226     EXPECT_EQ(result.size(), 6);
227 
228     nodes.clear();
229     node = { DistributedDB::QueryNodeType::ILLEGAL, "",  {int64_t(1)} };
230     nodes.push_back(node);
231     result = RdbCloud::ConvertQuery(std::move(nodes));
232     EXPECT_EQ(result.size(), 0);
233 }
234 } // namespace DistributedRDBTest
235 } // namespace OHOS::Test