XmlParserTest.cpp 14 KB

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