blob: 88c8ea2b3918d27d11e4dbb6492a01a5ea2ac492 [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
Chengyu Fan7b978f82015-12-09 17:03:23 -0700387 // use real data instead of ack data
Chengyu Fanb25835b2015-04-28 17:09:35 -0600388 BOOST_AUTO_TEST_CASE(QueryAdapterMakeAckDataTest)
389 {
390 ndn::Interest interest(ndn::Name("/test/ack/data/json"));
391 interest.setInterestLifetime(ndn::time::milliseconds(1000));
392 interest.setMustBeFresh(true);
393 std::shared_ptr<const ndn::Interest> interestPtr = std::make_shared<ndn::Interest>(interest);
394
395 const ndn::name::Component version
396 = ndn::name::Component::fromVersion(1);
397
398 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getAckData(interestPtr, version);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600399 BOOST_CHECK_EQUAL(data->getName().toUri(), "/test/ack/data/json");
Chengyu Fanb25835b2015-04-28 17:09:35 -0600400 }
401
402 BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest1)
403 {
404 Json::Value fileList;
405 fileList.append("/ndn/test1");
406 fileList.append("/ndn/test2");
407
408 const ndn::Name prefix("/atmos/test/prefix");
409
Chengyu Fan92440162015-07-09 14:43:31 -0600410 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList,
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600411 1, false, false, 2, 0, 1);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600412 BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%01");
413 BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component(""));
Chengyu Fan46398212015-08-11 11:23:13 -0600414 const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()),
415 data->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600416 Json::Value parsedFromString;
417 Json::Reader reader;
418 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600419 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 2);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600420 BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 2);
421 BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1");
422 BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2");
423 }
424
425 BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest2)
426 {
427 Json::Value fileList;
428 fileList.append("/ndn/test1");
429 const ndn::Name prefix("/atmos/test/prefix");
430
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600431 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList,
432 2, true, true, 1, 0, 0);
Chengyu Fan92440162015-07-09 14:43:31 -0600433
Chengyu Fanb25835b2015-04-28 17:09:35 -0600434 BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%02");
435 BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component::fromSegment(2));
Chengyu Fan46398212015-08-11 11:23:13 -0600436 const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()),
437 data->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600438 Json::Value parsedFromString;
439 Json::Reader reader;
440 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600441 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 1);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600442 BOOST_CHECK_EQUAL(parsedFromString["next"].size(), 1);
443 BOOST_CHECK_EQUAL(parsedFromString["next"][0], "/ndn/test1");
444 }
445
446 BOOST_AUTO_TEST_CASE(QueryAdapterQueryProcessTest)
447 {
448 initializeQueryAdapterTest2();
449 Json::Value query;
450 query["name"] = "test";
451 Json::FastWriter fastWriter;
452 std::string jsonMessage = fastWriter.write(query);
453 jsonMessage.erase(std::remove(jsonMessage.begin(), jsonMessage.end(), '\n'), jsonMessage.end());
454 std::shared_ptr<ndn::Interest> queryInterest
455 = std::make_shared<ndn::Interest>(ndn::Name("/test/query").append(jsonMessage.c_str()));
456
457 queryAdapterTest2.queryTest(queryInterest);
Chengyu Fan7b978f82015-12-09 17:03:23 -0700458
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600459 // TODO: the code below should be enabled when queryAdapter can get the correct the
460 // ChronoSync state; currently, we don't need the activeQuery to save the ACK data;
461 //auto ackData = queryAdapterTest2.getDataFromActiveQuery(jsonMessage);
462 //
463 //BOOST_CHECK(ackData);
464 //if (ackData) {
465 // BOOST_CHECK_EQUAL(ackData->getName().getPrefix(3),
466 // ndn::Name("/test/query/%7B%22name%22%3A%22test%22%7D"));
467 // BOOST_CHECK_EQUAL(ackData->getName().at(ackData->getName().size() - 1),
468 // ndn::Name::Component("OK"));
469 // BOOST_CHECK_EQUAL(ackData->getContent().value_size(), 0);
470 //}
Chengyu Fanb25835b2015-04-28 17:09:35 -0600471
Chengyu Fan7b978f82015-12-09 17:03:23 -0700472 // there is no query-results namespace data
473 auto replyData = queryAdapterTest2.getDataFromCache(*queryInterest);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600474 BOOST_CHECK(replyData);
475 if (replyData){
Chengyu Fan7b978f82015-12-09 17:03:23 -0700476 BOOST_CHECK_EQUAL(replyData->getName().getPrefix(2), ndn::Name("/test/query"));
Chengyu Fan46398212015-08-11 11:23:13 -0600477 const std::string jsonRes(reinterpret_cast<const char*>(replyData->getContent().value()),
478 replyData->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600479 Json::Value parsedFromString;
480 Json::Reader reader;
481 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600482 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 3);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600483 BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 3);
484 BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1");
485 BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2");
486 BOOST_CHECK_EQUAL(parsedFromString["results"][2], "/ndn/test3");
487 }
488 }
489
Chengyu Fan92440162015-07-09 14:43:31 -0600490 BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlSuccessTest)
491 {
492 initializeQueryAdapterTest2();
493
494 std::stringstream ss;
495 Json::Value testJson;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600496 bool lastComponent = false;
Chengyu Fan92440162015-07-09 14:43:31 -0600497 testJson["?"] = "/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600498 BOOST_CHECK_EQUAL(true,
499 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
500 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan46398212015-08-11 11:23:13 -0600501 BOOST_CHECK_EQUAL("SELECT DISTINCT activity FROM cmip5;", ss.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600502
503 ss.str("");
504 ss.clear();
505 testJson.clear();
506 testJson["?"] = "/Activity/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600507 BOOST_CHECK_EQUAL(true,
508 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
509 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan46398212015-08-11 11:23:13 -0600510 BOOST_CHECK_EQUAL("SELECT DISTINCT product FROM cmip5 WHERE activity='Activity';", ss.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600511
512 ss.str("");
513 ss.clear();
514 testJson.clear();
515 testJson["?"] = "/Activity/Product/Organization/Model/Experiment/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600516 BOOST_CHECK_EQUAL(true,
517 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
518 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan46398212015-08-11 11:23:13 -0600519 BOOST_CHECK_EQUAL("SELECT DISTINCT frequency FROM cmip5 WHERE activity='Activity' AND \
520experiment='Experiment' AND model='Model' AND organization='Organization' AND product='Product';",
521 ss.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600522
523 ss.str("");
524 ss.clear();
525 testJson.clear();
526 testJson["?"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\
527Variable/Ensemble/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600528 BOOST_CHECK_EQUAL(true,
529 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
530 BOOST_CHECK_EQUAL(lastComponent, true);
Chengyu Fan46398212015-08-11 11:23:13 -0600531 BOOST_CHECK_EQUAL("SELECT DISTINCT time FROM cmip5 WHERE activity='Activity' AND ensemble=\
532'Ensemble' AND experiment='Experiment' AND frequency='Frequency' AND model='Model' AND \
533modeling_realm='Modeling' AND organization='Organization' AND product='Product' AND variable_name=\
534'Variable';",ss.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600535 }
536
537 BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlFailTest)
538 {
539 initializeQueryAdapterTest2();
540
541 std::stringstream ss;
542 Json::Value testJson;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600543 bool lastComponent = false;
Chengyu Fan92440162015-07-09 14:43:31 -0600544 testJson["?"] = "serchTest";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600545 BOOST_CHECK_EQUAL(false,
546 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600547
548 ss.str("");
549 ss.clear();
550 testJson.clear();
551 testJson["?"] = "/cmip5";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600552 BOOST_CHECK_EQUAL(false,
553 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600554
555 ss.str("");
556 ss.clear();
557 Json::Value testJson2; //simply clear does not work
558 testJson2[0] = "test";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600559 BOOST_CHECK_EQUAL(false,
560 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson2, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600561
562 ss.str("");
563 ss.clear();
564 Json::Value testJson3;
565 testJson3 = Json::Value(Json::arrayValue);
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600566 BOOST_CHECK_EQUAL(false,
567 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson3, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600568
569 ss.str("");
570 ss.clear();
571 Json::Value testJson4;
572 Json::Value param;
573 param[0] = "test";
574 testJson4["name"] = param;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600575 BOOST_CHECK_EQUAL(false,
576 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson4, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600577}
578
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600579 BOOST_AUTO_TEST_CASE(QueryAdapterPrefixBasedSearchSuccessTest)
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600580 {
581 initializeQueryAdapterTest2();
582
583 std::stringstream ss;
584 Json::Value testJson;
585 testJson["??"] = "/";
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600586 BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600587 BOOST_CHECK_EQUAL("SELECT name FROM cmip5;", ss.str());
588
589 ss.str("");
590 ss.clear();
591 testJson.clear();
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600592 testJson["??"] = "/Activity/Product";
593 BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson));
594 BOOST_CHECK_EQUAL("SELECT name FROM cmip5 WHERE activity='Activity' AND product='Product';",
595 ss.str());
596
597 ss.str("");
598 ss.clear();
599 testJson.clear();
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600600 testJson["??"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600601Variable/Ensemble/Time/";
602
603 BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson));
604
605 BOOST_CHECK_EQUAL("SELECT name FROM cmip5 WHERE activity='Activity' AND product='Product' \
606AND organization='Organization' AND model='Model' AND experiment='Experiment' AND frequency=\
607'Frequency' AND modeling_realm='Modeling' AND variable_name='Variable' AND ensemble='Ensemble'\
608 AND time='Time';", ss.str());
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600609 }
610
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600611 BOOST_AUTO_TEST_CASE(QueryAdapterPrefixBasedSearchFailureTest)
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600612 {
613 initializeQueryAdapterTest2();
614
615 std::stringstream ss;
616 Json::Value testJson;
617
618 ss.str("");
619 ss.clear();
620 testJson.clear();
621 testJson["??"] = "";
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600622 BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600623
624 ss.str("");
625 ss.clear();
626 Json::Value testJson2; //simply clear does not work
627 testJson2[0] = "test"; // incorrect json object
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600628 BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson2));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600629
630 ss.str("");
631 ss.clear();
632 Json::Value testJson3;
633 testJson3 = Json::Value(Json::arrayValue); // incorrect json object
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600634 BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson3));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600635
636 ss.str("");
637 ss.clear();
638 Json::Value testJson4;
639 Json::Value param;
640 param[0] = "test";
641 testJson4["name"] = param; // incorrect json object
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600642 BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson4));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600643 }
644
Chengyu Fanb25835b2015-04-28 17:09:35 -0600645 BOOST_AUTO_TEST_SUITE_END()
646
647}//tests
648}//atmos