blob: b5c016130bbaba39d966ebddb5f21a82abe5cb7d [file] [log] [blame]
Chengyu Fanb25835b2015-04-28 17:09:35 -06001/** NDN-Atmos: Cataloging Service for distributed data originally developed
2 * for atmospheric science data
3 * Copyright (C) 2015 Colorado State University
4 *
5 * NDN-Atmos is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * NDN-Atmos is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NDN-Atmos. If not, see <http://www.gnu.org/licenses/>.
17**/
18
19#include "query/query-adapter.hpp"
20#include "boost-test.hpp"
21#include "../../unit-test-time-fixture.hpp"
22#include "util/config-file.hpp"
23
24#include <boost/mpl/list.hpp>
25#include <boost/thread.hpp>
26#include <ndn-cxx/util/dummy-client-face.hpp>
27#include <boost/property_tree/info_parser.hpp>
28
29namespace atmos{
30namespace tests{
31 using ndn::util::DummyClientFace;
32 using ndn::util::makeDummyClientFace;
33
34 class QueryAdapterTest : public query::QueryAdapter<std::string>
35 {
36 public:
37 QueryAdapterTest(const std::shared_ptr<ndn::util::DummyClientFace>& face,
Chengyu Fancfb80c72015-10-19 16:50:04 -060038 const std::shared_ptr<ndn::KeyChain>& keyChain,
39 const std::shared_ptr<chronosync::Socket>& syncSocket)
40 : query::QueryAdapter<std::string>(face, keyChain, syncSocket)
Chengyu Fanb25835b2015-04-28 17:09:35 -060041 {
42 }
43
44 virtual
45 ~QueryAdapterTest()
46 {
47 }
48
Chengyu Fanf4c747a2015-08-18 13:56:01 -060049 void setDatabaseTable(const std::string& databaseTable)
50 {
51 m_databaseTable.assign(databaseTable);
52 }
53
Chengyu Fan92440162015-07-09 14:43:31 -060054 void setNameFields(const std::vector<std::string>& nameFields)
55 {
56 m_nameFields = nameFields;
57 }
58
Chengyu Fanb25835b2015-04-28 17:09:35 -060059 void setPrefix(const ndn::Name& prefix)
60 {
61 m_prefix = prefix;
62 }
63
64 void setSigningId(const ndn::Name& signingId)
65 {
66 m_signingId = signingId;
67 }
68
69 const ndn::Name
70 getPrefix()
71 {
72 return m_prefix;
73 }
74
75 const ndn::Name
76 getSigningId()
77 {
78 return m_signingId;
79 }
80
81 std::shared_ptr<ndn::Data>
82 getAckData(std::shared_ptr<const ndn::Interest> interest, const ndn::Name::Component& version)
83 {
84 return makeAckData(interest, version);
85 }
86
Chengyu Fanb25835b2015-04-28 17:09:35 -060087 std::shared_ptr<ndn::Data>
88 getReplyData(const ndn::Name& segmentPrefix,
89 const Json::Value& value,
90 uint64_t segmentNo,
91 bool isFinalBlock,
Chengyu Fan92440162015-07-09 14:43:31 -060092 bool isAutocomplete,
Chengyu Fanf4c747a2015-08-18 13:56:01 -060093 uint64_t resultCount,
94 uint64_t viewStart,
95 uint64_t viewEnd)
Chengyu Fanb25835b2015-04-28 17:09:35 -060096 {
Chengyu Fan92440162015-07-09 14:43:31 -060097 return makeReplyData(segmentPrefix, value, segmentNo, isFinalBlock,
Chengyu Fan3b9bb342015-09-21 10:53:37 -060098 isAutocomplete, resultCount, viewStart, viewEnd, false);
Chengyu Fanb25835b2015-04-28 17:09:35 -060099 }
100
101 void
102 queryTest(std::shared_ptr<const ndn::Interest> interest)
103 {
104 runJsonQuery(interest);
105 }
106
107 void
Chengyu Fan31737f12016-01-12 21:08:50 -0700108 prepareSegmentsByParams(std::vector<std::pair<std::string, std::string>>& queryParams,
109 const ndn::Name& segmentPrefix)
Chengyu Fanb25835b2015-04-28 17:09:35 -0600110 {
Chengyu Fan31737f12016-01-12 21:08:50 -0700111 //BOOST_CHECK_EQUAL(sqlString, "SELECT name FROM cmip5 WHERE name=\'test\';");
112 for (auto it = queryParams.begin() ; it != queryParams.end(); ++it) {
113 std::cout << it->first << " " << it->second << std::endl;
114 }
115
Chengyu Fanb25835b2015-04-28 17:09:35 -0600116 Json::Value fileList;
117 fileList.append("/ndn/test1");
118 fileList.append("/ndn/test2");
119 fileList.append("/ndn/test3");
120
121 std::shared_ptr<ndn::Data> data = makeReplyData(segmentPrefix,
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600122 fileList, 0, true, false,
Chengyu Fan31737f12016-01-12 21:08:50 -0700123 3, 0, 2, true);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600124 m_mutex.lock();
125 m_cache.insert(*data);
126 m_mutex.unlock();
127 }
128
129 std::shared_ptr<const ndn::Data>
Chengyu Fanb25835b2015-04-28 17:09:35 -0600130 getDataFromCache(const ndn::Interest& interest)
131 {
132 return m_cache.find(interest);
133 }
134
135 void
136 configAdapter(const util::ConfigSection& section,
137 const ndn::Name& prefix)
138 {
139 onConfig(section, false, std::string("test.txt"), prefix);
140 }
Chengyu Fan92440162015-07-09 14:43:31 -0600141
142 bool
143 json2AutocompletionSqlTest(std::stringstream& sqlQuery,
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600144 Json::Value& jsonValue,
Chengyu Fan31737f12016-01-12 21:08:50 -0700145 bool& lastComponent,
146 std::stringstream& nameField)
Chengyu Fan92440162015-07-09 14:43:31 -0600147 {
Chengyu Fan31737f12016-01-12 21:08:50 -0700148 return json2AutocompletionSql(sqlQuery, jsonValue, lastComponent, nameField);
Chengyu Fan92440162015-07-09 14:43:31 -0600149 }
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600150
151 bool
Chengyu Fan31737f12016-01-12 21:08:50 -0700152 testDoPrefixBasedSearch(Json::Value& jsonValue,
153 std::vector<std::pair<std::string, std::string>>& typedComponents)
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600154 {
Chengyu Fan31737f12016-01-12 21:08:50 -0700155 return doPrefixBasedSearch(jsonValue, typedComponents);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600156 }
Chengyu Fan31737f12016-01-12 21:08:50 -0700157
158 bool
159 testDoFilterBasedSearch(Json::Value& jsonValue,
160 std::vector<std::pair<std::string, std::string>>& typedComponents)
161 {
162 return doFilterBasedSearch(jsonValue, typedComponents);
163 }
164
165
Chengyu Fanb25835b2015-04-28 17:09:35 -0600166 };
167
168 class QueryAdapterFixture : public UnitTestTimeFixture
169 {
170 public:
171 QueryAdapterFixture()
172 : face(makeDummyClientFace(io))
173 , keyChain(new ndn::KeyChain())
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600174 , databaseTable("cmip5")
Chengyu Fancfb80c72015-10-19 16:50:04 -0600175 , queryAdapterTest1(face, keyChain, syncSocket)
176 , queryAdapterTest2(face, keyChain, syncSocket)
Chengyu Fanb25835b2015-04-28 17:09:35 -0600177 {
Chengyu Fan92440162015-07-09 14:43:31 -0600178 std::string c1("activity"), c2("product"), c3("organization"), c4("model");
179 std::string c5("experiment"), c6("frequency"), c7("modeling_realm"), c8("variable_name");
180 std::string c9("ensemble"), c10("time");
181 nameFields.push_back(c1);
182 nameFields.push_back(c2);
183 nameFields.push_back(c3);
184 nameFields.push_back(c4);
185 nameFields.push_back(c5);
186 nameFields.push_back(c6);
187 nameFields.push_back(c7);
188 nameFields.push_back(c8);
189 nameFields.push_back(c9);
190 nameFields.push_back(c10);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600191
192 queryAdapterTest1.setDatabaseTable(databaseTable);
193 queryAdapterTest1.setNameFields(nameFields);
194 queryAdapterTest2.setDatabaseTable(databaseTable);
195 queryAdapterTest2.setNameFields(nameFields);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600196 }
197
198 virtual
199 ~QueryAdapterFixture()
200 {
201 }
202
203 protected:
204 void
205 initializeQueryAdapterTest1()
206 {
207 util::ConfigSection section;
208 try {
209 std::stringstream ss;
210 ss << "signingId /test/signingId\
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600211 filterCategoryNames activity,product,organization,model,experiment,frequency,modeling_realm,variable_name,ensemble\
Chengyu Fanb25835b2015-04-28 17:09:35 -0600212 database \
213 { \
214 dbServer localhost \
215 dbName testdb \
216 dbUser testuser \
217 dbPasswd testpwd \
218 }";
219 boost::property_tree::read_info(ss, section);
220 }
221 catch (boost::property_tree::info_parser_error &e) {
222 std::cout << "Failed to read config file " << e.what() << std::endl;;
223 }
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600224
Chengyu Fanb25835b2015-04-28 17:09:35 -0600225 queryAdapterTest1.configAdapter(section, ndn::Name("/test"));
226 }
227
228 void
229 initializeQueryAdapterTest2()
230 {
231 util::ConfigSection section;
232 try {
233 std::stringstream ss;
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600234 ss << "\
235 filterCategoryNames activity,product,organization,model,experiment,frequency,modeling_realm,variable_name,ensemble\
236 database\
Chengyu Fanb25835b2015-04-28 17:09:35 -0600237 { \
238 dbServer localhost \
239 dbName testdb \
240 dbUser testuser \
241 dbPasswd testpwd \
242 }";
243 boost::property_tree::read_info(ss, section);
244 }
245 catch (boost::property_tree::info_parser_error &e) {
246 std::cout << "Failed to read config file " << e.what() << std::endl;;
247 }
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600248
Chengyu Fanb25835b2015-04-28 17:09:35 -0600249 queryAdapterTest2.configAdapter(section, ndn::Name("/test"));
250 }
251
252 protected:
253 std::shared_ptr<DummyClientFace> face;
254 std::shared_ptr<ndn::KeyChain> keyChain;
Chengyu Fancfb80c72015-10-19 16:50:04 -0600255 std::shared_ptr<chronosync::Socket> syncSocket;
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600256 std::string databaseTable;
257 std::vector<std::string> nameFields;
Chengyu Fanb25835b2015-04-28 17:09:35 -0600258 QueryAdapterTest queryAdapterTest1;
259 QueryAdapterTest queryAdapterTest2;
260 };
261
262 BOOST_FIXTURE_TEST_SUITE(QueryAdapterTestSuite, QueryAdapterFixture)
263
264 BOOST_AUTO_TEST_CASE(BasicQueryAdapterTest1)
265 {
266 BOOST_CHECK(queryAdapterTest1.getPrefix() == ndn::Name());
267 BOOST_CHECK(queryAdapterTest1.getSigningId() == ndn::Name());
268 }
269
270 BOOST_AUTO_TEST_CASE(BasicQueryAdapterTest2)
271 {
272 initializeQueryAdapterTest1();
273 BOOST_CHECK(queryAdapterTest1.getPrefix() == ndn::Name("/test"));
274 BOOST_CHECK(queryAdapterTest1.getSigningId() == ndn::Name("/test/signingId"));
275 }
276
Chengyu Fan7b978f82015-12-09 17:03:23 -0700277 // use real data instead of ack data
Chengyu Fanb25835b2015-04-28 17:09:35 -0600278 BOOST_AUTO_TEST_CASE(QueryAdapterMakeAckDataTest)
279 {
280 ndn::Interest interest(ndn::Name("/test/ack/data/json"));
281 interest.setInterestLifetime(ndn::time::milliseconds(1000));
282 interest.setMustBeFresh(true);
283 std::shared_ptr<const ndn::Interest> interestPtr = std::make_shared<ndn::Interest>(interest);
284
285 const ndn::name::Component version
286 = ndn::name::Component::fromVersion(1);
287
288 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getAckData(interestPtr, version);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600289 BOOST_CHECK_EQUAL(data->getName().toUri(), "/test/ack/data/json");
Chengyu Fanb25835b2015-04-28 17:09:35 -0600290 }
291
292 BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest1)
293 {
294 Json::Value fileList;
295 fileList.append("/ndn/test1");
296 fileList.append("/ndn/test2");
297
298 const ndn::Name prefix("/atmos/test/prefix");
299
Chengyu Fan92440162015-07-09 14:43:31 -0600300 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList,
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600301 1, false, false, 2, 0, 1);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600302 BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%01");
303 BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component(""));
Chengyu Fan46398212015-08-11 11:23:13 -0600304 const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()),
305 data->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600306 Json::Value parsedFromString;
307 Json::Reader reader;
308 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600309 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 2);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600310 BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 2);
311 BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1");
312 BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2");
313 }
314
315 BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest2)
316 {
317 Json::Value fileList;
318 fileList.append("/ndn/test1");
319 const ndn::Name prefix("/atmos/test/prefix");
320
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600321 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList,
322 2, true, true, 1, 0, 0);
Chengyu Fan92440162015-07-09 14:43:31 -0600323
Chengyu Fanb25835b2015-04-28 17:09:35 -0600324 BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%02");
325 BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component::fromSegment(2));
Chengyu Fan46398212015-08-11 11:23:13 -0600326 const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()),
327 data->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600328 Json::Value parsedFromString;
329 Json::Reader reader;
330 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600331 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 1);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600332 BOOST_CHECK_EQUAL(parsedFromString["next"].size(), 1);
333 BOOST_CHECK_EQUAL(parsedFromString["next"][0], "/ndn/test1");
334 }
335
336 BOOST_AUTO_TEST_CASE(QueryAdapterQueryProcessTest)
337 {
338 initializeQueryAdapterTest2();
339 Json::Value query;
340 query["name"] = "test";
341 Json::FastWriter fastWriter;
342 std::string jsonMessage = fastWriter.write(query);
343 jsonMessage.erase(std::remove(jsonMessage.begin(), jsonMessage.end(), '\n'), jsonMessage.end());
344 std::shared_ptr<ndn::Interest> queryInterest
345 = std::make_shared<ndn::Interest>(ndn::Name("/test/query").append(jsonMessage.c_str()));
346
347 queryAdapterTest2.queryTest(queryInterest);
Chengyu Fan7b978f82015-12-09 17:03:23 -0700348
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600349 // TODO: the code below should be enabled when queryAdapter can get the correct the
350 // ChronoSync state; currently, we don't need the activeQuery to save the ACK data;
351 //auto ackData = queryAdapterTest2.getDataFromActiveQuery(jsonMessage);
352 //
353 //BOOST_CHECK(ackData);
354 //if (ackData) {
355 // BOOST_CHECK_EQUAL(ackData->getName().getPrefix(3),
356 // ndn::Name("/test/query/%7B%22name%22%3A%22test%22%7D"));
357 // BOOST_CHECK_EQUAL(ackData->getName().at(ackData->getName().size() - 1),
358 // ndn::Name::Component("OK"));
359 // BOOST_CHECK_EQUAL(ackData->getContent().value_size(), 0);
360 //}
Chengyu Fanb25835b2015-04-28 17:09:35 -0600361
Chengyu Fan7b978f82015-12-09 17:03:23 -0700362 // there is no query-results namespace data
363 auto replyData = queryAdapterTest2.getDataFromCache(*queryInterest);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600364 BOOST_CHECK(replyData);
365 if (replyData){
Chengyu Fan7b978f82015-12-09 17:03:23 -0700366 BOOST_CHECK_EQUAL(replyData->getName().getPrefix(2), ndn::Name("/test/query"));
Chengyu Fan46398212015-08-11 11:23:13 -0600367 const std::string jsonRes(reinterpret_cast<const char*>(replyData->getContent().value()),
368 replyData->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600369 Json::Value parsedFromString;
370 Json::Reader reader;
371 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600372 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 3);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600373 BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 3);
374 BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1");
375 BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2");
376 BOOST_CHECK_EQUAL(parsedFromString["results"][2], "/ndn/test3");
377 }
378 }
379
Chengyu Fan92440162015-07-09 14:43:31 -0600380 BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlSuccessTest)
381 {
382 initializeQueryAdapterTest2();
383
Chengyu Fan31737f12016-01-12 21:08:50 -0700384 std::stringstream ss, nameField;
Chengyu Fan92440162015-07-09 14:43:31 -0600385 Json::Value testJson;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600386 bool lastComponent = false;
Chengyu Fan92440162015-07-09 14:43:31 -0600387 testJson["?"] = "/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600388 BOOST_CHECK_EQUAL(true,
Chengyu Fan31737f12016-01-12 21:08:50 -0700389 queryAdapterTest2.json2AutocompletionSqlTest(ss,
390 testJson,
391 lastComponent,
392 nameField));
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600393 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan31737f12016-01-12 21:08:50 -0700394 BOOST_CHECK_EQUAL(";", ss.str());
395 BOOST_CHECK_EQUAL("activity", nameField.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600396
397 ss.str("");
398 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700399 nameField.str("");
400 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600401 testJson.clear();
402 testJson["?"] = "/Activity/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600403 BOOST_CHECK_EQUAL(true,
Chengyu Fan31737f12016-01-12 21:08:50 -0700404 queryAdapterTest2.json2AutocompletionSqlTest(ss,
405 testJson,
406 lastComponent,
407 nameField));
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600408 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan31737f12016-01-12 21:08:50 -0700409 BOOST_CHECK_EQUAL(" WHERE activity='Activity';", ss.str());
410 BOOST_CHECK_EQUAL("product", nameField.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600411
412 ss.str("");
413 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700414 nameField.str("");
415 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600416 testJson.clear();
417 testJson["?"] = "/Activity/Product/Organization/Model/Experiment/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600418 BOOST_CHECK_EQUAL(true,
Chengyu Fan31737f12016-01-12 21:08:50 -0700419 queryAdapterTest2.json2AutocompletionSqlTest(ss,
420 testJson,
421 lastComponent,
422 nameField));
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600423 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan31737f12016-01-12 21:08:50 -0700424 BOOST_CHECK_EQUAL(" WHERE activity='Activity' AND \
Chengyu Fan46398212015-08-11 11:23:13 -0600425experiment='Experiment' AND model='Model' AND organization='Organization' AND product='Product';",
426 ss.str());
Chengyu Fan31737f12016-01-12 21:08:50 -0700427 BOOST_CHECK_EQUAL("frequency", nameField.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600428
429 ss.str("");
430 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700431 nameField.str("");
432 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600433 testJson.clear();
434 testJson["?"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\
435Variable/Ensemble/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600436 BOOST_CHECK_EQUAL(true,
Chengyu Fan31737f12016-01-12 21:08:50 -0700437 queryAdapterTest2.json2AutocompletionSqlTest(ss,
438 testJson,
439 lastComponent,
440 nameField));
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600441 BOOST_CHECK_EQUAL(lastComponent, true);
Chengyu Fan31737f12016-01-12 21:08:50 -0700442 BOOST_CHECK_EQUAL(" WHERE activity='Activity' AND ensemble=\
Chengyu Fan46398212015-08-11 11:23:13 -0600443'Ensemble' AND experiment='Experiment' AND frequency='Frequency' AND model='Model' AND \
444modeling_realm='Modeling' AND organization='Organization' AND product='Product' AND variable_name=\
445'Variable';",ss.str());
Chengyu Fan31737f12016-01-12 21:08:50 -0700446 BOOST_CHECK_EQUAL("time", nameField.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600447 }
448
449 BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlFailTest)
450 {
451 initializeQueryAdapterTest2();
452
Chengyu Fan31737f12016-01-12 21:08:50 -0700453 std::stringstream ss, nameField;
Chengyu Fan92440162015-07-09 14:43:31 -0600454 Json::Value testJson;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600455 bool lastComponent = false;
Chengyu Fan92440162015-07-09 14:43:31 -0600456 testJson["?"] = "serchTest";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600457 BOOST_CHECK_EQUAL(false,
Chengyu Fan31737f12016-01-12 21:08:50 -0700458 queryAdapterTest2.json2AutocompletionSqlTest(ss,
459 testJson,
460 lastComponent,
461 nameField));
Chengyu Fan92440162015-07-09 14:43:31 -0600462
463 ss.str("");
464 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700465 nameField.str("");
466 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600467 testJson.clear();
468 testJson["?"] = "/cmip5";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600469 BOOST_CHECK_EQUAL(false,
Chengyu Fan31737f12016-01-12 21:08:50 -0700470 queryAdapterTest2.json2AutocompletionSqlTest(ss,
471 testJson,
472 lastComponent,
473 nameField));
Chengyu Fan92440162015-07-09 14:43:31 -0600474
475 ss.str("");
476 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700477 nameField.str("");
478 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600479 Json::Value testJson2; //simply clear does not work
480 testJson2[0] = "test";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600481 BOOST_CHECK_EQUAL(false,
Chengyu Fan31737f12016-01-12 21:08:50 -0700482 queryAdapterTest2.json2AutocompletionSqlTest(ss,
483 testJson,
484 lastComponent,
485 nameField));
Chengyu Fan92440162015-07-09 14:43:31 -0600486
487 ss.str("");
488 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700489 nameField.str("");
490 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600491 Json::Value testJson3;
492 testJson3 = Json::Value(Json::arrayValue);
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600493 BOOST_CHECK_EQUAL(false,
Chengyu Fan31737f12016-01-12 21:08:50 -0700494 queryAdapterTest2.json2AutocompletionSqlTest(ss,
495 testJson,
496 lastComponent,
497 nameField));
Chengyu Fan92440162015-07-09 14:43:31 -0600498
499 ss.str("");
500 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700501 nameField.str("");
502 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600503 Json::Value testJson4;
504 Json::Value param;
505 param[0] = "test";
506 testJson4["name"] = param;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600507 BOOST_CHECK_EQUAL(false,
Chengyu Fan31737f12016-01-12 21:08:50 -0700508 queryAdapterTest2.json2AutocompletionSqlTest(ss,
509 testJson,
510 lastComponent,
511 nameField));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600512 }
513
Chengyu Fan31737f12016-01-12 21:08:50 -0700514 BOOST_AUTO_TEST_CASE(QueryAdapterDoFilterBasedSearchTest)
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600515 {
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600516 Json::Value testJson;
Chengyu Fan31737f12016-01-12 21:08:50 -0700517 testJson["activity"] = "testActivity";
518 testJson["product"] = "testProduct";
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600519
Chengyu Fan31737f12016-01-12 21:08:50 -0700520 std::vector<std::pair<std::string, std::string>> resultComponents;
521
522 BOOST_CHECK_EQUAL(true, queryAdapterTest1.testDoFilterBasedSearch(testJson, resultComponents));
523 BOOST_CHECK_EQUAL(2, resultComponents.size());
524 for (auto it=resultComponents.begin(); it != resultComponents.end(); it++) {
525 if (it->first == "activity")
526 BOOST_CHECK_EQUAL(it->second, "testActivity");
527 if (it->first == "product")
528 BOOST_CHECK_EQUAL(it->second, "testProduct");
529 }
530
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600531 testJson.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700532 resultComponents.clear();
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600533
Chengyu Fan31737f12016-01-12 21:08:50 -0700534 BOOST_CHECK_EQUAL(true, queryAdapterTest1.testDoFilterBasedSearch(testJson, resultComponents));
535 BOOST_CHECK_EQUAL(0, resultComponents.size());
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600536
Chengyu Fan31737f12016-01-12 21:08:50 -0700537 testJson.clear();
538 resultComponents.clear();
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600539
Chengyu Fan31737f12016-01-12 21:08:50 -0700540 testJson["name"] = Json::nullValue;
541 BOOST_CHECK_EQUAL(false, queryAdapterTest1.testDoFilterBasedSearch(testJson, resultComponents));
542
543 testJson.clear();
544 resultComponents.clear();
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600545 Json::Value param;
546 param[0] = "test";
Chengyu Fan31737f12016-01-12 21:08:50 -0700547 testJson["name"] = param;
548
549 BOOST_CHECK_EQUAL(false, queryAdapterTest1.testDoFilterBasedSearch(testJson, resultComponents));
550
551 testJson.clear();
552 resultComponents.clear();
553 Json::Value tmp;
554 tmp[0] = "test";
555
556 BOOST_CHECK_EQUAL(false, queryAdapterTest1.testDoFilterBasedSearch(tmp, resultComponents));
557 }
558
559 BOOST_AUTO_TEST_CASE(QueryAdapterDoPrefixBasedSearchTest)
560 {
561 Json::Value testJson;
562 testJson["??"] = "/";
563
564 std::vector<std::pair<std::string, std::string>> resultComponents;
565
566 BOOST_CHECK_EQUAL(true, queryAdapterTest2.testDoPrefixBasedSearch(testJson, resultComponents));
567 BOOST_CHECK_EQUAL(0, resultComponents.size());
568
569 testJson.clear();
570 resultComponents.clear();
571
572 testJson["??"] = "/Activity/Product";
573 BOOST_CHECK_EQUAL(true, queryAdapterTest2.testDoPrefixBasedSearch(testJson, resultComponents));
574 BOOST_CHECK_EQUAL(2, resultComponents.size());
575
576 for (auto it=resultComponents.begin(); it != resultComponents.end(); it++) {
577 if (it->first == "activity")
578 BOOST_CHECK_EQUAL(it->second, "Activity");
579 if (it->first == "product")
580 BOOST_CHECK_EQUAL(it->second, "Product");
581 }
582
583 testJson.clear();
584 resultComponents.clear();
585
586 testJson["??"] = "";
587 BOOST_CHECK_EQUAL(false, queryAdapterTest2.testDoPrefixBasedSearch(testJson, resultComponents));
588
589 testJson.clear();
590 resultComponents.clear();
591
592 testJson = Json::Value(Json::arrayValue);
593 BOOST_CHECK_EQUAL(false, queryAdapterTest2.testDoPrefixBasedSearch(testJson, resultComponents));
594
595 testJson.clear();
596 resultComponents.clear();
597
598 Json::Value tmp;
599 tmp[0] = "test";
600 BOOST_CHECK_EQUAL(false, queryAdapterTest2.testDoPrefixBasedSearch(tmp, resultComponents));
601
602 testJson.clear();
603 resultComponents.clear();
604
605 Json::Value param, testJson2;
606 param[0] = "test";
607 testJson2["activity"] = param;
608 BOOST_CHECK_EQUAL(false, queryAdapterTest2.testDoPrefixBasedSearch(testJson2, resultComponents));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600609 }
610
Chengyu Fanb25835b2015-04-28 17:09:35 -0600611 BOOST_AUTO_TEST_SUITE_END()
612
613}//tests
614}//atmos