blob: ac6aadd885e099d86b006f91a57b7620d4cbb6a0 [file] [log] [blame]
Chengyu Fanb25835b2015-04-28 17:09:35 -06001/** 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
29namespace atmos{
30namespace tests{
Chengyu Fanb25835b2015-04-28 17:09:35 -060031
32 class QueryAdapterTest : public query::QueryAdapter<std::string>
33 {
34 public:
35 QueryAdapterTest(const std::shared_ptr<ndn::util::DummyClientFace>& face,
Chengyu Fancfb80c72015-10-19 16:50:04 -060036 const std::shared_ptr<ndn::KeyChain>& keyChain,
37 const std::shared_ptr<chronosync::Socket>& syncSocket)
38 : query::QueryAdapter<std::string>(face, keyChain, syncSocket)
Chengyu Fanb25835b2015-04-28 17:09:35 -060039 {
40 }
41
42 virtual
43 ~QueryAdapterTest()
44 {
45 }
46
Chengyu Fanf4c747a2015-08-18 13:56:01 -060047 void setDatabaseTable(const std::string& databaseTable)
48 {
49 m_databaseTable.assign(databaseTable);
50 }
51
Chengyu Fan92440162015-07-09 14:43:31 -060052 void setNameFields(const std::vector<std::string>& nameFields)
53 {
54 m_nameFields = nameFields;
55 }
56
Chengyu Fanb25835b2015-04-28 17:09:35 -060057 void setPrefix(const ndn::Name& prefix)
58 {
59 m_prefix = prefix;
60 }
61
62 void setSigningId(const ndn::Name& signingId)
63 {
64 m_signingId = signingId;
65 }
66
67 const ndn::Name
68 getPrefix()
69 {
70 return m_prefix;
71 }
72
73 const ndn::Name
74 getSigningId()
75 {
76 return m_signingId;
77 }
78
79 std::shared_ptr<ndn::Data>
80 getAckData(std::shared_ptr<const ndn::Interest> interest, const ndn::Name::Component& version)
81 {
82 return makeAckData(interest, version);
83 }
84
Chengyu Fanb25835b2015-04-28 17:09:35 -060085 std::shared_ptr<ndn::Data>
86 getReplyData(const ndn::Name& segmentPrefix,
87 const Json::Value& value,
88 uint64_t segmentNo,
89 bool isFinalBlock,
Chengyu Fan92440162015-07-09 14:43:31 -060090 bool isAutocomplete,
Chengyu Fanf4c747a2015-08-18 13:56:01 -060091 uint64_t resultCount,
92 uint64_t viewStart,
93 uint64_t viewEnd)
Chengyu Fanb25835b2015-04-28 17:09:35 -060094 {
Chengyu Fan92440162015-07-09 14:43:31 -060095 return makeReplyData(segmentPrefix, value, segmentNo, isFinalBlock,
Chengyu Fan3b9bb342015-09-21 10:53:37 -060096 isAutocomplete, resultCount, viewStart, viewEnd, false);
Chengyu Fanb25835b2015-04-28 17:09:35 -060097 }
98
99 void
100 queryTest(std::shared_ptr<const ndn::Interest> interest)
101 {
102 runJsonQuery(interest);
103 }
104
105 void
Chengyu Fan31737f12016-01-12 21:08:50 -0700106 prepareSegmentsByParams(std::vector<std::pair<std::string, std::string>>& queryParams,
107 const ndn::Name& segmentPrefix)
Chengyu Fanb25835b2015-04-28 17:09:35 -0600108 {
Chengyu Fan31737f12016-01-12 21:08:50 -0700109 //BOOST_CHECK_EQUAL(sqlString, "SELECT name FROM cmip5 WHERE name=\'test\';");
110 for (auto it = queryParams.begin() ; it != queryParams.end(); ++it) {
111 std::cout << it->first << " " << it->second << std::endl;
112 }
113
Chengyu Fanb25835b2015-04-28 17:09:35 -0600114 Json::Value fileList;
115 fileList.append("/ndn/test1");
116 fileList.append("/ndn/test2");
117 fileList.append("/ndn/test3");
118
119 std::shared_ptr<ndn::Data> data = makeReplyData(segmentPrefix,
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600120 fileList, 0, true, false,
Chengyu Fan31737f12016-01-12 21:08:50 -0700121 3, 0, 2, true);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600122 m_mutex.lock();
123 m_cache.insert(*data);
124 m_mutex.unlock();
125 }
126
127 std::shared_ptr<const ndn::Data>
Chengyu Fanb25835b2015-04-28 17:09:35 -0600128 getDataFromCache(const ndn::Interest& interest)
129 {
130 return m_cache.find(interest);
131 }
132
133 void
134 configAdapter(const util::ConfigSection& section,
135 const ndn::Name& prefix)
136 {
137 onConfig(section, false, std::string("test.txt"), prefix);
138 }
Chengyu Fan92440162015-07-09 14:43:31 -0600139
140 bool
141 json2AutocompletionSqlTest(std::stringstream& sqlQuery,
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600142 Json::Value& jsonValue,
Chengyu Fan31737f12016-01-12 21:08:50 -0700143 bool& lastComponent,
144 std::stringstream& nameField)
Chengyu Fan92440162015-07-09 14:43:31 -0600145 {
Chengyu Fan31737f12016-01-12 21:08:50 -0700146 return json2AutocompletionSql(sqlQuery, jsonValue, lastComponent, nameField);
Chengyu Fan92440162015-07-09 14:43:31 -0600147 }
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600148
149 bool
Chengyu Fan31737f12016-01-12 21:08:50 -0700150 testDoPrefixBasedSearch(Json::Value& jsonValue,
151 std::vector<std::pair<std::string, std::string>>& typedComponents)
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600152 {
Chengyu Fan31737f12016-01-12 21:08:50 -0700153 return doPrefixBasedSearch(jsonValue, typedComponents);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600154 }
Chengyu Fan31737f12016-01-12 21:08:50 -0700155
156 bool
157 testDoFilterBasedSearch(Json::Value& jsonValue,
158 std::vector<std::pair<std::string, std::string>>& typedComponents)
159 {
160 return doFilterBasedSearch(jsonValue, typedComponents);
161 }
162
163
Chengyu Fanb25835b2015-04-28 17:09:35 -0600164 };
165
166 class QueryAdapterFixture : public UnitTestTimeFixture
167 {
168 public:
169 QueryAdapterFixture()
Chengyu Fanca4d40d2016-08-22 13:47:08 -0600170 : face(std::make_shared<ndn::util::DummyClientFace>(io))
Chengyu Fanb25835b2015-04-28 17:09:35 -0600171 , keyChain(new ndn::KeyChain())
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600172 , databaseTable("cmip5")
Chengyu Fancfb80c72015-10-19 16:50:04 -0600173 , queryAdapterTest1(face, keyChain, syncSocket)
174 , queryAdapterTest2(face, keyChain, syncSocket)
Chengyu Fanb25835b2015-04-28 17:09:35 -0600175 {
Chengyu Fan92440162015-07-09 14:43:31 -0600176 std::string c1("activity"), c2("product"), c3("organization"), c4("model");
177 std::string c5("experiment"), c6("frequency"), c7("modeling_realm"), c8("variable_name");
178 std::string c9("ensemble"), c10("time");
179 nameFields.push_back(c1);
180 nameFields.push_back(c2);
181 nameFields.push_back(c3);
182 nameFields.push_back(c4);
183 nameFields.push_back(c5);
184 nameFields.push_back(c6);
185 nameFields.push_back(c7);
186 nameFields.push_back(c8);
187 nameFields.push_back(c9);
188 nameFields.push_back(c10);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600189
190 queryAdapterTest1.setDatabaseTable(databaseTable);
191 queryAdapterTest1.setNameFields(nameFields);
192 queryAdapterTest2.setDatabaseTable(databaseTable);
193 queryAdapterTest2.setNameFields(nameFields);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600194 }
195
196 virtual
197 ~QueryAdapterFixture()
198 {
199 }
200
201 protected:
202 void
203 initializeQueryAdapterTest1()
204 {
205 util::ConfigSection section;
206 try {
207 std::stringstream ss;
208 ss << "signingId /test/signingId\
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600209 filterCategoryNames activity,product,organization,model,experiment,frequency,modeling_realm,variable_name,ensemble\
Chengyu Fanb25835b2015-04-28 17:09:35 -0600210 database \
211 { \
212 dbServer localhost \
213 dbName testdb \
214 dbUser testuser \
215 dbPasswd testpwd \
216 }";
217 boost::property_tree::read_info(ss, section);
218 }
219 catch (boost::property_tree::info_parser_error &e) {
220 std::cout << "Failed to read config file " << e.what() << std::endl;;
221 }
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600222
Chengyu Fanb25835b2015-04-28 17:09:35 -0600223 queryAdapterTest1.configAdapter(section, ndn::Name("/test"));
224 }
225
226 void
227 initializeQueryAdapterTest2()
228 {
229 util::ConfigSection section;
230 try {
231 std::stringstream ss;
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600232 ss << "\
233 filterCategoryNames activity,product,organization,model,experiment,frequency,modeling_realm,variable_name,ensemble\
234 database\
Chengyu Fanb25835b2015-04-28 17:09:35 -0600235 { \
236 dbServer localhost \
237 dbName testdb \
238 dbUser testuser \
239 dbPasswd testpwd \
240 }";
241 boost::property_tree::read_info(ss, section);
242 }
243 catch (boost::property_tree::info_parser_error &e) {
244 std::cout << "Failed to read config file " << e.what() << std::endl;;
245 }
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600246
Chengyu Fanb25835b2015-04-28 17:09:35 -0600247 queryAdapterTest2.configAdapter(section, ndn::Name("/test"));
248 }
249
250 protected:
Chengyu Fanca4d40d2016-08-22 13:47:08 -0600251 std::shared_ptr<ndn::util::DummyClientFace> face;
Chengyu Fanb25835b2015-04-28 17:09:35 -0600252 std::shared_ptr<ndn::KeyChain> keyChain;
Chengyu Fancfb80c72015-10-19 16:50:04 -0600253 std::shared_ptr<chronosync::Socket> syncSocket;
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600254 std::string databaseTable;
255 std::vector<std::string> nameFields;
Chengyu Fanb25835b2015-04-28 17:09:35 -0600256 QueryAdapterTest queryAdapterTest1;
257 QueryAdapterTest queryAdapterTest2;
258 };
259
260 BOOST_FIXTURE_TEST_SUITE(QueryAdapterTestSuite, QueryAdapterFixture)
261
262 BOOST_AUTO_TEST_CASE(BasicQueryAdapterTest1)
263 {
264 BOOST_CHECK(queryAdapterTest1.getPrefix() == ndn::Name());
265 BOOST_CHECK(queryAdapterTest1.getSigningId() == ndn::Name());
266 }
267
268 BOOST_AUTO_TEST_CASE(BasicQueryAdapterTest2)
269 {
270 initializeQueryAdapterTest1();
271 BOOST_CHECK(queryAdapterTest1.getPrefix() == ndn::Name("/test"));
272 BOOST_CHECK(queryAdapterTest1.getSigningId() == ndn::Name("/test/signingId"));
273 }
274
Chengyu Fan7b978f82015-12-09 17:03:23 -0700275 // use real data instead of ack data
Chengyu Fanb25835b2015-04-28 17:09:35 -0600276 BOOST_AUTO_TEST_CASE(QueryAdapterMakeAckDataTest)
277 {
278 ndn::Interest interest(ndn::Name("/test/ack/data/json"));
279 interest.setInterestLifetime(ndn::time::milliseconds(1000));
280 interest.setMustBeFresh(true);
281 std::shared_ptr<const ndn::Interest> interestPtr = std::make_shared<ndn::Interest>(interest);
282
283 const ndn::name::Component version
284 = ndn::name::Component::fromVersion(1);
285
286 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getAckData(interestPtr, version);
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600287 BOOST_CHECK_EQUAL(data->getName().toUri(), "/test/ack/data/json");
Chengyu Fanb25835b2015-04-28 17:09:35 -0600288 }
289
290 BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest1)
291 {
292 Json::Value fileList;
293 fileList.append("/ndn/test1");
294 fileList.append("/ndn/test2");
295
296 const ndn::Name prefix("/atmos/test/prefix");
297
Chengyu Fan92440162015-07-09 14:43:31 -0600298 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList,
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600299 1, false, false, 2, 0, 1);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600300 BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%01");
301 BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component(""));
Chengyu Fan46398212015-08-11 11:23:13 -0600302 const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()),
303 data->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600304 Json::Value parsedFromString;
305 Json::Reader reader;
306 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600307 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 2);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600308 BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 2);
309 BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1");
310 BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2");
311 }
312
313 BOOST_AUTO_TEST_CASE(QueryAdapterMakeReplyDataTest2)
314 {
315 Json::Value fileList;
316 fileList.append("/ndn/test1");
317 const ndn::Name prefix("/atmos/test/prefix");
318
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600319 std::shared_ptr<ndn::Data> data = queryAdapterTest2.getReplyData(prefix, fileList,
320 2, true, true, 1, 0, 0);
Chengyu Fan92440162015-07-09 14:43:31 -0600321
Chengyu Fanb25835b2015-04-28 17:09:35 -0600322 BOOST_CHECK_EQUAL(data->getName().toUri(), "/atmos/test/prefix/%00%02");
323 BOOST_CHECK_EQUAL(data->getFinalBlockId(), ndn::Name::Component::fromSegment(2));
Chengyu Fan46398212015-08-11 11:23:13 -0600324 const std::string jsonRes(reinterpret_cast<const char*>(data->getContent().value()),
325 data->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600326 Json::Value parsedFromString;
327 Json::Reader reader;
328 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600329 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 1);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600330 BOOST_CHECK_EQUAL(parsedFromString["next"].size(), 1);
331 BOOST_CHECK_EQUAL(parsedFromString["next"][0], "/ndn/test1");
332 }
333
334 BOOST_AUTO_TEST_CASE(QueryAdapterQueryProcessTest)
335 {
336 initializeQueryAdapterTest2();
337 Json::Value query;
338 query["name"] = "test";
339 Json::FastWriter fastWriter;
340 std::string jsonMessage = fastWriter.write(query);
341 jsonMessage.erase(std::remove(jsonMessage.begin(), jsonMessage.end(), '\n'), jsonMessage.end());
342 std::shared_ptr<ndn::Interest> queryInterest
343 = std::make_shared<ndn::Interest>(ndn::Name("/test/query").append(jsonMessage.c_str()));
344
345 queryAdapterTest2.queryTest(queryInterest);
Chengyu Fan7b978f82015-12-09 17:03:23 -0700346
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600347 // TODO: the code below should be enabled when queryAdapter can get the correct the
348 // ChronoSync state; currently, we don't need the activeQuery to save the ACK data;
349 //auto ackData = queryAdapterTest2.getDataFromActiveQuery(jsonMessage);
350 //
351 //BOOST_CHECK(ackData);
352 //if (ackData) {
353 // BOOST_CHECK_EQUAL(ackData->getName().getPrefix(3),
354 // ndn::Name("/test/query/%7B%22name%22%3A%22test%22%7D"));
355 // BOOST_CHECK_EQUAL(ackData->getName().at(ackData->getName().size() - 1),
356 // ndn::Name::Component("OK"));
357 // BOOST_CHECK_EQUAL(ackData->getContent().value_size(), 0);
358 //}
Chengyu Fanb25835b2015-04-28 17:09:35 -0600359
Chengyu Fan7b978f82015-12-09 17:03:23 -0700360 // there is no query-results namespace data
361 auto replyData = queryAdapterTest2.getDataFromCache(*queryInterest);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600362 BOOST_CHECK(replyData);
363 if (replyData){
Chengyu Fan7b978f82015-12-09 17:03:23 -0700364 BOOST_CHECK_EQUAL(replyData->getName().getPrefix(2), ndn::Name("/test/query"));
Chengyu Fan46398212015-08-11 11:23:13 -0600365 const std::string jsonRes(reinterpret_cast<const char*>(replyData->getContent().value()),
366 replyData->getContent().value_size());
Chengyu Fanb25835b2015-04-28 17:09:35 -0600367 Json::Value parsedFromString;
368 Json::Reader reader;
369 BOOST_CHECK_EQUAL(reader.parse(jsonRes, parsedFromString), true);
Chengyu Fan92440162015-07-09 14:43:31 -0600370 BOOST_CHECK_EQUAL(parsedFromString["resultCount"], 3);
Chengyu Fanb25835b2015-04-28 17:09:35 -0600371 BOOST_CHECK_EQUAL(parsedFromString["results"].size(), 3);
372 BOOST_CHECK_EQUAL(parsedFromString["results"][0], "/ndn/test1");
373 BOOST_CHECK_EQUAL(parsedFromString["results"][1], "/ndn/test2");
374 BOOST_CHECK_EQUAL(parsedFromString["results"][2], "/ndn/test3");
375 }
376 }
377
Chengyu Fan92440162015-07-09 14:43:31 -0600378 BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlSuccessTest)
379 {
380 initializeQueryAdapterTest2();
381
Chengyu Fan31737f12016-01-12 21:08:50 -0700382 std::stringstream ss, nameField;
Chengyu Fan92440162015-07-09 14:43:31 -0600383 Json::Value testJson;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600384 bool lastComponent = false;
Chengyu Fan92440162015-07-09 14:43:31 -0600385 testJson["?"] = "/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600386 BOOST_CHECK_EQUAL(true,
Chengyu Fan31737f12016-01-12 21:08:50 -0700387 queryAdapterTest2.json2AutocompletionSqlTest(ss,
388 testJson,
389 lastComponent,
390 nameField));
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600391 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan31737f12016-01-12 21:08:50 -0700392 BOOST_CHECK_EQUAL(";", ss.str());
393 BOOST_CHECK_EQUAL("activity", nameField.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600394
395 ss.str("");
396 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700397 nameField.str("");
398 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600399 testJson.clear();
400 testJson["?"] = "/Activity/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600401 BOOST_CHECK_EQUAL(true,
Chengyu Fan31737f12016-01-12 21:08:50 -0700402 queryAdapterTest2.json2AutocompletionSqlTest(ss,
403 testJson,
404 lastComponent,
405 nameField));
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600406 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan31737f12016-01-12 21:08:50 -0700407 BOOST_CHECK_EQUAL(" WHERE activity='Activity';", ss.str());
408 BOOST_CHECK_EQUAL("product", nameField.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600409
410 ss.str("");
411 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700412 nameField.str("");
413 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600414 testJson.clear();
415 testJson["?"] = "/Activity/Product/Organization/Model/Experiment/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600416 BOOST_CHECK_EQUAL(true,
Chengyu Fan31737f12016-01-12 21:08:50 -0700417 queryAdapterTest2.json2AutocompletionSqlTest(ss,
418 testJson,
419 lastComponent,
420 nameField));
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600421 BOOST_CHECK_EQUAL(lastComponent, false);
Chengyu Fan31737f12016-01-12 21:08:50 -0700422 BOOST_CHECK_EQUAL(" WHERE activity='Activity' AND \
Chengyu Fan46398212015-08-11 11:23:13 -0600423experiment='Experiment' AND model='Model' AND organization='Organization' AND product='Product';",
424 ss.str());
Chengyu Fan31737f12016-01-12 21:08:50 -0700425 BOOST_CHECK_EQUAL("frequency", nameField.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600426
427 ss.str("");
428 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700429 nameField.str("");
430 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600431 testJson.clear();
432 testJson["?"] = "/Activity/Product/Organization/Model/Experiment/Frequency/Modeling/\
433Variable/Ensemble/";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600434 BOOST_CHECK_EQUAL(true,
Chengyu Fan31737f12016-01-12 21:08:50 -0700435 queryAdapterTest2.json2AutocompletionSqlTest(ss,
436 testJson,
437 lastComponent,
438 nameField));
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600439 BOOST_CHECK_EQUAL(lastComponent, true);
Chengyu Fan31737f12016-01-12 21:08:50 -0700440 BOOST_CHECK_EQUAL(" WHERE activity='Activity' AND ensemble=\
Chengyu Fan46398212015-08-11 11:23:13 -0600441'Ensemble' AND experiment='Experiment' AND frequency='Frequency' AND model='Model' AND \
442modeling_realm='Modeling' AND organization='Organization' AND product='Product' AND variable_name=\
443'Variable';",ss.str());
Chengyu Fan31737f12016-01-12 21:08:50 -0700444 BOOST_CHECK_EQUAL("time", nameField.str());
Chengyu Fan92440162015-07-09 14:43:31 -0600445 }
446
447 BOOST_AUTO_TEST_CASE(QueryAdapterAutocompletionSqlFailTest)
448 {
449 initializeQueryAdapterTest2();
450
Chengyu Fan31737f12016-01-12 21:08:50 -0700451 std::stringstream ss, nameField;
Chengyu Fan92440162015-07-09 14:43:31 -0600452 Json::Value testJson;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600453 bool lastComponent = false;
Chengyu Fan92440162015-07-09 14:43:31 -0600454 testJson["?"] = "serchTest";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600455 BOOST_CHECK_EQUAL(false,
Chengyu Fan31737f12016-01-12 21:08:50 -0700456 queryAdapterTest2.json2AutocompletionSqlTest(ss,
457 testJson,
458 lastComponent,
459 nameField));
Chengyu Fan92440162015-07-09 14:43:31 -0600460
461 ss.str("");
462 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700463 nameField.str("");
464 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600465 testJson.clear();
466 testJson["?"] = "/cmip5";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600467 BOOST_CHECK_EQUAL(false,
Chengyu Fan31737f12016-01-12 21:08:50 -0700468 queryAdapterTest2.json2AutocompletionSqlTest(ss,
469 testJson,
470 lastComponent,
471 nameField));
Chengyu Fan92440162015-07-09 14:43:31 -0600472
473 ss.str("");
474 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700475 nameField.str("");
476 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600477 Json::Value testJson2; //simply clear does not work
478 testJson2[0] = "test";
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600479 BOOST_CHECK_EQUAL(false,
Chengyu Fan31737f12016-01-12 21:08:50 -0700480 queryAdapterTest2.json2AutocompletionSqlTest(ss,
481 testJson,
482 lastComponent,
483 nameField));
Chengyu Fan92440162015-07-09 14:43:31 -0600484
485 ss.str("");
486 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700487 nameField.str("");
488 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600489 Json::Value testJson3;
490 testJson3 = Json::Value(Json::arrayValue);
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600491 BOOST_CHECK_EQUAL(false,
Chengyu Fan31737f12016-01-12 21:08:50 -0700492 queryAdapterTest2.json2AutocompletionSqlTest(ss,
493 testJson,
494 lastComponent,
495 nameField));
Chengyu Fan92440162015-07-09 14:43:31 -0600496
497 ss.str("");
498 ss.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700499 nameField.str("");
500 nameField.clear();
Chengyu Fan92440162015-07-09 14:43:31 -0600501 Json::Value testJson4;
502 Json::Value param;
503 param[0] = "test";
504 testJson4["name"] = param;
Chengyu Fan3b9bb342015-09-21 10:53:37 -0600505 BOOST_CHECK_EQUAL(false,
Chengyu Fan31737f12016-01-12 21:08:50 -0700506 queryAdapterTest2.json2AutocompletionSqlTest(ss,
507 testJson,
508 lastComponent,
509 nameField));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600510 }
511
Chengyu Fan31737f12016-01-12 21:08:50 -0700512 BOOST_AUTO_TEST_CASE(QueryAdapterDoFilterBasedSearchTest)
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600513 {
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600514 Json::Value testJson;
Chengyu Fan31737f12016-01-12 21:08:50 -0700515 testJson["activity"] = "testActivity";
516 testJson["product"] = "testProduct";
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600517
Chengyu Fan31737f12016-01-12 21:08:50 -0700518 std::vector<std::pair<std::string, std::string>> resultComponents;
519
520 BOOST_CHECK_EQUAL(true, queryAdapterTest1.testDoFilterBasedSearch(testJson, resultComponents));
521 BOOST_CHECK_EQUAL(2, resultComponents.size());
522 for (auto it=resultComponents.begin(); it != resultComponents.end(); it++) {
523 if (it->first == "activity")
524 BOOST_CHECK_EQUAL(it->second, "testActivity");
525 if (it->first == "product")
526 BOOST_CHECK_EQUAL(it->second, "testProduct");
527 }
528
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600529 testJson.clear();
Chengyu Fan31737f12016-01-12 21:08:50 -0700530 resultComponents.clear();
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600531
Chengyu Fan31737f12016-01-12 21:08:50 -0700532 BOOST_CHECK_EQUAL(true, queryAdapterTest1.testDoFilterBasedSearch(testJson, resultComponents));
533 BOOST_CHECK_EQUAL(0, resultComponents.size());
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600534
Chengyu Fan31737f12016-01-12 21:08:50 -0700535 testJson.clear();
536 resultComponents.clear();
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600537
Chengyu Fan31737f12016-01-12 21:08:50 -0700538 testJson["name"] = Json::nullValue;
539 BOOST_CHECK_EQUAL(false, queryAdapterTest1.testDoFilterBasedSearch(testJson, resultComponents));
540
541 testJson.clear();
542 resultComponents.clear();
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600543 Json::Value param;
544 param[0] = "test";
Chengyu Fan31737f12016-01-12 21:08:50 -0700545 testJson["name"] = param;
546
547 BOOST_CHECK_EQUAL(false, queryAdapterTest1.testDoFilterBasedSearch(testJson, resultComponents));
548
549 testJson.clear();
550 resultComponents.clear();
551 Json::Value tmp;
552 tmp[0] = "test";
553
554 BOOST_CHECK_EQUAL(false, queryAdapterTest1.testDoFilterBasedSearch(tmp, resultComponents));
555 }
556
557 BOOST_AUTO_TEST_CASE(QueryAdapterDoPrefixBasedSearchTest)
558 {
559 Json::Value testJson;
560 testJson["??"] = "/";
561
562 std::vector<std::pair<std::string, std::string>> resultComponents;
563
564 BOOST_CHECK_EQUAL(true, queryAdapterTest2.testDoPrefixBasedSearch(testJson, resultComponents));
565 BOOST_CHECK_EQUAL(0, resultComponents.size());
566
567 testJson.clear();
568 resultComponents.clear();
569
570 testJson["??"] = "/Activity/Product";
571 BOOST_CHECK_EQUAL(true, queryAdapterTest2.testDoPrefixBasedSearch(testJson, resultComponents));
572 BOOST_CHECK_EQUAL(2, resultComponents.size());
573
574 for (auto it=resultComponents.begin(); it != resultComponents.end(); it++) {
575 if (it->first == "activity")
576 BOOST_CHECK_EQUAL(it->second, "Activity");
577 if (it->first == "product")
578 BOOST_CHECK_EQUAL(it->second, "Product");
579 }
580
581 testJson.clear();
582 resultComponents.clear();
583
584 testJson["??"] = "";
585 BOOST_CHECK_EQUAL(false, queryAdapterTest2.testDoPrefixBasedSearch(testJson, resultComponents));
586
587 testJson.clear();
588 resultComponents.clear();
589
590 testJson = Json::Value(Json::arrayValue);
591 BOOST_CHECK_EQUAL(false, queryAdapterTest2.testDoPrefixBasedSearch(testJson, resultComponents));
592
593 testJson.clear();
594 resultComponents.clear();
595
596 Json::Value tmp;
597 tmp[0] = "test";
598 BOOST_CHECK_EQUAL(false, queryAdapterTest2.testDoPrefixBasedSearch(tmp, resultComponents));
599
600 testJson.clear();
601 resultComponents.clear();
602
603 Json::Value param, testJson2;
604 param[0] = "test";
605 testJson2["activity"] = param;
606 BOOST_CHECK_EQUAL(false, queryAdapterTest2.testDoPrefixBasedSearch(testJson2, resultComponents));
Chengyu Fanf4c747a2015-08-18 13:56:01 -0600607 }
608
Chengyu Fanb25835b2015-04-28 17:09:35 -0600609 BOOST_AUTO_TEST_SUITE_END()
610
611}//tests
612}//atmos