XmlParserTest.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-26
  6. * Changed: 2023-03-25
  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, statesChildren, memoryChildren, connectionChildren{};
  98. list<shared_ptr<XmlAttribute>> attributes{};
  99. byte_field data = readXmlStateMachine();
  100. xmlParser.parse(data);
  101. // check declaration
  102. ASSERT_STREQ("UTF-8", xmlParser.getDocument()->getDeclaration()->getEncoding().c_str());
  103. ASSERT_STREQ("1.0", xmlParser.getDocument()->getDeclaration()->getVersion().c_str());
  104. ASSERT_STREQ("yes", xmlParser.getDocument()->getDeclaration()->getStandalone().c_str());
  105. // check root element
  106. shared_ptr<XmlNode> root = xmlParser.getDocument()->getRootElement();
  107. ASSERT_STREQ("stateMachine", root->getName().c_str());
  108. ASSERT_STREQ("name", root->getAttributes().front()->getName().c_str());
  109. ASSERT_EQ(1, root->getAttributes().size());
  110. ASSERT_STREQ("test_machine", root->getAttributes().front()->getValue().c_str());
  111. // root children
  112. children = root->getChildren();
  113. ASSERT_EQ(3, children.size());
  114. ASSERT_STREQ("states", XmlParserTest::getChildAtPosition(children, 0)->getName().c_str());
  115. ASSERT_TRUE(XmlParserTest::getChildAtPosition(children, 0)->getAttributes().empty());
  116. ASSERT_STREQ("currentState", XmlParserTest::getChildAtPosition(children, 1)->getName().c_str());
  117. ASSERT_TRUE(XmlParserTest::getChildAtPosition(children, 1)->getAttributes().empty());
  118. ASSERT_STREQ("memory", XmlParserTest::getChildAtPosition(children, 2)->getName().c_str());
  119. ASSERT_TRUE(XmlParserTest::getChildAtPosition(children, 2)->getAttributes().empty());
  120. // states
  121. statesChildren = XmlParserTest::getChildAtPosition(children, 0)->getChildren();
  122. ASSERT_EQ(5, statesChildren.size());
  123. ASSERT_STREQ("state", XmlParserTest::getChildAtPosition(statesChildren, 0)->getName().c_str());
  124. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 0)->getAttributes().size());
  125. ASSERT_STREQ("id", XmlParserTest::getChildAtPosition(statesChildren, 0)->getAttributes().front()->getName().c_str());
  126. ASSERT_STREQ("A", XmlParserTest::getChildAtPosition(statesChildren, 0)->getAttributes().front()->getValue().c_str());
  127. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 0)->getValue().empty());
  128. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 0)->getChildren().size());
  129. ASSERT_STREQ("connections", XmlParserTest::getChildAtPosition(statesChildren, 0)->getChildren().front()->getName().c_str());
  130. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 0)->getChildren().front()->getValue().empty());
  131. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 0)->getChildren().front()->getAttributes().empty());
  132. connectionChildren = XmlParserTest::getChildAtPosition(statesChildren, 0)->getChildren().front()->getChildren();
  133. ASSERT_EQ(1, connectionChildren.size());
  134. ASSERT_STREQ("connection", XmlParserTest::getChildAtPosition(connectionChildren, 0)->getName().c_str());
  135. ASSERT_TRUE(XmlParserTest::getChildAtPosition(connectionChildren, 0)->getValue().empty());
  136. attributes = XmlParserTest::getChildAtPosition(connectionChildren, 0)->getAttributes();
  137. ASSERT_EQ(3, attributes.size());
  138. ASSERT_STREQ("connectionId", XmlParserTest::getAttributeAtPosition(attributes, 0)->getName().c_str());
  139. ASSERT_STREQ("AB", XmlParserTest::getAttributeAtPosition(attributes, 0)->getValue().c_str());
  140. ASSERT_STREQ("condition", XmlParserTest::getAttributeAtPosition(attributes, 1)->getName().c_str());
  141. ASSERT_STREQ("false", XmlParserTest::getAttributeAtPosition(attributes, 1)->getValue().c_str());
  142. ASSERT_STREQ("id", XmlParserTest::getAttributeAtPosition(attributes, 2)->getName().c_str());
  143. ASSERT_STREQ("B", XmlParserTest::getAttributeAtPosition(attributes, 2)->getValue().c_str());
  144. ASSERT_STREQ("state", XmlParserTest::getChildAtPosition(statesChildren, 1)->getName().c_str());
  145. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 1)->getAttributes().size());
  146. ASSERT_STREQ("id", XmlParserTest::getChildAtPosition(statesChildren, 1)->getAttributes().front()->getName().c_str());
  147. ASSERT_STREQ("B", XmlParserTest::getChildAtPosition(statesChildren, 1)->getAttributes().front()->getValue().c_str());
  148. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 1)->getValue().empty());
  149. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 1)->getChildren().size());
  150. ASSERT_STREQ("connections", XmlParserTest::getChildAtPosition(statesChildren, 1)->getChildren().front()->getName().c_str());
  151. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 1)->getChildren().front()->getValue().empty());
  152. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 1)->getChildren().front()->getAttributes().empty());
  153. connectionChildren = XmlParserTest::getChildAtPosition(statesChildren, 1)->getChildren().front()->getChildren();
  154. ASSERT_EQ(2, connectionChildren.size());
  155. ASSERT_STREQ("connection", XmlParserTest::getChildAtPosition(connectionChildren, 0)->getName().c_str());
  156. ASSERT_TRUE(XmlParserTest::getChildAtPosition(connectionChildren, 0)->getValue().empty());
  157. attributes = XmlParserTest::getChildAtPosition(connectionChildren, 0)->getAttributes();
  158. ASSERT_EQ(3, attributes.size());
  159. ASSERT_STREQ("connectionId", XmlParserTest::getAttributeAtPosition(attributes, 0)->getName().c_str());
  160. ASSERT_STREQ("BC", XmlParserTest::getAttributeAtPosition(attributes, 0)->getValue().c_str());
  161. ASSERT_STREQ("condition", XmlParserTest::getAttributeAtPosition(attributes, 1)->getName().c_str());
  162. ASSERT_STREQ("false", XmlParserTest::getAttributeAtPosition(attributes, 1)->getValue().c_str());
  163. ASSERT_STREQ("id", XmlParserTest::getAttributeAtPosition(attributes, 2)->getName().c_str());
  164. ASSERT_STREQ("C", XmlParserTest::getAttributeAtPosition(attributes, 2)->getValue().c_str());
  165. ASSERT_STREQ("connection", XmlParserTest::getChildAtPosition(connectionChildren, 1)->getName().c_str());
  166. ASSERT_TRUE(XmlParserTest::getChildAtPosition(connectionChildren, 1)->getValue().empty());
  167. attributes = XmlParserTest::getChildAtPosition(connectionChildren, 1)->getAttributes();
  168. ASSERT_EQ(3, attributes.size());
  169. ASSERT_STREQ("connectionId", XmlParserTest::getAttributeAtPosition(attributes, 0)->getName().c_str());
  170. ASSERT_STREQ("BD", XmlParserTest::getAttributeAtPosition(attributes, 0)->getValue().c_str());
  171. ASSERT_STREQ("condition", XmlParserTest::getAttributeAtPosition(attributes, 1)->getName().c_str());
  172. ASSERT_STREQ("false", XmlParserTest::getAttributeAtPosition(attributes, 1)->getValue().c_str());
  173. ASSERT_STREQ("id", XmlParserTest::getAttributeAtPosition(attributes, 2)->getName().c_str());
  174. ASSERT_STREQ("D", XmlParserTest::getAttributeAtPosition(attributes, 2)->getValue().c_str());
  175. ASSERT_STREQ("state", XmlParserTest::getChildAtPosition(statesChildren, 2)->getName().c_str());
  176. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 2)->getAttributes().size());
  177. ASSERT_STREQ("id", XmlParserTest::getChildAtPosition(statesChildren, 2)->getAttributes().front()->getName().c_str());
  178. ASSERT_STREQ("C", XmlParserTest::getChildAtPosition(statesChildren, 2)->getAttributes().front()->getValue().c_str());
  179. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 2)->getValue().empty());
  180. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 2)->getChildren().size());
  181. ASSERT_STREQ("connections", XmlParserTest::getChildAtPosition(statesChildren, 2)->getChildren().front()->getName().c_str());
  182. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 2)->getChildren().front()->getValue().empty());
  183. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 2)->getChildren().front()->getAttributes().empty());
  184. connectionChildren = XmlParserTest::getChildAtPosition(statesChildren, 2)->getChildren().front()->getChildren();
  185. ASSERT_EQ(1, connectionChildren.size());
  186. ASSERT_STREQ("connection", XmlParserTest::getChildAtPosition(connectionChildren, 0)->getName().c_str());
  187. ASSERT_TRUE(XmlParserTest::getChildAtPosition(connectionChildren, 0)->getValue().empty());
  188. attributes = XmlParserTest::getChildAtPosition(connectionChildren, 0)->getAttributes();
  189. ASSERT_EQ(3, attributes.size());
  190. ASSERT_STREQ("connectionId", XmlParserTest::getAttributeAtPosition(attributes, 0)->getName().c_str());
  191. ASSERT_STREQ("CE", XmlParserTest::getAttributeAtPosition(attributes, 0)->getValue().c_str());
  192. ASSERT_STREQ("condition", XmlParserTest::getAttributeAtPosition(attributes, 1)->getName().c_str());
  193. ASSERT_STREQ("false", XmlParserTest::getAttributeAtPosition(attributes, 1)->getValue().c_str());
  194. ASSERT_STREQ("id", XmlParserTest::getAttributeAtPosition(attributes, 2)->getName().c_str());
  195. ASSERT_STREQ("E", XmlParserTest::getAttributeAtPosition(attributes, 2)->getValue().c_str());
  196. ASSERT_STREQ("state", XmlParserTest::getChildAtPosition(statesChildren, 3)->getName().c_str());
  197. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 3)->getAttributes().size());
  198. ASSERT_STREQ("id", XmlParserTest::getChildAtPosition(statesChildren, 3)->getAttributes().front()->getName().c_str());
  199. ASSERT_STREQ("D", XmlParserTest::getChildAtPosition(statesChildren, 3)->getAttributes().front()->getValue().c_str());
  200. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 3)->getChildren().front()->getAttributes().empty());
  201. connectionChildren = XmlParserTest::getChildAtPosition(statesChildren, 3)->getChildren().front()->getChildren();
  202. ASSERT_EQ(1, connectionChildren.size());
  203. ASSERT_STREQ("connection", XmlParserTest::getChildAtPosition(connectionChildren, 0)->getName().c_str());
  204. ASSERT_TRUE(XmlParserTest::getChildAtPosition(connectionChildren, 0)->getValue().empty());
  205. attributes = XmlParserTest::getChildAtPosition(connectionChildren, 0)->getAttributes();
  206. ASSERT_EQ(3, attributes.size());
  207. ASSERT_STREQ("connectionId", XmlParserTest::getAttributeAtPosition(attributes, 0)->getName().c_str());
  208. ASSERT_STREQ("DE", XmlParserTest::getAttributeAtPosition(attributes, 0)->getValue().c_str());
  209. ASSERT_STREQ("condition", XmlParserTest::getAttributeAtPosition(attributes, 1)->getName().c_str());
  210. ASSERT_STREQ("false", XmlParserTest::getAttributeAtPosition(attributes, 1)->getValue().c_str());
  211. ASSERT_STREQ("id", XmlParserTest::getAttributeAtPosition(attributes, 2)->getName().c_str());
  212. ASSERT_STREQ("E", XmlParserTest::getAttributeAtPosition(attributes, 2)->getValue().c_str());
  213. ASSERT_STREQ("state", XmlParserTest::getChildAtPosition(statesChildren, 4)->getName().c_str());
  214. ASSERT_EQ(1, XmlParserTest::getChildAtPosition(statesChildren, 4)->getAttributes().size());
  215. ASSERT_STREQ("id", XmlParserTest::getChildAtPosition(statesChildren, 4)->getAttributes().front()->getName().c_str());
  216. ASSERT_STREQ("E", XmlParserTest::getChildAtPosition(statesChildren, 4)->getAttributes().front()->getValue().c_str());
  217. ASSERT_TRUE(XmlParserTest::getChildAtPosition(statesChildren, 4)->getChildren().empty());
  218. // current state
  219. ASSERT_STREQ("currentState", XmlParserTest::getChildAtPosition(children, 1)->getName().c_str());
  220. ASSERT_STREQ("A", XmlParserTest::getChildAtPosition(children, 1)->getValue().c_str());
  221. ASSERT_TRUE(XmlParserTest::getChildAtPosition(children, 1)->getChildren().empty());
  222. ASSERT_TRUE(XmlParserTest::getChildAtPosition(children, 1)->getAttributes().empty());
  223. // memory
  224. memoryChildren = XmlParserTest::getChildAtPosition(children, 2)->getChildren();
  225. ASSERT_EQ(3, memoryChildren.size());
  226. ASSERT_STREQ("location", XmlParserTest::getChildAtPosition(memoryChildren, 0)->getName().c_str());
  227. ASSERT_STREQ("A", XmlParserTest::getChildAtPosition(memoryChildren, 0)->getValue().c_str());
  228. ASSERT_TRUE(XmlParserTest::getChildAtPosition(memoryChildren, 0)->getChildren().empty());
  229. ASSERT_TRUE(XmlParserTest::getChildAtPosition(memoryChildren, 0)->getAttributes().empty());
  230. ASSERT_STREQ("location", XmlParserTest::getChildAtPosition(memoryChildren, 1)->getName().c_str());
  231. ASSERT_STREQ("B", XmlParserTest::getChildAtPosition(memoryChildren, 1)->getValue().c_str());
  232. ASSERT_TRUE(XmlParserTest::getChildAtPosition(memoryChildren, 1)->getChildren().empty());
  233. ASSERT_TRUE(XmlParserTest::getChildAtPosition(memoryChildren, 1)->getAttributes().empty());
  234. ASSERT_STREQ("location", XmlParserTest::getChildAtPosition(memoryChildren, 2)->getName().c_str());
  235. ASSERT_STREQ("C", XmlParserTest::getChildAtPosition(memoryChildren, 2)->getValue().c_str());
  236. ASSERT_TRUE(XmlParserTest::getChildAtPosition(memoryChildren, 2)->getChildren().empty());
  237. ASSERT_TRUE(XmlParserTest::getChildAtPosition(memoryChildren, 2)->getAttributes().empty());
  238. }
  239. TEST_F(XmlParserTest, getDocument)
  240. {
  241. XmlParser xmlParser{make_shared<XmlDocument>()};
  242. ASSERT_TRUE(xmlParser.getDocument() != nullptr);
  243. }
  244. TEST_F(XmlParserTest, setDocument)
  245. {
  246. shared_ptr<XmlDocument> document = make_shared<XmlDocument>();
  247. XmlParser xmlParser{document};
  248. ASSERT_TRUE(xmlParser.getDocument() == document);
  249. document = make_shared<XmlDocument>();
  250. xmlParser.setDocument(document);
  251. ASSERT_TRUE(xmlParser.getDocument() == document);
  252. }
  253. TEST_F(XmlParserTest, setDocument_no_reference)
  254. {
  255. shared_ptr<XmlDocument> document = make_shared<XmlDocument>();
  256. XmlParser xmlParser{document};
  257. EXPECT_THROW(
  258. {
  259. try
  260. {
  261. xmlParser.setDocument(nullptr);
  262. }
  263. catch (const IllegalArgumentException &_exception)
  264. {
  265. throw;
  266. }
  267. },
  268. IllegalArgumentException);
  269. }
  270. }