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("")); |
| 400 | const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value())); |
| 401 | Json::Value parsedFromString; |
| 402 | Json::Reader reader; |
| 403 | BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 404 | BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 2); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 405 | BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 2); |
| 406 | BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1"); |
| 407 | BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2"); |
| 408 | } |
| 409 | |
| 410 | BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest2) |
| 411 | { |
| 412 | Json::Value fileList; |
| 413 | fileList.append("/ndn/test1"); |
| 414 | const ndn::Name prefix("/atmos/test/prefix"); |
| 415 | |
| 416 | std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 417 | fileList, 2, true, true, 1); |
| 418 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 419 | BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%02"); |
| 420 | BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component::fromSegment(2)); |
| 421 | const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value())); |
| 422 | Json::Value parsedFromString; |
| 423 | Json::Reader reader; |
| 424 | BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 425 | BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 1); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 426 | BOOST_CHECK_EQUAL(parsedFromString["next"].size(), 1); |
| 427 | BOOST_CHECK_EQUAL(parsedFromString["next"][0], "/ndn/test1"); |
| 428 | } |
| 429 | |
| 430 | BOOST_AUTO_TEST_CASE(QueryAdapterQueryProcessTest) |
| 431 | { |
| 432 | initializeQueryAdapterTest2(); |
| 433 | Json::Value query; |
| 434 | query["name"] = "test"; |
| 435 | Json::FastWriter fastWriter; |
| 436 | std::string jsonMessage = fastWriter.write(query); |
| 437 | jsonMessage.erase(std::remove(jsonMessage.begin(), jsonMessage.end(), '\n'), jsonMessage.end()); |
| 438 | std::shared_ptr<ndn::Interest> queryInterest |
| 439 | = std::make_shared<ndn::Interest>(ndn::Name("/test/query").append(jsonMessage.c_str())); |
| 440 | |
| 441 | queryAdapterTest2.queryTest(queryInterest); |
| 442 | auto ackData = queryAdapterTest2.getDataFromActiveQuery(jsonMessage); |
| 443 | |
| 444 | BOOST_CHECK(ackData); |
| 445 | if (ackData) { |
| 446 | BOOST_CHECK_EQUAL(ackData->getName().getPrefix(3), |
| 447 | ndn::Name("/test/query/%7B%22name%22%3A%22test%22%7D")); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 448 | BOOST_CHECK_EQUAL(ackData->getName().at(ackData->getName().size() - 1), |
| 449 | ndn::Name::Component("OK")); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 450 | BOOST_CHECK_EQUAL(ackData->getContent().value_size(), 0); |
| 451 | } |
| 452 | |
| 453 | std::shared_ptr<ndn::Interest> resultInterest |
| 454 | = std::make_shared<ndn::Interest>(ndn::Name("/test/query-results")); |
| 455 | auto replyData = queryAdapterTest2.getDataFromCache(*resultInterest); |
| 456 | BOOST_CHECK(replyData); |
| 457 | if (replyData){ |
| 458 | BOOST_CHECK_EQUAL(replyData->getName().getPrefix(2), ndn::Name("/test/query-results")); |
| 459 | const std::string jsonRes(reinterpret_cast<const char*>(replyData->getContent().value())); |
| 460 | Json::Value parsedFromString; |
| 461 | Json::Reader reader; |
| 462 | BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 463 | BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 3); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 464 | BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 3); |
| 465 | BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1"); |
| 466 | BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2"); |
| 467 | BOOST_CHECK_EQUAL(parsedFromString["results"][2], "/ndn/test3"); |
| 468 | } |
| 469 | } |
| 470 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 471 | BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlSuccessTest) |
| 472 | { |
| 473 | initializeQueryAdapterTest2(); |
| 474 | |
| 475 | std::stringstream ss; |
| 476 | Json::Value testJson; |
| 477 | testJson["?"] = "/"; |
| 478 | BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson)); |
| 479 | BOOST_CHECK_EQUAL("SELECT activity FROM cmip5;", ss.str()); |
| 480 | |
| 481 | ss.str(""); |
| 482 | ss.clear(); |
| 483 | testJson.clear(); |
| 484 | testJson["?"] = "/Activity/"; |
| 485 | BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson)); |
| 486 | BOOST_CHECK_EQUAL("SELECT product FROM cmip5 WHERE activity='Activity';", ss.str()); |
| 487 | |
| 488 | ss.str(""); |
| 489 | ss.clear(); |
| 490 | testJson.clear(); |
| 491 | testJson["?"] = "/Activity/Product/Organization/Model/Experiment/"; |
| 492 | BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson)); |
| 493 | BOOST_CHECK_EQUAL("SELECT frequency FROM cmip5 WHERE activity='Activity' AND experiment=\ |
| 494 | 'Experiment' AND model='Model' AND organization='Organization' AND product='Product';", ss.str()); |
| 495 | |
| 496 | ss.str(""); |
| 497 | ss.clear(); |
| 498 | testJson.clear(); |
| 499 | testJson["?"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\ |
| 500 | Variable/Ensemble/"; |
| 501 | BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson)); |
| 502 | BOOST_CHECK_EQUAL("SELECT time FROM cmip5 WHERE activity='Activity' AND ensemble='Ensemble' AND\ |
| 503 | experiment='Experiment' AND frequency='Frequency' AND model='Model' AND modeling_realm='Modeling' \ |
| 504 | AND organization='Organization' AND product='Product' AND variable_name='Variable';",ss.str()); |
| 505 | } |
| 506 | |
| 507 | BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlFailTest) |
| 508 | { |
| 509 | initializeQueryAdapterTest2(); |
| 510 | |
| 511 | std::stringstream ss; |
| 512 | Json::Value testJson; |
| 513 | testJson["?"] = "serchTest"; |
| 514 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson)); |
| 515 | |
| 516 | ss.str(""); |
| 517 | ss.clear(); |
| 518 | testJson.clear(); |
| 519 | testJson["?"] = "/cmip5"; |
| 520 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson)); |
| 521 | |
| 522 | ss.str(""); |
| 523 | ss.clear(); |
| 524 | Json::Value testJson2; //simply clear does not work |
| 525 | testJson2[0] = "test"; |
| 526 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson2)); |
| 527 | |
| 528 | ss.str(""); |
| 529 | ss.clear(); |
| 530 | Json::Value testJson3; |
| 531 | testJson3 = Json::Value(Json::arrayValue); |
| 532 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson3)); |
| 533 | |
| 534 | ss.str(""); |
| 535 | ss.clear(); |
| 536 | Json::Value testJson4; |
| 537 | Json::Value param; |
| 538 | param[0] = "test"; |
| 539 | testJson4["name"] = param; |
| 540 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson4)); |
| 541 | } |
| 542 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 543 | BOOST_AUTO_TEST_SUITE_END() |
| 544 | |
| 545 | }//tests |
| 546 | }//atmos |