blob: 8b210b75104b3b5b70b5efa81a2f3b7deec5c330 [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,
38 const std::shared_ptr<ndn::KeyChain>& keyChain)
39 : query::QueryAdapter<std::string>(face, keyChain)
40 {
41 }
42
43 virtual
44 ~QueryAdapterTest()
45 {
46 }
47
Chengyu Fanf4c747a2015-08-18 13:56:01 -060048 void setDatabaseTable(const std::string& databaseTable)
49 {
50 m_databaseTable.assign(databaseTable);
51 }
52
Chengyu Fan92440162015-07-09 14:43:31 -060053 void setNameFields(const std::vector<std::string>& nameFields)
54 {
55 m_nameFields = nameFields;
56 }
57
Chengyu Fanb25835b2015-04-28 17:09:35 -060058 void setPrefix(const ndn::Name& prefix)
59 {
60 m_prefix = prefix;
61 }
62
63 void setSigningId(const ndn::Name& signingId)
64 {
65 m_signingId = signingId;
66 }
67
68 const ndn::Name
69 getPrefix()
70 {
71 return m_prefix;
72 }
73
74 const ndn::Name
75 getSigningId()
76 {
77 return m_signingId;
78 }
79
80 std::shared_ptr<ndn::Data>
81 getAckData(std::shared_ptr<const ndn::Interest> interest, const ndn::Name::Component& version)
82 {
83 return makeAckData(interest, version);
84 }
85
Chengyu Fan92440162015-07-09 14:43:31 -060086 bool
87 json2SqlTest(std::stringstream& ss,
88 Json::Value& parsedFromString)
Chengyu Fanb25835b2015-04-28 17:09:35 -060089 {
Chengyu Fan92440162015-07-09 14:43:31 -060090 return json2Sql(ss, parsedFromString);
Chengyu Fanb25835b2015-04-28 17:09:35 -060091 }
92
93 std::shared_ptr<ndn::Data>
94 getReplyData(const ndn::Name& segmentPrefix,
95 const Json::Value& value,
96 uint64_t segmentNo,
97 bool isFinalBlock,
Chengyu Fan92440162015-07-09 14:43:31 -060098 bool isAutocomplete,
Chengyu Fanf4c747a2015-08-18 13:56:01 -060099 uint64_t resultCount,
100 uint64_t viewStart,
101 uint64_t viewEnd)
Chengyu Fanb25835b2015-04-28 17:09:35 -0600102 {
Chengyu Fan92440162015-07-09 14:43:31 -0600103 return makeReplyData(segmentPrefix, value, segmentNo, isFinalBlock,
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600104 isAutocomplete, resultCount, viewStart, viewEnd, false);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600105 }
106
107 void
108 queryTest(std::shared_ptr<const ndn::Interest> interest)
109 {
110 runJsonQuery(interest);
111 }
112
113 void
114 prepareSegments(const ndn::Name& segmentPrefix,
115 const std::string& sqlString,
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600116 bool autocomplete,
117 bool lastComponent)
Chengyu Fanb25835b2015-04-28 17:09:35 -0600118 {
119 BOOST_CHECK_EQUAL(sqlString, "SELECT name FROM cmip5 WHERE name=\'test\';");
120 Json::Value fileList;
121 fileList.append("/ndn/test1");
122 fileList.append("/ndn/test2");
123 fileList.append("/ndn/test3");
124
125 std::shared_ptr<ndn::Data> data = makeReplyData(segmentPrefix,
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600126 fileList, 0, true, false,
127 3, 0, 2, lastComponent);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600128 m_mutex.lock();
129 m_cache.insert(*data);
130 m_mutex.unlock();
131 }
132
133 std::shared_ptr<const ndn::Data>
134 getDataFromActiveQuery(const std::string& jsonQuery)
135 {
136 m_mutex.lock();
137 if (m_activeQueryToFirstResponse.find(jsonQuery) != m_activeQueryToFirstResponse.end()) {
138 auto iter = m_activeQueryToFirstResponse.find(jsonQuery);
139 if (iter != m_activeQueryToFirstResponse.end()) {
140 m_mutex.unlock();
141 return iter->second;
142 }
143 }
144 m_mutex.unlock();
145 return std::shared_ptr<const ndn::Data>();
146 }
147
148 std::shared_ptr<const ndn::Data>
149 getDataFromCache(const ndn::Interest& interest)
150 {
151 return m_cache.find(interest);
152 }
153
154 void
155 configAdapter(const util::ConfigSection& section,
156 const ndn::Name& prefix)
157 {
158 onConfig(section, false, std::string("test.txt"), prefix);
159 }
Chengyu Fan92440162015-07-09 14:43:31 -0600160
161 bool
162 json2AutocompletionSqlTest(std::stringstream& sqlQuery,
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600163 Json::Value& jsonValue,
164 bool& lastComponent)
Chengyu Fan92440162015-07-09 14:43:31 -0600165 {
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600166 return json2AutocompletionSql(sqlQuery, jsonValue, lastComponent);
Chengyu Fan92440162015-07-09 14:43:31 -0600167 }
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600168
169 bool
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600170 json2PrefixBasedSearchSqlTest(std::stringstream& sqlQuery,
171 Json::Value& jsonValue)
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600172 {
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600173 return json2PrefixBasedSearchSql(sqlQuery, jsonValue);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600174 }
Chengyu Fanb25835b2015-04-28 17:09:35 -0600175 };
176
177 class QueryAdapterFixture : public UnitTestTimeFixture
178 {
179 public:
180 QueryAdapterFixture()
181 : face(makeDummyClientFace(io))
182 , keyChain(new ndn::KeyChain())
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600183 , databaseTable("cmip5")
Chengyu Fanb25835b2015-04-28 17:09:35 -0600184 , queryAdapterTest1(face, keyChain)
185 , queryAdapterTest2(face, keyChain)
186 {
Chengyu Fan92440162015-07-09 14:43:31 -0600187 std::string c1("activity"), c2("product"), c3("organization"), c4("model");
188 std::string c5("experiment"), c6("frequency"), c7("modeling_realm"), c8("variable_name");
189 std::string c9("ensemble"), c10("time");
190 nameFields.push_back(c1);
191 nameFields.push_back(c2);
192 nameFields.push_back(c3);
193 nameFields.push_back(c4);
194 nameFields.push_back(c5);
195 nameFields.push_back(c6);
196 nameFields.push_back(c7);
197 nameFields.push_back(c8);
198 nameFields.push_back(c9);
199 nameFields.push_back(c10);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600200
201 queryAdapterTest1.setDatabaseTable(databaseTable);
202 queryAdapterTest1.setNameFields(nameFields);
203 queryAdapterTest2.setDatabaseTable(databaseTable);
204 queryAdapterTest2.setNameFields(nameFields);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600205 }
206
207 virtual
208 ~QueryAdapterFixture()
209 {
210 }
211
212 protected:
213 void
214 initializeQueryAdapterTest1()
215 {
216 util::ConfigSection section;
217 try {
218 std::stringstream ss;
219 ss << "signingId /test/signingId\
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600220 filterCategoryNames activity,product,organization,model,experiment,frequency,modeling_realm,variable_name,ensemble\
Chengyu Fanb25835b2015-04-28 17:09:35 -0600221 database \
222 { \
223 dbServer localhost \
224 dbName testdb \
225 dbUser testuser \
226 dbPasswd testpwd \
227 }";
228 boost::property_tree::read_info(ss, section);
229 }
230 catch (boost::property_tree::info_parser_error &e) {
231 std::cout << "Failed to read config file " << e.what() << std::endl;;
232 }
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600233
Chengyu Fanb25835b2015-04-28 17:09:35 -0600234 queryAdapterTest1.configAdapter(section, ndn::Name("/test"));
235 }
236
237 void
238 initializeQueryAdapterTest2()
239 {
240 util::ConfigSection section;
241 try {
242 std::stringstream ss;
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600243 ss << "\
244 filterCategoryNames activity,product,organization,model,experiment,frequency,modeling_realm,variable_name,ensemble\
245 database\
Chengyu Fanb25835b2015-04-28 17:09:35 -0600246 { \
247 dbServer localhost \
248 dbName testdb \
249 dbUser testuser \
250 dbPasswd testpwd \
251 }";
252 boost::property_tree::read_info(ss, section);
253 }
254 catch (boost::property_tree::info_parser_error &e) {
255 std::cout << "Failed to read config file " << e.what() << std::endl;;
256 }
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600257
Chengyu Fanb25835b2015-04-28 17:09:35 -0600258 queryAdapterTest2.configAdapter(section, ndn::Name("/test"));
259 }
260
261 protected:
262 std::shared_ptr<DummyClientFace> face;
263 std::shared_ptr<ndn::KeyChain> keyChain;
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600264 std::string databaseTable;
265 std::vector<std::string> nameFields;
Chengyu Fanb25835b2015-04-28 17:09:35 -0600266 QueryAdapterTest queryAdapterTest1;
267 QueryAdapterTest queryAdapterTest2;
268 };
269
270 BOOST_FIXTURE_TEST_SUITE(QueryAdapterTestSuite, QueryAdapterFixture)
271
272 BOOST_AUTO_TEST_CASE(BasicQueryAdapterTest1)
273 {
274 BOOST_CHECK(queryAdapterTest1.getPrefix() == ndn::Name());
275 BOOST_CHECK(queryAdapterTest1.getSigningId() == ndn::Name());
276 }
277
278 BOOST_AUTO_TEST_CASE(BasicQueryAdapterTest2)
279 {
280 initializeQueryAdapterTest1();
281 BOOST_CHECK(queryAdapterTest1.getPrefix() == ndn::Name("/test"));
282 BOOST_CHECK(queryAdapterTest1.getSigningId() == ndn::Name("/test/signingId"));
283 }
284
285 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseNormalTest)
286 {
287 Json::Value testJson;
288 testJson["name"] = "test";
289 testJson["activity"] = "testActivity";
290 testJson["product"] = "testProduct";
291
Chengyu Fan92440162015-07-09 14:43:31 -0600292 std::stringstream ss;
293 BOOST_CHECK_EQUAL(true, queryAdapterTest1.json2SqlTest(ss, testJson));
294 BOOST_CHECK_EQUAL(ss.str(), "SELECT name FROM cmip5 WHERE\
Chengyu Fanb25835b2015-04-28 17:09:35 -0600295 activity=\'testActivity\' AND name='test\' AND product=\'testProduct\';");
Chengyu Fanb25835b2015-04-28 17:09:35 -0600296 }
297
298 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseEmptyTest)
299 {
300 Json::Value testJson;
301
Chengyu Fan92440162015-07-09 14:43:31 -0600302 std::stringstream ss;
303 BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson));
Chengyu Fanb25835b2015-04-28 17:09:35 -0600304 }
305
306 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseAllItemsTest)
307 {
308 Json::Value testJson;
309 testJson["name"] = "test";
310 testJson["activity"] = "testActivity";
311 testJson["product"] = "testProduct";
312 testJson["origanization"] = "testOrg";
313 testJson["model"] = "testModel";
314 testJson["experiment"] = "testExperiment";
315 testJson["frequency"] = "testFrenquency";
316 testJson["modeling realm"] = "testModeling";
317 testJson["variable name"] = "testVarName";
318 testJson["ensemble member"] = "testEnsembleMember";
319 testJson["ensemble"] = "testEnsemble";
320 testJson["sample granularity"] = "testSampleGranularity";
321 testJson["start time"] = "testStartTime";
322 testJson["field campaign"] = "testFieldCampaign";
323 testJson["optical properties for radiation"] = "testOptProperties";
324 testJson["grid resolution"] = "testGridResolution";
325 testJson["output type"] = "testOutputType";
326 testJson["timestamp"] = "testTimestamp";
327
Chengyu Fan92440162015-07-09 14:43:31 -0600328 std::stringstream ss;
329 BOOST_CHECK_EQUAL(true, queryAdapterTest1.json2SqlTest(ss, testJson));
330 BOOST_CHECK_EQUAL(ss.str(), "SELECT name FROM cmip5 WHERE activity=\'testActivity\' AND \
Chengyu Fanb25835b2015-04-28 17:09:35 -0600331ensemble=\'testEnsemble\' AND ensemble member=\'testEnsembleMember\' AND \
332experiment=\'testExperiment\' AND field campaign=\'testFieldCampaign\' AND \
333frequency=\'testFrenquency\' AND grid resolution=\'testGridResolution\' AND \
334model=\'testModel\' AND modeling realm=\'testModeling\' AND name=\'test\' AND \
335optical properties for radiation=\'testOptProperties\' AND origanization=\'testOrg\' AND \
336output type=\'testOutputType\' AND product=\'testProduct\' AND sample \
337granularity=\'testSampleGranularity\' AND start time=\'testStartTime\' AND \
338timestamp=\'testTimestamp\' AND variable name=\'testVarName\';");
Chengyu Fanb25835b2015-04-28 17:09:35 -0600339 }
340
341 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseSearchTest)
342 {
Chengyu Fan92440162015-07-09 14:43:31 -0600343 // incorrect autocompletion is ok for sql conversion
Chengyu Fanb25835b2015-04-28 17:09:35 -0600344 Json::Value testJson;
345 testJson["name"] = "test";
346 testJson["?"] = "serchTest";
347
Chengyu Fan92440162015-07-09 14:43:31 -0600348 std::stringstream ss;
349 BOOST_CHECK_EQUAL(true, queryAdapterTest1.json2SqlTest(ss, testJson));
350 BOOST_CHECK_EQUAL(ss.str(),
351 "SELECT name FROM cmip5 WHERE name=\'test\';");
352 }
353
354 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest1)
355 {
356 Json::Value testJson;
357 testJson["name"] = Json::nullValue;
358
359 std::stringstream ss;
360 BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson));
361 }
362
363 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest2)
364 {
365 Json::Value testJson;
366
367 std::stringstream ss;
368 BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson));
369 }
370
371 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest3)
372 {
373 Json::Value testJson;
374 testJson = Json::Value(Json::arrayValue);
375
376 std::stringstream ss;
377 BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson));
378 }
379
380 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest4)
381 {
382 Json::Value testJson;
383 testJson[0] = "test";
384
385 std::stringstream ss;
386 BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson));
387 }
388
389 BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest5)
390 {
391 Json::Value testJson;
392 Json::Value param;
393 param[0] = "test";
394 testJson["name"] = param;
395
396 std::stringstream ss;
397 BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson));
Chengyu Fanb25835b2015-04-28 17:09:35 -0600398 }
399
400 BOOST_AUTO_TEST_CASE(QueryAdapterMakeAckDataTest)
401 {
402 ndn::Interest interest(ndn::Name("/test/ack/data/json"));
403 interest.setInterestLifetime(ndn::time::milliseconds(1000));
404 interest.setMustBeFresh(true);
405 std::shared_ptr<const ndn::Interest> interestPtr = std::make_shared<ndn::Interest>(interest);
406
407 const ndn::name::Component version
408 = ndn::name::Component::fromVersion(1);
409
410 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getAckData(interestPtr, version);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600411 BOOST_CHECK_EQUAL(data->getName().toUri(), "/test/ack/data/json");
412 BOOST_CHECK_EQUAL(std::string(reinterpret_cast<const char*>(data->getContent().value()),
413 data->getContent().value_size()),
414 "/query-results/catalogIdPlaceHolder/json/%FD%01");
Chengyu Fanb25835b2015-04-28 17:09:35 -0600415 }
416
417 BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest1)
418 {
419 Json::Value fileList;
420 fileList.append("/ndn/test1");
421 fileList.append("/ndn/test2");
422
423 const ndn::Name prefix("/atmos/test/prefix");
424
Chengyu Fan92440162015-07-09 14:43:31 -0600425 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList,
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600426 1, false, false, 2, 0, 1);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600427 BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%01");
428 BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component(""));
Chengyu Fan46398212015-08-11 11:23:13 -0600429 const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()),
430 data->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600431 Json::Value parsedFromString;
432 Json::Reader reader;
433 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600434 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 2);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600435 BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 2);
436 BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1");
437 BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2");
438 }
439
440 BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest2)
441 {
442 Json::Value fileList;
443 fileList.append("/ndn/test1");
444 const ndn::Name prefix("/atmos/test/prefix");
445
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600446 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList,
447 2, true, true, 1, 0, 0);
Chengyu Fan92440162015-07-09 14:43:31 -0600448
Chengyu Fanb25835b2015-04-28 17:09:35 -0600449 BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%02");
450 BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component::fromSegment(2));
Chengyu Fan46398212015-08-11 11:23:13 -0600451 const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()),
452 data->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600453 Json::Value parsedFromString;
454 Json::Reader reader;
455 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600456 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 1);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600457 BOOST_CHECK_EQUAL(parsedFromString["next"].size(), 1);
458 BOOST_CHECK_EQUAL(parsedFromString["next"][0], "/ndn/test1");
459 }
460
461 BOOST_AUTO_TEST_CASE(QueryAdapterQueryProcessTest)
462 {
463 initializeQueryAdapterTest2();
464 Json::Value query;
465 query["name"] = "test";
466 Json::FastWriter fastWriter;
467 std::string jsonMessage = fastWriter.write(query);
468 jsonMessage.erase(std::remove(jsonMessage.begin(), jsonMessage.end(), '\n'), jsonMessage.end());
469 std::shared_ptr<ndn::Interest> queryInterest
470 = std::make_shared<ndn::Interest>(ndn::Name("/test/query").append(jsonMessage.c_str()));
471
472 queryAdapterTest2.queryTest(queryInterest);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600473 // TODO: the code below should be enabled when queryAdapter can get the correct the
474 // ChronoSync state; currently, we don't need the activeQuery to save the ACK data;
475 //auto ackData = queryAdapterTest2.getDataFromActiveQuery(jsonMessage);
476 //
477 //BOOST_CHECK(ackData);
478 //if (ackData) {
479 // BOOST_CHECK_EQUAL(ackData->getName().getPrefix(3),
480 // ndn::Name("/test/query/%7B%22name%22%3A%22test%22%7D"));
481 // BOOST_CHECK_EQUAL(ackData->getName().at(ackData->getName().size() - 1),
482 // ndn::Name::Component("OK"));
483 // BOOST_CHECK_EQUAL(ackData->getContent().value_size(), 0);
484 //}
Chengyu Fanb25835b2015-04-28 17:09:35 -0600485
486 std::shared_ptr<ndn::Interest> resultInterest
487 = std::make_shared<ndn::Interest>(ndn::Name("/test/query-results"));
488 auto replyData = queryAdapterTest2.getDataFromCache(*resultInterest);
489 BOOST_CHECK(replyData);
490 if (replyData){
491 BOOST_CHECK_EQUAL(replyData->getName().getPrefix(2), ndn::Name("/test/query-results"));
Chengyu Fan46398212015-08-11 11:23:13 -0600492 const std::string jsonRes(reinterpret_cast<const char*>(replyData->getContent().value()),
493 replyData->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600494 Json::Value parsedFromString;
495 Json::Reader reader;
496 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600497 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 3);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600498 BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 3);
499 BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1");
500 BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2");
501 BOOST_CHECK_EQUAL(parsedFromString["results"][2], "/ndn/test3");
502 }
503 }
504
Chengyu Fan92440162015-07-09 14:43:31 -0600505 BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlSuccessTest)
506 {
507 initializeQueryAdapterTest2();
508
509 std::stringstream ss;
510 Json::Value testJson;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600511 bool lastComponent = false;
Chengyu Fan92440162015-07-09 14:43:31 -0600512 testJson["?"] = "/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600513 BOOST_CHECK_EQUAL(true,
514 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
515 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan46398212015-08-11 11:23:13 -0600516 BOOST_CHECK_EQUAL("SELECT DISTINCT activity FROM cmip5;", ss.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600517
518 ss.str("");
519 ss.clear();
520 testJson.clear();
521 testJson["?"] = "/Activity/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600522 BOOST_CHECK_EQUAL(true,
523 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
524 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan46398212015-08-11 11:23:13 -0600525 BOOST_CHECK_EQUAL("SELECT DISTINCT product FROM cmip5 WHERE activity='Activity';", ss.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600526
527 ss.str("");
528 ss.clear();
529 testJson.clear();
530 testJson["?"] = "/Activity/Product/Organization/Model/Experiment/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600531 BOOST_CHECK_EQUAL(true,
532 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
533 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan46398212015-08-11 11:23:13 -0600534 BOOST_CHECK_EQUAL("SELECT DISTINCT frequency FROM cmip5 WHERE activity='Activity' AND \
535experiment='Experiment' AND model='Model' AND organization='Organization' AND product='Product';",
536 ss.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600537
538 ss.str("");
539 ss.clear();
540 testJson.clear();
541 testJson["?"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\
542Variable/Ensemble/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600543 BOOST_CHECK_EQUAL(true,
544 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
545 BOOST_CHECK_EQUAL(lastComponent, true);
Chengyu Fan46398212015-08-11 11:23:13 -0600546 BOOST_CHECK_EQUAL("SELECT DISTINCT time FROM cmip5 WHERE activity='Activity' AND ensemble=\
547'Ensemble' AND experiment='Experiment' AND frequency='Frequency' AND model='Model' AND \
548modeling_realm='Modeling' AND organization='Organization' AND product='Product' AND variable_name=\
549'Variable';",ss.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600550 }
551
552 BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlFailTest)
553 {
554 initializeQueryAdapterTest2();
555
556 std::stringstream ss;
557 Json::Value testJson;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600558 bool lastComponent = false;
Chengyu Fan92440162015-07-09 14:43:31 -0600559 testJson["?"] = "serchTest";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600560 BOOST_CHECK_EQUAL(false,
561 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600562
563 ss.str("");
564 ss.clear();
565 testJson.clear();
566 testJson["?"] = "/cmip5";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600567 BOOST_CHECK_EQUAL(false,
568 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600569
570 ss.str("");
571 ss.clear();
572 Json::Value testJson2; //simply clear does not work
573 testJson2[0] = "test";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600574 BOOST_CHECK_EQUAL(false,
575 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson2, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600576
577 ss.str("");
578 ss.clear();
579 Json::Value testJson3;
580 testJson3 = Json::Value(Json::arrayValue);
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600581 BOOST_CHECK_EQUAL(false,
582 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson3, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600583
584 ss.str("");
585 ss.clear();
586 Json::Value testJson4;
587 Json::Value param;
588 param[0] = "test";
589 testJson4["name"] = param;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600590 BOOST_CHECK_EQUAL(false,
591 queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson4, lastComponent));
Chengyu Fan92440162015-07-09 14:43:31 -0600592}
593
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600594 BOOST_AUTO_TEST_CASE(QueryAdapterPrefixBasedSearchSuccessTest)
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600595 {
596 initializeQueryAdapterTest2();
597
598 std::stringstream ss;
599 Json::Value testJson;
600 testJson["??"] = "/";
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600601 BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600602 BOOST_CHECK_EQUAL("SELECT name FROM cmip5;", ss.str());
603
604 ss.str("");
605 ss.clear();
606 testJson.clear();
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600607 testJson["??"] = "/Activity/Product";
608 BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson));
609 BOOST_CHECK_EQUAL("SELECT name FROM cmip5 WHERE activity='Activity' AND product='Product';",
610 ss.str());
611
612 ss.str("");
613 ss.clear();
614 testJson.clear();
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600615 testJson["??"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600616Variable/Ensemble/Time/";
617
618 BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson));
619
620 BOOST_CHECK_EQUAL("SELECT name FROM cmip5 WHERE activity='Activity' AND product='Product' \
621AND organization='Organization' AND model='Model' AND experiment='Experiment' AND frequency=\
622'Frequency' AND modeling_realm='Modeling' AND variable_name='Variable' AND ensemble='Ensemble'\
623 AND time='Time';", ss.str());
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600624 }
625
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600626 BOOST_AUTO_TEST_CASE(QueryAdapterPrefixBasedSearchFailureTest)
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600627 {
628 initializeQueryAdapterTest2();
629
630 std::stringstream ss;
631 Json::Value testJson;
632
633 ss.str("");
634 ss.clear();
635 testJson.clear();
636 testJson["??"] = "";
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600637 BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600638
639 ss.str("");
640 ss.clear();
641 Json::Value testJson2; //simply clear does not work
642 testJson2[0] = "test"; // incorrect json object
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600643 BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson2));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600644
645 ss.str("");
646 ss.clear();
647 Json::Value testJson3;
648 testJson3 = Json::Value(Json::arrayValue); // incorrect json object
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600649 BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson3));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600650
651 ss.str("");
652 ss.clear();
653 Json::Value testJson4;
654 Json::Value param;
655 param[0] = "test";
656 testJson4["name"] = param; // incorrect json object
Chengyu Fan4d5fbd22015-09-18 14:34:08 -0600657 BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson4));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600658 }
659
Chengyu Fanb25835b2015-04-28 17:09:35 -0600660 BOOST_AUTO_TEST_SUITE_END()
661
662}//tests
663}//atmos