XmlParserTest.cpp 16 KB

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