using System; using System.IO; using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Common; using NzbDrone.Core.Configuration; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles.Events; using NzbDrone.Core.Parser; using NzbDrone.Core.Tv; using NzbDrone.Core.Test.Framework; namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests { [TestFixture] public class DropFolderImportServiceFixture : CoreTest { private EpisodeFile _fakeEpisodeFile; private string[] _subFolders = new[] { "c:\\root\\foldername" }; private string[] _videoFiles = new[] { "c:\\root\\foldername\\video.ext" }; [SetUp] public void Setup() { _fakeEpisodeFile = Builder.CreateNew().Build(); Mocker.GetMock().Setup(c => c.GetVideoFiles(It.IsAny(), It.IsAny())) .Returns(_videoFiles); Mocker.GetMock().Setup(c => c.GetDirectories(It.IsAny())) .Returns(_subFolders); Mocker.GetMock().SetupGet(c => c.DownloadedEpisodesFolder) .Returns("c:\\drop\\"); } private void WithOldWrite() { Mocker.GetMock() .Setup(c => c.GetLastFolderWrite(It.IsAny())) .Returns(DateTime.Now.AddDays(-5)); } private void WithRecentFolderWrite() { Mocker.GetMock() .Setup(c => c.GetLastFolderWrite(It.IsAny())) .Returns(DateTime.UtcNow); Mocker.GetMock() .Setup(c => c.GetLastFileWrite(It.IsAny())) .Returns(DateTime.UtcNow); } [Test] public void should_import_file() { Subject.ProcessDownloadedEpisodesFolder(); VerifyImport(); } [Test] public void should_skip_if_folder_is_too_fresh() { WithRecentFolderWrite(); Subject.ProcessDownloadedEpisodesFolder(); VerifyNoImport(); } [Test] public void should_search_for_series_using_folder_name() { WithOldWrite(); Subject.ProcessDownloadedEpisodesFolder(); Mocker.GetMock().Verify(c => c.GetSeries("foldername"), Times.Once()); } [Test] public void all_imported_files_should_be_moved() { Mocker.GetMock().Setup(c => c.ImportFile(It.IsAny(), It.IsAny())) .Returns(_fakeEpisodeFile); Subject.ProcessDownloadedEpisodesFolder(); Mocker.GetMock().Verify(c => c.MoveEpisodeFile(_fakeEpisodeFile, true), Times.Once()); } [Test] public void should_trigger_import_event_on_import() { Mocker.GetMock().Setup(c => c.ImportFile(It.IsAny(), It.IsAny())) .Returns(_fakeEpisodeFile); Subject.ProcessDownloadedEpisodesFolder(); VerifyEventPublished(); } [Test] public void should_not_attempt_move_if_nothing_is_imported() { Mocker.GetMock().Setup(c => c.ImportFile(It.IsAny(), It.IsAny())) .Returns(null); Subject.ProcessDownloadedEpisodesFolder(); Mocker.GetMock().Verify(c => c.MoveEpisodeFile(It.IsAny(), It.IsAny()), Times.Never()); } [Test] public void should_not_publish_import_event_if_nothing_is_imported() { Mocker.GetMock().Setup(c => c.ImportFile(It.IsAny(), It.IsAny())) .Returns(null); Subject.ProcessDownloadedEpisodesFolder(); VerifyEventNotPublished(); } [Test] public void should_skip_if_folder_is_in_use_by_another_process() { Mocker.GetMock().Setup(c => c.IsFileLocked(It.IsAny())) .Returns(true); Subject.ProcessDownloadedEpisodesFolder(); VerifyNoImport(); } private void VerifyNoImport() { Mocker.GetMock().Verify(c => c.ImportFile(It.IsAny(), It.IsAny()), Times.Never()); } private void VerifyImport() { Mocker.GetMock().Verify(c => c.ImportFile(It.IsAny(), It.IsAny()), Times.Once()); } } }