SectionPairFileReaderTest.cpp 4.6 KB

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