KvFileReaderTest.cpp 4.3 KB

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