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