Преглед изворни кода

Add file extension validator to SectionPairFileReader class

Patrick-Christopher Mattulat пре 1 година
родитељ
комит
42fd014112

+ 2 - 0
CMakeLists.txt

@@ -186,6 +186,7 @@ set(SOURCE_FILES_IO
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/reader/SectionPairFileReader.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/reader/SectionPairFileReaderParameter.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/validator/SectionPairDocumentValidator.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/validator/SectionPairFileExtensionValidator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/validator/SectionPairIdentifierValidator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/validator/SectionPairRowListValueValidator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/validator/SectionPairRowSingleValueValidator.cpp
@@ -287,6 +288,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/evaluator/SectionPairSectionArgumentEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/evaluator/SectionPairValueArgumentEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/validator/SectionPairDocumentValidatorTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/validator/SectionPairFileExtensionValidatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/validator/SectionPairIdentifierValidatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/validator/SectionPairRowListValueValidatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/validator/SectionPairRowSingleValueValidatorTest.cpp

+ 1 - 0
include/ls-std/io/section-pair/reader/SectionPairFileReader.hpp

@@ -32,6 +32,7 @@ namespace ls::std::io
 
       ls::std::io::SectionPairFileReaderParameter parameter{};
 
+      void _checkFileExtension();
       void _createDocument();
       void _createFileExistenceEvaluator();
       void _createReader();

+ 35 - 0
include/ls-std/io/section-pair/validator/SectionPairFileExtensionValidator.hpp

@@ -0,0 +1,35 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#ifndef LS_STD_SECTION_PAIR_FILE_EXTENSION_VALIDATOR_HPP
+#define LS_STD_SECTION_PAIR_FILE_EXTENSION_VALIDATOR_HPP
+
+#include <ls-std/core/Class.hpp>
+#include <ls-std/core/interface/IValidator.hpp>
+#include <ls-std/os/dynamic-goal.hpp>
+#include <string>
+
+namespace ls::std::io
+{
+  class LS_STD_DYNAMIC_GOAL SectionPairFileExtensionValidator : public ls::std::core::Class, public ls::std::core::interface_type::IValidator
+  {
+    public:
+
+      explicit SectionPairFileExtensionValidator(::std::string _fileName);
+      ~SectionPairFileExtensionValidator() override;
+
+      [[nodiscard]] bool isValid() override;
+
+    private:
+
+      ::std::string fileName{};
+  };
+}
+
+#endif

+ 1 - 0
include/ls-std/ls-std-io.hpp

@@ -41,6 +41,7 @@
 #include <ls-std/io/section-pair/type/SectionPairDocumentTypes.hpp>
 #include <ls-std/io/section-pair/type/SectionPairSectionTypes.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairDocumentValidator.hpp>
+#include <ls-std/io/section-pair/validator/SectionPairFileExtensionValidator.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairIdentifierValidator.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairRowListValueValidator.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairRowSingleValueValidator.hpp>

+ 12 - 0
source/ls-std/io/section-pair/reader/SectionPairFileReader.cpp

@@ -8,10 +8,12 @@
 * */
 
 #include <ls-std/core/ConditionalFunctionExecutor.hpp>
+#include <ls-std/core/exception/IllegalArgumentException.hpp>
 #include <ls-std/io/FileReader.hpp>
 #include <ls-std/io/evaluator/FileExistenceEvaluator.hpp>
 #include <ls-std/io/section-pair/model/SectionPairDocument.hpp>
 #include <ls-std/io/section-pair/reader/SectionPairFileReader.hpp>
+#include <ls-std/io/section-pair/validator/SectionPairFileExtensionValidator.hpp>
 #include <memory>
 
 ls::std::io::SectionPairFileReader::SectionPairFileReader(const ls::std::io::SectionPairFileReaderParameter &_parameter) : ls::std::core::Class("SectionPairFileReader")
@@ -19,6 +21,7 @@ ls::std::io::SectionPairFileReader::SectionPairFileReader(const ls::std::io::Sec
   this->parameter = _parameter;
   ls::std::core::ConditionalFunctionExecutor{this->parameter.getFileExistenceEvaluator() == nullptr}.execute([this] { _createFileExistenceEvaluator(); });
   this->parameter.getFileExistenceEvaluator()->evaluate();
+  this->_checkFileExtension();
   ls::std::core::ConditionalFunctionExecutor{this->parameter.getReader() == nullptr}.execute([this] { _createReader(); });
   ls::std::core::ConditionalFunctionExecutor{this->parameter.getDocument() == nullptr}.execute([this] { _createDocument(); });
 }
@@ -39,6 +42,15 @@ ls::std::core::type::byte_field ls::std::io::SectionPairFileReader::read()
   return data;
 }
 
+void ls::std::io::SectionPairFileReader::_checkFileExtension()
+{
+  if (!ls::std::io::SectionPairFileExtensionValidator{this->parameter.getFilePath()}.isValid())
+  {
+    ::std::string message = "\"" + this->parameter.getFilePath() + "\" does not have a valid section pair file extension (.txt or .sp)!";
+    throw ls::std::core::IllegalArgumentException{message};
+  }
+}
+
 void ls::std::io::SectionPairFileReader::_createDocument()
 {
   this->parameter.setDocument(::std::make_shared<ls::std::io::SectionPairDocument>());

+ 26 - 0
source/ls-std/io/section-pair/validator/SectionPairFileExtensionValidator.cpp

@@ -0,0 +1,26 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#include <ls-std/io/section-pair/validator/SectionPairFileExtensionValidator.hpp>
+
+ls::std::io::SectionPairFileExtensionValidator::SectionPairFileExtensionValidator(::std::string _fileName) : ls::std::core::Class("SectionPairFileExtensionValidator"), fileName(::std::move(_fileName))
+{}
+
+ls::std::io::SectionPairFileExtensionValidator::~SectionPairFileExtensionValidator() = default;
+
+bool ls::std::io::SectionPairFileExtensionValidator::isValid()
+{
+  ::std::string textFileExtension = ".txt";
+  ::std::string sectionPairFileExtension = ".sp";
+  bool endsWithTextFileExtension = this->fileName.rfind(textFileExtension) == (this->fileName.size() - textFileExtension.size());
+  bool endsWithSectionPairFileExtension = this->fileName.rfind(sectionPairFileExtension) == (this->fileName.size() - sectionPairFileExtension.size());
+
+
+  return endsWithTextFileExtension || endsWithSectionPairFileExtension;
+}

+ 46 - 10
test/cases/io/section-pair/reader/SectionPairFileReaderTest.cpp

@@ -34,16 +34,6 @@ namespace
       void TearDown() override
       {}
 
-      static SectionPairFileReaderParameter createMockParameter(bool _fileExists)
-      {
-        SectionPairFileReaderParameter parameter{};
-        parameter.setFileExistenceEvaluator(make_shared<MockFileExistenceEvaluator>(_fileExists));
-        parameter.setReader(make_shared<MockFileReader>());
-        parameter.setDocument(make_shared<SectionPairDocument>());
-
-        return parameter;
-      }
-
       static string getMockSectionPairFileContent()
       {
         return "# section-pair document\n"
@@ -56,6 +46,27 @@ namespace
                "  1989\n"
                "host=localhost\n";
       }
+
+    public:
+
+      static SectionPairFileReaderParameter createMockParameter(bool _fileExists)
+      {
+        SectionPairFileReaderParameter parameter{};
+        parameter.setFileExistenceEvaluator(make_shared<MockFileExistenceEvaluator>(_fileExists));
+        parameter.setReader(make_shared<MockFileReader>());
+        parameter.setDocument(make_shared<SectionPairDocument>());
+        parameter.setFilePath("settings.txt");
+
+        return parameter;
+      }
+  };
+
+  class SectionPairFileReaderTest_NotValidFileExtension : public ::testing::TestWithParam<string>
+  {
+    protected:
+
+      SectionPairFileReaderTest_NotValidFileExtension() = default;
+      ~SectionPairFileReaderTest_NotValidFileExtension() override = default;
   };
 
   TEST_F(SectionPairFileReaderTest, constructor_file_does_not_exist)
@@ -74,6 +85,29 @@ namespace
         FileNotFoundException);
   }
 
+  TEST_P(SectionPairFileReaderTest_NotValidFileExtension, constructor_no_valid_file_extension)
+  {
+    SectionPairFileReaderParameter parameter = SectionPairFileReaderTest::createMockParameter(true);
+    parameter.setFilePath(GetParam());
+
+    EXPECT_THROW(
+        {
+          try
+          {
+            SectionPairFileReader reader{parameter};
+          }
+          catch (const IllegalArgumentException &_exception)
+          {
+            string message = _exception.what();
+            string expected = "IllegalArgumentException thrown - \"" + GetParam() + "\" does not have a valid section pair file extension (.txt or .sp)!";
+
+            ASSERT_STREQ(expected.c_str(), message.c_str());
+            throw;
+          }
+        },
+        IllegalArgumentException);
+  }
+
   TEST_F(SectionPairFileReaderTest, getClassName)
   {
     ASSERT_STREQ("SectionPairFileReader", SectionPairFileReader{SectionPairFileReaderTest::createMockParameter(true)}.getClassName().c_str());
@@ -101,4 +135,6 @@ namespace
     ASSERT_STREQ("general", reader.getDocument()->get(0)->getSectionId().c_str());
     ASSERT_EQ(2, reader.getDocument()->get(0)->getRowAmount());
   }
+
+  INSTANTIATE_TEST_SUITE_P(NotValidFileExtension, SectionPairFileReaderTest_NotValidFileExtension, ::testing::Values("settings.json", "/var/log/document.html"));
 }

+ 67 - 0
test/cases/io/section-pair/validator/SectionPairFileExtensionValidatorTest.cpp

@@ -0,0 +1,67 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#include <gtest/gtest.h>
+#include <ls-std/ls-std-io.hpp>
+#include <string>
+
+using namespace ls::std::core;
+using namespace ls::std::io;
+using namespace ::std;
+
+namespace
+{
+  class SectionPairFileExtensionValidatorTest : public ::testing::Test
+  {
+    protected:
+
+      SectionPairFileExtensionValidatorTest() = default;
+      ~SectionPairFileExtensionValidatorTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  class SectionPairFileExtensionValidatorTest_ValidExtensionTest : public ::testing::TestWithParam<string>
+  {
+    protected:
+
+      SectionPairFileExtensionValidatorTest_ValidExtensionTest() = default;
+      ~SectionPairFileExtensionValidatorTest_ValidExtensionTest() override = default;
+  };
+
+  class SectionPairFileExtensionValidatorTest_NotValidExtensionTest : public ::testing::TestWithParam<string>
+  {
+    protected:
+
+      SectionPairFileExtensionValidatorTest_NotValidExtensionTest() = default;
+      ~SectionPairFileExtensionValidatorTest_NotValidExtensionTest() override = default;
+  };
+
+  TEST_F(SectionPairFileExtensionValidatorTest, getClassName)
+  {
+    ASSERT_STREQ("SectionPairFileExtensionValidator", SectionPairFileExtensionValidator{"tmp.txt"}.getClassName().c_str());
+  }
+
+  TEST_P(SectionPairFileExtensionValidatorTest_ValidExtensionTest, isValid)
+  {
+    ASSERT_TRUE(SectionPairFileExtensionValidator{GetParam()}.isValid());
+  }
+
+  TEST_P(SectionPairFileExtensionValidatorTest_NotValidExtensionTest, isValid)
+  {
+    ASSERT_FALSE(SectionPairFileExtensionValidator{GetParam()}.isValid());
+  }
+
+  INSTANTIATE_TEST_SUITE_P(ValidExtensionTest, SectionPairFileExtensionValidatorTest_ValidExtensionTest, ::testing::Values("server-settings.txt", "settings.sp", "/var/log/settings.txt"));
+  INSTANTIATE_TEST_SUITE_P(NotValidExtensionTest, SectionPairFileExtensionValidatorTest_NotValidExtensionTest, ::testing::Values("server-settings.json", "settings.html", "/var/log/settings"));
+}