blob: c9252d3213c4177b221272f905b82754cb0dbbd9 [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 Fan92440162015-07-09 14:43:31 -060087 bool
88 json2SqlTest(std::stringstream& ss,
89 Json::Value& parsedFromString)
Chengyu Fanb25835b2015-04-28 17:09:35 -060090 {
Chengyu Fan92440162015-07-09 14:43:31 -060091 return json2Sql(ss, parsedFromString);
Chengyu Fanb25835b2015-04-28 17:09:35 -060092 }
93
94 std::shared_ptr<ndn::Data>
95 getReplyData(const ndn::Name& segmentPrefix,
96 const Json::Value& value,
97 uint64_t segmentNo,
98 bool isFinalBlock,
Chengyu Fan92440162015-07-09 14:43:31 -060099 bool isAutocomplete,
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600100 uint64_t resultCount,
101 uint64_t viewStart,
102 uint64_t viewEnd)
Chengyu Fanb25835b2015-04-28 17:09:35 -0600103 {
Chengyu Fan92440162015-07-09 14:43:31 -0600104 return makeReplyData(segmentPrefix, value, segmentNo, isFinalBlock,
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600105 isAutocomplete, resultCount, viewStart, viewEnd, false);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600106 }
107
108 void
109 queryTest(std::shared_ptr<const ndn::Interest> interest)
110 {
111 runJsonQuery(interest);
112 }
113
114 void
115 prepareSegments(const ndn::Name& segmentPrefix,
116 const std::string& sqlString,
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600117 bool autocomplete,
118 bool lastComponent)
Chengyu Fanb25835b2015-04-28 17:09:35 -0600119 {
120 BOOST_CHECK_EQUAL(sqlString, "SELECT name FROM cmip5 WHERE name=\'test\';");
121 Json::Value fileList;
122 fileList.append("/ndn/test1");
123 fileList.append("/ndn/test2");
124 fileList.append("/ndn/test3");
125
126 std::shared_ptr<ndn::Data> data = makeReplyData(segmentPrefix,
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600127 fileList, 0, true, false,
128 3, 0, 2, lastComponent);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600129 m_mutex.lock();
130 m_cache.insert(*data);
131 m_mutex.unlock();
132 }
133
134 std::shared_ptr<const ndn::Data>
Chengyu Fanb25835b2015-04-28 17:09:35 -0600135 getDataFromCache(const ndn::Interest& interest)
136 {
137 return m_cache.find(interest);
138 }
139
140 void
141 configAdapter(const util::ConfigSection& section,
142 const ndn::Name& prefix)
143 {
144 onConfig(section, false, std::string("test.txt"), prefix);
145 }
Chengyu Fan92440162015-07-09 14:43:31 -0600146
147 bool
148 json2AutocompletionSqlTest(std::stringstream& sqlQuery,
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600149 Json::Value& jsonValue,
150 bool& lastComponent)
Chengyu Fan92440162015-07-09 14:43:31 -0600151 {
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600152 return json2AutocompletionSql(sqlQuery, jsonValue, lastComponent);
Chengyu Fan92440162015-07-09 14:43:31 -0600153 }
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600154
155 bool
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600156 json2PrefixBasedSearchSqlTest(std::stringstream& sqlQuery,
157 Json::Value& jsonValue)
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600158 {
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600159 return json2PrefixBasedSearchSql(sqlQuery, jsonValue);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600160 }
Chengyu Fanb25835b2015-04-28 17:09:35 -0600161 };
162
163 class QueryAdapterFixture : public UnitTestTimeFixture
164 {
165 public:
166 QueryAdapterFixture()
167 : face(makeDummyClientFace(io))
168 , keyChain(new ndn::KeyChain())
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600169 , databaseTable("cmip5")
Chengyu Fancfb80c72015-10-19 16:50:04 -0600170 , queryAdapterTest1(face, keyChain, syncSocket)
171 , queryAdapterTest2(face, keyChain, syncSocket)
Chengyu Fanb25835b2015-04-28 17:09:35 -0600172 {
Chengyu Fan92440162015-07-09 14:43:31 -0600173 std::string c1("activity"), c2("product"), c3("organization"), c4("model");
174 std::string c5("experiment"), c6("frequency"), c7("modeling_realm"), c8("variable_name");
175 std::string c9("ensemble"), c10("time");
176 nameFields.push_back(c1);
177 nameFields.push_back(c2);
178 nameFields.push_back(c3);
179 nameFields.push_back(c4);
180 nameFields.push_back(c5);
181 nameFields.push_back(c6);
182 nameFields.push_back(c7);
183 nameFields.push_back(c8);
184 nameFields.push_back(c9);
185 nameFields.push_back(c10);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600186
187 queryAdapterTest1.setDatabaseTable(databaseTable);
188 queryAdapterTest1.setNameFields(nameFields);
189 queryAdapterTest2.setDatabaseTable(databaseTable);
190 queryAdapterTest2.setNameFields(nameFields);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600191 }
192
193 virtual
194 ~QueryAdapterFixture()
195 {
196 }
197
198 protected:
199 void
200 initializeQueryAdapterTest1()
201 {
202 util::ConfigSection section;
203 try {
204 std::stringstream ss;
205 ss << "signingId /test/signingId\
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600206 filterCategoryNames activity,product,organization,model,experiment,frequency,modeling_realm,variable_name,ensemble\
Chengyu Fanb25835b2015-04-28 17:09:35 -0600207 database \
208 { \
209 dbServer localhost \
210 dbName testdb \
211 dbUser testuser \
212 dbPasswd testpwd \
213 }";
214 boost::property_tree::read_info(ss, section);
215 }
216 catch (boost::property_tree::info_parser_error &e) {
217 std::cout << "Failed to read config file " << e.what() << std::endl;;
218 }
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600219
Chengyu Fanb25835b2015-04-28 17:09:35 -0600220 queryAdapterTest1.configAdapter(section, ndn::Name("/test"));
221 }
222
223 void
224 initializeQueryAdapterTest2()
225 {
226 util::ConfigSection section;
227 try {
228 std::stringstream ss;
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600229 ss << "\
230 filterCategoryNames activity,product,organization,model,experiment,frequency,modeling_realm,variable_name,ensemble\
231 database\
Chengyu Fanb25835b2015-04-28 17:09:35 -0600232 { \
233 dbServer localhost \
234 dbName testdb \
235 dbUser testuser \
236 dbPasswd testpwd \
237 }";
238 boost::property_tree::read_info(ss, section);
239 }
240 catch (boost::property_tree::info_parser_error &e) {
241 std::cout << "Failed to read config file " << e.what() << std::endl;;
242 }
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600243
Chengyu Fanb25835b2015-04-28 17:09:35 -0600244 queryAdapterTest2.configAdapter(section, ndn::Name("/test"));
245 }
246
247 protected:
248 std::shared_ptr<DummyClientFace> face;
249 std::shared_ptr<ndn::KeyChain> keyChain;
Chengyu Fancfb80c72015-10-19 16:50:04 -0600250 std::shared_ptr<chronosync::Socket> syncSocket;
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600251 std::string databaseTable;
252 std::vector<std::string> nameFields;
Chengyu Fanb25835b2015-04-28 17:09:35 -0600253 QueryAdapterTest queryAdapterTest1;
254 QueryAdapterTest queryAdapterTest2;
255 };
256
257 BOOST_FIXTURE_TEST_SUITE(QueryAdapterTestSuite, QueryAdapterFixture)
258
259 BOOST_AUTO_TEST_CASE(BasicQueryAdapterTest1)
260 {
261 BOOST_CHECK(queryAdapterTest1.getPrefix() == ndn::Name());
262 BOOST_CHECK(queryAdapterTest1.getSigningId() == ndn::Name());
263 }
264
265 BOOST_AUTO_TEST_CASE(BasicQueryAdapterTest2)
266 {
267 initializeQueryAdapterTest1();
268 BOOST_CHECK(queryAdapterTest1.getPrefix() == ndn::Name("/test"));
269 BOOST_CHECK(queryAdapterTest1.getSigningId() == ndn::Name("/test/signingId"));
270 }
271
272 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseNormalTest)
273 {
274 Json::Value testJson;
275 testJson["name"] = "test";
276 testJson["activity"] = "testActivity";
277 testJson["product"] = "testProduct";
278
Chengyu Fan92440162015-07-09 14:43:31 -0600279 std::stringstream ss;
280 BOOST_CHECK_EQUAL(true, queryAdapterTest1.json2SqlTest(ss, testJson));
281 BOOST_CHECK_EQUAL(ss.str(), "SELECT name FROM cmip5 WHERE\
Chengyu Fanb25835b2015-04-28 17:09:35 -0600282 activity=\'testActivity\' AND name='test\' AND product=\'testProduct\';");
Chengyu Fanb25835b2015-04-28 17:09:35 -0600283 }
284
285 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseEmptyTest)
286 {
287 Json::Value testJson;
288
Chengyu Fan92440162015-07-09 14:43:31 -0600289 std::stringstream ss;
290 BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson));
Chengyu Fanb25835b2015-04-28 17:09:35 -0600291 }
292
293 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseAllItemsTest)
294 {
295 Json::Value testJson;
296 testJson["name"] = "test";
297 testJson["activity"] = "testActivity";
298 testJson["product"] = "testProduct";
299 testJson["origanization"] = "testOrg";
300 testJson["model"] = "testModel";
301 testJson["experiment"] = "testExperiment";
302 testJson["frequency"] = "testFrenquency";
303 testJson["modeling realm"] = "testModeling";
304 testJson["variable name"] = "testVarName";
305 testJson["ensemble member"] = "testEnsembleMember";
306 testJson["ensemble"] = "testEnsemble";
307 testJson["sample granularity"] = "testSampleGranularity";
308 testJson["start time"] = "testStartTime";
309 testJson["field campaign"] = "testFieldCampaign";
310 testJson["optical properties for radiation"] = "testOptProperties";
311 testJson["grid resolution"] = "testGridResolution";
312 testJson["output type"] = "testOutputType";
313 testJson["timestamp"] = "testTimestamp";
314
Chengyu Fan92440162015-07-09 14:43:31 -0600315 std::stringstream ss;
316 BOOST_CHECK_EQUAL(true, queryAdapterTest1.json2SqlTest(ss, testJson));
317 BOOST_CHECK_EQUAL(ss.str(), "SELECT name FROM cmip5 WHERE activity=\'testActivity\' AND \
Chengyu Fanb25835b2015-04-28 17:09:35 -0600318ensemble=\'testEnsemble\' AND ensemble member=\'testEnsembleMember\' AND \
319experiment=\'testExperiment\' AND field campaign=\'testFieldCampaign\' AND \
320frequency=\'testFrenquency\' AND grid resolution=\'testGridResolution\' AND \
321model=\'testModel\' AND modeling realm=\'testModeling\' AND name=\'test\' AND \
322optical properties for radiation=\'testOptProperties\' AND origanization=\'testOrg\' AND \
323output type=\'testOutputType\' AND product=\'testProduct\' AND sample \
324granularity=\'testSampleGranularity\' AND start time=\'testStartTime\' AND \
325timestamp=\'testTimestamp\' AND variable name=\'testVarName\';");
Chengyu Fanb25835b2015-04-28 17:09:35 -0600326 }
327
328 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseSearchTest)
329 {
Chengyu Fan92440162015-07-09 14:43:31 -0600330 // incorrect autocompletion is ok for sql conversion
Chengyu Fanb25835b2015-04-28 17:09:35 -0600331 Json::Value testJson;
332 testJson["name"] = "test";
333 testJson["?"] = "serchTest";
334
Chengyu Fan92440162015-07-09 14:43:31 -0600335 std::stringstream ss;
336 BOOST_CHECK_EQUAL(true, queryAdapterTest1.json2SqlTest(ss, testJson));
337 BOOST_CHECK_EQUAL(ss.str(),
338 "SELECT name FROM cmip5 WHERE name=\'test\';");
339 }
340
341 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest1)
342 {
343 Json::Value testJson;
344 testJson["name"] = Json::nullValue;
345
346 std::stringstream ss;
347 BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson));
348 }
349
350 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest2)
351 {
352 Json::Value testJson;
353
354 std::stringstream ss;
355 BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson));
356 }
357
358 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest3)
359 {
360 Json::Value testJson;
361 testJson = Json::Value(Json::arrayValue);
362
363 std::stringstream ss;
364 BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson));
365 }
366
367 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest4)
368 {
369 Json::Value testJson;
370 testJson[0] = "test";
371
372 std::stringstream ss;
373 BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson));
374 }
375
376 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest5)
377 {
378 Json::Value testJson;
379 Json::Value param;
380 param[0] = "test";
381 testJson["name"] = param;
382
383 std::stringstream ss;
384 BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson));
Chengyu Fanb25835b2015-04-28 17:09:35 -0600385 }
386
387 BOOST_AUTO_TEST_CASE(QueryAdapterMakeAckDataTest)
388 {
389 ndn::Interest interest(ndn::Name("/test/ack/data/json"));
390 interest.setInterestLifetime(ndn::time::milliseconds(1000));
391 interest.setMustBeFresh(true);
392 std::shared_ptr<const ndn::Interest> interestPtr = std::make_shared<ndn::Interest>(interest);
393
394 const ndn::name::Component version
395 = ndn::name::Component::fromVersion(1);
396
397 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getAckData(interestPtr, version);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600398 BOOST_CHECK_EQUAL(data->getName().toUri(), "/test/ack/data/json");
399 BOOST_CHECK_EQUAL(std::string(reinterpret_cast<const char*>(data->getContent().value()),
400 data->getContent().value_size()),
401 "/query-results/catalogIdPlaceHolder/json/%FD%01");
Chengyu Fanb25835b2015-04-28 17:09:35 -0600402 }
403
404 BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest1)
405 {
406 Json::Value fileList;
407 fileList.append("/ndn/test1");
408 fileList.append("/ndn/test2");
409
410 const ndn::Name prefix("/atmos/test/prefix");
411
Chengyu Fan92440162015-07-09 14:43:31 -0600412 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList,
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600413 1, false, false, 2, 0, 1);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600414 BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%01");
415 BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component(""));
Chengyu Fan46398212015-08-11 11:23:13 -0600416 const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()),
417 data->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600418 Json::Value parsedFromString;
419 Json::Reader reader;
420 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600421 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 2);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600422 BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 2);
423 BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1");
424 BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2");
425 }
426
427 BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest2)
428 {
429 Json::Value fileList;
430 fileList.append("/ndn/test1");
431 const ndn::Name prefix("/atmos/test/prefix");
432
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600433 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList,
434 2, true, true, 1, 0, 0);
Chengyu Fan92440162015-07-09 14:43:31 -0600435
Chengyu Fanb25835b2015-04-28 17:09:35 -0600436 BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%02");
437 BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component::fromSegment(2));
Chengyu Fan46398212015-08-11 11:23:13 -0600438 const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()),
439 data->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600440 Json::Value parsedFromString;
441 Json::Reader reader;
442 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600443 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 1);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600444 BOOST_CHECK_EQUAL(parsedFromString["next"].size(), 1);
445 BOOST_CHECK_EQUAL(parsedFromString["next"][0], "/ndn/test1");
446 }
447
448 BOOST_AUTO_TEST_CASE(QueryAdapterQueryProcessTest)
449 {
450 initializeQueryAdapterTest2();
451 Json::Value query;
452 query["name"] = "test";
453 Json::FastWriter fastWriter;
454 std::string jsonMessage = fastWriter.write(query);
455 jsonMessage.erase(std::remove(jsonMessage.begin(), jsonMessage.end(), '\n'), jsonMessage.end());
456 std::shared_ptr<ndn::Interest> queryInterest
457 = std::make_shared<ndn::Interest>(ndn::Name("/test/query").append(jsonMessage.c_str()));
458
459 queryAdapterTest2.queryTest(queryInterest);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600460 // TODO: the code below should be enabled when queryAdapter can get the correct the
461 // ChronoSync state; currently, we don't need the activeQuery to save the ACK data;
462 //auto ackData = queryAdapterTest2.getDataFromActiveQuery(jsonMessage);
463 //
464 //BOOST_CHECK(ackData);
465 //if (ackData) {
466 // BOOST_CHECK_EQUAL(ackData->getName().getPrefix(3),
467 // ndn::Name("/test/query/%7B%22name%22%3A%22test%22%7D"));
468 // BOOST_CHECK_EQUAL(ackData->getName().at(ackData->getName().size() - 1),
469 // ndn::Name::Component("OK"));
470 // BOOST_CHECK_EQUAL(ackData->getContent().value_size(), 0);
471 //}
Chengyu Fanb25835b2015-04-28 17:09:35 -0600472
473 std::shared_ptr<ndn::Interest> resultInterest
474 = std::make_shared<ndn::Interest>(ndn::Name("/test/query-results"));
475 auto replyData = queryAdapterTest2.getDataFromCache(*resultInterest);
476 BOOST_CHECK(replyData);
477 if (replyData){
478 BOOST_CHECK_EQUAL(replyData->getName().getPrefix(2), ndn::Name("/test/query-results"));
Chengyu Fan46398212015-08-11 11:23:13 -0600479 const std::string jsonRes(reinterpret_cast<const char*>(replyData->getContent().value()),
480 replyData->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600481 Json::Value parsedFromString;
482 Json::Reader reader;
483 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600484 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 3);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600485 BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 3);
486 BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1");
487 BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2");
488 BOOST_CHECK_EQUAL(parsedFromString["results"][2], "/ndn/test3");
489 }
490 }
491
Chengyu Fan92440162015-07-09 14:43:31 -0600492 BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlSuccessTest)
493 {
494 initializeQueryAdapterTest2();
495
496 std::stringstream ss;
497 Json::Value testJson;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600498 bool lastComponent = false;
Chengyu Fan92440162015-07-09 14:43:31 -0600499 testJson["?"] = "/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600500 BOOST_CHECK_EQUAL(true,
501 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
502 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan46398212015-08-11 11:23:13 -0600503 BOOST_CHECK_EQUAL("SELECT DISTINCT activity FROM cmip5;", ss.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600504
505 ss.str("");
506 ss.clear();
507 testJson.clear();
508 testJson["?"] = "/Activity/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600509 BOOST_CHECK_EQUAL(true,
510 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
511 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan46398212015-08-11 11:23:13 -0600512 BOOST_CHECK_EQUAL("SELECT DISTINCT product FROM cmip5 WHERE activity='Activity';", ss.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600513
514 ss.str("");
515 ss.clear();
516 testJson.clear();
517 testJson["?"] = "/Activity/Product/Organization/Model/Experiment/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600518 BOOST_CHECK_EQUAL(true,
519 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
520 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan46398212015-08-11 11:23:13 -0600521 BOOST_CHECK_EQUAL("SELECT DISTINCT frequency FROM cmip5 WHERE activity='Activity' AND \
522experiment='Experiment' AND model='Model' AND organization='Organization' AND product='Product';",
523 ss.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600524
525 ss.str("");
526 ss.clear();
527 testJson.clear();
528 testJson["?"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\
529Variable/Ensemble/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600530 BOOST_CHECK_EQUAL(true,
531 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
532 BOOST_CHECK_EQUAL(lastComponent, true);
Chengyu Fan46398212015-08-11 11:23:13 -0600533 BOOST_CHECK_EQUAL("SELECT DISTINCT time FROM cmip5 WHERE activity='Activity' AND ensemble=\
534'Ensemble' AND experiment='Experiment' AND frequency='Frequency' AND model='Model' AND \
535modeling_realm='Modeling' AND organization='Organization' AND product='Product' AND variable_name=\
536'Variable';",ss.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600537 }
538
539 BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlFailTest)
540 {
541 initializeQueryAdapterTest2();
542
543 std::stringstream ss;
544 Json::Value testJson;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600545 bool lastComponent = false;
Chengyu Fan92440162015-07-09 14:43:31 -0600546 testJson["?"] = "serchTest";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600547 BOOST_CHECK_EQUAL(false,
548 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600549
550 ss.str("");
551 ss.clear();
552 testJson.clear();
553 testJson["?"] = "/cmip5";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600554 BOOST_CHECK_EQUAL(false,
555 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600556
557 ss.str("");
558 ss.clear();
559 Json::Value testJson2; //simply clear does not work
560 testJson2[0] = "test";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600561 BOOST_CHECK_EQUAL(false,
562 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson2, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600563
564 ss.str("");
565 ss.clear();
566 Json::Value testJson3;
567 testJson3 = Json::Value(Json::arrayValue);
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600568 BOOST_CHECK_EQUAL(false,
569 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson3, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600570
571 ss.str("");
572 ss.clear();
573 Json::Value testJson4;
574 Json::Value param;
575 param[0] = "test";
576 testJson4["name"] = param;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600577 BOOST_CHECK_EQUAL(false,
578 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson4, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600579}
580
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600581 BOOST_AUTO_TEST_CASE(QueryAdapterPrefixBasedSearchSuccessTest)
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600582 {
583 initializeQueryAdapterTest2();
584
585 std::stringstream ss;
586 Json::Value testJson;
587 testJson["??"] = "/";
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600588 BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600589 BOOST_CHECK_EQUAL("SELECT name FROM cmip5;", ss.str());
590
591 ss.str("");
592 ss.clear();
593 testJson.clear();
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600594 testJson["??"] = "/Activity/Product";
595 BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson));
596 BOOST_CHECK_EQUAL("SELECT name FROM cmip5 WHERE activity='Activity' AND product='Product';",
597 ss.str());
598
599 ss.str("");
600 ss.clear();
601 testJson.clear();
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600602 testJson["??"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600603Variable/Ensemble/Time/";
604
605 BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson));
606
607 BOOST_CHECK_EQUAL("SELECT name FROM cmip5 WHERE activity='Activity' AND product='Product' \
608AND organization='Organization' AND model='Model' AND experiment='Experiment' AND frequency=\
609'Frequency' AND modeling_realm='Modeling' AND variable_name='Variable' AND ensemble='Ensemble'\
610 AND time='Time';", ss.str());
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600611 }
612
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600613 BOOST_AUTO_TEST_CASE(QueryAdapterPrefixBasedSearchFailureTest)
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600614 {
615 initializeQueryAdapterTest2();
616
617 std::stringstream ss;
618 Json::Value testJson;
619
620 ss.str("");
621 ss.clear();
622 testJson.clear();
623 testJson["??"] = "";
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600624 BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600625
626 ss.str("");
627 ss.clear();
628 Json::Value testJson2; //simply clear does not work
629 testJson2[0] = "test"; // incorrect json object
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600630 BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson2));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600631
632 ss.str("");
633 ss.clear();
634 Json::Value testJson3;
635 testJson3 = Json::Value(Json::arrayValue); // incorrect json object
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600636 BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson3));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600637
638 ss.str("");
639 ss.clear();
640 Json::Value testJson4;
641 Json::Value param;
642 param[0] = "test";
643 testJson4["name"] = param; // incorrect json object
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600644 BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson4));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600645 }
646
Chengyu Fanb25835b2015-04-28 17:09:35 -0600647 BOOST_AUTO_TEST_SUITE_END()
648
649}//tests
650}//atmos