1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #define LOG_TAG "StoreTest"
16 
17 #include "access_token.h"
18 #include "gtest/gtest.h"
19 #include "general_store_mock.h"
20 #include "log_print.h"
21 #include "metadata/store_meta_data.h"
22 #include "rdb_query.h"
23 #include "rdb_types.h"
24 #include "store/auto_cache.h"
25 #include "store/general_store.h"
26 #include "store/general_value.h"
27 #include "store/general_watcher.h"
28 
29 using namespace testing::ext;
30 using namespace OHOS::DistributedData;
31 namespace OHOS::Test {
32 class GeneralValueTest : public testing::Test {
33 public:
SetUpTestCase(void)34     static void SetUpTestCase(void){};
TearDownTestCase(void)35     static void TearDownTestCase(void){};
SetUp()36     void SetUp(){};
TearDown()37     void TearDown(){};
38 };
39 
40 class GeneralStoreTest : public testing::Test {
41 public:
SetUpTestCase(void)42     static void SetUpTestCase(void){};
TearDownTestCase(void)43     static void TearDownTestCase(void){};
SetUp()44     void SetUp(){};
TearDown()45     void TearDown(){};
46 };
47 
48 class AutoCacheTest : public testing::Test {
49 public:
SetUpTestCase(void)50     static void SetUpTestCase(void){};
TearDownTestCase(void)51     static void TearDownTestCase(void){};
SetUp()52     void SetUp(){};
TearDown()53     void TearDown(){};
54 };
55 
56 /**
57  * @tc.name: SetQueryNodesTest
58  * @tc.desc: Set and query nodes.
59  * @tc.type: FUNC
60  * @tc.require: AR000F8N0
61  */
62 HWTEST_F(GeneralValueTest, SetQueryNodesTest, TestSize.Level2)
63 {
64     ZLOGI("GeneralValueTest SetQueryNodesTest begin.");
65     std::string tableName = "test_tableName";
66     QueryNode node;
67     node.op = OHOS::DistributedData::QueryOperation::EQUAL_TO;
68     node.fieldName =  "test_fieldName";
69     node.fieldValue = {"aaa", "bbb", "ccc"};
70     QueryNodes nodes{
71         {node}
72     };
73     OHOS::DistributedRdb::RdbQuery query;
74     query.SetQueryNodes(tableName, std::move(nodes));
75     QueryNodes nodes1 =  query.GetQueryNodes("test_tableName");
76     EXPECT_EQ(nodes1[0].fieldName, "test_fieldName");
77     EXPECT_EQ(nodes1[0].op, OHOS::DistributedData::QueryOperation::EQUAL_TO);
78     EXPECT_EQ(nodes1[0].fieldValue.size(), 3);
79 }
80 
81 /**
82 * @tc.name: GetMixModeTest
83 * @tc.desc: get mix mode
84 * @tc.type: FUNC
85 * @tc.require:
86 * @tc.author:
87 */
88 HWTEST_F(GeneralStoreTest, GetMixModeTest, TestSize.Level2)
89 {
90     ZLOGI("GeneralStoreTest GetMixModeTest begin.");
91     auto mode1 = OHOS::DistributedRdb::TIME_FIRST;
92     auto mode2 = GeneralStore::AUTO_SYNC_MODE;
93 
94     auto mixMode = GeneralStore::MixMode(mode1, mode2);
95     EXPECT_EQ(mixMode, mode1 | mode2);
96 
97     auto syncMode = GeneralStore::GetSyncMode(mixMode);
98     EXPECT_EQ(syncMode, OHOS::DistributedRdb::TIME_FIRST);
99 
100     auto highMode = GeneralStore::GetHighMode(mixMode);
101     EXPECT_EQ(highMode, GeneralStore::AUTO_SYNC_MODE);
102 }
103 
104 /**
105 * @tc.name: OnChange001
106 * @tc.desc: AutoCache Delegate OnChange
107 * @tc.type: FUNC
108 * @tc.require:
109 * @tc.author: SQL
110 */
111 HWTEST_F(AutoCacheTest, OnChange001, TestSize.Level2)
112 {
113     GeneralStoreMock* store = new (std::nothrow) GeneralStoreMock();
114     ASSERT_NE(store, nullptr);
115     AutoCache::Watchers watchers;
116     int32_t user = 0;
117     StoreMetaData meta;
118     AutoCache::Delegate delegate(store, watchers, user, meta);
119     delegate.SetObservers(watchers);
120     GeneralWatcher::Origin origin;
121     GeneralWatcher::PRIFields primaryFields;
122     GeneralWatcher::ChangeInfo values;
123     auto ret = delegate.OnChange(origin, primaryFields, std::move(values));
124     EXPECT_EQ(ret, GeneralError::E_OK);
125 }
126 
127 /**
128 * @tc.name: OnChange002
129 * @tc.desc: AutoCache Delegate OnChange
130 * @tc.type: FUNC
131 * @tc.require:
132 * @tc.author: SQL
133 */
134 HWTEST_F(AutoCacheTest, OnChange002, TestSize.Level2)
135 {
136     GeneralStoreMock* store = new (std::nothrow) GeneralStoreMock();
137     ASSERT_NE(store, nullptr);
138     AutoCache::Watchers watchers;
139     int32_t user = 0;
140     StoreMetaData meta;
141     AutoCache::Delegate delegate(store, watchers, user, meta);
142     delegate.SetObservers(watchers);
143     GeneralWatcher::Origin origin;
144     GeneralWatcher::Fields fields;
145     GeneralWatcher::ChangeData datas;
146     auto ret = delegate.OnChange(origin, fields, std::move(datas));
147     EXPECT_EQ(ret, GeneralError::E_OK);
148 }
149 
150 /**
151 * @tc.name: operatorStore
152 * @tc.desc: AutoCache Delegate operator Store()
153 * @tc.type: FUNC
154 * @tc.require:
155 * @tc.author: SQL
156 */
157 HWTEST_F(AutoCacheTest, operatorStore, TestSize.Level2)
158 {
159     GeneralStoreMock* store = new (std::nothrow) GeneralStoreMock();
160     ASSERT_NE(store, nullptr);
161     AutoCache::Watchers watchers;
162     int32_t user = 0;
163     StoreMetaData meta;
164     AutoCache::Delegate delegate(store, watchers, user, meta);
165     AutoCache::Store result = static_cast<AutoCache::Store>(delegate);
166     EXPECT_NE(result, nullptr);
167     GeneralWatcher::Origin origin;
168     GeneralWatcher::Fields fields;
169     GeneralWatcher::ChangeData datas;
170     auto ret = delegate.OnChange(origin, fields, std::move(datas));
171     EXPECT_EQ(ret, GeneralError::E_OK);
172     EXPECT_EQ(delegate.GetUser(), user);
173 }
174 } // namespace OHOS::Test