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