KvFileReaderTest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-25
  6. * Changed: 2021-07-15
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <TestHelper.hpp>
  12. namespace
  13. {
  14. class KvFileReaderTest : public ::testing::Test
  15. {
  16. protected:
  17. KvFileReaderTest() = default;
  18. ~KvFileReaderTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. static std::shared_ptr<ls_std::KvFileReader> createTestKVFileReader()
  24. {
  25. std::string kvPath = TestHelper::getResourcesFolderLocation() + "server_settings.kv";
  26. std::shared_ptr<ls_std::KvDocument> document = std::make_shared<ls_std::KvDocument>();
  27. return std::make_shared<ls_std::KvFileReader>(document, kvPath);
  28. }
  29. };
  30. TEST_F(KvFileReaderTest, constructor_no_document_reference)
  31. {
  32. EXPECT_THROW({
  33. try
  34. {
  35. std::string kvPath = TestHelper::getResourcesFolderLocation() + "server_settings.kv";
  36. ls_std::KvFileReader reader = ls_std::KvFileReader(nullptr, kvPath);
  37. }
  38. catch (const ls_std::IllegalArgumentException &_exception)
  39. {
  40. throw;
  41. }
  42. }, ls_std::IllegalArgumentException);
  43. }
  44. TEST_F(KvFileReaderTest, constructor_invalid_file_path)
  45. {
  46. EXPECT_THROW({
  47. try
  48. {
  49. ls_std::KvFileReader reader = ls_std::KvFileReader(std::make_shared<ls_std::KvDocument>(), "invalid_path");
  50. }
  51. catch (const ls_std::IllegalArgumentException &_exception)
  52. {
  53. throw;
  54. }
  55. }, ls_std::IllegalArgumentException);
  56. }
  57. TEST_F(KvFileReaderTest, getDocument)
  58. {
  59. const std::shared_ptr<ls_std::KvFileReader> &reader = createTestKVFileReader();
  60. ASSERT_TRUE(reader->getDocument() != nullptr);
  61. }
  62. TEST_F(KvFileReaderTest, read)
  63. {
  64. // preparation
  65. const std::shared_ptr<ls_std::KvFileReader> &reader = createTestKVFileReader();
  66. // read file and check
  67. reader->read();
  68. const std::shared_ptr<ls_std::KvDocument> &document = reader->getDocument();
  69. ASSERT_EQ(3, document->getPairs().size());
  70. ASSERT_TRUE(document->hasPair("port"));
  71. ASSERT_TRUE(document->hasPair("host"));
  72. ASSERT_TRUE(document->hasPair("service-name"));
  73. ASSERT_STREQ("8080", document->getPairs().at("port").getValue().c_str());
  74. ASSERT_STREQ("localhost", document->getPairs().at("host").getValue().c_str());
  75. ASSERT_STREQ("deamon", document->getPairs().at("service-name").getValue().c_str());
  76. }
  77. TEST_F(KvFileReaderTest, setFile_no_existing_file)
  78. {
  79. const std::shared_ptr<ls_std::KvFileReader> &reader = createTestKVFileReader();
  80. EXPECT_THROW({
  81. try
  82. {
  83. reader->setFile(ls_std::File{"invalid_path"});
  84. }
  85. catch (const ls_std::IllegalArgumentException &_exception)
  86. {
  87. throw;
  88. }
  89. }, ls_std::IllegalArgumentException);
  90. }
  91. TEST_F(KvFileReaderTest, setDocument)
  92. {
  93. // preparation
  94. const std::shared_ptr<ls_std::KvFileReader> &reader = createTestKVFileReader();
  95. // set new document and check
  96. std::shared_ptr<ls_std::KvDocument> newDocument = std::make_shared<ls_std::KvDocument>();
  97. reader->setDocument(newDocument);
  98. ASSERT_TRUE(reader->getDocument() == newDocument);
  99. }
  100. TEST_F(KvFileReaderTest, setDocument_no_reference)
  101. {
  102. const std::shared_ptr<ls_std::KvFileReader> &reader = createTestKVFileReader();
  103. EXPECT_THROW({
  104. try
  105. {
  106. reader->setDocument(nullptr);
  107. }
  108. catch (const ls_std::IllegalArgumentException &_exception)
  109. {
  110. throw;
  111. }
  112. }, ls_std::IllegalArgumentException);
  113. }
  114. }