SectionPairFileReaderTest.cpp 4.2 KB

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