1 /*
2 * Copyright (c) 2022 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 #include <gtest/gtest.h>
16
17 #include <condition_variable>
18 #include <vector>
19
20 #include "dev_manager.h"
21 #include "distributed_kv_data_manager.h"
22 #include "store_manager.h"
23 #include "types.h"
24
25 namespace OHOS::Test {
26 using namespace testing::ext;
27 using namespace OHOS::DistributedKv;
28 class SingleStoreImplGetTopTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31
32 static void TearDownTestCase(void);
33
34 void SetUp();
35
36 void TearDown();
37
38 static std::shared_ptr<SingleKvStore> singleKvStore; // declare kvstore instance.
39 static Status initStatus;
40 };
41
42 const std::string VALID_SCHEMA_STRICT_DEFINE = "{\"SCHEMA_VERSION\":\"1.0\","
43 "\"SCHEMA_MODE\":\"STRICT\","
44 "\"SCHEMA_SKIPSIZE\":0,"
45 "\"SCHEMA_DEFINE\":{"
46 "\"age\":\"INTEGER, NOT NULL\""
47 "},"
48 "\"SCHEMA_INDEXES\":[\"$.age\"]}";
49
50 std::shared_ptr<SingleKvStore> SingleStoreImplGetTopTest::singleKvStore = nullptr;
51 Status SingleStoreImplGetTopTest::initStatus = Status::ERROR;
52
SetUpTestCase(void)53 void SingleStoreImplGetTopTest::SetUpTestCase(void)
54 {
55 DistributedKvDataManager manager;
56 Options options = {
57 .createIfMissing = true, .encrypt = false, .autoSync = true, .kvStoreType = KvStoreType::SINGLE_VERSION
58 };
59 options.area = EL1;
60 options.securityLevel = S1;
61 options.baseDir = std::string("/data/service/el1/public/database/odmf");
62 AppId appId = { "odmf" };
63 StoreId storeId = { "test_single" }; // define kvstore(database) name.
64 mkdir(options.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
65 // [create and] open and initialize kvstore instance.
66 initStatus = manager.GetSingleKvStore(options, appId, storeId, singleKvStore);
67 }
68
TearDownTestCase(void)69 void SingleStoreImplGetTopTest::TearDownTestCase(void)
70 {
71 (void)remove("/data/service/el1/public/database/odmf/key");
72 (void)remove("/data/service/el1/public/database/odmf/kvdb");
73 (void)remove("/data/service/el1/public/database/odmf");
74 }
75
SetUp(void)76 void SingleStoreImplGetTopTest::SetUp(void) { }
77
TearDown(void)78 void SingleStoreImplGetTopTest::TearDown(void) { }
79
80 /**
81 * @tc.name: GetEntries
82 * @tc.desc: get entries order by write time Asc
83 * @tc.type: FUNC
84 * @tc.require:I5OM83
85 * @tc.author:ht
86 */
87 HWTEST_F(SingleStoreImplGetTopTest, GetEntriesOrderByWriteTimeAsc, TestSize.Level0)
88 {
89 ASSERT_NE(singleKvStore, nullptr);
90 std::vector<Entry> input;
91 for (size_t i = 10; i < 30; ++i) {
92 Entry entry;
93 entry.key = std::to_string(i).append("_k");
94 entry.value = std::to_string(i).append("_v");
95 input.push_back(entry);
96 auto status = singleKvStore->Put(entry.key, entry.value);
97 ASSERT_EQ(status, SUCCESS);
98 }
99 DataQuery query;
100 query.KeyPrefix("1");
101 query.OrderByWriteTime(true);
102 std::vector<Entry> output;
103 auto status = singleKvStore->GetEntries(query, output);
104 ASSERT_EQ(status, SUCCESS);
105 ASSERT_EQ(output.size(), 10);
106 for (size_t i = 0; i < output.size(); ++i) {
107 ASSERT_TRUE(input[i].key == output[i].key);
108 ASSERT_TRUE(input[i].value == output[i].value);
109 }
110 }
111
112 /**
113 * @tc.name: GetEntries
114 * @tc.desc: get entries order by write time Desc
115 * @tc.type: FUNC
116 * @tc.require:I5OM83
117 * @tc.author:ht
118 */
119 HWTEST_F(SingleStoreImplGetTopTest, GetEntriesOrderByWriteTimeDesc, TestSize.Level0)
120 {
121 ASSERT_NE(singleKvStore, nullptr);
122 std::vector<Entry> input;
123 for (size_t i = 10; i < 30; ++i) {
124 Entry entry;
125 entry.key = std::to_string(i).append("_k");
126 entry.value = std::to_string(i).append("_v");
127 input.push_back(entry);
128 auto status = singleKvStore->Put(entry.key, entry.value);
129 ASSERT_EQ(status, SUCCESS);
130 }
131 DataQuery query;
132 query.KeyPrefix("1");
133 query.OrderByWriteTime(false);
134 std::vector<Entry> output;
135 auto status = singleKvStore->GetEntries(query, output);
136 ASSERT_EQ(status, SUCCESS);
137 ASSERT_EQ(output.size(), 10);
138 for (size_t i = 0; i < output.size(); ++i) {
139 ASSERT_TRUE(input[9 - i].key == output[i].key);
140 ASSERT_TRUE(input[9 - i].value == output[i].value);
141 }
142 }
143
144 /**
145 * @tc.name: GetEntries
146 * @tc.desc: get entries order by write time no prefix
147 * @tc.type: FUNC
148 * @tc.require:I5OM83
149 * @tc.author:ht
150 */
151 HWTEST_F(SingleStoreImplGetTopTest, GetEntriesOrderByWriteTimeNoPrefix, TestSize.Level0)
152 {
153 ASSERT_NE(singleKvStore, nullptr);
154 std::vector<Entry> input;
155 for (size_t i = 10; i < 30; ++i) {
156 Entry entry;
157 entry.key = std::to_string(i).append("_k");
158 entry.value = std::to_string(i).append("_v");
159 input.push_back(entry);
160 auto status = singleKvStore->Put(entry.key, entry.value);
161 ASSERT_EQ(status, SUCCESS);
162 }
163 singleKvStore->Put("test_key_1", "{\"name\":1}");
164 DataQuery query;
165 query.OrderByWriteTime(true);
166 query.EqualTo("$.name", 1);
167 std::vector<Entry> output;
168 auto status = singleKvStore->GetEntries(query, output);
169 ASSERT_EQ(status, NOT_SUPPORT);
170 ASSERT_EQ(output.size(), 0);
171 }
172
173 /**
174 * @tc.name: GetResultSet
175 * @tc.desc: get result set order by write time Asc
176 * @tc.type: FUNC
177 * @tc.require:I5OM83
178 * @tc.author:ht
179 */
180 HWTEST_F(SingleStoreImplGetTopTest, GetResultSetOrderByWriteTimeAsc, TestSize.Level0)
181 {
182 ASSERT_NE(singleKvStore, nullptr);
183 std::vector<Entry> input;
184 for (size_t i = 10; i < 30; ++i) {
185 Entry entry;
186 entry.key = std::to_string(i).append("_k");
187 entry.value = std::to_string(i).append("_v");
188 input.push_back(entry);
189 auto status = singleKvStore->Put(entry.key, entry.value);
190 ASSERT_EQ(status, SUCCESS);
191 }
192 DataQuery query;
193 query.InKeys({ "10_k", "11_k" });
194 query.OrderByWriteTime(true);
195 std::shared_ptr<KvStoreResultSet> output;
196 auto status = singleKvStore->GetResultSet(query, output);
197 ASSERT_EQ(status, SUCCESS);
198 ASSERT_NE(output, nullptr);
199 ASSERT_EQ(output->GetCount(), 2);
200 for (size_t i = 0; i < 2; ++i) {
201 output->MoveToNext();
202 Entry entry;
203 status = output->GetEntry(entry);
204 ASSERT_EQ(status, SUCCESS);
205 ASSERT_TRUE(input[i].key == entry.key);
206 ASSERT_TRUE(input[i].value == entry.value);
207 }
208 }
209
210 /**
211 * @tc.name: GetResultSet
212 * @tc.desc: get result set order by write time Desc
213 * @tc.type: FUNC
214 * @tc.require:I5OM83
215 * @tc.author:ht
216 */
217 HWTEST_F(SingleStoreImplGetTopTest, GetResultSetOrderByWriteTimeDesc, TestSize.Level0)
218 {
219 ASSERT_NE(singleKvStore, nullptr);
220 std::vector<Entry> input;
__anon07a2e94c0102(const Key &entry, const Key &sentry) 221 auto cmp = [](const Key &entry, const Key &sentry) {
222 return entry.Data() < sentry.Data();
223 };
224 std::map<Key, Value, decltype(cmp)> dictionary(cmp);
225 for (size_t i = 10; i < 30; ++i) {
226 Entry entry;
227 entry.key = std::to_string(i).append("_k");
228 entry.value = std::to_string(i).append("_v");
229 input.push_back(entry);
230 dictionary[entry.key] = entry.value;
231 auto status = singleKvStore->Put(entry.key, entry.value);
232 ASSERT_EQ(status, SUCCESS);
233 }
234 DataQuery query;
235 query.KeyPrefix("1");
236 query.OrderByWriteTime(false);
237 std::shared_ptr<KvStoreResultSet> output;
238 auto status = singleKvStore->GetResultSet(query, output);
239 ASSERT_EQ(status, SUCCESS);
240 ASSERT_NE(output, nullptr);
241 ASSERT_EQ(output->GetCount(), 10);
242 for (size_t i = 0; i < 10; ++i) {
243 output->MoveToNext();
244 Entry entry;
245 output->GetEntry(entry);
246 ASSERT_TRUE(input[9 - i].key == entry.key);
247 ASSERT_TRUE(input[9 - i].value == entry.value);
248 }
249 }
250
251 /**
252 * @tc.name: GetResultSet
253 * @tc.desc: get result set order by write time no prefix
254 * @tc.type: FUNC
255 * @tc.require:I5OM83
256 * @tc.author:ht
257 */
258 HWTEST_F(SingleStoreImplGetTopTest, GetResultSetOrderByWriteTimeNoPrefix, TestSize.Level0)
259 {
260 ASSERT_NE(singleKvStore, nullptr);
261 std::vector<Entry> input;
__anon07a2e94c0202(const Key &entry, const Key &sentry) 262 auto cmp = [](const Key &entry, const Key &sentry) {
263 return entry.Data() < sentry.Data();
264 };
265 std::map<Key, Value, decltype(cmp)> dictionary(cmp);
266 for (size_t i = 10; i < 30; ++i) {
267 Entry entry;
268 entry.key = std::to_string(i).append("_k");
269 entry.value = std::to_string(i).append("_v");
270 input.push_back(entry);
271 dictionary[entry.key] = entry.value;
272 auto status = singleKvStore->Put(entry.key, entry.value);
273 ASSERT_EQ(status, SUCCESS);
274 }
275 singleKvStore->Put("test_key_1", "{\"name\":1}");
276 DataQuery query;
277 query.OrderByWriteTime(true);
278 query.EqualTo("$.name", 1);
279 std::shared_ptr<KvStoreResultSet> output;
280 auto status = singleKvStore->GetResultSet(query, output);
281 ASSERT_EQ(status, NOT_SUPPORT);
282 ASSERT_EQ(output, nullptr);
283 }
284 } // namespace OHOS::Test