<commit_before>﻿using System.Collections.Generic;
using System.Threading;

using Moq;
using NUnit.Framework;

using Parser.FileReader.Contracts;
using Parser.FileReader.Engines;
using Parser.FileReader.Factories;

namespace Parser.FileReader.Tests.EnginesTests.FileReaderEngineTests
{
    [TestFixture]
    public class Start_Should
    {
        [Test]
        public void InvokeIFileReaderAutoResetEventFactory_CreateFileReaderAutoResetEventMethod_OnceWithCorrectArgument()
        {
            // Arrange
            var commandParsingStrategy = new Mock<ICommandParsingStrategy>();
            var commandUtilizationStrategy = new Mock<ICommandUtilizationStrategy>();
            var logFilePathDiscoveryStrategy = new Mock<ILogFilePathDiscoveryStrategy>();
            var fileReaderAutoResetEventFactory = new Mock<IFileReaderAutoResetEventFactory>();
            var fileReaderFileSystemWatcherFactory = new Mock<IFileReaderFileSystemWatcherFactory>();
            var fileReaderInputProviderFactory = new Mock<IFileReaderInputProviderFactory>();

            var fileReaderInputProviderMock = new Mock<IFileReaderInputProvider>();
            fileReaderInputProviderMock.Setup(i => i.ReadLine()).Returns<string>(null);
            fileReaderInputProviderFactory.Setup(f => f.CreateFileReaderInputProvider(It.IsAny<string>())).Returns(fileReaderInputProviderMock.Object);

            var fileReaderAutoResetEventMock = new Mock<IFileReaderAutoResetEvent>();
            fileReaderAutoResetEventMock.Setup(e => e.WaitOne(It.IsAny<int>())).Returns(true);
            fileReaderAutoResetEventFactory.Setup(f => f.CreateFileReaderAutoResetEvent(It.IsAny<bool>())).Returns(fileReaderAutoResetEventMock.Object);

            var logFilePath = "Fake Log Path";
            logFilePathDiscoveryStrategy.Setup(s => s.DiscoverLogFile()).Returns(logFilePath);

            var fileReaderEngine = new FileReaderEngine(commandParsingStrategy.Object, commandUtilizationStrategy.Object, logFilePathDiscoveryStrategy.Object, fileReaderAutoResetEventFactory.Object, fileReaderFileSystemWatcherFactory.Object, fileReaderInputProviderFactory.Object);

            // Act
            fileReaderEngine.StartAsync();
            Thread.Sleep(50);
            fileReaderEngine.Stop();

            // Assert
            fileReaderAutoResetEventFactory.Verify(f => f.CreateFileReaderAutoResetEvent(false), Times.Once());
        }

        [Test]
        public void InvokeIFileReaderFileSystemWatcherFactory_CreateFileReaderFileSystemWatcherMethod_OnceWithCorrectArguments()
        {
            // Arrange
            var commandParsingStrategy = new Mock<ICommandParsingStrategy>();
            var commandUtilizationStrategy = new Mock<ICommandUtilizationStrategy>();
            var logFilePathDiscoveryStrategy = new Mock<ILogFilePathDiscoveryStrategy>();
            var fileReaderAutoResetEventFactory = new Mock<IFileReaderAutoResetEventFactory>();
            var fileReaderFileSystemWatcherFactory = new Mock<IFileReaderFileSystemWatcherFactory>();
            var fileReaderInputProviderFactory = new Mock<IFileReaderInputProviderFactory>();

            var fileReaderInputProviderMock = new Mock<IFileReaderInputProvider>();
            fileReaderInputProviderMock.Setup(i => i.ReadLine()).Returns<string>(null);
            fileReaderInputProviderFactory.Setup(f => f.CreateFileReaderInputProvider(It.IsAny<string>())).Returns(fileReaderInputProviderMock.Object);

            var fileReaderAutoResetEventMock = new Mock<IFileReaderAutoResetEvent>();
            fileReaderAutoResetEventMock.Setup(e => e.WaitOne(It.IsAny<int>())).Returns(true);
            fileReaderAutoResetEventFactory.Setup(f => f.CreateFileReaderAutoResetEvent(It.IsAny<bool>())).Returns(fileReaderAutoResetEventMock.Object);

            var logFilePath = "Fake Log Path";
            logFilePathDiscoveryStrategy.Setup(s => s.DiscoverLogFile()).Returns(logFilePath);

            var fileReaderEngine = new FileReaderEngine(commandParsingStrategy.Object, commandUtilizationStrategy.Object, logFilePathDiscoveryStrategy.Object, fileReaderAutoResetEventFactory.Object, fileReaderFileSystemWatcherFactory.Object, fileReaderInputProviderFactory.Object);

            // Act
            fileReaderEngine.StartAsync();
            Thread.Sleep(50);
            fileReaderEngine.Stop();

            // Assert
            fileReaderFileSystemWatcherFactory.Verify(f => f.CreateFileReaderFileSystemWatcher(logFilePath, true, fileReaderAutoResetEventMock.Object), Times.Once());
        }

        [Test]
        public void InvokeIFileReaderInputProviderFactory_CreateFileReaderInputProviderMethod_OnceWithCorrectArgument()
        {
            // Arrange
            var commandParsingStrategy = new Mock<ICommandParsingStrategy>();
            var commandUtilizationStrategy = new Mock<ICommandUtilizationStrategy>();
            var logFilePathDiscoveryStrategy = new Mock<ILogFilePathDiscoveryStrategy>();
            var fileReaderAutoResetEventFactory = new Mock<IFileReaderAutoResetEventFactory>();
            var fileReaderFileSystemWatcherFactory = new Mock<IFileReaderFileSystemWatcherFactory>();
            var fileReaderInputProviderFactory = new Mock<IFileReaderInputProviderFactory>();

            var fileReaderInputProviderMock = new Mock<IFileReaderInputProvider>();
            fileReaderInputProviderMock.Setup(i => i.ReadLine()).Returns<string>(null);
            fileReaderInputProviderFactory.Setup(f => f.CreateFileReaderInputProvider(It.IsAny<string>())).Returns(fileReaderInputProviderMock.Object);

            var fileReaderAutoResetEventMock = new Mock<IFileReaderAutoResetEvent>();
            fileReaderAutoResetEventMock.Setup(e => e.WaitOne(It.IsAny<int>())).Returns(true);
            fileReaderAutoResetEventFactory.Setup(f => f.CreateFileReaderAutoResetEvent(It.IsAny<bool>())).Returns(fileReaderAutoResetEventMock.Object);

            var logFilePath = "Fake Log Path";
            logFilePathDiscoveryStrategy.Setup(s => s.DiscoverLogFile()).Returns(logFilePath);

            var fileReaderEngine = new FileReaderEngine(commandParsingStrategy.Object, commandUtilizationStrategy.Object, logFilePathDiscoveryStrategy.Object, fileReaderAutoResetEventFactory.Object, fileReaderFileSystemWatcherFactory.Object, fileReaderInputProviderFactory.Object);

            // Act
            fileReaderEngine.StartAsync();
            Thread.Sleep(50);
            fileReaderEngine.Stop();

            // Assert
            fileReaderInputProviderFactory.Verify(f => f.CreateFileReaderInputProvider(logFilePath), Times.Once());
        }

        [Test]
        public void InvokeIFileReaderInputProvider_DisposeMethod()
        {
            // Arrange
            var commandParsingStrategy = new Mock<ICommandParsingStrategy>();
            var commandUtilizationStrategy = new Mock<ICommandUtilizationStrategy>();
            var logFilePathDiscoveryStrategy = new Mock<ILogFilePathDiscoveryStrategy>();
            var fileReaderAutoResetEventFactory = new Mock<IFileReaderAutoResetEventFactory>();
            var fileReaderFileSystemWatcherFactory = new Mock<IFileReaderFileSystemWatcherFactory>();
            var fileReaderInputProviderFactory = new Mock<IFileReaderInputProviderFactory>();

            var fileReaderInputProviderMock = new Mock<IFileReaderInputProvider>();
            fileReaderInputProviderMock.Setup(i => i.ReadLine()).Returns<string>(null);
            fileReaderInputProviderFactory.Setup(f => f.CreateFileReaderInputProvider(It.IsAny<string>())).Returns(fileReaderInputProviderMock.Object);

            var fileReaderAutoResetEventMock = new Mock<IFileReaderAutoResetEvent>();
            fileReaderAutoResetEventMock.Setup(e => e.WaitOne(It.IsAny<int>())).Returns(true);
            fileReaderAutoResetEventFactory.Setup(f => f.CreateFileReaderAutoResetEvent(It.IsAny<bool>())).Returns(fileReaderAutoResetEventMock.Object);

            var logFilePath = "Fake Log Path";
            logFilePathDiscoveryStrategy.Setup(s => s.DiscoverLogFile()).Returns(logFilePath);

            var fileReaderEngine = new FileReaderEngine(commandParsingStrategy.Object, commandUtilizationStrategy.Object, logFilePathDiscoveryStrategy.Object, fileReaderAutoResetEventFactory.Object, fileReaderFileSystemWatcherFactory.Object, fileReaderInputProviderFactory.Object);

            // Act
            fileReaderEngine.StartAsync();
            Thread.Sleep(50);
            fileReaderEngine.Stop();

            // Assert
            fileReaderInputProviderMock.Verify(i => i.Dispose(), Times.Once());
        }

        [Test]
        public void InvokeICommandParsingStrategy_ParseInputCommandMethod_WithCorrectArgument_WhenInputProviderProvidesInput()
        {
            // Arrange
            var commandParsingStrategy = new Mock<ICommandParsingStrategy>();
            var commandUtilizationStrategy = new Mock<ICommandUtilizationStrategy>();
            var logFilePathDiscoveryStrategy = new Mock<ILogFilePathDiscoveryStrategy>();
            var fileReaderAutoResetEventFactory = new Mock<IFileReaderAutoResetEventFactory>();
            var fileReaderFileSystemWatcherFactory = new Mock<IFileReaderFileSystemWatcherFactory>();
            var fileReaderInputProviderFactory = new Mock<IFileReaderInputProviderFactory>();

            var fakeNextInputLine = "fake input command";<commit_msg>updated FileReaderEngine tests<commit_after>﻿using System.Collections.Generic;
using System.Threading;

using Moq;
using NUnit.Framework;

using Parser.FileReader.Contracts;
using Parser.FileReader.Engines;
using Parser.FileReader.Factories;

namespace Parser.FileReader.Tests.EnginesTests.FileReaderEngineTests
{
    [TestFixture]
    public class Start_Should
    {
        [Test]
        public void InvokeIFileReaderAutoResetEventFactory_CreateFileReaderAutoResetEventMethod_OnceWithCorrectArgument()
        {
            // Arrange
            var commandParsingStrategy = new Mock<ICommandParsingStrategy>();
            var commandUtilizationStrategy = new Mock<ICommandUtilizationStrategy>();
            var logFilePathDiscoveryStrategy = new Mock<ILogFilePathDiscoveryStrategy>();
            var fileReaderAutoResetEventFactory = new Mock<IFileReaderAutoResetEventFactory>();
            var fileReaderFileSystemWatcherFactory = new Mock<IFileReaderFileSystemWatcherFactory>();
            var fileReaderInputProviderFactory = new Mock<IFileReaderInputProviderFactory>();

            var fileReaderInputProviderMock = new Mock<IFileReaderInputProvider>();
            fileReaderInputProviderMock.Setup(i => i.ReadLine()).Returns<string>(null);
            fileReaderInputProviderFactory.Setup(f => f.CreateFileReaderInputProvider(It.IsAny<string>())).Returns(fileReaderInputProviderMock.Object);

            var fileReaderAutoResetEventMock = new Mock<IFileReaderAutoResetEvent>();
            fileReaderAutoResetEventMock.Setup(e => e.WaitOne(It.IsAny<int>())).Returns(true);
            fileReaderAutoResetEventFactory.Setup(f => f.CreateFileReaderAutoResetEvent(It.IsAny<bool>())).Returns(fileReaderAutoResetEventMock.Object);

            var logFilePath = "Fake Log Path";
            logFilePathDiscoveryStrategy.Setup(s => s.DiscoverLogFile()).Returns(logFilePath);

            var fileReaderEngine = new FileReaderEngine(commandParsingStrategy.Object, commandUtilizationStrategy.Object, logFilePathDiscoveryStrategy.Object, fileReaderAutoResetEventFactory.Object, fileReaderFileSystemWatcherFactory.Object, fileReaderInputProviderFactory.Object);

            // Act
            fileReaderEngine.StartAsync();
            Thread.Sleep(50);
            fileReaderEngine.Stop();

            // Assert
            fileReaderAutoResetEventFactory.Verify(f => f.CreateFileReaderAutoResetEvent(false), Times.Once());
        }

        [Test]
        public void InvokeIFileReaderFileSystemWatcherFactory_CreateFileReaderFileSystemWatcherMethod_OnceWithCorrectArguments()
        {
            // Arrange
            var commandParsingStrategy = new Mock<ICommandParsingStrategy>();
            var commandUtilizationStrategy = new Mock<ICommandUtilizationStrategy>();
            var logFilePathDiscoveryStrategy = new Mock<ILogFilePathDiscoveryStrategy>();
            var fileReaderAutoResetEventFactory = new Mock<IFileReaderAutoResetEventFactory>();
            var fileReaderFileSystemWatcherFactory = new Mock<IFileReaderFileSystemWatcherFactory>();
            var fileReaderInputProviderFactory = new Mock<IFileReaderInputProviderFactory>();

            var fileReaderInputProviderMock = new Mock<IFileReaderInputProvider>();
            fileReaderInputProviderMock.Setup(i => i.ReadLine()).Returns<string>(null);
            fileReaderInputProviderFactory.Setup(f => f.CreateFileReaderInputProvider(It.IsAny<string>())).Returns(fileReaderInputProviderMock.Object);

            var fileReaderAutoResetEventMock = new Mock<IFileReaderAutoResetEvent>();
            fileReaderAutoResetEventMock.Setup(e => e.WaitOne(It.IsAny<int>())).Returns(true);
            fileReaderAutoResetEventFactory.Setup(f => f.CreateFileReaderAutoResetEvent(It.IsAny<bool>())).Returns(fileReaderAutoResetEventMock.Object);

            var logFilePath = "Fake Log Path";
            logFilePathDiscoveryStrategy.Setup(s => s.DiscoverLogFile()).Returns(logFilePath);

            var fileReaderEngine = new FileReaderEngine(commandParsingStrategy.Object, commandUtilizationStrategy.Object, logFilePathDiscoveryStrategy.Object, fileReaderAutoResetEventFactory.Object, fileReaderFileSystemWatcherFactory.Object, fileReaderInputProviderFactory.Object);

            // Act
            fileReaderEngine.StartAsync();
            Thread.Sleep(50);
            fileReaderEngine.Stop();

            // Assert
            fileReaderFileSystemWatcherFactory.Verify(f => f.CreateFileReaderFileSystemWatcher(logFilePath, true, fileReaderAutoResetEventMock.Object), Times.Once());
        }

        [Test]
        public void InvokeIFileReaderInputProviderFactory_CreateFileReaderInputProviderMethod_OnceWithCorrectArgument()
        {
            // Arrange
            var commandParsingStrategy = new Mock<ICommandParsingStrategy>();
            var commandUtilizationStrategy = new Mock<ICommandUtilizationStrategy>();
            var logFilePathDiscoveryStrategy = new Mock<ILogFilePathDiscoveryStrategy>();
            var fileReaderAutoResetEventFactory = new Mock<IFileReaderAutoResetEventFactory>();
            var fileReaderFileSystemWatcherFactory = new Mock<IFileReaderFileSystemWatcherFactory>();
            var fileReaderInputProviderFactory = new Mock<IFileReaderInputProviderFactory>();

            var fileReaderInputProviderMock = new Mock<IFileReaderInputProvider>();
            fileReaderInputProviderMock.Setup(i => i.ReadLine()).Returns<string>(null);
            fileReaderInputProviderFactory.Setup(f => f.CreateFileReaderInputProvider(It.IsAny<string>())).Returns(fileReaderInputProviderMock.Object);

            var fileReaderAutoResetEventMock = new Mock<IFileReaderAutoResetEvent>();
            fileReaderAutoResetEventMock.Setup(e => e.WaitOne(It.IsAny<int>())).Returns(true);
            fileReaderAutoResetEventFactory.Setup(f => f.CreateFileReaderAutoResetEvent(It.IsAny<bool>())).Returns(fileReaderAutoResetEventMock.Object);

            var logFilePath = "Fake Log Path";
            logFilePathDiscoveryStrategy.Setup(s => s.DiscoverLogFile()).Returns(logFilePath);

            var fileReaderEngine = new FileReaderEngine(commandParsingStrategy.Object, commandUtilizationStrategy.Object, logFilePathDiscoveryStrategy.Object, fileReaderAutoResetEventFactory.Object, fileReaderFileSystemWatcherFactory.Object, fileReaderInputProviderFactory.Object);

            // Act
            fileReaderEngine.StartAsync();
            Thread.Sleep(50);
            fileReaderEngine.Stop();

            // Assert
            fileReaderInputProviderFactory.Verify(f => f.CreateFileReaderInputProvider(logFilePath), Times.Once());
        }

        [Test]
        public void InvokeIFileReaderInputProvider_DisposeMethod()
        {
            // Arrange
            var commandParsingStrategy = new Mock<ICommandParsingStrategy>();
            var commandUtilizationStrategy = new Mock<ICommandUtilizationStrategy>();
            var logFilePathDiscoveryStrategy = new Mock<ILogFilePathDiscoveryStrategy>();
            var fileReaderAutoResetEventFactory = new Mock<IFileReaderAutoResetEventFactory>();
            var fileReaderFileSystemWatcherFactory = new Mock<IFileReaderFileSystemWatcherFactory>();
            var fileReaderInputProviderFactory = new Mock<IFileReaderInputProviderFactory>();

            var fileReaderInputProviderMock = new Mock<IFileReaderInputProvider>();
            fileReaderInputProviderMock.Setup(i => i.ReadLine()).Returns<string>(null);
            fileReaderInputProviderFactory.Setup(f => f.CreateFileReaderInputProvider(It.IsAny<string>())).Returns(fileReaderInputProviderMock.Object);

            var fileReaderAutoResetEventMock = new Mock<IFileReaderAutoResetEvent>();
            fileReaderAutoResetEventMock.Setup(e => e.WaitOne(It.IsAny<int>())).Returns(true);
            fileReaderAutoResetEventFactory.Setup(f => f.CreateFileReaderAutoResetEvent(It.IsAny<bool>())).Returns(fileReaderAutoResetEventMock.Object);

            var logFilePath = "Fake Log Path";
            logFilePathDiscoveryStrategy.Setup(s => s.DiscoverLogFile()).Returns(logFilePath);

            var fileReaderEngine = new FileReaderEngine(commandParsingStrategy.Object, commandUtilizationStrategy.Object, logFilePathDiscoveryStrategy.Object, fileReaderAutoResetEventFactory.Object, fileReaderFileSystemWatcherFactory.Object, fileReaderInputProviderFactory.Object);

            // Act
            fileReaderEngine.StartAsync();
            Thread.Sleep(50);
            fileReaderEngine.Stop();

            // Assert
            fileReaderInputProviderMock.Verify(i => i.Dispose(), Times.Once());
        }

        [Test]
        public void InvokeIFileReaderAutoResetEvent_WaitOneMethod()
        {
            // Arrange
            var commandParsingStrategy = new Mock<ICommandParsingStrategy>();
            var commandUtilizationStrategy = new Mock<ICommandUtilizationStrategy>();
            var logFilePathDiscoveryStrategy = new Mock<ILogFilePathDiscoveryStrategy>();
            var fileReaderAutoResetEventFactory = new Mock<IFileReaderAutoResetEventFactory>();
            var fileReaderFileSystemWatcherFactory = new Mock<IFileReaderFileSystemWatcherFactory>();
            var fileReaderInputProviderFactory = new Mock<IFileReaderInputProviderFactory>();

            var fileReaderInputProviderMock = new Mock<IFileReaderInputProvider>();
            fileReaderInputProviderMock.SetupSequence(i => i.ReadLine()).Returns(null);
            fileReaderInputProviderFactory.Setup(f => f.CreateFileReaderInputProvider(It.IsAny<string>())).Returns(fileReaderInputProviderMock.Object);

            var fileReaderAutoResetEventMock = new Mock<IFileReaderAutoResetEvent>();
            fileReaderAutoResetEventMock.Setup(e => e.WaitOne(It.IsAny<int>())).Returns(true);
            fileReaderAutoResetEventFactory.Setup(f => f.CreateFileReaderAutoResetEvent(It.IsAny<bool>())).Returns(fileReaderAutoResetEventMock.Object);

            var logFilePath = "Fake Log Path";
            logFilePathDiscoveryStrategy.Setup(s => s.DiscoverLogFile()).Returns(logFilePath);

            var fileReaderEngine = new FileReaderEngine(commandParsingStrategy.Object, commandUtilizationStrategy.Object, logFilePathDiscoveryStrategy.Object, fileReaderAutoResetEventFactory.Object, fileReaderFileSystemWatcherFactory.Object, fileReaderInputProviderFactory.Object);

            var AutoResetEventWaitTimeoutInMiliseconds = 250;

            // Act
            fileReaderEngine.StartAsync();
            Thread.Sleep(50);
            fileReaderEngine.Stop();

            // Assert
            fileReaderAutoResetEventMock.Verify(e => e.WaitOne(AutoResetEventWaitTimeoutInMiliseconds), Times.AtLeastOnce());
        }

        [Test]
        public void InvokeICommandParsingStrategy_ParseInputCommandMethod_WithCorrectArgument_WhenInputProviderProvidesInput()
        {
            // Arrange
            var commandParsingStrategy = new Mock<ICommandParsingStrategy>();
            var commandUtilizationStrategy = new Mock<ICommandUtilizationStrategy>();
            var logFilePathDiscoveryStrategy = new Mock<ILogFilePathDiscoveryStrategy>();
            var fileReaderAutoResetEventFactory = new Mock<IFileReaderAutoResetEventFactory>();
            var fileReaderFileSystemWatcherFactory = new Mock<IFileReaderFileSystemWatcherFactory>();
            var fileReaderInputProviderFactory = new Mock<IFileReaderInputProviderFactory>();

            var fakeNextInputLine = "fake input command";
{'fraction_non_alphanumeric': 0.08613284978049246, 'fraction_numerical': 0.0010020996373353694, 'mean_word_length': 3.832142033663823, 'pattern_counts': {'":': 0, '<': 122, '<?xml version=': 0, '>': 176, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3440850', 'n_tokens_mistral': 5054, 'n_tokens_neox': 4966, 'n_words': 737}