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