KvFileReaderTest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-25
  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::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. try
  39. {
  40. string kvPath = TestHelper::getResourcesFolderLocation() + "server_settings.kv";
  41. KvFileReader reader = KvFileReader(nullptr, kvPath);
  42. }
  43. catch (const IllegalArgumentException &_exception)
  44. {
  45. throw;
  46. }
  47. }, IllegalArgumentException);
  48. }
  49. TEST_F(KvFileReaderTest, constructor_invalid_file_path)
  50. {
  51. EXPECT_THROW({
  52. try
  53. {
  54. KvFileReader reader = KvFileReader(make_shared<KvDocument>(), "invalid_path");
  55. }
  56. catch (const IllegalArgumentException &_exception)
  57. {
  58. throw;
  59. }
  60. }, IllegalArgumentException);
  61. }
  62. TEST_F(KvFileReaderTest, getDocument)
  63. {
  64. const shared_ptr<KvFileReader> &reader = createTestKVFileReader();
  65. ASSERT_TRUE(reader->getDocument() != nullptr);
  66. }
  67. TEST_F(KvFileReaderTest, read)
  68. {
  69. // preparation
  70. const shared_ptr<KvFileReader> &reader = createTestKVFileReader();
  71. // read file and check
  72. reader->read();
  73. const shared_ptr<KvDocument> &document = reader->getDocument();
  74. ASSERT_EQ(3, document->getPairs().size());
  75. ASSERT_TRUE(document->hasPair("port"));
  76. ASSERT_TRUE(document->hasPair("host"));
  77. ASSERT_TRUE(document->hasPair("service-name"));
  78. ASSERT_STREQ("8080", document->getPairs().at("port").getValue().c_str());
  79. ASSERT_STREQ("localhost", document->getPairs().at("host").getValue().c_str());
  80. ASSERT_STREQ("deamon", document->getPairs().at("service-name").getValue().c_str());
  81. }
  82. TEST_F(KvFileReaderTest, setFile_no_existing_file)
  83. {
  84. const shared_ptr<KvFileReader> &reader = createTestKVFileReader();
  85. EXPECT_THROW({
  86. try
  87. {
  88. reader->setFile(File{"invalid_path"});
  89. }
  90. catch (const IllegalArgumentException &_exception)
  91. {
  92. throw;
  93. }
  94. }, IllegalArgumentException);
  95. }
  96. TEST_F(KvFileReaderTest, setDocument)
  97. {
  98. // preparation
  99. const shared_ptr<KvFileReader> &reader = createTestKVFileReader();
  100. // set new document and check
  101. shared_ptr<KvDocument> newDocument = make_shared<KvDocument>();
  102. reader->setDocument(newDocument);
  103. ASSERT_TRUE(reader->getDocument() == newDocument);
  104. }
  105. TEST_F(KvFileReaderTest, setDocument_no_reference)
  106. {
  107. const shared_ptr<KvFileReader> &reader = createTestKVFileReader();
  108. EXPECT_THROW({
  109. try
  110. {
  111. reader->setDocument(nullptr);
  112. }
  113. catch (const IllegalArgumentException &_exception)
  114. {
  115. throw;
  116. }
  117. }, IllegalArgumentException);
  118. }
  119. }