KvFileReaderTest.cpp 3.7 KB

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