SectionPairFileReaderTest.cpp 4.5 KB

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