SectionPairFileReaderTest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Co-Author: Claude Sonnet 4.6 (LLM)
  4. * Company: Lynar Studios
  5. * E-Mail: webmaster@lynarstudios.com
  6. * Created: 2023-02-21
  7. * Changed: 2026-06-23
  8. *
  9. * */
  10. #include <gtest/gtest.h>
  11. #include <ls-std-io-test.hpp>
  12. #include <ls-std/ls-std-core.hpp>
  13. #include <ls-std/ls-std-io.hpp>
  14. #include <memory>
  15. using ls::standard::core::FileNotFoundException;
  16. using ls::standard::core::IllegalArgumentException;
  17. using ls::standard::io::NewLine;
  18. using ls::standard::io::SectionPairDocument;
  19. using ls::standard::io::SectionPairFileReader;
  20. using ls::standard::io::SectionPairFileReaderParameter;
  21. using std::dynamic_pointer_cast;
  22. using std::make_shared;
  23. using std::shared_ptr;
  24. using std::string;
  25. using test::io::MockFileExistenceEvaluator;
  26. using test::io::MockFileReader;
  27. using testing::AtLeast;
  28. using testing::Return;
  29. using testing::Test;
  30. using testing::TestWithParam;
  31. using testing::Values;
  32. namespace
  33. {
  34. class SectionPairFileReaderTest : public Test
  35. {
  36. public:
  37. SectionPairFileReaderTest() = default;
  38. ~SectionPairFileReaderTest() override = default;
  39. static string getMockSectionPairFileContent()
  40. {
  41. return "# section-pair document\n"
  42. "\n"
  43. "[general]\n"
  44. "\n"
  45. "ports:\n"
  46. " 9090\n"
  47. " 8001\n"
  48. " 1989\n"
  49. "host=localhost\n";
  50. }
  51. static SectionPairFileReaderParameter createMockParameter(bool _fileExists)
  52. {
  53. SectionPairFileReaderParameter parameter{};
  54. parameter.setFileExistenceEvaluator(make_shared<MockFileExistenceEvaluator>(_fileExists));
  55. parameter.setReader(make_shared<MockFileReader>());
  56. parameter.setDocument(make_shared<SectionPairDocument>());
  57. parameter.setFilePath("settings.txt");
  58. return parameter;
  59. }
  60. };
  61. class SectionPairFileReaderTest_NotValidFileExtension : public TestWithParam<string>
  62. {
  63. public:
  64. SectionPairFileReaderTest_NotValidFileExtension() = default;
  65. ~SectionPairFileReaderTest_NotValidFileExtension() override = default;
  66. };
  67. TEST_F(SectionPairFileReaderTest, constructor_file_does_not_exist)
  68. {
  69. EXPECT_THROW(
  70. {
  71. try
  72. {
  73. SectionPairFileReader reader{SectionPairFileReaderTest::createMockParameter(false)};
  74. }
  75. catch (const FileNotFoundException &_exception)
  76. {
  77. throw;
  78. }
  79. },
  80. FileNotFoundException);
  81. }
  82. TEST_P(SectionPairFileReaderTest_NotValidFileExtension, constructor_no_valid_file_extension)
  83. {
  84. SectionPairFileReaderParameter parameter = SectionPairFileReaderTest::createMockParameter(true);
  85. parameter.setFilePath(GetParam());
  86. EXPECT_THROW(
  87. {
  88. try
  89. {
  90. SectionPairFileReader reader{parameter};
  91. }
  92. catch (const IllegalArgumentException &_exception)
  93. {
  94. const string message = _exception.what();
  95. const string expected = _exception.getName() + " thrown - \"" + GetParam() + "\" does not have a valid section pair file extension (.txt or .sp)!";
  96. ASSERT_STREQ(expected.c_str(), message.c_str());
  97. throw;
  98. }
  99. },
  100. IllegalArgumentException);
  101. }
  102. TEST_F(SectionPairFileReaderTest, getClassName)
  103. {
  104. ASSERT_STREQ("SectionPairFileReader", SectionPairFileReader{SectionPairFileReaderTest::createMockParameter(true)}.getClassName().c_str());
  105. }
  106. TEST_F(SectionPairFileReaderTest, getDocument)
  107. {
  108. const SectionPairFileReader reader{SectionPairFileReaderTest::createMockParameter(true)};
  109. ASSERT_FALSE(reader.getDocument() == nullptr);
  110. }
  111. TEST_F(SectionPairFileReaderTest, read)
  112. {
  113. const SectionPairFileReaderParameter parameter = SectionPairFileReaderTest::createMockParameter(true);
  114. parameter.getDocument()->reserveNewLine(NewLine::getUnixNewLine());
  115. SectionPairFileReader reader{parameter};
  116. const shared_ptr<MockFileReader> mockFileReader = dynamic_pointer_cast<MockFileReader>(parameter.getReader());
  117. EXPECT_CALL(*mockFileReader, read()).Times(AtLeast(1));
  118. ON_CALL(*mockFileReader, read()).WillByDefault(Return(SectionPairFileReaderTest::getMockSectionPairFileContent()));
  119. reader.read();
  120. ASSERT_TRUE(!reader.getDocument()->getSectionList().empty());
  121. ASSERT_STREQ("general", reader.getDocument()->get(0)->getSectionId().c_str());
  122. ASSERT_EQ(2, reader.getDocument()->get(0)->getRowAmount());
  123. }
  124. INSTANTIATE_TEST_SUITE_P(NotValidFileExtension, SectionPairFileReaderTest_NotValidFileExtension, Values("settings.json", "/var/log/document.html"));
  125. }