/*
 * Author:          Patrick-Christopher Mattulat
 * Co-Author:       Claude Sonnet 4.6 (LLM)
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2020-10-10
 * Changed:         2026-06-23
 *
 * */

#include <classes/TestHelper.hpp>
#include <gtest/gtest.h>
#include <ls-std/ls-std-core.hpp>
#include <ls-std/ls-std-io.hpp>

using ls::standard::core::IllegalArgumentException;
using ls::standard::io::File;
using ls::standard::io::XmlDocument;
using ls::standard::io::XmlReader;
using ls::standard::test::TestHelper;
using std::make_shared;
using std::shared_ptr;
using std::string;
using testing::Test;

namespace
{
  class XmlReaderTest : public Test
  {
    public:

      XmlReaderTest() = default;
      ~XmlReaderTest() override = default;
  };

  TEST_F(XmlReaderTest, read)
  {
    const string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
    XmlReader xmlReader{make_shared<XmlDocument>(), xmlPath};

    ASSERT_TRUE(!xmlReader.read().empty());
  }

  TEST_F(XmlReaderTest, getDocument)
  {
    const string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
    const XmlReader xmlReader{make_shared<XmlDocument>(), xmlPath};

    ASSERT_TRUE(xmlReader.getDocument() != nullptr);
  }

  TEST_F(XmlReaderTest, setDocument)
  {
    const string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
    auto document = make_shared<XmlDocument>();
    XmlReader xmlReader{document, xmlPath};
    ASSERT_TRUE(xmlReader.getDocument() == document);

    document = make_shared<XmlDocument>();
    xmlReader.setDocument(document);
    ASSERT_TRUE(xmlReader.getDocument() == document);
  }

  TEST_F(XmlReaderTest, setDocument_no_reference)
  {
    const string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
    const auto document = make_shared<XmlDocument>();
    XmlReader xmlReader{document, xmlPath};

    EXPECT_THROW(
        {
          try
          {
            xmlReader.setDocument(nullptr);
          }
          catch (const IllegalArgumentException &_exception)
          {
            throw;
          }
        },
        IllegalArgumentException);
  }

  TEST_F(XmlReaderTest, setFile)
  {
    const string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
    XmlReader xmlReader{make_shared<XmlDocument>(), xmlPath};
    const File xmlFile{xmlPath};
    xmlReader.setFile(xmlFile);

    ASSERT_TRUE(true);
  }

  TEST_F(XmlReaderTest, setFile_does_not_exist)
  {
    const string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
    XmlReader xmlReader{make_shared<XmlDocument>(), xmlPath};
    File xmlFile{xmlPath};

    EXPECT_THROW(
        {
          try
          {
            xmlReader.setFile(File{""});
          }
          catch (const IllegalArgumentException &_exception)
          {
            throw;
          }
        },
        IllegalArgumentException);
  }
}
