Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 1 | /** 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 | |
| 29 | namespace atmos{ |
| 30 | namespace 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 Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 48 | void setDatabaseTable(const std::string& databaseTable) |
| 49 | { |
| 50 | m_databaseTable.assign(databaseTable); |
| 51 | } |
| 52 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 53 | void setNameFields(const std::vector<std::string>& nameFields) |
| 54 | { |
| 55 | m_nameFields = nameFields; |
| 56 | } |
| 57 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 58 | 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 Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 86 | bool |
| 87 | json2SqlTest(std::stringstream& ss, |
| 88 | Json::Value& parsedFromString) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 89 | { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 90 | return json2Sql(ss, parsedFromString); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 91 | } |
| 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 Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 98 | bool isAutocomplete, |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 99 | uint64_t resultCount, |
| 100 | uint64_t viewStart, |
| 101 | uint64_t viewEnd) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 102 | { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 103 | return makeReplyData(segmentPrefix, value, segmentNo, isFinalBlock, |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 104 | isAutocomplete, resultCount, viewStart, viewEnd); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 105 | } |
| 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, |
| 116 | bool autocomplete) |
| 117 | { |
| 118 | BOOST_CHECK_EQUAL(sqlString, "SELECT name FROM cmip5 WHERE name=\'test\';"); |
| 119 | Json::Value fileList; |
| 120 | fileList.append("/ndn/test1"); |
| 121 | fileList.append("/ndn/test2"); |
| 122 | fileList.append("/ndn/test3"); |
| 123 | |
| 124 | std::shared_ptr<ndn::Data> data = makeReplyData(segmentPrefix, |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 125 | fileList, 0, true, false, 3, 0, 2); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 126 | m_mutex.lock(); |
| 127 | m_cache.insert(*data); |
| 128 | m_mutex.unlock(); |
| 129 | } |
| 130 | |
| 131 | std::shared_ptr<const ndn::Data> |
| 132 | getDataFromActiveQuery(const std::string& jsonQuery) |
| 133 | { |
| 134 | m_mutex.lock(); |
| 135 | if (m_activeQueryToFirstResponse.find(jsonQuery) != m_activeQueryToFirstResponse.end()) { |
| 136 | auto iter = m_activeQueryToFirstResponse.find(jsonQuery); |
| 137 | if (iter != m_activeQueryToFirstResponse.end()) { |
| 138 | m_mutex.unlock(); |
| 139 | return iter->second; |
| 140 | } |
| 141 | } |
| 142 | m_mutex.unlock(); |
| 143 | return std::shared_ptr<const ndn::Data>(); |
| 144 | } |
| 145 | |
| 146 | std::shared_ptr<const ndn::Data> |
| 147 | getDataFromCache(const ndn::Interest& interest) |
| 148 | { |
| 149 | return m_cache.find(interest); |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | configAdapter(const util::ConfigSection& section, |
| 154 | const ndn::Name& prefix) |
| 155 | { |
| 156 | onConfig(section, false, std::string("test.txt"), prefix); |
| 157 | } |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 158 | |
| 159 | bool |
| 160 | json2AutocompletionSqlTest(std::stringstream& sqlQuery, |
| 161 | Json::Value& jsonValue) |
| 162 | { |
| 163 | return json2AutocompletionSql(sqlQuery, jsonValue); |
| 164 | } |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 165 | |
| 166 | bool |
| 167 | json2CompleteSearchSqlTest(std::stringstream& sqlQuery, |
| 168 | Json::Value& jsonValue) |
| 169 | { |
| 170 | return json2CompleteSearchSql(sqlQuery, jsonValue); |
| 171 | } |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | class QueryAdapterFixture : public UnitTestTimeFixture |
| 175 | { |
| 176 | public: |
| 177 | QueryAdapterFixture() |
| 178 | : face(makeDummyClientFace(io)) |
| 179 | , keyChain(new ndn::KeyChain()) |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 180 | , databaseTable("cmip5") |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 181 | , queryAdapterTest1(face, keyChain) |
| 182 | , queryAdapterTest2(face, keyChain) |
| 183 | { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 184 | std::string c1("activity"), c2("product"), c3("organization"), c4("model"); |
| 185 | std::string c5("experiment"), c6("frequency"), c7("modeling_realm"), c8("variable_name"); |
| 186 | std::string c9("ensemble"), c10("time"); |
| 187 | nameFields.push_back(c1); |
| 188 | nameFields.push_back(c2); |
| 189 | nameFields.push_back(c3); |
| 190 | nameFields.push_back(c4); |
| 191 | nameFields.push_back(c5); |
| 192 | nameFields.push_back(c6); |
| 193 | nameFields.push_back(c7); |
| 194 | nameFields.push_back(c8); |
| 195 | nameFields.push_back(c9); |
| 196 | nameFields.push_back(c10); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 197 | |
| 198 | queryAdapterTest1.setDatabaseTable(databaseTable); |
| 199 | queryAdapterTest1.setNameFields(nameFields); |
| 200 | queryAdapterTest2.setDatabaseTable(databaseTable); |
| 201 | queryAdapterTest2.setNameFields(nameFields); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | virtual |
| 205 | ~QueryAdapterFixture() |
| 206 | { |
| 207 | } |
| 208 | |
| 209 | protected: |
| 210 | void |
| 211 | initializeQueryAdapterTest1() |
| 212 | { |
| 213 | util::ConfigSection section; |
| 214 | try { |
| 215 | std::stringstream ss; |
| 216 | ss << "signingId /test/signingId\ |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 217 | filterCategoryNames activity,product,organization,model,experiment,frequency,modeling_realm,variable_name,ensemble\ |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 218 | database \ |
| 219 | { \ |
| 220 | dbServer localhost \ |
| 221 | dbName testdb \ |
| 222 | dbUser testuser \ |
| 223 | dbPasswd testpwd \ |
| 224 | }"; |
| 225 | boost::property_tree::read_info(ss, section); |
| 226 | } |
| 227 | catch (boost::property_tree::info_parser_error &e) { |
| 228 | std::cout << "Failed to read config file " << e.what() << std::endl;; |
| 229 | } |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 230 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 231 | queryAdapterTest1.configAdapter(section, ndn::Name("/test")); |
| 232 | } |
| 233 | |
| 234 | void |
| 235 | initializeQueryAdapterTest2() |
| 236 | { |
| 237 | util::ConfigSection section; |
| 238 | try { |
| 239 | std::stringstream ss; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 240 | ss << "\ |
| 241 | filterCategoryNames activity,product,organization,model,experiment,frequency,modeling_realm,variable_name,ensemble\ |
| 242 | database\ |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 243 | { \ |
| 244 | dbServer localhost \ |
| 245 | dbName testdb \ |
| 246 | dbUser testuser \ |
| 247 | dbPasswd testpwd \ |
| 248 | }"; |
| 249 | boost::property_tree::read_info(ss, section); |
| 250 | } |
| 251 | catch (boost::property_tree::info_parser_error &e) { |
| 252 | std::cout << "Failed to read config file " << e.what() << std::endl;; |
| 253 | } |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 254 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 255 | queryAdapterTest2.configAdapter(section, ndn::Name("/test")); |
| 256 | } |
| 257 | |
| 258 | protected: |
| 259 | std::shared_ptr<DummyClientFace> face; |
| 260 | std::shared_ptr<ndn::KeyChain> keyChain; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 261 | std::string databaseTable; |
| 262 | std::vector<std::string> nameFields; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 263 | QueryAdapterTest queryAdapterTest1; |
| 264 | QueryAdapterTest queryAdapterTest2; |
| 265 | }; |
| 266 | |
| 267 | BOOST_FIXTURE_TEST_SUITE(QueryAdapterTestSuite, QueryAdapterFixture) |
| 268 | |
| 269 | BOOST_AUTO_TEST_CASE(BasicQueryAdapterTest1) |
| 270 | { |
| 271 | BOOST_CHECK(queryAdapterTest1.getPrefix() == ndn::Name()); |
| 272 | BOOST_CHECK(queryAdapterTest1.getSigningId() == ndn::Name()); |
| 273 | } |
| 274 | |
| 275 | BOOST_AUTO_TEST_CASE(BasicQueryAdapterTest2) |
| 276 | { |
| 277 | initializeQueryAdapterTest1(); |
| 278 | BOOST_CHECK(queryAdapterTest1.getPrefix() == ndn::Name("/test")); |
| 279 | BOOST_CHECK(queryAdapterTest1.getSigningId() == ndn::Name("/test/signingId")); |
| 280 | } |
| 281 | |
| 282 | BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseNormalTest) |
| 283 | { |
| 284 | Json::Value testJson; |
| 285 | testJson["name"] = "test"; |
| 286 | testJson["activity"] = "testActivity"; |
| 287 | testJson["product"] = "testProduct"; |
| 288 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 289 | std::stringstream ss; |
| 290 | BOOST_CHECK_EQUAL(true, queryAdapterTest1.json2SqlTest(ss, testJson)); |
| 291 | BOOST_CHECK_EQUAL(ss.str(), "SELECT name FROM cmip5 WHERE\ |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 292 | activity=\'testActivity\' AND name='test\' AND product=\'testProduct\';"); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseEmptyTest) |
| 296 | { |
| 297 | Json::Value testJson; |
| 298 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 299 | std::stringstream ss; |
| 300 | BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson)); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseAllItemsTest) |
| 304 | { |
| 305 | Json::Value testJson; |
| 306 | testJson["name"] = "test"; |
| 307 | testJson["activity"] = "testActivity"; |
| 308 | testJson["product"] = "testProduct"; |
| 309 | testJson["origanization"] = "testOrg"; |
| 310 | testJson["model"] = "testModel"; |
| 311 | testJson["experiment"] = "testExperiment"; |
| 312 | testJson["frequency"] = "testFrenquency"; |
| 313 | testJson["modeling realm"] = "testModeling"; |
| 314 | testJson["variable name"] = "testVarName"; |
| 315 | testJson["ensemble member"] = "testEnsembleMember"; |
| 316 | testJson["ensemble"] = "testEnsemble"; |
| 317 | testJson["sample granularity"] = "testSampleGranularity"; |
| 318 | testJson["start time"] = "testStartTime"; |
| 319 | testJson["field campaign"] = "testFieldCampaign"; |
| 320 | testJson["optical properties for radiation"] = "testOptProperties"; |
| 321 | testJson["grid resolution"] = "testGridResolution"; |
| 322 | testJson["output type"] = "testOutputType"; |
| 323 | testJson["timestamp"] = "testTimestamp"; |
| 324 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 325 | std::stringstream ss; |
| 326 | BOOST_CHECK_EQUAL(true, queryAdapterTest1.json2SqlTest(ss, testJson)); |
| 327 | BOOST_CHECK_EQUAL(ss.str(), "SELECT name FROM cmip5 WHERE activity=\'testActivity\' AND \ |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 328 | ensemble=\'testEnsemble\' AND ensemble member=\'testEnsembleMember\' AND \ |
| 329 | experiment=\'testExperiment\' AND field campaign=\'testFieldCampaign\' AND \ |
| 330 | frequency=\'testFrenquency\' AND grid resolution=\'testGridResolution\' AND \ |
| 331 | model=\'testModel\' AND modeling realm=\'testModeling\' AND name=\'test\' AND \ |
| 332 | optical properties for radiation=\'testOptProperties\' AND origanization=\'testOrg\' AND \ |
| 333 | output type=\'testOutputType\' AND product=\'testProduct\' AND sample \ |
| 334 | granularity=\'testSampleGranularity\' AND start time=\'testStartTime\' AND \ |
| 335 | timestamp=\'testTimestamp\' AND variable name=\'testVarName\';"); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseSearchTest) |
| 339 | { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 340 | // incorrect autocompletion is ok for sql conversion |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 341 | Json::Value testJson; |
| 342 | testJson["name"] = "test"; |
| 343 | testJson["?"] = "serchTest"; |
| 344 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 345 | std::stringstream ss; |
| 346 | BOOST_CHECK_EQUAL(true, queryAdapterTest1.json2SqlTest(ss, testJson)); |
| 347 | BOOST_CHECK_EQUAL(ss.str(), |
| 348 | "SELECT name FROM cmip5 WHERE name=\'test\';"); |
| 349 | } |
| 350 | |
| 351 | BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest1) |
| 352 | { |
| 353 | Json::Value testJson; |
| 354 | testJson["name"] = Json::nullValue; |
| 355 | |
| 356 | std::stringstream ss; |
| 357 | BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson)); |
| 358 | } |
| 359 | |
| 360 | BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest2) |
| 361 | { |
| 362 | Json::Value testJson; |
| 363 | |
| 364 | std::stringstream ss; |
| 365 | BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson)); |
| 366 | } |
| 367 | |
| 368 | BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest3) |
| 369 | { |
| 370 | Json::Value testJson; |
| 371 | testJson = Json::Value(Json::arrayValue); |
| 372 | |
| 373 | std::stringstream ss; |
| 374 | BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson)); |
| 375 | } |
| 376 | |
| 377 | BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest4) |
| 378 | { |
| 379 | Json::Value testJson; |
| 380 | testJson[0] = "test"; |
| 381 | |
| 382 | std::stringstream ss; |
| 383 | BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson)); |
| 384 | } |
| 385 | |
| 386 | BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseFailTest5) |
| 387 | { |
| 388 | Json::Value testJson; |
| 389 | Json::Value param; |
| 390 | param[0] = "test"; |
| 391 | testJson["name"] = param; |
| 392 | |
| 393 | std::stringstream ss; |
| 394 | BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson)); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | BOOST_AUTO_TEST_CASE(QueryAdapterMakeAckDataTest) |
| 398 | { |
| 399 | ndn::Interest interest(ndn::Name("/test/ack/data/json")); |
| 400 | interest.setInterestLifetime(ndn::time::milliseconds(1000)); |
| 401 | interest.setMustBeFresh(true); |
| 402 | std::shared_ptr<const ndn::Interest> interestPtr = std::make_shared<ndn::Interest>(interest); |
| 403 | |
| 404 | const ndn::name::Component version |
| 405 | = ndn::name::Component::fromVersion(1); |
| 406 | |
| 407 | std::shared_ptr<ndn::Data> data = queryAdapterTest2.getAckData(interestPtr, version); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 408 | BOOST_CHECK_EQUAL(data->getName().toUri(), "/test/ack/data/json"); |
| 409 | BOOST_CHECK_EQUAL(std::string(reinterpret_cast<const char*>(data->getContent().value()), |
| 410 | data->getContent().value_size()), |
| 411 | "/query-results/catalogIdPlaceHolder/json/%FD%01"); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest1) |
| 415 | { |
| 416 | Json::Value fileList; |
| 417 | fileList.append("/ndn/test1"); |
| 418 | fileList.append("/ndn/test2"); |
| 419 | |
| 420 | const ndn::Name prefix("/atmos/test/prefix"); |
| 421 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 422 | std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList, |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 423 | 1, false, false, 2, 0, 1); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 424 | BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%01"); |
| 425 | BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component("")); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 426 | const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()), |
| 427 | data->getContent().value_size()); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 428 | Json::Value parsedFromString; |
| 429 | Json::Reader reader; |
| 430 | BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 431 | BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 2); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 432 | BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 2); |
| 433 | BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1"); |
| 434 | BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2"); |
| 435 | } |
| 436 | |
| 437 | BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest2) |
| 438 | { |
| 439 | Json::Value fileList; |
| 440 | fileList.append("/ndn/test1"); |
| 441 | const ndn::Name prefix("/atmos/test/prefix"); |
| 442 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 443 | std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList, |
| 444 | 2, true, true, 1, 0, 0); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 445 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 446 | BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%02"); |
| 447 | BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component::fromSegment(2)); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 448 | const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()), |
| 449 | data->getContent().value_size()); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 450 | Json::Value parsedFromString; |
| 451 | Json::Reader reader; |
| 452 | BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 453 | BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 1); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 454 | BOOST_CHECK_EQUAL(parsedFromString["next"].size(), 1); |
| 455 | BOOST_CHECK_EQUAL(parsedFromString["next"][0], "/ndn/test1"); |
| 456 | } |
| 457 | |
| 458 | BOOST_AUTO_TEST_CASE(QueryAdapterQueryProcessTest) |
| 459 | { |
| 460 | initializeQueryAdapterTest2(); |
| 461 | Json::Value query; |
| 462 | query["name"] = "test"; |
| 463 | Json::FastWriter fastWriter; |
| 464 | std::string jsonMessage = fastWriter.write(query); |
| 465 | jsonMessage.erase(std::remove(jsonMessage.begin(), jsonMessage.end(), '\n'), jsonMessage.end()); |
| 466 | std::shared_ptr<ndn::Interest> queryInterest |
| 467 | = std::make_shared<ndn::Interest>(ndn::Name("/test/query").append(jsonMessage.c_str())); |
| 468 | |
| 469 | queryAdapterTest2.queryTest(queryInterest); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 470 | // TODO: the code below should be enabled when queryAdapter can get the correct the |
| 471 | // ChronoSync state; currently, we don't need the activeQuery to save the ACK data; |
| 472 | //auto ackData = queryAdapterTest2.getDataFromActiveQuery(jsonMessage); |
| 473 | // |
| 474 | //BOOST_CHECK(ackData); |
| 475 | //if (ackData) { |
| 476 | // BOOST_CHECK_EQUAL(ackData->getName().getPrefix(3), |
| 477 | // ndn::Name("/test/query/%7B%22name%22%3A%22test%22%7D")); |
| 478 | // BOOST_CHECK_EQUAL(ackData->getName().at(ackData->getName().size() - 1), |
| 479 | // ndn::Name::Component("OK")); |
| 480 | // BOOST_CHECK_EQUAL(ackData->getContent().value_size(), 0); |
| 481 | //} |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 482 | |
| 483 | std::shared_ptr<ndn::Interest> resultInterest |
| 484 | = std::make_shared<ndn::Interest>(ndn::Name("/test/query-results")); |
| 485 | auto replyData = queryAdapterTest2.getDataFromCache(*resultInterest); |
| 486 | BOOST_CHECK(replyData); |
| 487 | if (replyData){ |
| 488 | BOOST_CHECK_EQUAL(replyData->getName().getPrefix(2), ndn::Name("/test/query-results")); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 489 | const std::string jsonRes(reinterpret_cast<const char*>(replyData->getContent().value()), |
| 490 | replyData->getContent().value_size()); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 491 | Json::Value parsedFromString; |
| 492 | Json::Reader reader; |
| 493 | BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 494 | BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 3); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 495 | BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 3); |
| 496 | BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1"); |
| 497 | BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2"); |
| 498 | BOOST_CHECK_EQUAL(parsedFromString["results"][2], "/ndn/test3"); |
| 499 | } |
| 500 | } |
| 501 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 502 | BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlSuccessTest) |
| 503 | { |
| 504 | initializeQueryAdapterTest2(); |
| 505 | |
| 506 | std::stringstream ss; |
| 507 | Json::Value testJson; |
| 508 | testJson["?"] = "/"; |
| 509 | BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson)); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 510 | BOOST_CHECK_EQUAL("SELECT DISTINCT activity FROM cmip5;", ss.str()); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 511 | |
| 512 | ss.str(""); |
| 513 | ss.clear(); |
| 514 | testJson.clear(); |
| 515 | testJson["?"] = "/Activity/"; |
| 516 | BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson)); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 517 | BOOST_CHECK_EQUAL("SELECT DISTINCT product FROM cmip5 WHERE activity='Activity';", ss.str()); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 518 | |
| 519 | ss.str(""); |
| 520 | ss.clear(); |
| 521 | testJson.clear(); |
| 522 | testJson["?"] = "/Activity/Product/Organization/Model/Experiment/"; |
| 523 | BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson)); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 524 | BOOST_CHECK_EQUAL("SELECT DISTINCT frequency FROM cmip5 WHERE activity='Activity' AND \ |
| 525 | experiment='Experiment' AND model='Model' AND organization='Organization' AND product='Product';", |
| 526 | ss.str()); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 527 | |
| 528 | ss.str(""); |
| 529 | ss.clear(); |
| 530 | testJson.clear(); |
| 531 | testJson["?"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\ |
| 532 | Variable/Ensemble/"; |
| 533 | BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson)); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 534 | BOOST_CHECK_EQUAL("SELECT DISTINCT time FROM cmip5 WHERE activity='Activity' AND ensemble=\ |
| 535 | 'Ensemble' AND experiment='Experiment' AND frequency='Frequency' AND model='Model' AND \ |
| 536 | modeling_realm='Modeling' AND organization='Organization' AND product='Product' AND variable_name=\ |
| 537 | 'Variable';",ss.str()); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlFailTest) |
| 541 | { |
| 542 | initializeQueryAdapterTest2(); |
| 543 | |
| 544 | std::stringstream ss; |
| 545 | Json::Value testJson; |
| 546 | testJson["?"] = "serchTest"; |
| 547 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson)); |
| 548 | |
| 549 | ss.str(""); |
| 550 | ss.clear(); |
| 551 | testJson.clear(); |
| 552 | testJson["?"] = "/cmip5"; |
| 553 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson)); |
| 554 | |
| 555 | ss.str(""); |
| 556 | ss.clear(); |
| 557 | Json::Value testJson2; //simply clear does not work |
| 558 | testJson2[0] = "test"; |
| 559 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson2)); |
| 560 | |
| 561 | ss.str(""); |
| 562 | ss.clear(); |
| 563 | Json::Value testJson3; |
| 564 | testJson3 = Json::Value(Json::arrayValue); |
| 565 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson3)); |
| 566 | |
| 567 | ss.str(""); |
| 568 | ss.clear(); |
| 569 | Json::Value testJson4; |
| 570 | Json::Value param; |
| 571 | param[0] = "test"; |
| 572 | testJson4["name"] = param; |
| 573 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson4)); |
| 574 | } |
| 575 | |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame^] | 576 | BOOST_AUTO_TEST_CASE(QueryAdapterCompleteSearchSuccessTest) |
| 577 | { |
| 578 | initializeQueryAdapterTest2(); |
| 579 | |
| 580 | std::stringstream ss; |
| 581 | Json::Value testJson; |
| 582 | testJson["??"] = "/"; |
| 583 | BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2CompleteSearchSqlTest(ss, testJson)); |
| 584 | BOOST_CHECK_EQUAL("SELECT name FROM cmip5;", ss.str()); |
| 585 | |
| 586 | ss.str(""); |
| 587 | ss.clear(); |
| 588 | testJson.clear(); |
| 589 | testJson["??"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\ |
| 590 | Variable/Ensemble/"; |
| 591 | BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2CompleteSearchSqlTest(ss, testJson)); |
| 592 | BOOST_CHECK_EQUAL("SELECT name FROM cmip5 WHERE activity='Activity' AND ensemble=\ |
| 593 | 'Ensemble' AND experiment='Experiment' AND frequency='Frequency' AND model='Model' AND \ |
| 594 | modeling_realm='Modeling' AND organization='Organization' AND product='Product' AND variable_name=\ |
| 595 | 'Variable';",ss.str()); |
| 596 | } |
| 597 | |
| 598 | BOOST_AUTO_TEST_CASE(QueryAdapterCompleteSearchFailureTest) |
| 599 | { |
| 600 | initializeQueryAdapterTest2(); |
| 601 | |
| 602 | std::stringstream ss; |
| 603 | Json::Value testJson; |
| 604 | |
| 605 | ss.str(""); |
| 606 | ss.clear(); |
| 607 | testJson.clear(); |
| 608 | testJson["??"] = ""; |
| 609 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2CompleteSearchSqlTest(ss, testJson)); |
| 610 | |
| 611 | ss.str(""); |
| 612 | ss.clear(); |
| 613 | Json::Value testJson2; //simply clear does not work |
| 614 | testJson2[0] = "test"; // incorrect json object |
| 615 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2CompleteSearchSqlTest(ss, testJson2)); |
| 616 | |
| 617 | ss.str(""); |
| 618 | ss.clear(); |
| 619 | Json::Value testJson3; |
| 620 | testJson3 = Json::Value(Json::arrayValue); // incorrect json object |
| 621 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2CompleteSearchSqlTest(ss, testJson3)); |
| 622 | |
| 623 | ss.str(""); |
| 624 | ss.clear(); |
| 625 | Json::Value testJson4; |
| 626 | Json::Value param; |
| 627 | param[0] = "test"; |
| 628 | testJson4["name"] = param; // incorrect json object |
| 629 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2CompleteSearchSqlTest(ss, testJson4)); |
| 630 | } |
| 631 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 632 | BOOST_AUTO_TEST_SUITE_END() |
| 633 | |
| 634 | }//tests |
| 635 | }//atmos |