hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ORMFunctionalObjectTest.cpp
Go to the documentation of this file.
1 
14 #include "ORMFunctionalObjectTest.hpp"
15 
16 HCE::KVDB_TESTS::Document::Document() {}
17 
18 const std::string HCE::KVDB_TESTS::Document::json() {
19  std::string s_type = Poco::NumberFormatter::format(type);
20  items.set("type",s_type);
21  items.set("data",item);
22  std::ostringstream oss;
23  items.stringify(oss);
24  data = oss.str();
25  return data;
26 }
27 
28 void readDocuments(const std::vector<std::string>& vector_file_names, HCE::KVDB_TESTS::Document& doc)
29 {
33  Poco::JSON::Object item;
34  Poco::JSON::Array arr;
35  for(auto file_name : vector_file_names) {
36  Poco::File xml_file(file_name);
37  Poco::SharedMemory xml(xml_file, Poco::SharedMemory::AM_READ);
38 
39  try
40  {
41  Poco::XML::DOMParser parser;
42  Poco::XML::AutoPtr<Poco::XML::Document> pDoc = parser.parseString(xml.begin());
43 
44  Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ALL);
45  Poco::XML::Node* pNode = it.nextNode();
46  while (pNode)
47  {
51  Poco::XML::NamedNodeMap* pmap = pNode->attributes();
52  std::string key;
53  if(pmap) {
54  for(unsigned i=0; i<pmap->length(); ++i) {
55  Poco::XML::Node* p_node = pmap->item(i);
56  if(p_node->nodeName()=="id") {
57  key = p_node->nodeValue();
58  switch(doc.type) {
59  case HCE::KVDB_TESTS::Document::get :
60  case HCE::KVDB_TESTS::Document::del :
61  case HCE::KVDB_TESTS::Document::check : {
62  Poco::Dynamic::Var var(key);
63  arr.add(var);
64  break;
65  }
66  default: break;
67  }
68  }
69  //std::cout << (pmap->item(i))->nodeName() << ":" << (pmap->item(i))->nodeValue() << std::endl;
70  }
71  //pmap->autoRelease();
72  pmap->release();
73  }
74  if(doc.type == HCE::KVDB_TESTS::Document::put) {
78  if(pNode->nodeName()=="sphinx:document") {
79  std::string value;
80  value = pNode->innerText();
81  item.set(key, value);
82  //std::cout << pNode->innerText() << std::endl;
83  }
84  }
85  //std::cout << pNode->nodeName() << ":" << pNode->nodeValue() << std::endl;
86  pNode = it.nextNode();
87  }
88  }
89  catch (Poco::Exception& exc)
90  {
91  std::cerr << exc.displayText() << std::endl;
92  }
93  }
94  switch(doc.type) {
95  case HCE::KVDB_TESTS::Document::put : {
96  doc.item.set("documents",item);
97  break;
98  }
99  case HCE::KVDB_TESTS::Document::get :
100  case HCE::KVDB_TESTS::Document::del :
101  case HCE::KVDB_TESTS::Document::check : {
102  doc.item.set("documents",arr);
103  break;
104  }
105  default: break;
106  }
107 }
108 
109 
110 Poco::SharedPtr<HCE::ORMFunctionalObject> pOrmFuncObj;
111 
112 
117 TEST(ORMFunctionalObject, init)
118 {
119  HCE::DataBaseDriverConfig db_config;
120  db_config.setDataBaseName ("test");
121  db_config.setDataBaseUrl (HCE::KVDB_TESTS::URL);
122  pOrmFuncObj = Poco::SharedPtr<HCE::ORMFunctionalObject>(new HCE::ORMFunctionalObject(db_config));
123 }
124 
129 TEST(ORMFunctionalObjectTest, put_documents)
130 {
131  try {
132  HCE::KVDB_TESTS::Document doc;
133  doc.type = HCE::KVDB_TESTS::Document::put;
134  readDocuments(doc.vector_file_names, doc);
135  //std::cout << doc.json() << std::endl;
136  #ifdef _DEBUG_
137  std::cout << pOrmFuncObj->processJSON(doc.json()) << std::endl;
138  #endif
139  }
140  catch (const Poco::Exception& ex) {
141  std::cout << ex.displayText() << std::endl;
142  throw;
143  }
144 }
145 
146 
150 TEST(ORMFunctionalObjectTest, get_documents_by_id)
151 {
152  HCE::KVDB_TESTS::Document doc;
153  doc.type = HCE::KVDB_TESTS::Document::get;
154  readDocuments(doc.vector_file_names, doc);
155  #ifdef _DEBUG_
156  std::cout << doc.json() << std::endl;
157  std::cout << pOrmFuncObj->processJSON(doc.json()) << std::endl;
158  #endif
159 }
160 
161 
165 TEST(ORMFunctionalObjectTest, del_documents_by_id)
166 {
167  HCE::KVDB_TESTS::Document doc;
168  doc.type = HCE::KVDB_TESTS::Document::del;
169  readDocuments(doc.vector_file_names, doc);
170  #ifdef _DEBUG_
171  std::cout << doc.json() << std::endl;
172  std::cout << pOrmFuncObj->processJSON(doc.json()) << std::endl;
173  #endif
174 }
175 
176 /*
180 TEST(ORMFunctionalObjectTest, check_documents_by_id)
181 {
182  Document doc;
183  doc.type = Document::check;
184  readDocuments(vector_file_names, doc);
185 // std::cout << doc.json() << std::endl;
186  std::cout << pOrmFuncObj->processJSON(doc.json()) << std::endl;
187 }
188 
192 TEST(ORMFunctionalObjectTest, command_not_support)
193 {
194  Document doc;
195  doc.type = (a::Document::admin)-1;
196  readDocuments(vector_file_names, doc);
197  std::cout << doc.json() << std::endl;
198  std::cout << pOrmFuncObj->processJSON(doc.json()) << std::endl;
199 }
200 
204 TEST(ORMFunctionalObjectTest, invalid_message_type_put)
205 {
206  Document doc;
207  doc.type = Document::get;
208  readDocuments(vector_file_names, doc);
209  doc.type = Document::put;
210  std::cout << doc.json() << std::endl;
211  std::cout << pOrmFuncObj->processJSON(doc.json()) << std::endl;
212 }
213 
214 
218 TEST(ORMFunctionalObjectTest, invalid_message_type_get)
219 {
220  Document doc;
221  doc.type = Document::put;
222  readDocuments(vector_file_names, doc);
223  doc.type = Document::get;
224  std::cout << doc.json() << std::endl;
225  std::cout << pOrmFuncObj->processJSON(doc.json()) << std::endl;
226 }
227 
228 
232 TEST(ORMFunctionalObjectTest, invalid_json)
233 {
234  std::vector<std::string> jsons {
235  //Request protocol
236  //0 - PUT
237  "{\"type\":\"0\",\"data\":{\"documents\":{\"123\":\"content 123\",\"456\":\"content 456\"}}}"
238  //1,2,3 - GET, CHECK, DELETE
239  , "\{\"type\":\"1\",\"data\":{\"documents\":[\"123\",\"456\"]}}"
240 
241  //Response protocol
242  //0, 2, 3 - PUT, DELETE, CHECK
243  , "{\"error_code\":\"0\",\"error_message\":\"\",\"data\":{\"documents\":{\"123\":\"0\",\"456\":\"0\"}},\"time\":\"1\"}"
244  //1 - GET
245  , "{\"error_code\":\"0\",\"error_message\":\"\",\"data\":{\"documents\":{\"123\":\"content 123\",\"456\":\"content 456\"}}\",\"time\":\"1\"}"
246 
247  //wrong json
248  , "{\"error_code\":\"0\",\"error_message\":\"\",\"data\":{\"documents\":{\"123\":\"content 123\"\"456\":\"content 456\"}}\",\"time\":\"1\"}"
249  };
250  for(auto json:jsons) {
251  std::cout << pOrmFuncObj->processJSON(json) << std::endl;
252  }
253 }
254 */