XmlParserTest.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-26
  6. * Changed: 2023-05-16
  7. *
  8. * */
  9. #include <classes/TestHelper.hpp>
  10. #include <gtest/gtest.h>
  11. #include <ls-std/ls-std-core.hpp>
  12. #include <ls-std/ls-std-io.hpp>
  13. using ls::std::core::IllegalArgumentException;
  14. using ls::std::core::type::byte_field;
  15. using ls::std::io::File;
  16. using ls::std::io::FileReader;
  17. using ls::std::io::XmlAttribute;
  18. using ls::std::io::XmlDocument;
  19. using ls::std::io::XmlNode;
  20. using ls::std::io::XmlParser;
  21. using ls::std::test::TestHelper;
  22. using std::list;
  23. using std::make_shared;
  24. using std::shared_ptr;
  25. using std::string;
  26. using testing::Test;
  27. namespace
  28. {
  29. class XmlParserTest : public Test
  30. {
  31. public:
  32. XmlParserTest() = default;
  33. ~XmlParserTest() override = default;
  34. static shared_ptr<XmlAttribute> getAttributeAtPosition(const list<shared_ptr<XmlAttribute>> &_attributes, size_t _index)
  35. {
  36. shared_ptr<XmlAttribute> attribute{};
  37. if (_index < _attributes.size())
  38. {
  39. size_t counter{};
  40. for (const auto &_value : _attributes)
  41. {
  42. if (counter == _index)
  43. {
  44. attribute = _value;
  45. break;
  46. }
  47. counter++;
  48. }
  49. }
  50. return attribute;
  51. }
  52. static shared_ptr<XmlNode> getChildAtPosition(const list<shared_ptr<XmlNode>> &_children, size_t _index)
  53. {
  54. shared_ptr<XmlNode> node{};
  55. if (_index < _children.size())
  56. {
  57. size_t counter{};
  58. for (const auto &_value : _children)
  59. {
  60. if (counter == _index)
  61. {
  62. node = _value;
  63. break;
  64. }
  65. counter++;
  66. }
  67. }
  68. return node;
  69. }
  70. static byte_field readXmlStateMachine()
  71. {
  72. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  73. File file{xmlPath};
  74. byte_field data = FileReader{file}.read();
  75. return data;
  76. }
  77. };
  78. TEST_F(XmlParserTest, constructor)
  79. {
  80. shared_ptr<XmlDocument> document{};
  81. EXPECT_THROW(
  82. {
  83. try
  84. {
  85. XmlParser xmlParser = XmlParser{document};
  86. }
  87. catch (const IllegalArgumentException &_exception)
  88. {
  89. throw;
  90. }
  91. },
  92. IllegalArgumentException);
  93. }
  94. TEST_F(XmlParserTest, read)
  95. {
  96. XmlParser xmlParser{make_shared<XmlDocument>()};
  97. list<shared_ptr<XmlNode>> children{};
  98. list<shared_ptr<XmlNode>> statesChildren{};
  99. list<shared_ptr<XmlNode>> memoryChildren{};
  100. list<shared_ptr<XmlNode>> connectionChildren{};
  101. list<shared_ptr<XmlAttribute>> attributes{};
  102. byte_field data = readXmlStateMachine();
  103. xmlParser.parse(data);
  104. // check declaration
  105. ASSERT_STREQ("UTF-8", xmlParser.getDocument()->getDeclaration()->getEncoding().c_str());
  106. ASSERT_STREQ("1.0", xmlParser.getDocument()->getDeclaration()->getVersion().c_str());
  107. ASSERT_STREQ("yes", xmlParser.getDocument()->getDeclaration()->getStandalone().c_str());
  108. // check root element
  109. shared_ptr<XmlNode> root = xmlParser.getDocument()->getRootElement();
  110. ASSERT_STREQ("stateMachine", root->getName().c_str());
  111. ASSERT_STREQ("name", root->getAttributes().front()->getName().c_str());
  112. ASSERT_EQ(1, root->getAttributes().size());
  113. ASSERT_STREQ("test_machine", root->getAttributes().front()->getValue().c_str());
  114. // root children
  115. children = root->getChildren();
  116. ASSERT_EQ(3, children.size());
  117. ASSERT_STREQ("states", XmlParserTest::getChildAtPosition(children, 0)->getName().c_str());
  118. ASSERT_TRUE(XmlParserTest::getChildAtPosition(children, 0)->getAttributes().empty());
  119. ASSERT_STREQ("currentState", XmlParserTest::getChildAtPosition(children, 1)->getName().c_str());
  120. ASSERT_TRUE(XmlParserTest::getChildAtPosition(children, 1)->getAttributes().empty());
  121. ASSERT_STREQ("memory", XmlParserTest::getChildAtPosition(children, 2)->getName().c_str());
  122. ASSERT_TRUE(XmlParserTest::getChildAtPosition(children, 2)->getAttributes().empty());
  123. // states
  124. statesChildren = XmlParserTest::getChildAtPosition(children, 0)->getChildren();
  125. ASSERT_EQ(5, statesChildren.size());
  126. ASSERT_STREQ("state", XmlParserTest::getChildAtPosition(statesChildren, 0)->getName().c_str());
  127. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 0)->getAttributes().size());
  128. ASSERT_STREQ("id", XmlParserTest::getChildAtPosition(statesChildren, 0)->getAttributes().front()->getName().c_str());
  129. ASSERT_STREQ("A", XmlParserTest::getChildAtPosition(statesChildren, 0)->getAttributes().front()->getValue().c_str());
  130. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 0)->getValue().empty());
  131. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 0)->getChildren().size());
  132. ASSERT_STREQ("connections", XmlParserTest::getChildAtPosition(statesChildren, 0)->getChildren().front()->getName().c_str());
  133. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 0)->getChildren().front()->getValue().empty());
  134. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 0)->getChildren().front()->getAttributes().empty());
  135. connectionChildren = XmlParserTest::getChildAtPosition(statesChildren, 0)->getChildren().front()->getChildren();
  136. ASSERT_EQ(1, connectionChildren.size());
  137. ASSERT_STREQ("connection", XmlParserTest::getChildAtPosition(connectionChildren, 0)->getName().c_str());
  138. ASSERT_TRUE(XmlParserTest::getChildAtPosition(connectionChildren, 0)->getValue().empty());
  139. attributes = XmlParserTest::getChildAtPosition(connectionChildren, 0)->getAttributes();
  140. ASSERT_EQ(3, attributes.size());
  141. ASSERT_STREQ("connectionId", XmlParserTest::getAttributeAtPosition(attributes, 0)->getName().c_str());
  142. ASSERT_STREQ("AB", XmlParserTest::getAttributeAtPosition(attributes, 0)->getValue().c_str());
  143. ASSERT_STREQ("condition", XmlParserTest::getAttributeAtPosition(attributes, 1)->getName().c_str());
  144. ASSERT_STREQ("false", XmlParserTest::getAttributeAtPosition(attributes, 1)->getValue().c_str());
  145. ASSERT_STREQ("id", XmlParserTest::getAttributeAtPosition(attributes, 2)->getName().c_str());
  146. ASSERT_STREQ("B", XmlParserTest::getAttributeAtPosition(attributes, 2)->getValue().c_str());
  147. ASSERT_STREQ("state", XmlParserTest::getChildAtPosition(statesChildren, 1)->getName().c_str());
  148. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 1)->getAttributes().size());
  149. ASSERT_STREQ("id", XmlParserTest::getChildAtPosition(statesChildren, 1)->getAttributes().front()->getName().c_str());
  150. ASSERT_STREQ("B", XmlParserTest::getChildAtPosition(statesChildren, 1)->getAttributes().front()->getValue().c_str());
  151. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 1)->getValue().empty());
  152. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 1)->getChildren().size());
  153. ASSERT_STREQ("connections", XmlParserTest::getChildAtPosition(statesChildren, 1)->getChildren().front()->getName().c_str());
  154. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 1)->getChildren().front()->getValue().empty());
  155. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 1)->getChildren().front()->getAttributes().empty());
  156. connectionChildren = XmlParserTest::getChildAtPosition(statesChildren, 1)->getChildren().front()->getChildren();
  157. ASSERT_EQ(2, connectionChildren.size());
  158. ASSERT_STREQ("connection", XmlParserTest::getChildAtPosition(connectionChildren, 0)->getName().c_str());
  159. ASSERT_TRUE(XmlParserTest::getChildAtPosition(connectionChildren, 0)->getValue().empty());
  160. attributes = XmlParserTest::getChildAtPosition(connectionChildren, 0)->getAttributes();
  161. ASSERT_EQ(3, attributes.size());
  162. ASSERT_STREQ("connectionId", XmlParserTest::getAttributeAtPosition(attributes, 0)->getName().c_str());
  163. ASSERT_STREQ("BC", XmlParserTest::getAttributeAtPosition(attributes, 0)->getValue().c_str());
  164. ASSERT_STREQ("condition", XmlParserTest::getAttributeAtPosition(attributes, 1)->getName().c_str());
  165. ASSERT_STREQ("false", XmlParserTest::getAttributeAtPosition(attributes, 1)->getValue().c_str());
  166. ASSERT_STREQ("id", XmlParserTest::getAttributeAtPosition(attributes, 2)->getName().c_str());
  167. ASSERT_STREQ("C", XmlParserTest::getAttributeAtPosition(attributes, 2)->getValue().c_str());
  168. ASSERT_STREQ("connection", XmlParserTest::getChildAtPosition(connectionChildren, 1)->getName().c_str());
  169. ASSERT_TRUE(XmlParserTest::getChildAtPosition(connectionChildren, 1)->getValue().empty());
  170. attributes = XmlParserTest::getChildAtPosition(connectionChildren, 1)->getAttributes();
  171. ASSERT_EQ(3, attributes.size());
  172. ASSERT_STREQ("connectionId", XmlParserTest::getAttributeAtPosition(attributes, 0)->getName().c_str());
  173. ASSERT_STREQ("BD", XmlParserTest::getAttributeAtPosition(attributes, 0)->getValue().c_str());
  174. ASSERT_STREQ("condition", XmlParserTest::getAttributeAtPosition(attributes, 1)->getName().c_str());
  175. ASSERT_STREQ("false", XmlParserTest::getAttributeAtPosition(attributes, 1)->getValue().c_str());
  176. ASSERT_STREQ("id", XmlParserTest::getAttributeAtPosition(attributes, 2)->getName().c_str());
  177. ASSERT_STREQ("D", XmlParserTest::getAttributeAtPosition(attributes, 2)->getValue().c_str());
  178. ASSERT_STREQ("state", XmlParserTest::getChildAtPosition(statesChildren, 2)->getName().c_str());
  179. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 2)->getAttributes().size());
  180. ASSERT_STREQ("id", XmlParserTest::getChildAtPosition(statesChildren, 2)->getAttributes().front()->getName().c_str());
  181. ASSERT_STREQ("C", XmlParserTest::getChildAtPosition(statesChildren, 2)->getAttributes().front()->getValue().c_str());
  182. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 2)->getValue().empty());
  183. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 2)->getChildren().size());
  184. ASSERT_STREQ("connections", XmlParserTest::getChildAtPosition(statesChildren, 2)->getChildren().front()->getName().c_str());
  185. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 2)->getChildren().front()->getValue().empty());
  186. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 2)->getChildren().front()->getAttributes().empty());
  187. connectionChildren = XmlParserTest::getChildAtPosition(statesChildren, 2)->getChildren().front()->getChildren();
  188. ASSERT_EQ(1, connectionChildren.size());
  189. ASSERT_STREQ("connection", XmlParserTest::getChildAtPosition(connectionChildren, 0)->getName().c_str());
  190. ASSERT_TRUE(XmlParserTest::getChildAtPosition(connectionChildren, 0)->getValue().empty());
  191. attributes = XmlParserTest::getChildAtPosition(connectionChildren, 0)->getAttributes();
  192. ASSERT_EQ(3, attributes.size());
  193. ASSERT_STREQ("connectionId", XmlParserTest::getAttributeAtPosition(attributes, 0)->getName().c_str());
  194. ASSERT_STREQ("CE", XmlParserTest::getAttributeAtPosition(attributes, 0)->getValue().c_str());
  195. ASSERT_STREQ("condition", XmlParserTest::getAttributeAtPosition(attributes, 1)->getName().c_str());
  196. ASSERT_STREQ("false", XmlParserTest::getAttributeAtPosition(attributes, 1)->getValue().c_str());
  197. ASSERT_STREQ("id", XmlParserTest::getAttributeAtPosition(attributes, 2)->getName().c_str());
  198. ASSERT_STREQ("E", XmlParserTest::getAttributeAtPosition(attributes, 2)->getValue().c_str());
  199. ASSERT_STREQ("state", XmlParserTest::getChildAtPosition(statesChildren, 3)->getName().c_str());
  200. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 3)->getAttributes().size());
  201. ASSERT_STREQ("id", XmlParserTest::getChildAtPosition(statesChildren, 3)->getAttributes().front()->getName().c_str());
  202. ASSERT_STREQ("D", XmlParserTest::getChildAtPosition(statesChildren, 3)->getAttributes().front()->getValue().c_str());
  203. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 3)->getChildren().front()->getAttributes().empty());
  204. connectionChildren = XmlParserTest::getChildAtPosition(statesChildren, 3)->getChildren().front()->getChildren();
  205. ASSERT_EQ(1, connectionChildren.size());
  206. ASSERT_STREQ("connection", XmlParserTest::getChildAtPosition(connectionChildren, 0)->getName().c_str());
  207. ASSERT_TRUE(XmlParserTest::getChildAtPosition(connectionChildren, 0)->getValue().empty());
  208. attributes = XmlParserTest::getChildAtPosition(connectionChildren, 0)->getAttributes();
  209. ASSERT_EQ(3, attributes.size());
  210. ASSERT_STREQ("connectionId", XmlParserTest::getAttributeAtPosition(attributes, 0)->getName().c_str());
  211. ASSERT_STREQ("DE", XmlParserTest::getAttributeAtPosition(attributes, 0)->getValue().c_str());
  212. ASSERT_STREQ("condition", XmlParserTest::getAttributeAtPosition(attributes, 1)->getName().c_str());
  213. ASSERT_STREQ("false", XmlParserTest::getAttributeAtPosition(attributes, 1)->getValue().c_str());
  214. ASSERT_STREQ("id", XmlParserTest::getAttributeAtPosition(attributes, 2)->getName().c_str());
  215. ASSERT_STREQ("E", XmlParserTest::getAttributeAtPosition(attributes, 2)->getValue().c_str());
  216. ASSERT_STREQ("state", XmlParserTest::getChildAtPosition(statesChildren, 4)->getName().c_str());
  217. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 4)->getAttributes().size());
  218. ASSERT_STREQ("id", XmlParserTest::getChildAtPosition(statesChildren, 4)->getAttributes().front()->getName().c_str());
  219. ASSERT_STREQ("E", XmlParserTest::getChildAtPosition(statesChildren, 4)->getAttributes().front()->getValue().c_str());
  220. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 4)->getChildren().empty());
  221. // current state
  222. ASSERT_STREQ("currentState", XmlParserTest::getChildAtPosition(children, 1)->getName().c_str());
  223. ASSERT_STREQ("A", XmlParserTest::getChildAtPosition(children, 1)->getValue().c_str());
  224. ASSERT_TRUE(XmlParserTest::getChildAtPosition(children, 1)->getChildren().empty());
  225. ASSERT_TRUE(XmlParserTest::getChildAtPosition(children, 1)->getAttributes().empty());
  226. // memory
  227. memoryChildren = XmlParserTest::getChildAtPosition(children, 2)->getChildren();
  228. ASSERT_EQ(3, memoryChildren.size());
  229. ASSERT_STREQ("location", XmlParserTest::getChildAtPosition(memoryChildren, 0)->getName().c_str());
  230. ASSERT_STREQ("A", XmlParserTest::getChildAtPosition(memoryChildren, 0)->getValue().c_str());
  231. ASSERT_TRUE(XmlParserTest::getChildAtPosition(memoryChildren, 0)->getChildren().empty());
  232. ASSERT_TRUE(XmlParserTest::getChildAtPosition(memoryChildren, 0)->getAttributes().empty());
  233. ASSERT_STREQ("location", XmlParserTest::getChildAtPosition(memoryChildren, 1)->getName().c_str());
  234. ASSERT_STREQ("B", XmlParserTest::getChildAtPosition(memoryChildren, 1)->getValue().c_str());
  235. ASSERT_TRUE(XmlParserTest::getChildAtPosition(memoryChildren, 1)->getChildren().empty());
  236. ASSERT_TRUE(XmlParserTest::getChildAtPosition(memoryChildren, 1)->getAttributes().empty());
  237. ASSERT_STREQ("location", XmlParserTest::getChildAtPosition(memoryChildren, 2)->getName().c_str());
  238. ASSERT_STREQ("C", XmlParserTest::getChildAtPosition(memoryChildren, 2)->getValue().c_str());
  239. ASSERT_TRUE(XmlParserTest::getChildAtPosition(memoryChildren, 2)->getChildren().empty());
  240. ASSERT_TRUE(XmlParserTest::getChildAtPosition(memoryChildren, 2)->getAttributes().empty());
  241. }
  242. TEST_F(XmlParserTest, getDocument)
  243. {
  244. XmlParser xmlParser{make_shared<XmlDocument>()};
  245. ASSERT_TRUE(xmlParser.getDocument() != nullptr);
  246. }
  247. TEST_F(XmlParserTest, setDocument)
  248. {
  249. auto document = make_shared<XmlDocument>();
  250. XmlParser xmlParser{document};
  251. ASSERT_TRUE(xmlParser.getDocument() == document);
  252. document = make_shared<XmlDocument>();
  253. xmlParser.setDocument(document);
  254. ASSERT_TRUE(xmlParser.getDocument() == document);
  255. }
  256. TEST_F(XmlParserTest, setDocument_no_reference)
  257. {
  258. auto document = make_shared<XmlDocument>();
  259. XmlParser xmlParser{document};
  260. EXPECT_THROW(
  261. {
  262. try
  263. {
  264. xmlParser.setDocument(nullptr);
  265. }
  266. catch (const IllegalArgumentException &_exception)
  267. {
  268. throw;
  269. }
  270. },
  271. IllegalArgumentException);
  272. }
  273. }