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 | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 104 | isAutocomplete, resultCount, viewStart, viewEnd, false); |
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, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 116 | bool autocomplete, |
| 117 | bool lastComponent) |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 118 | { |
| 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 Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 126 | fileList, 0, true, false, |
| 127 | 3, 0, 2, lastComponent); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 128 | 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 Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 160 | |
| 161 | bool |
| 162 | json2AutocompletionSqlTest(std::stringstream& sqlQuery, |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 163 | Json::Value& jsonValue, |
| 164 | bool& lastComponent) |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 165 | { |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 166 | return json2AutocompletionSql(sqlQuery, jsonValue, lastComponent); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 167 | } |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 168 | |
| 169 | bool |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 170 | json2PrefixBasedSearchSqlTest(std::stringstream& sqlQuery, |
| 171 | Json::Value& jsonValue) |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 172 | { |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 173 | return json2PrefixBasedSearchSql(sqlQuery, jsonValue); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 174 | } |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | class QueryAdapterFixture : public UnitTestTimeFixture |
| 178 | { |
| 179 | public: |
| 180 | QueryAdapterFixture() |
| 181 | : face(makeDummyClientFace(io)) |
| 182 | , keyChain(new ndn::KeyChain()) |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 183 | , databaseTable("cmip5") |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 184 | , queryAdapterTest1(face, keyChain) |
| 185 | , queryAdapterTest2(face, keyChain) |
| 186 | { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 187 | 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 Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 200 | |
| 201 | queryAdapterTest1.setDatabaseTable(databaseTable); |
| 202 | queryAdapterTest1.setNameFields(nameFields); |
| 203 | queryAdapterTest2.setDatabaseTable(databaseTable); |
| 204 | queryAdapterTest2.setNameFields(nameFields); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 205 | } |
| 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 Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 220 | filterCategoryNames activity,product,organization,model,experiment,frequency,modeling_realm,variable_name,ensemble\ |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 221 | 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 Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 233 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 234 | queryAdapterTest1.configAdapter(section, ndn::Name("/test")); |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | initializeQueryAdapterTest2() |
| 239 | { |
| 240 | util::ConfigSection section; |
| 241 | try { |
| 242 | std::stringstream ss; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 243 | ss << "\ |
| 244 | filterCategoryNames activity,product,organization,model,experiment,frequency,modeling_realm,variable_name,ensemble\ |
| 245 | database\ |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 246 | { \ |
| 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 Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 257 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 258 | queryAdapterTest2.configAdapter(section, ndn::Name("/test")); |
| 259 | } |
| 260 | |
| 261 | protected: |
| 262 | std::shared_ptr<DummyClientFace> face; |
| 263 | std::shared_ptr<ndn::KeyChain> keyChain; |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 264 | std::string databaseTable; |
| 265 | std::vector<std::string> nameFields; |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 266 | 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 Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 292 | std::stringstream ss; |
| 293 | BOOST_CHECK_EQUAL(true, queryAdapterTest1.json2SqlTest(ss, testJson)); |
| 294 | BOOST_CHECK_EQUAL(ss.str(), "SELECT name FROM cmip5 WHERE\ |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 295 | activity=\'testActivity\' AND name='test\' AND product=\'testProduct\';"); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseEmptyTest) |
| 299 | { |
| 300 | Json::Value testJson; |
| 301 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 302 | std::stringstream ss; |
| 303 | BOOST_CHECK_EQUAL(false, queryAdapterTest1.json2SqlTest(ss, testJson)); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 304 | } |
| 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 Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 328 | 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 Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 331 | ensemble=\'testEnsemble\' AND ensemble member=\'testEnsembleMember\' AND \ |
| 332 | experiment=\'testExperiment\' AND field campaign=\'testFieldCampaign\' AND \ |
| 333 | frequency=\'testFrenquency\' AND grid resolution=\'testGridResolution\' AND \ |
| 334 | model=\'testModel\' AND modeling realm=\'testModeling\' AND name=\'test\' AND \ |
| 335 | optical properties for radiation=\'testOptProperties\' AND origanization=\'testOrg\' AND \ |
| 336 | output type=\'testOutputType\' AND product=\'testProduct\' AND sample \ |
| 337 | granularity=\'testSampleGranularity\' AND start time=\'testStartTime\' AND \ |
| 338 | timestamp=\'testTimestamp\' AND variable name=\'testVarName\';"); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | BOOST_AUTO_TEST_CASE(QueryAdapterJsonParseSearchTest) |
| 342 | { |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 343 | // incorrect autocompletion is ok for sql conversion |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 344 | Json::Value testJson; |
| 345 | testJson["name"] = "test"; |
| 346 | testJson["?"] = "serchTest"; |
| 347 | |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 348 | 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 Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 398 | } |
| 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 Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 411 | 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 Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 415 | } |
| 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 Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 425 | std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList, |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 426 | 1, false, false, 2, 0, 1); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 427 | BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%01"); |
| 428 | BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component("")); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 429 | const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()), |
| 430 | data->getContent().value_size()); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 431 | Json::Value parsedFromString; |
| 432 | Json::Reader reader; |
| 433 | BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 434 | BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 2); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 435 | 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 Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 446 | std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList, |
| 447 | 2, true, true, 1, 0, 0); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 448 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 449 | BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%02"); |
| 450 | BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component::fromSegment(2)); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 451 | const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()), |
| 452 | data->getContent().value_size()); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 453 | Json::Value parsedFromString; |
| 454 | Json::Reader reader; |
| 455 | BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 456 | BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 1); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 457 | 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 Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 473 | // 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 Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 485 | |
| 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 Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 492 | const std::string jsonRes(reinterpret_cast<const char*>(replyData->getContent().value()), |
| 493 | replyData->getContent().value_size()); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 494 | Json::Value parsedFromString; |
| 495 | Json::Reader reader; |
| 496 | BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 497 | BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 3); |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 498 | 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 Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 505 | BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlSuccessTest) |
| 506 | { |
| 507 | initializeQueryAdapterTest2(); |
| 508 | |
| 509 | std::stringstream ss; |
| 510 | Json::Value testJson; |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 511 | bool lastComponent = false; |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 512 | testJson["?"] = "/"; |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 513 | BOOST_CHECK_EQUAL(true, |
| 514 | queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent)); |
| 515 | BOOST_CHECK_EQUAL(lastComponent, false); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 516 | BOOST_CHECK_EQUAL("SELECT DISTINCT activity FROM cmip5;", ss.str()); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 517 | |
| 518 | ss.str(""); |
| 519 | ss.clear(); |
| 520 | testJson.clear(); |
| 521 | testJson["?"] = "/Activity/"; |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 522 | BOOST_CHECK_EQUAL(true, |
| 523 | queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent)); |
| 524 | BOOST_CHECK_EQUAL(lastComponent, false); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 525 | 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] | 526 | |
| 527 | ss.str(""); |
| 528 | ss.clear(); |
| 529 | testJson.clear(); |
| 530 | testJson["?"] = "/Activity/Product/Organization/Model/Experiment/"; |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 531 | BOOST_CHECK_EQUAL(true, |
| 532 | queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent)); |
| 533 | BOOST_CHECK_EQUAL(lastComponent, false); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 534 | BOOST_CHECK_EQUAL("SELECT DISTINCT frequency FROM cmip5 WHERE activity='Activity' AND \ |
| 535 | experiment='Experiment' AND model='Model' AND organization='Organization' AND product='Product';", |
| 536 | ss.str()); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 537 | |
| 538 | ss.str(""); |
| 539 | ss.clear(); |
| 540 | testJson.clear(); |
| 541 | testJson["?"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\ |
| 542 | Variable/Ensemble/"; |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 543 | BOOST_CHECK_EQUAL(true, |
| 544 | queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent)); |
| 545 | BOOST_CHECK_EQUAL(lastComponent, true); |
Chengyu Fan | 4639821 | 2015-08-11 11:23:13 -0600 | [diff] [blame] | 546 | 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 \ |
| 548 | modeling_realm='Modeling' AND organization='Organization' AND product='Product' AND variable_name=\ |
| 549 | 'Variable';",ss.str()); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlFailTest) |
| 553 | { |
| 554 | initializeQueryAdapterTest2(); |
| 555 | |
| 556 | std::stringstream ss; |
| 557 | Json::Value testJson; |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 558 | bool lastComponent = false; |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 559 | testJson["?"] = "serchTest"; |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 560 | BOOST_CHECK_EQUAL(false, |
| 561 | queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent)); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 562 | |
| 563 | ss.str(""); |
| 564 | ss.clear(); |
| 565 | testJson.clear(); |
| 566 | testJson["?"] = "/cmip5"; |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 567 | BOOST_CHECK_EQUAL(false, |
| 568 | queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson, lastComponent)); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 569 | |
| 570 | ss.str(""); |
| 571 | ss.clear(); |
| 572 | Json::Value testJson2; //simply clear does not work |
| 573 | testJson2[0] = "test"; |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 574 | BOOST_CHECK_EQUAL(false, |
| 575 | queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson2, lastComponent)); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 576 | |
| 577 | ss.str(""); |
| 578 | ss.clear(); |
| 579 | Json::Value testJson3; |
| 580 | testJson3 = Json::Value(Json::arrayValue); |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 581 | BOOST_CHECK_EQUAL(false, |
| 582 | queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson3, lastComponent)); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 583 | |
| 584 | ss.str(""); |
| 585 | ss.clear(); |
| 586 | Json::Value testJson4; |
| 587 | Json::Value param; |
| 588 | param[0] = "test"; |
| 589 | testJson4["name"] = param; |
Chengyu Fan | 3b9bb34 | 2015-09-21 10:53:37 -0600 | [diff] [blame] | 590 | BOOST_CHECK_EQUAL(false, |
| 591 | queryAdapterTest2.json2AutocompletionSqlTest(ss, testJson4, lastComponent)); |
Chengyu Fan | 9244016 | 2015-07-09 14:43:31 -0600 | [diff] [blame] | 592 | } |
| 593 | |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 594 | BOOST_AUTO_TEST_CASE(QueryAdapterPrefixBasedSearchSuccessTest) |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 595 | { |
| 596 | initializeQueryAdapterTest2(); |
| 597 | |
| 598 | std::stringstream ss; |
| 599 | Json::Value testJson; |
| 600 | testJson["??"] = "/"; |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 601 | BOOST_CHECK_EQUAL(true, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson)); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 602 | BOOST_CHECK_EQUAL("SELECT name FROM cmip5;", ss.str()); |
| 603 | |
| 604 | ss.str(""); |
| 605 | ss.clear(); |
| 606 | testJson.clear(); |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 607 | 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 Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 615 | testJson["??"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\ |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 616 | Variable/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' \ |
| 621 | AND 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 Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 624 | } |
| 625 | |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 626 | BOOST_AUTO_TEST_CASE(QueryAdapterPrefixBasedSearchFailureTest) |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 627 | { |
| 628 | initializeQueryAdapterTest2(); |
| 629 | |
| 630 | std::stringstream ss; |
| 631 | Json::Value testJson; |
| 632 | |
| 633 | ss.str(""); |
| 634 | ss.clear(); |
| 635 | testJson.clear(); |
| 636 | testJson["??"] = ""; |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 637 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson)); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 638 | |
| 639 | ss.str(""); |
| 640 | ss.clear(); |
| 641 | Json::Value testJson2; //simply clear does not work |
| 642 | testJson2[0] = "test"; // incorrect json object |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 643 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson2)); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 644 | |
| 645 | ss.str(""); |
| 646 | ss.clear(); |
| 647 | Json::Value testJson3; |
| 648 | testJson3 = Json::Value(Json::arrayValue); // incorrect json object |
Chengyu Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 649 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson3)); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 650 | |
| 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 Fan | 4d5fbd2 | 2015-09-18 14:34:08 -0600 | [diff] [blame] | 657 | BOOST_CHECK_EQUAL(false, queryAdapterTest2.json2PrefixBasedSearchSqlTest(ss, testJson4)); |
Chengyu Fan | f4c747a | 2015-08-18 13:56:01 -0600 | [diff] [blame] | 658 | } |
| 659 | |
Chengyu Fan | b25835b | 2015-04-28 17:09:35 -0600 | [diff] [blame] | 660 | BOOST_AUTO_TEST_SUITE_END() |
| 661 | |
| 662 | }//tests |
| 663 | }//atmos |