hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxIndexTest.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 #include <vector>
3 #include <assert.h>
4 #include <sstream>
5 #include <iomanip>
6 #include <fstream>
7 
8 #include "EncodeDecodeBase64.hpp"
9 #include "SphinxIndexTest.hpp"
10 #include "SphinxSearchTest.hpp"
11 #include "SphinxManageTest.hpp"
14 #include "SphinxDataSource.hpp"
19 #include "SphinxPrintStatus.hpp"
20 
21 namespace HCE
22 {
23 namespace sphinx
24 {
25 namespace tests
26 {
27 //-----------------------------------------------------------------------------
29 {
30  const std::string testOutFile = "./out.xml";
31 
32  std::stringstream inSchema, inDocument;
33  inSchema << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<sphinx:docset xmlns:sphinx=\"http://sphinxsearch.com\">\n<sphinx:schema>\n<sphinx:field name=\"title\"/>\n<sphinx:field name=\"h\"/>\n</sphinx:schema>\n</sphinx:docset>";
34 
35  inDocument << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<sphinx:docset xmlns:sphinx=\"http://sphinxsearch.com\">\n<sphinx:document id=\"123\">\n"
36  << "<title><![CDATA[[- Information interchange - Representation of dates and times. A discussion of ISO 8601 has been written by Markus Kuhn. ISO 8601 describes a large number of date/time formats. For example it defines Basic Format, without punctuation, and Extended Format, with punctuation, and it allows elements to be omitted. This profile defines a restricted range of formats, all of which are valid ISO 8601 dates and times. The aim is to simplify the use of ISO 8601 in World Wide Web-related standards, and to avoid the need for the developers and users of these standards to obtain copies of ISO 8601 itself.]]></title>\n"
37  << "<h><![CDATA[[time, so this profile defines six levels. Standards that reference this profile should specify one or more of these granularities. If a given standard allows more than one granularity, it should specify the meaning of the dates and times with reduced precision, for example, the result of comparing two dates with different precisions. The formats are as follows. Exactly the components shown here must be present, with exactly this punctuation. Note that the \"T\" appears literally in the string, to indicate the beginning of the time element, as specified in ISO 8601. Year: YYYY (eg 1997) Year and month: YYYY-MM (eg 1997-07)]]></h>\n"
38  << "</sphinx:document>\n</sphinx:docset>";
39 
40  SphinxDataSource dataSource(inSchema, inDocument);
41 
42  std::fstream outSource(testOutFile.c_str(), std::fstream::out | std::fstream::trunc);
43  ASSERT_TRUE(outSource.is_open());
44 
45  outSource << dataSource;
46  outSource.close();
47  ASSERT_FALSE(dataSource.isError());
48  ASSERT_TRUE(dataSource.getErrorMsg().empty());
49  ASSERT_TRUE(remove((testOutFile).c_str())==0);
50  printSuccess("Test create document storage");
51 }
52 //-----------------------------------------------------------------------------
53 std::string SphinxIndexTest::makeJson(unsigned int type)
54 {
55  std::string json;
56 
57  if (type==0)
58  {
60  msgSearch.setQueryString("(piece -war)");
61  msgSearch.addQueryParameters("queryId", "777");
62  msgSearch.addQueryParameters("JsonType", "7");
63  msgSearch.serialize(json);
64  }
65  else if (type==1)
66  {
68  msgIndex.setIndexName("branch_0001");
69  msgIndex.setDocumentFile("");
70  msgIndex.addParameters("n1", "1");
71  msgIndex.addParameters("n2", "2");
72  msgIndex.addParameters("n3", "3");
73  msgIndex.serialize(json);
74  }
75  else if (type==2)
76  {
77  SphinxInputJsonMessageManage msgManage;
78  msgManage.setCommandString("Add index");
79  msgManage.setCommandOptionsString("--All --rotate");
80  msgManage.serialize(json);
81  }
82  else
83  std::cerr << "Unknown type of handler\n";
84 
85  return json;
86 }
87 //-----------------------------------------------------------------------------
88 void SphinxIndexTest::testInputJsonSearch(void)
89 {
90  std::string json = makeJson(0);
91 
92  Poco::SharedPtr<SphinxInputJsonMessageSearch> msgSearch(new SphinxInputJsonMessageSearch());
93  msgSearch->unserialize(json);
94  ASSERT_FALSE(msgSearch->isError());
95 
96  std::stringstream str(json);
97  str >> *msgSearch;
98  std::stringstream out;
99  out << *msgSearch;
100  ASSERT_FALSE(msgSearch->isError());
101 
102 // std::cout << "json: " << json << std::endl;
103 // std::cout << " out: " << out.str() << std::endl;
104 
105  ASSERT_TRUE(json==out.str());
106 
107  Poco::SharedPtr<SphinxInputJsonMessage> handler(new SphinxInputJsonMessage());
108  handler->setData(json);
109 
110  json = "";
111  handler->serialize(json);
112  ASSERT_FALSE(handler->isError());
113  handler->getData().clear();
114  handler->unserialize(json);
115  out.str("");
116  out << *handler;
117  ASSERT_FALSE(handler->isError());
118 
119 // std::cout << "json: " << json << std::endl;
120 // std::cout << " out: " << out.str() << std::endl;
121 
122  ASSERT_TRUE(json==out.str());
123  printSuccess("Test input json search");
124 }
125 //-----------------------------------------------------------------------------
126 void SphinxIndexTest::testInputJsonIndex(void)
127 {
128  std::string json = makeJson(1);
129 
130  Poco::SharedPtr<SphinxInputJsonMessageIndex> msgIndex(new SphinxInputJsonMessageIndex());
131  msgIndex->unserialize(json);
132 
133  std::stringstream str(json);
134  str >> *msgIndex;
135  ASSERT_FALSE(msgIndex->isError());
136  std::stringstream out;
137  out << *msgIndex;
138  ASSERT_FALSE(msgIndex->isError());
139 
140 // std::cout << "json: " << json << std::endl;
141 // std::cout << " out: " << out.str() << std::endl;
142 
143  ASSERT_TRUE(json==out.str());
144 
145  SphinxInputJsonMessage handler(SphinxInputJsonMessage::MessageType::mtIndex);
146  handler.setData(json);
147 
148  json = "";
149  handler.serialize(json);
150  ASSERT_FALSE(handler.isError());
151  handler.getData().clear();
152  handler.unserialize(json);
153  out.str("");
154  out << handler;
155  ASSERT_FALSE(handler.isError());
156 
157 // std::cout << "json: " << json << std::endl;
158 // std::cout << " out: " << out.str() << std::endl;
159 
160  ASSERT_TRUE(json==out.str());
161  printSuccess("Test input json index");
162 }
163 //-----------------------------------------------------------------------------
164 void SphinxIndexTest::testInputJsonManage(void)
165 {
166  std::string json = makeJson(2);
167 
168  Poco::SharedPtr<SphinxInputJsonMessageManage> msgManage(new SphinxInputJsonMessageManage());
169  msgManage->unserialize(json);
170 
171  std::stringstream str(json);
172  str >> *msgManage;
173  ASSERT_FALSE(msgManage->isError());
174  std::stringstream out;
175  out << *msgManage;
176  ASSERT_FALSE(msgManage->isError());
177 
178 // std::cout << "json: " << json << std::endl;
179 // std::cout << " out: " << out.str() << std::endl;
180 
181  ASSERT_TRUE(json==out.str());
182 
183  SphinxInputJsonMessage handler(SphinxInputJsonMessage::MessageType::mtManage);
184  handler.setData(json);
185 
186  json = "";
187  handler.serialize(json);
188  ASSERT_FALSE(handler.isError());
189  handler.getData().clear();
190  handler.unserialize(json);
191  out.str("");
192  out << handler;
193  ASSERT_FALSE(handler.isError());
194 
195 // std::cout << "json: " << json << std::endl;
196 // std::cout << " out: " << out.str() << std::endl;
197 
198  ASSERT_TRUE(json==out.str());
199  printSuccess("Test input json manage");
200 }
201 //-----------------------------------------------------------------------------
203 {
204  testInputJsonSearch();
205  testInputJsonIndex();
206  testInputJsonManage();
207 }
208 //-----------------------------------------------------------------------------
210 {
211  const std::string schemaContent =
212  "<?xml version=\"1.0\" encoding=\"utf-8\"?> \n\
213  <sphinx:docset xmlns:sphinx=\"http://sphinxsearch.com\">\n\
214  <sphinx:schema>\n\
215  <sphinx:field name=\"bodyn\"/>\n\
216  <sphinx:attr name=\"cdate\" type=\"timestamp\"/>\n\
217  </sphinx:schema>\n\
218  </sphinx:docset>\n";
219 
220  const std::string documentContents [2] =
221  {"<sphinx:docset xmlns:sphinx=\"http://sphinxsearch.com\">\n\
222  <sphinx:document id=\"1111\">\n\
223  <bodyn>Some body for test...</bodyn>\n\
224  <cdate>1232244134</cdate>\n\
225  </sphinx:document>\n\
226  \n\
227  <sphinx:document id=\"2222\">\n\
228  <bodyn>Next some body for test...</bodyn>\n\
229  <cdate>1232567575</cdate>\n\
230  </sphinx:document>\n\
231  </sphinx:docset>\n",
232 
233  "<sphinx:docset xmlns:sphinx=\"http://sphinxsearch.com\">\n\
234  <sphinx:document id=\"3333\">\n\
235  <bodyn>Some body for test...</bodyn>\n\
236  <cdate>1232244134</cdate>\n\
237  </sphinx:document>\n\
238  \n\
239  <sphinx:document id=\"4444\">\n\
240  <bodyn>Next some body for test...</bodyn>\n\
241  <cdate>1232567575</cdate>\n\
242  </sphinx:document>\n\
243  </sphinx:docset>\n"};
244 
245  const std::string nodeName = "test_node";
246  const std::string indexName = "i003";
247 
248  SphinxManageTest::testIndexCreate(fObj, indexName);
249  SphinxManageTest::testIndexCheck(fObj, indexName, true);
250  SphinxManageTest::testIndexStoreSchemaFile(fObj, indexName, schemaContent);
251 
252  const size_t jsonType = 16;
253  for (size_t i=0;i<jsonType;++i)
254  {
255  std::string json;
256  Poco::SharedPtr<SphinxInputJsonMessageIndex> msgIndex = new SphinxInputJsonMessageIndex();
257  msgIndex->setIndexName(indexName);
258  msgIndex->setDocumentFile(HCE::encodeBase64(documentContents[0]));
259  msgIndex->addParameters("params", "111");
260  msgIndex->serialize(json);
261 
262  SphinxInputJsonMessage handler(SphinxInputJsonMessage::MessageType::mtIndex);
263  handler.setData(json);
264 
265  std::stringstream str;
266  str << handler;
267 
268  std::string ret = fObj.Process(str.str());
269 
270  std::cout << "ret: '" << ret << "'\n";
271 
272  if (!fObj.getErrorMsg().empty())
273  std::cout << "ErrorMsg: '" << fObj.getErrorMsg() << "'\n";
274 
275  if (fObj.getErrorCode())
276  std::cout << "ErrorCode = " << fObj.getErrorCode() << std::endl;
277 
278  ASSERT_TRUE(fObj.getErrorMsg().empty());
279 
280  SphinxOutputJsonMessage ouputJsonMessage(ret);
281  assert(!ouputJsonMessage.isError());
282  assert(ouputJsonMessage.getErrorCode()==0);
283  assert(ouputJsonMessage.getErrorMessage().empty());
284 
285 
287  SphinxSearchTest::testSearcherProcess(fObject, "for", 4);
288  ASSERT_FALSE(fObject.isError());
289  }
290  SphinxManageTest::testIndexRemove(fObj, indexName);
291  printSuccess("Test Indexer Process");
292 }
293 //-----------------------------------------------------------------------------
294 //-----------------------------------------------------------------------------
295 } // end namespace tests
296 } // end namespace sphinx
297 } // end namespace HCE
298 
299 
300 
301 
302