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