1 /*
2 * Copyright (c) 2021 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 <thread>
18
19 #include "db_constant.h"
20 #include "db_errno.h"
21 #include "distributeddb_data_generate_unit_test.h"
22 #include "distributeddb_tools_unit_test.h"
23 #include "kv_store_nb_delegate_impl.h"
24 #include "log_print.h"
25 #include "sqlite_single_ver_natural_store.h"
26
27 using namespace testing::ext;
28 using namespace DistributedDB;
29 using namespace DistributedDBUnitTest;
30 using namespace std;
31
32 namespace {
33 string g_testDir;
34 KvStoreConfig g_config;
35 KvStoreDelegateManager g_mgr(APP_ID, USER_ID);
36 DBStatus g_kvDelegateStatus = INVALID_ARGS;
37 KvStoreNbDelegate *g_kvNbDelegatePtr = nullptr;
38
39 const int OBSERVER_SLEEP_TIME = 100;
40 const int BATCH_PRESET_SIZE_TEST = 10;
41 const int DIVIDE_BATCH_PRESET_SIZE = 5;
42 const int VALUE_OFFSET = 5;
43 const int DEFAULT_KEY_VALUE_SIZE = 10;
44
45 const Key KEY1{'k', 'e', 'y', '1'};
46 const Key KEY2{'k', 'e', 'y', '2'};
47 const Value VALUE1{'v', 'a', 'l', 'u', 'e', '1'};
48 const Value VALUE2{'v', 'a', 'l', 'u', 'e', '2'};
49
50 const std::string VALID_SCHEMA_STRICT_DEFINE = "{\"SCHEMA_VERSION\":\"1.0\","
51 "\"SCHEMA_MODE\":\"STRICT\","
52 "\"SCHEMA_DEFINE\":{"
53 "\"field_name1\":\"BOOL\","
54 "\"field_name2\":\"INTEGER, NOT NULL\""
55 "},"
56 "\"SCHEMA_INDEXES\":[\"$.field_name1\"]}";
57
58 CipherPassword g_passwd;
59 KvStoreNbDelegate::Option g_strictOpt = {
60 true, false, false, CipherType::DEFAULT, g_passwd,
61 VALID_SCHEMA_STRICT_DEFINE
62 };
63
64 // the type of g_kvNbDelegateCallback is function<void(DBStatus, KvStoreDelegate*)>
65 auto g_kvNbDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback, placeholders::_1,
66 placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvNbDelegatePtr));
67
CreatEntrys(int recordSize,vector<Key> & keys,vector<Value> & values,vector<Entry> & entries)68 static void CreatEntrys(int recordSize, vector<Key> &keys, vector<Value> &values, vector<Entry> &entries)
69 {
70 keys.clear();
71 values.clear();
72 entries.clear();
73 for (int i = 0; i < recordSize; i++) {
74 string temp = to_string(i);
75 Entry entry;
76 Key keyTemp;
77 Value valueTemp;
78 for (auto &iter : temp) {
79 entry.key.push_back(iter);
80 entry.value.push_back(iter);
81 keyTemp.push_back(iter);
82 valueTemp.push_back(iter);
83 }
84 keys.push_back(keyTemp);
85 values.push_back(valueTemp);
86 entries.push_back(entry);
87 }
88 }
89 }
90
91 class DistributedDBInterfacesNBDelegateLocalBatchTest : public testing::Test {
92 public:
93 static void SetUpTestCase(void);
94 static void TearDownTestCase(void);
95 void SetUp();
96 void TearDown();
97 };
98
SetUpTestCase(void)99 void DistributedDBInterfacesNBDelegateLocalBatchTest::SetUpTestCase(void)
100 {
101 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
102 g_config.dataDir = g_testDir;
103 g_mgr.SetKvStoreConfig(g_config);
104 }
105
TearDownTestCase(void)106 void DistributedDBInterfacesNBDelegateLocalBatchTest::TearDownTestCase(void)
107 {
108 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
109 LOGE("rm test db files error!");
110 }
111 }
112
SetUp(void)113 void DistributedDBInterfacesNBDelegateLocalBatchTest::SetUp(void)
114 {
115 DistributedDBToolsUnitTest::PrintTestCaseInfo();
116 g_kvDelegateStatus = INVALID_ARGS;
117 g_kvNbDelegatePtr = nullptr;
118 }
119
TearDown(void)120 void DistributedDBInterfacesNBDelegateLocalBatchTest::TearDown(void)
121 {
122 if (g_kvNbDelegatePtr != nullptr) {
123 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
124 g_kvNbDelegatePtr = nullptr;
125 }
126 }
127
128 /**
129 * @tc.name: PutLocalBatch001
130 * @tc.desc: This test case use to verify the PutLocalBatch interface function
131 * @tc.type: FUNC
132 * @tc.require: AR000EPAS8
133 * @tc.author: changguicai
134 */
135 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, PutLocalBatch001, TestSize.Level1)
136 {
137 /**
138 * @tc.steps: step1. Get singleVer kvStore by GetKvStore.
139 * @tc.expected: step1. Get database success.
140 */
141 const KvStoreNbDelegate::Option option = {true, true};
142 g_mgr.SetKvStoreConfig(g_config);
143 g_mgr.GetKvStore("distributed_PutLocalBatch_001", option, g_kvNbDelegateCallback);
144 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
145 EXPECT_TRUE(g_kvDelegateStatus == OK);
146
147 /**
148 * @tc.steps: step2. Insert 10 records into database.
149 * @tc.expected: step2. Insert successfully.
150 */
151 vector<Entry> entries;
152 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
153 Entry entry;
154 entry.key.push_back(i);
155 entry.value.push_back(i);
156 entries.push_back(entry);
157 }
158
159 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entries), OK);
160
161 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
162 Key key;
163 key.push_back(i);
164 Value value;
165 g_kvNbDelegatePtr->GetLocal(key, value);
166 EXPECT_EQ(key, value);
167 }
168
169 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
170 g_kvNbDelegatePtr = nullptr;
171 }
172
173 /**
174 * @tc.name: PutLocalBatch002
175 * @tc.desc: This testCase use to verify the PutLocalBatch and DeleteLocalBatch function while conn is nullptr
176 * @tc.type: FUNC
177 * @tc.require:
178 * @tc.author: caihaoting
179 */
180 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, PutLocalBatch002, TestSize.Level1)
181 {
182 /**
183 * @tc.steps: step1. Get singleVer kvStore by GetKvStore.
184 * @tc.expected: step1. Get database success.
185 */
186 const KvStoreNbDelegate::Option option = {true, true};
187 g_mgr.SetKvStoreConfig(g_config);
188 g_mgr.GetKvStore("distributed_PutLocalBatch_002", option, g_kvNbDelegateCallback);
189 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
190 EXPECT_TRUE(g_kvDelegateStatus == OK);
191
192 /**
193 * @tc.steps: step2. PutLocalBatch and DeleteLocalBatch 10 records into database while conn is nullptr.
194 * @tc.expected: step2. DB_ERROR.
195 */
196 vector<Entry> entries;
197 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
198 Entry entry;
199 entry.key.push_back(i);
200 entry.value.push_back(i);
201 entries.push_back(entry);
202 }
203
204 auto kvStoreImpl = static_cast<KvStoreNbDelegateImpl *>(g_kvNbDelegatePtr);
205 EXPECT_EQ(kvStoreImpl->Close(), OK);
206 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entries), DB_ERROR);
207 vector<Key> keys;
208 vector<Value> values;
209 CreatEntrys(BATCH_PRESET_SIZE_TEST, keys, values, entries);
210 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocalBatch(keys), DB_ERROR);
211
212 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
213 g_kvNbDelegatePtr = nullptr;
214 }
215
216 /**
217 * @tc.name: SingleVerPutLocalBatch001
218 * @tc.desc: Check for illegal parameters
219 * @tc.type: FUNC
220 * @tc.require: AR000EPAS8
221 * @tc.author: changguicai
222 */
223 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, SingleVerPutLocalBatch001, TestSize.Level1)
224 {
225 /**
226 * @tc.steps: step1.
227 * Create and construct three sets of vector <Entry>, each set of three data contains records:
228 * (K1, V1) It is illegal for K1 to be greater than 1K, and V1 is 1K in size
229 * (K2, V2) K2 is legal, V2 is greater than 4M
230 * (K3, V3) are not legal.
231 */
232 Key illegalKey;
233 DistributedDBToolsUnitTest::GetRandomKeyValue(illegalKey, DBConstant::MAX_KEY_SIZE + 1); // 1K + 1
234 Value illegalValue;
235 DistributedDBToolsUnitTest::GetRandomKeyValue(illegalValue, DBConstant::MAX_VALUE_SIZE + 1); // 4M + 1
236 vector<Entry> entrysKeyIllegal = {KV_ENTRY_1, KV_ENTRY_2, {illegalKey, VALUE_3}};
237 vector<Entry> entrysValueIllegal = {KV_ENTRY_1, KV_ENTRY_2, {KEY_3, illegalValue}};
238 vector<Entry> entrysIllegal = {KV_ENTRY_1, KV_ENTRY_2, {illegalKey, illegalValue}};
239
240 const KvStoreNbDelegate::Option option = {true, false};
241 g_mgr.SetKvStoreConfig(g_config);
242 g_mgr.GetKvStore("distributed_SingleVerPutLocalBatch_001", option, g_kvNbDelegateCallback);
243 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
244 EXPECT_TRUE(g_kvDelegateStatus == OK);
245 /**
246 * @tc.steps: step2. PutBatch operates on three sets of data.
247 * @tc.expected: step2. All three operations return INVALID_ARGS.
248 */
249 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrysKeyIllegal), INVALID_ARGS);
250 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrysValueIllegal), INVALID_ARGS);
251 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrysIllegal), INVALID_ARGS);
252
253 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
254 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutLocalBatch_001"), OK);
255 g_kvNbDelegatePtr = nullptr;
256 }
257
258 /**
259 * @tc.name: SingleVerPutLocalBatch002
260 * @tc.desc: PutLocalBatch normal insert function test.
261 * @tc.type: FUNC
262 * @tc.require: AR000EPAS8
263 * @tc.author: changguicai
264 */
265 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, SingleVerPutLocalBatch002, TestSize.Level1)
266 {
267 const KvStoreNbDelegate::Option option = {true, false};
268 g_mgr.SetKvStoreConfig(g_config);
269 g_mgr.GetKvStore("distributed_SingleVerPutLocalBatch_002", option, g_kvNbDelegateCallback);
270 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
271 EXPECT_TRUE(g_kvDelegateStatus == OK);
272 /**
273 * @tc.steps: step1.
274 * Create and build 4 groups of vector <Entry>, which are:
275 * Vect of empty objects;
276 * Vect1 of a legal Entry record;
277 * 128 legal Entry records Vect2;
278 * 129 legal Entry records Vect3;
279 */
280 vector<Entry> entrysMaxNumber;
281 for (size_t i = 0; i < DBConstant::MAX_BATCH_SIZE; i++) {
282 Entry entry;
283 entry.key.push_back(i);
284 entry.value.push_back(i);
285 entrysMaxNumber.push_back(entry);
286 }
287 Key keyTemp = {'1', '1'};
288 Value valueTemp;
289 Entry entryTemp = {keyTemp, VALUE_1};
290 vector<Entry> entrysOneRecord = {entryTemp};
291 vector<Entry> entrysOverSize = entrysMaxNumber;
292 entrysOverSize.push_back(entryTemp);
293 /**
294 * @tc.steps: step2. PutBatch operates on four sets of data. and use get check the result of Vect3.
295 * @tc.expected: step2. Returns OK for 129 records.
296 */
297 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrysOverSize), OK);
298 for (size_t i = 0; i < entrysOverSize.size(); i++) {
299 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(entrysOverSize[i].key, valueTemp), OK);
300 }
301 /**
302 * @tc.steps: step3. Use get check the result of Vect2.
303 * @tc.expected: step3. Return OK and get the correct value.
304 */
305 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrysOneRecord), OK);
306 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(keyTemp, valueTemp), OK);
307 EXPECT_EQ(valueTemp, VALUE_1);
308 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrysMaxNumber), OK);
309 /**
310 * @tc.steps: step4. Use get check the result of Vect3.
311 * @tc.expected: step4. Return OK and get the correct value.
312 */
313 for (size_t i = 0; i < entrysMaxNumber.size(); i++) {
314 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(entrysMaxNumber[i].key, valueTemp), OK);
315 EXPECT_EQ(valueTemp, entrysMaxNumber[i].value);
316 }
317
318 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
319 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutLocalBatch_002"), OK);
320 g_kvNbDelegatePtr = nullptr;
321 }
322
323 /**
324 * @tc.name: SingleVerPutLocalBatch003
325 * @tc.desc: Check interface atomicity
326 * @tc.type: FUNC
327 * @tc.require: AR000EPAS8
328 * @tc.author: changguicai
329 */
330 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, SingleVerPutLocalBatch003, TestSize.Level1)
331 {
332 const KvStoreNbDelegate::Option option = {true, false};
333 g_mgr.SetKvStoreConfig(g_config);
334 g_mgr.GetKvStore("distributed_SingleVerPutLocalBatch_003", option, g_kvNbDelegateCallback);
335 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
336 EXPECT_TRUE(g_kvDelegateStatus == OK);
337 /**
338 * @tc.steps: step1. Create and construct a set of vector <Entry> with a total of 128 data,
339 * including one illegal data. And call PutBatch interface to insert.
340 */
341 vector<Entry> entrysMaxNumber;
342 for (size_t i = 0; i < DBConstant::MAX_BATCH_SIZE; i++) {
343 Entry entry;
344 entry.key.push_back(i);
345 entry.value.push_back(i);
346 entrysMaxNumber.push_back(entry);
347 }
348 Key illegalKey;
349 Value valueTemp;
350 DistributedDBToolsUnitTest::GetRandomKeyValue(illegalKey, DBConstant::MAX_KEY_SIZE + 1); // 1K + 1
351 entrysMaxNumber[0].key = illegalKey;
352
353 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrysMaxNumber), INVALID_ARGS);
354 /**
355 * @tc.steps: step2. Use Get interface to query 128 corresponding key values.
356 * @tc.expected: step2. All Get interface return NOT_FOUND.
357 */
358 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(entrysMaxNumber[0].key, valueTemp), INVALID_ARGS);
359 for (size_t i = 1; i < entrysMaxNumber.size(); i++) {
360 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(entrysMaxNumber[i].key, valueTemp), NOT_FOUND);
361 }
362 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
363 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutLocalBatch_003"), OK);
364 g_kvNbDelegatePtr = nullptr;
365 }
366
PreparePutLocalBatch004(vector<Entry> & entrys1,vector<Entry> & entrys2,vector<Entry> & entrys3)367 static void PreparePutLocalBatch004(vector<Entry> &entrys1, vector<Entry> &entrys2, vector<Entry> &entrys3)
368 {
369 const KvStoreNbDelegate::Option option = {true, false};
370 g_mgr.SetKvStoreConfig(g_config);
371 g_mgr.GetKvStore("distributed_SingleVerPutLocalBatch_004", option, g_kvNbDelegateCallback);
372 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
373 EXPECT_TRUE(g_kvDelegateStatus == OK);
374
375 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
376 Entry entry;
377 entry.key.push_back(i);
378 entry.value.push_back(i);
379 entrys1.push_back(entry);
380 }
381
382 for (int i = 0; i < DIVIDE_BATCH_PRESET_SIZE; i++) {
383 Entry entry;
384 entry.key.push_back(i);
385 entry.value.push_back(i + VALUE_OFFSET);
386 entrys2.push_back(entry);
387 }
388
389 for (int i = DIVIDE_BATCH_PRESET_SIZE; i < BATCH_PRESET_SIZE_TEST; i++) {
390 Entry entry;
391 entry.key.push_back(i);
392 entry.value.push_back(i - VALUE_OFFSET);
393 entrys3.push_back(entry);
394 }
395 }
396
397 /**
398 * @tc.name: SingleVerPutLocalBatch004
399 * @tc.desc: Check interface data insertion and update functions.
400 * @tc.type: FUNC
401 * @tc.require: AR000EPAS8
402 * @tc.author: changguicai
403 */
404 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, SingleVerPutLocalBatch004, TestSize.Level1)
405 {
406 /**
407 * @tc.steps: step1.
408 * Construct three groups of three vector <Entry>:
409 * (1) entrys1: key1 ~ 10, corresponding to Value1 ~ 10;
410 * (2) entrys2: key1 ~ 5, corresponding to Value6 ~ 10;
411 * (3) entrys3: key6 ~ 10, corresponding to Value1 ~ 5;
412 */
413 vector<Entry> entrys1;
414 vector<Entry> entrys2;
415 vector<Entry> entrys3;
416 PreparePutLocalBatch004(entrys1, entrys2, entrys3);
417 /**
418 * @tc.steps: step2. PutBatch entrys2.
419 * @tc.expected: step2. PutBatch return OK.
420 */
421 Value valueRead;
422 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrys2), OK);
423 /**
424 * @tc.steps: step3. Check PutBatch result.
425 * @tc.expected: step3. Get correct value of key1~5. Key6~10 return NOT_FOUND.
426 */
427 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
428 Key keyTemp;
429 keyTemp.push_back(i);
430 if (i < DIVIDE_BATCH_PRESET_SIZE) {
431 Value valueTemp;
432 valueTemp.push_back(i + VALUE_OFFSET);
433 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(keyTemp, valueRead), OK);
434 EXPECT_EQ(valueRead, valueTemp);
435 continue;
436 }
437 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(keyTemp, valueRead), NOT_FOUND);
438 }
439 /**
440 * @tc.steps: step4. PutBatch entrys1.
441 * @tc.expected: step4. PutBatch return OK.
442 */
443 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrys1), OK);
444 /**
445 * @tc.steps: step5. Check PutBatch result.
446 * @tc.expected: step5. Update and insert value of key1~10 to value1~10.
447 */
448 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
449 Key keyTemp;
450 keyTemp.push_back(i);
451 if (i < DIVIDE_BATCH_PRESET_SIZE) {
452 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(keyTemp, valueRead), OK);
453 EXPECT_EQ(valueRead, keyTemp);
454 continue;
455 }
456 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(keyTemp, valueRead), OK);
457 EXPECT_EQ(valueRead, keyTemp);
458 }
459 /**
460 * @tc.steps: step6. PutBatch entrys3.
461 * @tc.expected: step6. PutBatch return OK.
462 */
463 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrys3), OK);
464 /**
465 * @tc.steps: step7. Check PutBatch result of key1~10.
466 * @tc.expected: step7. Update value of key5~10 to value1~5.
467 */
468 for (int i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
469 Key keyTemp;
470 keyTemp.push_back(i);
471 if (i < DIVIDE_BATCH_PRESET_SIZE) {
472 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(keyTemp, valueRead), OK);
473 EXPECT_EQ(valueRead, keyTemp);
474 continue;
475 }
476 Value valueTemp;
477 valueTemp.push_back(i - VALUE_OFFSET);
478 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(keyTemp, valueRead), OK);
479 EXPECT_EQ(valueRead, valueTemp);
480 }
481
482 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
483 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutLocalBatch_004"), OK);
484 g_kvNbDelegatePtr = nullptr;
485 }
486
487 /**
488 * @tc.name: SingleVerDeleteLocalBatch001
489 * @tc.desc: Check for illegal parameters.
490 * @tc.type: FUNC
491 * @tc.require: AR000EPAS8
492 * @tc.author: changguicai
493 */
494 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, SingleVerDeleteLocalBatch001, TestSize.Level1)
495 {
496 const KvStoreNbDelegate::Option option = {true, false};
497 g_mgr.SetKvStoreConfig(g_config);
498 g_mgr.GetKvStore("distributed_SingleVerDeleteLocalBatch_001", option, g_kvNbDelegateCallback);
499 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
500 EXPECT_TRUE(g_kvDelegateStatus == OK);
501 /**
502 * @tc.steps: step1. Create and construct a set of vector <Entry>, containing a total of 10 data keys1 ~ 10,
503 * Value1 ~ 10, and call Putbatch interface to insert data.
504 * @tc.expected: step1. PutBatch successfully.
505 */
506 vector<Entry> entries;
507 vector<Key> keys;
508 vector<Value> values;
509 Value valueRead;
510 CreatEntrys(BATCH_PRESET_SIZE_TEST, keys, values, entries);
511 vector<Entry> entrysBase = entries;
512 vector<Key> keysBase = keys;
513 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrysBase), OK);
514 /**
515 * @tc.steps: step2. Use Get to check data in database.
516 * @tc.expected: step2. Get value1~10 by key1~10 successfully.
517 */
518 for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
519 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(entrysBase[i].key, valueRead), OK);
520 }
521 /**
522 * @tc.steps: step3. Use Get to check data in database.
523 * @tc.expected: step3. Key1~10 still in database.
524 */
525 for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
526 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(entrysBase[i].key, valueRead), OK);
527 }
528 /**
529 * @tc.steps: step4. Use the DeleteBatch interface to pass in 10 included
530 * keys6 ~ 10 + 123 additional key values (128 in total).
531 * @tc.expected: step4. DeleteBatch OK.
532 */
533 CreatEntrys(DBConstant::MAX_BATCH_SIZE + DIVIDE_BATCH_PRESET_SIZE, keys, values, entries);
534 keys.erase(keys.begin(), keys.begin() + DIVIDE_BATCH_PRESET_SIZE);
535 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocalBatch(keys), OK);
536 /**
537 * @tc.steps: step5. Use Get to check key1~10 in database.
538 * @tc.expected: step5. Key1~5 in database, key6~10 have been deleted.
539 */
540 for (size_t i = 0; i < DIVIDE_BATCH_PRESET_SIZE; i++) {
541 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(entrysBase[i].key, valueRead), OK);
542 }
543 for (size_t i = DIVIDE_BATCH_PRESET_SIZE; i < BATCH_PRESET_SIZE_TEST; i++) {
544 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(entrysBase[i].key, valueRead), NOT_FOUND);
545 }
546 /**
547 * @tc.steps: step6. Repeat Putbatch key1~10, value1~10.
548 * @tc.expected: step6. Return OK.
549 */
550 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrysBase), OK);
551
552 Key illegalKey;
553 DistributedDBToolsUnitTest::GetRandomKeyValue(illegalKey, DBConstant::MAX_KEY_SIZE + 1); // 1K + 1
554 keysBase.push_back(illegalKey);
555 /**
556 * @tc.steps: step7. Use DeleteBatch interface to pass in 10 + 1(larger than 1K) keys.
557 * @tc.expected: step7. Return INVALID_ARGS.
558 */
559 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocalBatch(keysBase), INVALID_ARGS);
560 /**
561 * @tc.steps: step8. Use Get to check key1~10 in database.
562 * @tc.expected: step8. Delete those data failed.
563 */
564 for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
565 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(entrysBase[i].key, valueRead), OK);
566 }
567 /**
568 * @tc.steps: step9. Use DeleteBatch interface to pass in 10(in database) + 1 valid keys.
569 * @tc.expected: step9. Delete those data successfully.
570 */
571 keysBase.back().erase(keysBase.back().begin(), keysBase.back().begin() + 1);
572 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocalBatch(keysBase), OK);
573 /**
574 * @tc.steps: step10. Check data.
575 * @tc.expected: step10. DeleteBatch successfully.
576 */
577 for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
578 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(entrysBase[i].key, valueRead), NOT_FOUND);
579 }
580
581 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
582 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerDeleteLocalBatch_001"), OK);
583 g_kvNbDelegatePtr = nullptr;
584 }
585
586 /**
587 * @tc.name: SingleVerDeleteLocalBatch002
588 * @tc.desc: Check normal delete batch ability.
589 * @tc.type: FUNC
590 * @tc.require: AR000EPAS8
591 * @tc.author: changguicai
592 */
593 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, SingleVerDeleteLocalBatch002, TestSize.Level1)
594 {
595 const KvStoreNbDelegate::Option option = {true, false};
596 g_mgr.SetKvStoreConfig(g_config);
597 g_mgr.GetKvStore("distributed_SingleVerDeleteLocalBatch_002", option, g_kvNbDelegateCallback);
598 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
599 EXPECT_TRUE(g_kvDelegateStatus == OK);
600 /**
601 * @tc.steps: step1. Create a group of vector <Entry>, containing a total of 10 data keys1 ~ 10, Value1 ~ 10,
602 * call the Putbatch interface to insert data.
603 * @tc.expected: step1. Insert to database successfully.
604 */
605 vector<Entry> entries;
606 vector<Key> keysBase;
607 vector<Value> values;
608 CreatEntrys(BATCH_PRESET_SIZE_TEST, keysBase, values, entries);
609
610 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entries), OK);
611 /**
612 * @tc.steps: step2. Check data.
613 * @tc.expected: step2. Get key1~10 successfully.
614 */
615 Value valueRead;
616 for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
617 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(keysBase[i], valueRead), OK);
618 }
619 /**
620 * @tc.steps: step3. DeleteBatch key1~5.
621 * @tc.expected: step3. Return OK.
622 */
623 vector<Key> keys(keysBase.begin(), keysBase.begin() + DIVIDE_BATCH_PRESET_SIZE);
624 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocalBatch(keys), OK);
625 /**
626 * @tc.steps: step4. Check key1~10.
627 * @tc.expected: step4. Key1~5 deleted, key6~10 existed.
628 */
629 for (size_t i = 0; i < DIVIDE_BATCH_PRESET_SIZE; i++) {
630 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(keysBase[i], valueRead), NOT_FOUND);
631 }
632 for (size_t i = DIVIDE_BATCH_PRESET_SIZE; i < BATCH_PRESET_SIZE_TEST; i++) {
633 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(keysBase[i], valueRead), OK);
634 }
635 /**
636 * @tc.steps: step5. DeleteBatch key1~10.
637 * @tc.expected: step5. Return OK.
638 */
639 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocalBatch(keysBase), OK);
640 /**
641 * @tc.steps: step6. Check key1~10.
642 * @tc.expected: step6. Key1~10 deleted successfully.
643 */
644 for (size_t i = 0; i < BATCH_PRESET_SIZE_TEST; i++) {
645 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(keysBase[i], valueRead), NOT_FOUND);
646 }
647 /**
648 * @tc.steps: step7. DeleteBatch key1~10 once again.
649 * @tc.expected: step7. Return OK.
650 */
651 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocalBatch(keysBase), OK);
652
653 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
654 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerDeleteLocalBatch_002"), OK);
655 g_kvNbDelegatePtr = nullptr;
656 }
657
658 /**
659 * @tc.name: SingleVerPutLocalBatchObserver001
660 * @tc.desc: Test the observer function of PutLocalBatch() interface.
661 * @tc.type: FUNC
662 * @tc.require: AR000EPAS8
663 * @tc.author: changguicai
664 */
665 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, SingleVerPutLocalBatchObserver001, TestSize.Level1)
666 {
667 /**
668 * @tc.steps:step1. Get the nb delegate.
669 * @tc.expected: step1. Get results OK and non-null delegate.
670 */
671 KvStoreNbDelegate::Option option = {true, false, false};
672 g_mgr.GetKvStore("distributed_SingleVerPutLocalBatchObserver_001", option, g_kvNbDelegateCallback);
673 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
674 EXPECT_TRUE(g_kvDelegateStatus == OK);
675
676 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
677 ASSERT_TRUE(observer != nullptr);
678 /**
679 * @tc.steps:step2. Register the non-null observer for the special key.
680 * @tc.expected: step2. Register results OK.
681 */
682 Key key;
683 EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_LOCAL_ONLY, observer), OK);
684 /**
685 * @tc.steps:step3. Put batch data.
686 * @tc.expected: step3. Returns OK.
687 */
688 vector<Entry> entrysBase;
689 vector<Key> keysBase;
690 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST + 1, entrysBase, keysBase);
691
692 vector<Entry> entries(entrysBase.begin(), entrysBase.end() - 1);
693 EXPECT_EQ(entries.size(), 10UL);
694 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entries), OK);
695 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
696 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entries, observer->GetEntriesInserted()));
697 /**
698 * @tc.steps:step4. Delete the batch data.
699 * @tc.expected: step4. Returns OK.
700 */
701 vector<Key> keys(keysBase.begin() + 5, keysBase.end());
702 EXPECT_EQ(keys.size(), 6UL);
703 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocalBatch(keys), OK);
704 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
705 vector<Entry> entrysDel(entrysBase.begin() + 5, entrysBase.end() - 1);
706 EXPECT_EQ(entrysDel.size(), 5UL);
707 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrysDel, observer->GetEntriesDeleted()));
708 /**
709 * @tc.steps:step5. UnRegister the observer.
710 * @tc.expected: step5. Returns OK.
711 */
712 EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
713 delete observer;
714 observer = nullptr;
715
716 /**
717 * @tc.steps:step6. Close the kv store.
718 * @tc.expected: step6. Results OK and delete successfully.
719 */
720 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
721 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutLocalBatchObserver_001"), OK);
722 g_kvNbDelegatePtr = nullptr;
723 }
724
725 /**
726 * @tc.name: SingleVerPutLocalBatchObserver002
727 * @tc.desc: Test the observer function of PutLocalBatch() for invalid input.
728 * @tc.type: FUNC
729 * @tc.require: AR000EPAS8
730 * @tc.author: changguicai
731 */
732 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, SingleVerPutLocalBatchObserver002, TestSize.Level4)
733 {
734 /**
735 * @tc.steps:step1. Get the nb delegate.
736 * @tc.expected: step1. Get results OK and non-null delegate.
737 */
738 KvStoreNbDelegate::Option option = {true, false, false};
739 g_mgr.GetKvStore("distributed_SingleVerPutLocalBatchObserver_002", option, g_kvNbDelegateCallback);
740 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
741 EXPECT_TRUE(g_kvDelegateStatus == OK);
742
743 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
744 ASSERT_TRUE(observer != nullptr);
745 /**
746 * @tc.steps:step2. Register the non-null observer for the special key.
747 * @tc.expected: step2. Register results OK.
748 */
749 Key key;
750 EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_LOCAL_ONLY, observer), OK);
751 /**
752 * @tc.steps:step3. Put invalid batch data.
753 * @tc.expected: step3. Returns INVALID_ARGS.
754 */
755 vector<Entry> entrys2;
756 vector<Key> keys2;
757 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys2, keys2);
758 EXPECT_EQ(entrys2.size(), 10UL);
759
760 vector<Entry> entrysInvalid;
761 vector<Key> keysInvalid;
762 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrysInvalid, keysInvalid,
763 DBConstant::MAX_KEY_SIZE + 10);
764 EXPECT_EQ(entrysInvalid.size(), 10UL);
765 entrys2[0].key = entrysInvalid[0].key;
766
767 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrys2), INVALID_ARGS);
768 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
769 EXPECT_TRUE(observer->GetEntriesInserted().empty());
770 /**
771 * @tc.steps:step4. Put MAX valid value batch data.
772 * @tc.expected: step4. Returns OK.
773 */
774 vector<Entry> entrys3;
775 vector<Key> keys3;
776
777 DistributedDBUnitTest::GenerateRecords(DBConstant::MAX_BATCH_SIZE, entrys3, keys3);
778 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrys3), OK);
779 LOGD("sleep begin");
780 // sleep 20 seconds
781 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME * 10));
782 LOGD("sleep end");
783 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrys3, observer->GetEntriesInserted()));
784 /**
785 * @tc.steps:step5. UnRegister the observer.
786 * @tc.expected: step5. Returns OK.
787 */
788 EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
789 delete observer;
790 observer = nullptr;
791
792 /**
793 * @tc.steps:step6. Close the kv store.
794 * @tc.expected: step6. Results OK and delete successfully.
795 */
796 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
797 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutLocalBatchObserver_002"), OK);
798 g_kvNbDelegatePtr = nullptr;
799 }
800
801 /**
802 * @tc.name: SingleVerPutLocalBatchObserver003
803 * @tc.desc: Test the observer function of PutLocalBatch() update function.
804 * @tc.type: FUNC
805 * @tc.require: AR000EPAS8
806 * @tc.author: changguicai
807 */
808 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, SingleVerPutLocalBatchObserver003, TestSize.Level1)
809 {
810 /**
811 * @tc.steps:step1. Get the nb delegate.
812 * @tc.expected: step1. Get results OK and non-null delegate.
813 */
814 KvStoreNbDelegate::Option option = {true, false, false};
815 g_mgr.GetKvStore("distributed_SingleVerPutLocalBatchObserver_003", option, g_kvNbDelegateCallback);
816 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
817 EXPECT_TRUE(g_kvDelegateStatus == OK);
818
819 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
820 ASSERT_TRUE(observer != nullptr);
821 /**
822 * @tc.steps:step2. Register the non-null observer for the special key.
823 * @tc.expected: step2. Register results OK.
824 */
825 Key key;
826 EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_LOCAL_ONLY, observer), OK);
827 /**
828 * @tc.steps:step3. Put batch data.
829 * @tc.expected: step3. Returns OK.
830 */
831 vector<Entry> entrysAdd;
832 vector<Key> keysAdd;
833 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrysAdd, keysAdd);
834
835 EXPECT_EQ(entrysAdd.size(), 10UL);
836 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrysAdd), OK);
837 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
838 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrysAdd, observer->GetEntriesInserted()));
839 /**
840 * @tc.steps:step4. Update the batch data.
841 * @tc.expected: step4. Returns OK.
842 */
843 vector<Entry> entrysUpdate;
844 vector<Key> keysUpdate;
845 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrysUpdate, keysUpdate, DEFAULT_KEY_VALUE_SIZE,
846 DEFAULT_KEY_VALUE_SIZE + 10);
847
848 EXPECT_EQ(entrysUpdate.size(), 10UL);
849 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrysUpdate), OK);
850 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
851 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrysUpdate, observer->GetEntriesUpdated()));
852 /**
853 * @tc.steps:step5. UnRegister the observer.
854 * @tc.expected: step5. Returns OK.
855 */
856 EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
857 delete observer;
858 observer = nullptr;
859
860 /**
861 * @tc.steps:step6. Close the kv store.
862 * @tc.expected: step6. Results OK and delete successfully.
863 */
864 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
865 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutLocalBatchObserver_003"), OK);
866 g_kvNbDelegatePtr = nullptr;
867 }
868
869 /**
870 * @tc.name: SingleVerPutLocalBatchObserver004
871 * @tc.desc: Test the observer function of PutLocalBatch(), same keys handle.
872 * @tc.type: FUNC
873 * @tc.require: AR000EPAS8
874 * @tc.author: changguicai
875 */
876 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, SingleVerPutLocalBatchObserver004, TestSize.Level1)
877 {
878 /**
879 * @tc.steps:step1. Get the nb delegate.
880 * @tc.expected: step1. Get results OK and non-null delegate.
881 */
882 KvStoreNbDelegate::Option option = {true, false, false};
883 g_mgr.GetKvStore("distributed_SingleVerPutLocalBatchObserver_004", option, g_kvNbDelegateCallback);
884 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
885 EXPECT_TRUE(g_kvDelegateStatus == OK);
886
887 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
888 ASSERT_TRUE(observer != nullptr);
889 /**
890 * @tc.steps:step2. Register the non-null observer for the special key.
891 * @tc.expected: step2. Register results OK.
892 */
893 Key key;
894 EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_LOCAL_ONLY, observer), OK);
895 /**
896 * @tc.steps:step3. Put batch data.
897 * @tc.expected: step3. Returns OK.
898 */
899 vector<Entry> entrys1;
900 vector<Key> keys1;
901 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys1, keys1);
902 vector<Entry> entrys2;
903 vector<Key> keys2;
904 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys2, keys2, DEFAULT_KEY_VALUE_SIZE,
905 DEFAULT_KEY_VALUE_SIZE + 10);
906 entrys1.insert(entrys1.end(), entrys2.begin(), entrys2.end());
907
908 EXPECT_EQ(entrys1.size(), 20UL);
909 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrys1), OK);
910 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
911 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrys2, observer->GetEntriesInserted()));
912 EXPECT_EQ(observer->GetEntriesUpdated().size(), 0UL);
913
914 vector<Entry> entrys3;
915 vector<Key> keys3;
916 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys3, keys3, DEFAULT_KEY_VALUE_SIZE,
917 DEFAULT_KEY_VALUE_SIZE + 20);
918 vector<Entry> entrys4;
919 vector<Key> keys4;
920 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entrys4, keys4, DEFAULT_KEY_VALUE_SIZE,
921 DEFAULT_KEY_VALUE_SIZE + 30);
922 entrys3.insert(entrys3.end(), entrys4.begin(), entrys4.end());
923 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entrys3), OK);
924 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
925 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entrys4, observer->GetEntriesUpdated()));
926 EXPECT_EQ(observer->GetEntriesInserted().size(), 0UL);
927
928 /**
929 * @tc.steps:step4. UnRegister the observer.
930 * @tc.expected: step4. Returns OK.
931 */
932 EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
933 delete observer;
934 observer = nullptr;
935
936 /**
937 * @tc.steps:step5. Close the kv store.
938 * @tc.expected: step5. Results OK and delete successfully.
939 */
940 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
941 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerPutLocalBatchObserver_004"), OK);
942 g_kvNbDelegatePtr = nullptr;
943 }
944
945 /**
946 * @tc.name: SingleVerDeleteLocalBatchObserver001
947 * @tc.desc: Test the observer function of DeleteLocalBatch() interface.
948 * @tc.type: FUNC
949 * @tc.require: AR000EPAS8
950 * @tc.author: changguicai
951 */
952 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, SingleVerDeleteLocalBatchObserver001, TestSize.Level1)
953 {
954 /**
955 * @tc.steps:step1. Get the nb delegate.
956 * @tc.expected: step1. Get results OK and non-null delegate.
957 */
958 KvStoreNbDelegate::Option option = {true, false, false};
959 g_mgr.GetKvStore("distributed_SingleVerDeleteLocalBatchObserver_001", option, g_kvNbDelegateCallback);
960 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
961 EXPECT_TRUE(g_kvDelegateStatus == OK);
962
963 KvStoreObserverUnitTest *observer = new (std::nothrow) KvStoreObserverUnitTest;
964 ASSERT_TRUE(observer != nullptr);
965 /**
966 * @tc.steps:step2. Register the non-null observer for the special key.
967 * @tc.expected: step2. Register results OK.
968 */
969 Key key;
970 EXPECT_EQ(g_kvNbDelegatePtr->RegisterObserver(key, OBSERVER_CHANGES_LOCAL_ONLY, observer), OK);
971 /**
972 * @tc.steps:step3. Put batch data.
973 * @tc.expected: step3. Returns OK.
974 */
975 vector<Entry> entries;
976 vector<Key> keys;
977 DistributedDBUnitTest::GenerateRecords(BATCH_PRESET_SIZE_TEST, entries, keys);
978 EXPECT_EQ(entries.size(), 10UL);
979
980 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entries), OK);
981 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
982 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entries, observer->GetEntriesInserted()));
983 /**
984 * @tc.steps:step4. Delete the batch data.
985 * @tc.expected: step4. Returns OK.
986 */
987 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocalBatch(keys), OK);
988 std::this_thread::sleep_for(std::chrono::milliseconds(OBSERVER_SLEEP_TIME));
989 EXPECT_TRUE(DistributedDBToolsUnitTest::CheckObserverResult(entries, observer->GetEntriesDeleted()));
990 /**
991 * @tc.steps:step5. UnRegister the observer.
992 * @tc.expected: step5. Returns OK.
993 */
994 EXPECT_EQ(g_kvNbDelegatePtr->UnRegisterObserver(observer), OK);
995 delete observer;
996 observer = nullptr;
997
998 /**
999 * @tc.steps:step6. Close the kv store.
1000 * @tc.expected: step6. Results OK and delete successfully.
1001 */
1002 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1003 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_SingleVerDeleteLocalBatchObserver_001"), OK);
1004 g_kvNbDelegatePtr = nullptr;
1005 }
1006 #ifndef OMIT_JSON
1007 /**
1008 * @tc.name: LocalDataBatchNotCheckSchema001
1009 * @tc.desc: Local data does not check schema.
1010 * @tc.type: FUNC
1011 * @tc.require: AR000EPAS8
1012 * @tc.author: changguicai
1013 */
1014 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, LocalDataBatchNotCheckSchema001, TestSize.Level1)
1015 {
1016 g_mgr.GetKvStore("distributed_LocalDataBatchNotCheckSchema_001", g_strictOpt, g_kvNbDelegateCallback);
1017 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1018 EXPECT_TRUE(g_kvDelegateStatus == OK);
1019
1020 /**
1021 * @tc.steps:step1. Put one data whose value has more fields than the schema.
1022 * @tc.expected: step1. Return OK, because PutLocal does not verify the validity of the schema.
1023 */
1024 Key key;
1025 DistributedDBToolsUnitTest::GetRandomKeyValue(key);
1026 std::string moreData = "{\"field_name1\":true,\"field_name2\":10,\"field_name3\":10}";
1027 Value value(moreData.begin(), moreData.end());
1028 EXPECT_EQ(g_kvNbDelegatePtr->PutLocal(key, value), OK);
1029 Value getValue;
1030 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(key, getValue), OK);
1031 EXPECT_TRUE(DistributedDBToolsUnitTest::IsValueEqual(getValue, value));
1032
1033 /**
1034 * @tc.steps:step2. Delete local data
1035 * @tc.expected: step2. DeleteLocal return OK, GetLocal return NOT_FOUND
1036 */
1037 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocal(key), OK);
1038 getValue.clear();
1039 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(key, getValue), NOT_FOUND);
1040
1041 /**
1042 * @tc.steps:step3. PutLocalBatch local data whose value is mismatch with the schema.
1043 * @tc.expected: step3. return OK.
1044 */
1045 key.clear();
1046 DistributedDBToolsUnitTest::GetRandomKeyValue(key);
1047 std::string invalidData = "{\"field_name1\":true, \"field_name2\":null}";
1048 value.assign(invalidData.begin(), invalidData.end());
1049 std::vector<Key> keys;
1050 std::vector<Entry> entries;
1051 entries.push_back({key, value});
1052 keys.push_back(key);
1053
1054 DistributedDBToolsUnitTest::GetRandomKeyValue(key);
1055 std::string validData = "{\"field_name1\":true, \"field_name2\":0}";
1056 value.assign(validData.begin(), validData.end());
1057 entries.push_back({key, value});
1058 keys.push_back(key);
1059
1060 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entries), OK);
1061 std::vector<Entry> getEntries;
1062 Key keyPrefix;
1063 EXPECT_EQ(g_kvNbDelegatePtr->GetLocalEntries(keyPrefix, getEntries), OK);
1064 EXPECT_TRUE(DistributedDBToolsUnitTest::IsEntriesEqual(entries, getEntries, true));
1065
1066 /**
1067 * @tc.steps:step4. Delete local data
1068 * @tc.expected: step4. DeleteLocal return OK, GetLocal return NOT_FOUND
1069 */
1070 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocalBatch(keys), OK);
1071 getEntries.clear();
1072 EXPECT_EQ(g_kvNbDelegatePtr->GetLocalEntries(keyPrefix, getEntries), NOT_FOUND);
1073 EXPECT_TRUE(getEntries.empty());
1074
1075 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1076 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_LocalDataBatchNotCheckSchema_001"), OK);
1077 g_kvNbDelegatePtr = nullptr;
1078 }
1079
1080 /**
1081 * @tc.name: LocalDataBatchNotCheckReadOnly001
1082 * @tc.desc: Local data does not check readOnly.
1083 * @tc.type: FUNC
1084 * @tc.require: AR000EPAS8
1085 * @tc.author: changguicai
1086 */
1087 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, LocalDataBatchNotCheckReadOnly001, TestSize.Level1)
1088 {
1089 /**
1090 * @tc.steps:step1. Open the kv store with valid schema, and close it.
1091 * @tc.expected: step1. opened & closeed successfully - return OK.
1092 */
1093 g_mgr.GetKvStore("distributed_LocalDataBatchNotCheckReadOnly_001", g_strictOpt, g_kvNbDelegateCallback);
1094 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1095 EXPECT_TRUE(g_kvDelegateStatus == OK);
1096 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1097
1098 /**
1099 * @tc.steps:step2. Open the kv store with no schema.
1100 * @tc.expected: step2. return OK.
1101 */
1102 DistributedDB::KvStoreNbDelegate::Option option = g_strictOpt;
1103 option.schema.clear();
1104 g_mgr.GetKvStore("distributed_LocalDataBatchNotCheckReadOnly_001", option, g_kvNbDelegateCallback);
1105 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1106 EXPECT_TRUE(g_kvDelegateStatus == OK);
1107
1108 /**
1109 * @tc.steps:step3. CRUD single local the data.
1110 * @tc.expected: step3. return OK.
1111 */
1112 Key key;
1113 DistributedDBToolsUnitTest::GetRandomKeyValue(key);
1114 std::string valueData = "{\"field_name1\":true,\"field_name2\":20}";
1115 Value value(valueData.begin(), valueData.end());
1116 EXPECT_EQ(g_kvNbDelegatePtr->PutLocal(key, value), OK);
1117
1118 Value getValue;
1119 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(key, getValue), OK);
1120 EXPECT_TRUE(DistributedDBToolsUnitTest::IsValueEqual(getValue, value));
1121
1122 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocal(key), OK);
1123 getValue.clear();
1124 EXPECT_EQ(g_kvNbDelegatePtr->GetLocal(key, getValue), NOT_FOUND);
1125
1126 /**
1127 * @tc.steps:step3. CRUD batch local the data.
1128 * @tc.expected: step3. return OK.
1129 */
1130 key.clear();
1131 DistributedDBToolsUnitTest::GetRandomKeyValue(key);
1132 std::string invalidData = "{\"field_name1\":true, \"field_name2\":null}";
1133 value.assign(invalidData.begin(), invalidData.end());
1134 std::vector<Key> keys;
1135 std::vector<Entry> entries;
1136 entries.push_back({key, value});
1137 keys.push_back(key);
1138
1139 DistributedDBToolsUnitTest::GetRandomKeyValue(key);
1140 std::string validData = "{\"field_name1\":true, \"field_name2\":0}";
1141 value.assign(validData.begin(), validData.end());
1142 entries.push_back({key, value});
1143 keys.push_back(key);
1144
1145 EXPECT_EQ(g_kvNbDelegatePtr->PutLocalBatch(entries), OK);
1146 std::vector<Entry> getEntries;
1147 Key keyPrefix;
1148 EXPECT_EQ(g_kvNbDelegatePtr->GetLocalEntries(keyPrefix, getEntries), OK);
1149 EXPECT_TRUE(DistributedDBToolsUnitTest::IsEntriesEqual(entries, getEntries, true));
1150
1151 EXPECT_EQ(g_kvNbDelegatePtr->DeleteLocalBatch(keys), OK);
1152 getEntries.clear();
1153 EXPECT_EQ(g_kvNbDelegatePtr->GetLocalEntries(keyPrefix, getEntries), NOT_FOUND);
1154 EXPECT_TRUE(getEntries.empty());
1155
1156 /**
1157 * @tc.steps:step4. Close the kv store.
1158 * @tc.expected: step4. Results OK and delete successfully.
1159 */
1160 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1161 EXPECT_EQ(g_mgr.DeleteKvStore("distributed_LocalDataBatchNotCheckReadOnly_001"), OK);
1162 g_kvNbDelegatePtr = nullptr;
1163 }
1164 #endif
1165
1166 /**
1167 * @tc.name: RemoveDeviceDataEx001
1168 * @tc.desc: Test the remove device data without communicator.
1169 * @tc.type: FUNC
1170 * @tc.require:
1171 * @tc.author: zhangqiquan
1172 */
1173 HWTEST_F(DistributedDBInterfacesNBDelegateLocalBatchTest, RemoveDeviceDataEx001, TestSize.Level0)
1174 {
1175 const KvStoreNbDelegate::Option option = {true, true};
1176 g_mgr.SetKvStoreConfig(g_config);
1177 g_mgr.GetKvStore("RemoveDeviceDataEx001", option, g_kvNbDelegateCallback);
1178 ASSERT_TRUE(g_kvNbDelegatePtr != nullptr);
1179 EXPECT_TRUE(g_kvDelegateStatus == OK);
1180
1181 EXPECT_EQ(g_kvNbDelegatePtr->RemoveDeviceData("device"), OK);
1182 EXPECT_EQ(g_kvNbDelegatePtr->RemoveDeviceData(), OK);
1183
1184 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
1185 g_kvNbDelegatePtr = nullptr;
1186 }
1187