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 #ifndef OMIT_MULTI_VER
16 #include "distributeddb_interfaces_transaction_testcase.h"
17 
18 using namespace testing::ext;
19 using namespace DistributedDB;
20 using namespace DistributedDBUnitTest;
21 using namespace std;
22 
StartTransaction001(KvStoreDelegate * & kvDelegatePtr)23 void DistributedDBInterfacesTransactionTestCase::StartTransaction001(KvStoreDelegate *&kvDelegatePtr)
24 {
25     /**
26      * @tc.steps:step1. call StartTransaction interface the 1st time.
27      * @tc.expected: step1. call succeed.
28      */
29     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
30     /**
31      * @tc.steps:step2. call StartTransaction interface the 2nd time.
32      * @tc.expected: step2. call failed and return ERROR.
33      */
34     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == DB_ERROR);
35     EXPECT_EQ(kvDelegatePtr->Commit(), OK);
36 }
37 
StartTransaction002(KvStoreDelegate * & kvDelegatePtr)38 void DistributedDBInterfacesTransactionTestCase::StartTransaction002(KvStoreDelegate *&kvDelegatePtr)
39 {
40     /**
41      * @tc.steps:step1. call StartTransaction interface.
42      * @tc.expected: step1. call succeed.
43      */
44     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
45     /**
46      * @tc.steps:step2. call commit interface.
47      * @tc.expected: step2. call succeed.
48      */
49     EXPECT_TRUE(kvDelegatePtr->Commit() == OK);
50 }
51 
StartTransaction003(KvStoreDelegate * & kvDelegatePtr)52 void DistributedDBInterfacesTransactionTestCase::StartTransaction003(KvStoreDelegate *&kvDelegatePtr)
53 {
54     /**
55      * @tc.steps:step1. call StartTransaction interface.
56      * @tc.expected: step1. call succeed.
57      */
58     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
59     /**
60      * @tc.steps:step2. call rollback interface.
61      * @tc.expected: step2. call succeed.
62      */
63     EXPECT_TRUE(kvDelegatePtr->Rollback() == OK);
64 }
65 
GetSnapshotUnitTest(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)66 static void GetSnapshotUnitTest(KvStoreDelegate *&kvDelegatePtr, KvStoreSnapshotDelegate *&snapshotDelegatePtr)
67 {
68     DBStatus snapshotDelegateStatus = INVALID_ARGS;
69     auto snapshotDelegateCallback = bind(&DistributedDBToolsUnitTest::SnapshotDelegateCallback,
70         placeholders::_1, placeholders::_2, std::ref(snapshotDelegateStatus), std::ref(snapshotDelegatePtr));
71 
72     kvDelegatePtr->GetKvStoreSnapshot(nullptr, snapshotDelegateCallback);
73     EXPECT_TRUE(snapshotDelegateStatus == OK);
74     ASSERT_TRUE(snapshotDelegatePtr != nullptr);
75 }
76 
StartTransaction004(KvStoreDelegate * & kvDelegatePtr,const string & storeId,bool localOnly,KvStoreDelegateManager & mgr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)77 void DistributedDBInterfacesTransactionTestCase::StartTransaction004(KvStoreDelegate *&kvDelegatePtr,
78     const string &storeId, bool localOnly, KvStoreDelegateManager &mgr, KvStoreSnapshotDelegate *&snapshotDelegatePtr)
79 {
80     DBStatus kvDelegateStatus = INVALID_ARGS;
81     auto kvDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreDelegateCallback,
82         placeholders::_1, placeholders::_2, std::ref(kvDelegateStatus), std::ref(kvDelegatePtr));
83 
84     DBStatus valueStatus = INVALID_ARGS;
85     Value value;
86     auto valueCallback = bind(&DistributedDBToolsUnitTest::ValueCallback,
87         placeholders::_1, placeholders::_2, std::ref(valueStatus), std::ref(value));
88 
89     /**
90      * @tc.steps:step1. call StartTransaction interface.
91      * @tc.expected: step1. call succeed.
92      */
93     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
94     /**
95      * @tc.steps:step2. put (k1, v1) to data base.
96      * @tc.expected: step2. put succeed.
97      */
98     EXPECT_TRUE(kvDelegatePtr->Put(KEY_1, VALUE_1) == OK);
99     /**
100      * @tc.steps:step3. close data base.
101      * @tc.expected: step3. close succeed.
102      */
103     EXPECT_EQ(mgr.CloseKvStore(kvDelegatePtr), OK);
104     kvDelegatePtr = nullptr;
105 
106     /**
107      * @tc.steps:step4. use GetKvStore interface to open db.
108      * @tc.expected: step4. open succeed.
109      */
110     KvStoreDelegate::Option option = {true, localOnly};
111     mgr.GetKvStore(storeId, option, kvDelegateCallback);
112     EXPECT_EQ(kvDelegateStatus, OK);
113     ASSERT_TRUE(kvDelegatePtr != nullptr);
114 
115     /**
116      * @tc.steps:step5. use snapshot interface to check the value of k1.
117      * @tc.expected: step5. can't get the record of k1.
118      */
119     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
120     snapshotDelegatePtr->Get(KEY_1, valueCallback);
121     EXPECT_TRUE(valueStatus == NOT_FOUND);
122     EXPECT_TRUE(value.size() == 0);
123 }
124 
StartTransaction005(KvStoreDelegate * & kvDelegatePtr,const string & storeId,bool localOnly,KvStoreDelegateManager & mgr)125 void DistributedDBInterfacesTransactionTestCase::StartTransaction005(KvStoreDelegate *&kvDelegatePtr,
126     const string &storeId, bool localOnly, KvStoreDelegateManager &mgr)
127 {
128     DBStatus kvDelegateStatus = INVALID_ARGS;
129     auto kvDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreDelegateCallback,
130         placeholders::_1, placeholders::_2, std::ref(kvDelegateStatus), std::ref(kvDelegatePtr));
131 
132     /**
133      * @tc.steps:step1. call StartTransaction interface the 1st time.
134      * @tc.expected: step1. call succeed.
135      */
136     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
137     KvStoreDelegate *temp = kvDelegatePtr;
138     temp->Put(KEY_1, VALUE_1);
139 
140     KvStoreDelegate::Option option = {true, localOnly};
141     mgr.GetKvStore(storeId, option, kvDelegateCallback);
142     EXPECT_TRUE(kvDelegateStatus == OK);
143     ASSERT_TRUE(kvDelegatePtr != nullptr);
144     /**
145      * @tc.steps:step2. call StartTransaction interface the 2nd time using another .
146      * @tc.expected: step2. call failed.
147      */
148     EXPECT_NE(kvDelegatePtr->StartTransaction(), OK);
149 
150     kvDelegatePtr->Put(KEY_2, VALUE_2);
151     /**
152      * @tc.steps:step4. call commit interface the 1st time.
153      * @tc.expected: step4. call failed.
154      */
155     EXPECT_EQ(temp->Commit(), OK);
156     EXPECT_EQ(mgr.CloseKvStore(temp), OK);
157     temp = nullptr;
158     /**
159      * @tc.steps:step5. call commit interface the 2nd time.
160      * @tc.expected: step5. call failed.
161      */
162     EXPECT_NE(kvDelegatePtr->Commit(), OK);
163 }
164 
Commit001(KvStoreDelegate * & kvDelegatePtr)165 void DistributedDBInterfacesTransactionTestCase::Commit001(KvStoreDelegate *&kvDelegatePtr)
166 {
167     /**
168      * @tc.steps:step1. commit Transaction without start it.
169      * @tc.expected: step1. commit failed and returned ERROR.
170      */
171     EXPECT_TRUE(kvDelegatePtr->Commit() == DB_ERROR);
172 }
173 
Commit002(KvStoreDelegate * & kvDelegatePtr)174 void DistributedDBInterfacesTransactionTestCase::Commit002(KvStoreDelegate *&kvDelegatePtr)
175 {
176     /**
177      * @tc.steps:step1. call StartTransaction interface.
178      * @tc.expected: step1. call succeed.
179      */
180     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
181     /**
182      * @tc.steps:step2. call commit interface the 1st time.
183      * @tc.expected: step2. call succeed.
184      */
185     EXPECT_TRUE(kvDelegatePtr->Commit() == OK);
186     /**
187      * @tc.steps:step3. call commit interface the 2nd time.
188      * @tc.expected: step3. call failed and returned ERROR.
189      */
190     EXPECT_TRUE(kvDelegatePtr->Commit() == DB_ERROR);
191 }
192 
Commit003(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)193 void DistributedDBInterfacesTransactionTestCase::Commit003(KvStoreDelegate *&kvDelegatePtr,
194     KvStoreSnapshotDelegate *&snapshotDelegatePtr)
195 {
196     DBStatus valueStatus = INVALID_ARGS;
197     Value value;
198     auto valueCallback = bind(&DistributedDBToolsUnitTest::ValueCallback,
199         placeholders::_1, placeholders::_2, std::ref(valueStatus), std::ref(value));
200 
201     /**
202      * @tc.steps:step1. call StartTransaction interface.
203      * @tc.expected: step1. call succeed.
204      */
205     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
206     /**
207      * @tc.steps:step2. put (k1, v1) to db.
208      * @tc.expected: step2. put succeed.
209      */
210     EXPECT_TRUE(kvDelegatePtr->Put(KEY_1, VALUE_1) == OK);
211     /**
212      * @tc.steps:step3. call commit interface.
213      * @tc.expected: step3. call succeed.
214      */
215     EXPECT_TRUE(kvDelegatePtr->Commit() == OK);
216 
217     /**
218      * @tc.steps:step4. use snapshot interface to check if (k1, v1) is put succeed.
219      * @tc.expected: step4. can find (k1, v1) from db.
220      */
221     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
222     snapshotDelegatePtr->Get(KEY_1, valueCallback);
223     EXPECT_TRUE(valueStatus == OK);
224     ASSERT_TRUE(value.size() > 0);
225     EXPECT_TRUE(value.front() == VALUE_1.front());
226 }
227 
Commit004(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)228 void DistributedDBInterfacesTransactionTestCase::Commit004(KvStoreDelegate *&kvDelegatePtr,
229     KvStoreSnapshotDelegate *&snapshotDelegatePtr)
230 {
231     DBStatus valueStatus = INVALID_ARGS;
232     Value value;
233     auto valueCallback = bind(&DistributedDBToolsUnitTest::ValueCallback,
234         placeholders::_1, placeholders::_2, std::ref(valueStatus), std::ref(value));
235     /**
236      * @tc.steps:step1. put one data.
237      * @tc.expected: step1. call succeed.
238      */
239     EXPECT_TRUE(kvDelegatePtr->Put(KEY_1, VALUE_1) == OK);
240     /**
241      * @tc.steps:step2. call StartTransaction interface.
242      * @tc.expected: step2. call succeed.
243      */
244     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
245     /**
246      * @tc.steps:step3. update the data to another value.
247      * @tc.expected: step3. call succeed.
248      */
249     EXPECT_TRUE(kvDelegatePtr->Put(KEY_1, VALUE_2) == OK);
250     /**
251      * @tc.steps:step4. call commit interface.
252      * @tc.expected: step4. call succeed.
253      */
254     EXPECT_TRUE(kvDelegatePtr->Commit() == OK);
255 
256     /**
257      * @tc.steps:step5. use snapshot interface to check the updated data.
258      * @tc.expected: step5. the value is updated.
259      */
260     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
261     snapshotDelegatePtr->Get(KEY_1, valueCallback);
262     EXPECT_TRUE(valueStatus == OK);
263     ASSERT_TRUE(value.size() > 0);
264     EXPECT_TRUE(value.front() == VALUE_2.front());
265 }
266 
Commit005(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)267 void DistributedDBInterfacesTransactionTestCase::Commit005(KvStoreDelegate *&kvDelegatePtr,
268     KvStoreSnapshotDelegate *&snapshotDelegatePtr)
269 {
270     DBStatus valueStatus = INVALID_ARGS;
271     Value value;
272     auto valueCallback = bind(&DistributedDBToolsUnitTest::ValueCallback,
273         placeholders::_1, placeholders::_2, std::ref(valueStatus), std::ref(value));
274 
275     EXPECT_TRUE(kvDelegatePtr->Put(KEY_1, VALUE_1) == OK);
276     /**
277      * @tc.steps:step1. call StartTransaction interface.
278      * @tc.expected: step1. call succeed.
279      */
280     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
281     /**
282      * @tc.steps:step2. delete record from db where key = k1.
283      * @tc.expected: step2. delete succeed.
284      */
285     EXPECT_TRUE(kvDelegatePtr->Delete(KEY_1) == OK);
286     /**
287      * @tc.steps:step3. call commit interface.
288      * @tc.expected: step3. commit succeed.
289      */
290     EXPECT_TRUE(kvDelegatePtr->Commit() == OK);
291 
292     /**
293      * @tc.steps:step4. use snapshot interface to check if (k1, v1) is delete succeed.
294      * @tc.expected: step4. can't find (k1, v1) in the db.
295      */
296     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
297     snapshotDelegatePtr->Get(KEY_1, valueCallback);
298     EXPECT_TRUE(valueStatus == NOT_FOUND);
299 }
300 
Commit006(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)301 void DistributedDBInterfacesTransactionTestCase::Commit006(KvStoreDelegate *&kvDelegatePtr,
302     KvStoreSnapshotDelegate *&snapshotDelegatePtr)
303 {
304     DBStatus entryVectorStatus = INVALID_ARGS;
305     unsigned long matchSize = 0;
306     std::vector<Entry> entriesVector;
307     auto entryVectorCallback = bind(&DistributedDBToolsUnitTest::EntryVectorCallback, placeholders::_1,
308         placeholders::_2, std::ref(entryVectorStatus), std::ref(matchSize), std::ref(entriesVector));
309 
310     EXPECT_TRUE(kvDelegatePtr->PutBatch(ENTRY_VECTOR) == OK);
311     /**
312      * @tc.steps:step1. call StartTransaction interface.
313      * @tc.expected: step1. call succeed.
314      */
315     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
316     /**
317      * @tc.steps:step2. clear all the records from db.
318      * @tc.expected: step2. clear succeed.
319      */
320     EXPECT_TRUE(kvDelegatePtr->Clear() == OK);
321     /**
322      * @tc.steps:step3. call commit interface.
323      * @tc.expected: step3. commit succeed.
324      */
325     EXPECT_TRUE(kvDelegatePtr->Commit() == OK);
326 
327     /**
328      * @tc.steps:step4. use snapshot interface to check if there are any data in db.
329      * @tc.expected: step4. can't find any data in db.
330      */
331     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
332     snapshotDelegatePtr->GetEntries(NULL_KEY_1, entryVectorCallback);
333     EXPECT_TRUE(entryVectorStatus == NOT_FOUND);
334 }
335 
Commit007(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)336 void DistributedDBInterfacesTransactionTestCase::Commit007(KvStoreDelegate *&kvDelegatePtr,
337     KvStoreSnapshotDelegate *&snapshotDelegatePtr)
338 {
339     DBStatus valueStatus = INVALID_ARGS;
340     Value value;
341     auto valueCallback = bind(&DistributedDBToolsUnitTest::ValueCallback,
342         placeholders::_1, placeholders::_2, std::ref(valueStatus), std::ref(value));
343 
344     EXPECT_TRUE(kvDelegatePtr->PutBatch(ENTRY_VECTOR) == OK);
345     /**
346      * @tc.steps:step1. call StartTransaction interface.
347      * @tc.expected: step1. call succeed.
348      */
349     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
350     /**
351      * @tc.steps:step2. delete record from db where key = k1.
352      * @tc.expected: step2. delete succeed.
353      */
354     EXPECT_TRUE(kvDelegatePtr->Delete(KEY_1) == OK);
355     /**
356      * @tc.steps:step3. put (k2, v1) to db.
357      * @tc.expected: step3. put succeed.
358      */
359     EXPECT_TRUE(kvDelegatePtr->Put(KEY_2, VALUE_1) == OK);
360     /**
361      * @tc.steps:step4. call commit interface.
362      * @tc.expected: step4. commit succeed.
363      */
364     EXPECT_TRUE(kvDelegatePtr->Commit() == OK);
365 
366     /**
367      * @tc.steps:step5. use snapshot interface to check the data in db.
368      * @tc.expected: step5. can't find (k1, v1) but can find (k2, v1) in db.
369      */
370     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
371     snapshotDelegatePtr->Get(KEY_1, valueCallback);
372     EXPECT_TRUE(valueStatus == NOT_FOUND);
373     snapshotDelegatePtr->Get(KEY_2, valueCallback);
374     EXPECT_TRUE(valueStatus == OK);
375     ASSERT_TRUE(value.size() > 0);
376     EXPECT_TRUE(value.front() == VALUE_1.front());
377 }
378 
Commit008(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)379 void DistributedDBInterfacesTransactionTestCase::Commit008(KvStoreDelegate *&kvDelegatePtr,
380     KvStoreSnapshotDelegate *&snapshotDelegatePtr)
381 {
382     DBStatus entryVectorStatus = INVALID_ARGS;
383     unsigned long matchSizeCallback = 0;
384     std::vector<Entry> entriesVector;
385     auto entryVectorCallback = bind(&DistributedDBToolsUnitTest::EntryVectorCallback, placeholders::_1,
386         placeholders::_2, std::ref(entryVectorStatus), std::ref(matchSizeCallback), std::ref(entriesVector));
387 
388     EXPECT_TRUE(kvDelegatePtr->PutBatch(ENTRY_VECTOR) == OK);
389     /**
390      * @tc.steps:step1. call StartTransaction interface.
391      * @tc.expected: step1. call succeed.
392      */
393     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
394     /**
395      * @tc.steps:step2. clear all the records from db.
396      * @tc.expected: step2. clear succeed.
397      */
398     EXPECT_TRUE(kvDelegatePtr->Clear() == OK);
399     /**
400      * @tc.steps:step3. put (k3, v3) to db.
401      * @tc.expected: step3. put succeed.
402      */
403     Entry entry;
404     GenerateEntry(1, 3, entry);
405     EXPECT_TRUE(kvDelegatePtr->Put(entry.key, entry.value) == OK);
406     /**
407      * @tc.steps:step4. call commit interface.
408      * @tc.expected: step4. commit succeed.
409      */
410     EXPECT_TRUE(kvDelegatePtr->Commit() == OK);
411 
412     /**
413      * @tc.steps:step5. use snapshot interface to check the data in db.
414      * @tc.expected: step5. can only find (k3, v3) in db.
415      */
416     unsigned long matchSize = 1;
417     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
418     snapshotDelegatePtr->GetEntries(NULL_KEY_1, entryVectorCallback);
419     EXPECT_TRUE(entryVectorStatus == OK);
420     ASSERT_TRUE(matchSizeCallback == matchSize);
421 }
422 
RollBack001(KvStoreDelegate * & kvDelegatePtr)423 void DistributedDBInterfacesTransactionTestCase::RollBack001(KvStoreDelegate *&kvDelegatePtr)
424 {
425     /**
426      * @tc.steps:step1. Test g_kvDelegatePtr->Rollback
427      * @tc.expected: step1. Return ERROR.
428      */
429     EXPECT_TRUE(kvDelegatePtr->Rollback() == DB_ERROR);
430 }
431 
RollBack002(KvStoreDelegate * & kvDelegatePtr)432 void DistributedDBInterfacesTransactionTestCase::RollBack002(KvStoreDelegate *&kvDelegatePtr)
433 {
434     /**
435      * @tc.steps:step1. start a transaction
436      * @tc.expected: step1. Return OK.
437      */
438     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
439     /**
440      * @tc.steps:step2. rollback the transaction
441      * @tc.expected: step2. Return OK.
442      */
443     EXPECT_TRUE(kvDelegatePtr->Rollback() == OK);
444     /**
445      * @tc.steps:step3. rollback the transaction the second time
446      * @tc.expected: step3. Return ERROR.
447      */
448     EXPECT_TRUE(kvDelegatePtr->Rollback() == DB_ERROR);
449 }
450 
RollBack003(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)451 void DistributedDBInterfacesTransactionTestCase::RollBack003(KvStoreDelegate *&kvDelegatePtr,
452     KvStoreSnapshotDelegate *&snapshotDelegatePtr)
453 {
454     DBStatus valueStatus = INVALID_ARGS;
455     Value value;
456     auto valueCallback = bind(&DistributedDBToolsUnitTest::ValueCallback,
457         placeholders::_1, placeholders::_2, std::ref(valueStatus), std::ref(value));
458     /**
459      * @tc.steps:step1. start a transaction
460      * @tc.expected: step1. Return OK.
461      */
462     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
463     /**
464      * @tc.steps:step2. Put (k1,v1)
465      * @tc.expected: step2. Return OK.
466      */
467     EXPECT_TRUE(kvDelegatePtr->Put(KEY_1, VALUE_1) == OK);
468     /**
469      * @tc.steps:step3. rollback a transaction
470      * @tc.expected: step3. Return OK.
471      */
472     EXPECT_TRUE(kvDelegatePtr->Rollback() == OK);
473 
474     /**
475      * @tc.steps:step4. check if (k1,v1) exists
476      * @tc.expected: step4. Return NOT_FOUND.
477      */
478     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
479     snapshotDelegatePtr->Get(KEY_1, valueCallback);
480     EXPECT_TRUE(valueStatus == NOT_FOUND);
481 }
482 
RollBack004(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)483 void DistributedDBInterfacesTransactionTestCase::RollBack004(KvStoreDelegate *&kvDelegatePtr,
484     KvStoreSnapshotDelegate *&snapshotDelegatePtr)
485 {
486     DBStatus valueStatus = INVALID_ARGS;
487     Value value;
488     auto valueCallback = bind(&DistributedDBToolsUnitTest::ValueCallback,
489         placeholders::_1, placeholders::_2, std::ref(valueStatus), std::ref(value));
490     /**
491      * @tc.steps:step1. Put (k1,v1)
492      * @tc.expected: step1. Return OK.
493      */
494     EXPECT_TRUE(kvDelegatePtr->Put(KEY_1, VALUE_1) == OK);
495     /**
496      * @tc.steps:step2. start a transaction
497      * @tc.expected: step2. Return OK.
498      */
499     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
500     /**
501      * @tc.steps:step3. Update (k1,v1) to (k1,v2) in the transaction
502      * @tc.expected: step3. Return OK.
503      */
504     EXPECT_TRUE(kvDelegatePtr->Put(KEY_1, VALUE_2) == OK);
505     /**
506      * @tc.steps:step4. rollback the transaction
507      * @tc.expected: step4. Return OK.
508      */
509     EXPECT_TRUE(kvDelegatePtr->Rollback() == OK);
510 
511     /**
512      * @tc.steps:step5. check the value of k1 is v1
513      * @tc.expected: step5. verification is OK .
514      */
515     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
516     snapshotDelegatePtr->Get(KEY_1, valueCallback);
517     EXPECT_TRUE(valueStatus == OK);
518     ASSERT_TRUE(value.size() > 0);
519     EXPECT_TRUE(value.front() == VALUE_1.front());
520 }
521 
RollBack005(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)522 void DistributedDBInterfacesTransactionTestCase::RollBack005(KvStoreDelegate *&kvDelegatePtr,
523     KvStoreSnapshotDelegate *&snapshotDelegatePtr)
524 {
525     DBStatus valueStatus = INVALID_ARGS;
526     Value value;
527     auto valueCallback = bind(&DistributedDBToolsUnitTest::ValueCallback,
528         placeholders::_1, placeholders::_2, std::ref(valueStatus), std::ref(value));
529     /**
530      * @tc.steps:step1. Put (k1,v1)
531      * @tc.expected: step1. Return OK.
532      */
533     EXPECT_TRUE(kvDelegatePtr->Put(KEY_1, VALUE_1) == OK);
534     /**
535      * @tc.steps:step2. start a transaction
536      * @tc.expected: step2. Return OK.
537      */
538     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
539     /**
540      * @tc.steps:step3. Delete (k1,v1) in the transaction
541      * @tc.expected: step3. Return OK.
542      */
543     EXPECT_TRUE(kvDelegatePtr->Delete(KEY_1) == OK);
544     /**
545      * @tc.steps:step4. rollback the transaction
546      * @tc.expected: step4. Return OK.
547      */
548     EXPECT_TRUE(kvDelegatePtr->Rollback() == OK);
549 
550     /**
551      * @tc.steps:step5. check the value of k1 is v1
552      * @tc.expected: step5. verification is OK .
553      */
554     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
555     snapshotDelegatePtr->Get(KEY_1, valueCallback);
556     EXPECT_TRUE(valueStatus == OK);
557     ASSERT_TRUE(value.size() > 0);
558     EXPECT_TRUE(value.front() == VALUE_1.front());
559 }
560 
RollBack006(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)561 void DistributedDBInterfacesTransactionTestCase::RollBack006(KvStoreDelegate *&kvDelegatePtr,
562     KvStoreSnapshotDelegate *&snapshotDelegatePtr)
563 {
564     DBStatus entryVectorStatus = INVALID_ARGS;
565     unsigned long matchSizeCallback = 0;
566     std::vector<Entry> entriesVector;
567     auto entryVectorCallback = bind(&DistributedDBToolsUnitTest::EntryVectorCallback, placeholders::_1,
568         placeholders::_2, std::ref(entryVectorStatus), std::ref(matchSizeCallback), std::ref(entriesVector));
569     /**
570      * @tc.steps:step1. PutBatch records: (k1,v1), (k2,v2)
571      * @tc.expected: step1. Return OK.
572      */
573     EXPECT_TRUE(kvDelegatePtr->PutBatch(ENTRY_VECTOR) == OK);
574     /**
575      * @tc.steps:step2. start a transaction
576      * @tc.expected: step2. Return OK.
577      */
578     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
579     /**
580      * @tc.steps:step3. Clear all records in the transaction
581      * @tc.expected: step3. Return OK.
582      */
583     EXPECT_TRUE(kvDelegatePtr->Clear() == OK);
584     /**
585      * @tc.steps:step4. rollback the transaction
586      * @tc.expected: step4. Return OK.
587      */
588     EXPECT_TRUE(kvDelegatePtr->Rollback() == OK);
589 
590     /**
591      * @tc.steps:step5. check if there are 2 records in the db
592      * @tc.expected: step5. verification is OK .
593      */
594     unsigned long matchSize = 2;
595     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
596     snapshotDelegatePtr->GetEntries(NULL_KEY_1, entryVectorCallback);
597     EXPECT_TRUE(entryVectorStatus == OK);
598     ASSERT_TRUE(matchSizeCallback == matchSize);
599 }
600 
RollBack007(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)601 void DistributedDBInterfacesTransactionTestCase::RollBack007(KvStoreDelegate *&kvDelegatePtr,
602     KvStoreSnapshotDelegate *&snapshotDelegatePtr)
603 {
604     DBStatus valueStatus = INVALID_ARGS;
605     Value value;
606     auto valueCallback = bind(&DistributedDBToolsUnitTest::ValueCallback,
607         placeholders::_1, placeholders::_2, std::ref(valueStatus), std::ref(value));
608 
609     DBStatus entryVectorStatus = INVALID_ARGS;
610     unsigned long matchSizeCallback = 0;
611     std::vector<Entry> entriesVector;
612     auto entryVectorCallback = bind(&DistributedDBToolsUnitTest::EntryVectorCallback, placeholders::_1,
613         placeholders::_2, std::ref(entryVectorStatus), std::ref(matchSizeCallback), std::ref(entriesVector));
614 
615     /**
616      * @tc.steps:step1. PutBatch records: (k1,v1), (k2,v2)
617      * @tc.expected: step1. Return OK.
618      */
619     EXPECT_TRUE(kvDelegatePtr->PutBatch(ENTRY_VECTOR) == OK);
620     /**
621      * @tc.steps:step2. start a transaction
622      * @tc.expected: step2. Return OK.
623      */
624     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
625     /**
626      * @tc.steps:step3. Delete (k1,v1) in the transaction
627      * @tc.expected: step3. Return OK.
628      */
629     EXPECT_TRUE(kvDelegatePtr->Delete(KEY_1) == OK);
630     /**
631      * @tc.steps:step4. Update (k2,v2) to (k2,v1) in the transaction
632      * @tc.expected: step4. Return OK.
633      */
634     EXPECT_TRUE(kvDelegatePtr->Put(KEY_2, VALUE_1) == OK);
635     /**
636      * @tc.steps:step5. rollback the transaction
637      * @tc.expected: step5. Return OK.
638      */
639     EXPECT_TRUE(kvDelegatePtr->Rollback() == OK);
640 
641     /**
642      * @tc.steps:step6. check if (k1,v1),(k2,v2) exist and no more records in the db
643      * @tc.expected: step6. verification is OK .
644      */
645     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
646     snapshotDelegatePtr->Get(KEY_1, valueCallback);
647     EXPECT_TRUE(valueStatus == OK);
648     ASSERT_TRUE(value.size() > 0);
649     EXPECT_TRUE(value.front() == VALUE_1.front());
650     snapshotDelegatePtr->Get(KEY_2, valueCallback);
651     EXPECT_TRUE(valueStatus == OK);
652     ASSERT_TRUE(value.size() > 0);
653     EXPECT_TRUE(value.front() == VALUE_2.front());
654 
655     unsigned long matchSize = 2;
656     snapshotDelegatePtr->GetEntries(NULL_KEY_1, entryVectorCallback);
657     EXPECT_TRUE(entryVectorStatus == OK);
658     ASSERT_TRUE(matchSizeCallback == matchSize);
659 }
660 
RollBack008(KvStoreDelegate * & kvDelegatePtr,KvStoreSnapshotDelegate * & snapshotDelegatePtr)661 void DistributedDBInterfacesTransactionTestCase::RollBack008(KvStoreDelegate *&kvDelegatePtr,
662     KvStoreSnapshotDelegate *&snapshotDelegatePtr)
663 {
664     DBStatus valueStatus = INVALID_ARGS;
665     Value value;
666     auto valueCallback = bind(&DistributedDBToolsUnitTest::ValueCallback,
667         placeholders::_1, placeholders::_2, std::ref(valueStatus), std::ref(value));
668     DBStatus entryVectorStatus = INVALID_ARGS;
669     unsigned long matchSizeCallback = 0;
670     std::vector<Entry> entriesVector;
671     auto entryVectorCallback = bind(&DistributedDBToolsUnitTest::EntryVectorCallback, placeholders::_1,
672         placeholders::_2, std::ref(entryVectorStatus), std::ref(matchSizeCallback), std::ref(entriesVector));
673 
674     /**
675      * @tc.steps:step1. PutBatch records: (k1,v1), (k2,v2)
676      * @tc.expected: step1. Return OK.
677      */
678     EXPECT_TRUE(kvDelegatePtr->PutBatch(ENTRY_VECTOR) == OK);
679     /**
680      * @tc.steps:step2. start a transaction
681      * @tc.expected: step2. Return OK.
682      */
683     EXPECT_TRUE(kvDelegatePtr->StartTransaction() == OK);
684     /**
685      * @tc.steps:step3. Clear all records in the transaction
686      * @tc.expected: step3. Return OK.
687      */
688     EXPECT_TRUE(kvDelegatePtr->Clear() == OK);
689     /**
690      * @tc.steps:step4. Put (012, ABC) in the transaction
691      * @tc.expected: step4. Return OK.
692      */
693     Entry entry;
694     GenerateEntry(1, 3, entry);
695     EXPECT_TRUE(kvDelegatePtr->Put(entry.key, entry.value) == OK);
696     /**
697      * @tc.steps:step5. rollback the transaction
698      * @tc.expected: step5. Return OK.
699      */
700     EXPECT_TRUE(kvDelegatePtr->Rollback() == OK);
701 
702     /**
703      * @tc.steps:step6. check if (k1,v1),(k2,v2) exist and no more records in the db
704      * @tc.expected: step6. verification is OK .
705      */
706     GetSnapshotUnitTest(kvDelegatePtr, snapshotDelegatePtr);
707     snapshotDelegatePtr->Get(KEY_1, valueCallback);
708     EXPECT_TRUE(valueStatus == OK);
709     ASSERT_TRUE(value.size() > 0);
710     EXPECT_TRUE(value.front() == VALUE_1.front());
711     snapshotDelegatePtr->Get(KEY_2, valueCallback);
712     EXPECT_TRUE(valueStatus == OK);
713     ASSERT_TRUE(value.size() > 0);
714     EXPECT_TRUE(value.front() == VALUE_2.front());
715 
716     unsigned long matchSize = 2;
717     snapshotDelegatePtr->GetEntries(NULL_KEY_1, entryVectorCallback);
718     EXPECT_TRUE(entryVectorStatus == OK);
719     ASSERT_TRUE(matchSizeCallback == matchSize);
720 }
721 #endif // OMIT_MULTI_VER