using System.Collections.Generic; using FizzWare.NBuilder; using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; using NzbDrone.Core.Download; using NzbDrone.Core.Download.TrackedDownloads; using NzbDrone.Core.History; using NzbDrone.Core.Music; using NzbDrone.Core.Parser; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.Download.CompletedDownloadServiceTests { [TestFixture] public class ProcessFixture : CoreTest { private TrackedDownload _trackedDownload; [SetUp] public void Setup() { var completed = Builder.CreateNew() .With(h => h.Status = DownloadItemStatus.Completed) .With(h => h.OutputPath = new OsPath(@"C:\DropFolder\MyDownload".AsOsAgnostic())) .With(h => h.Title = "Drone.S01E01.HDTV") .Build(); var remoteAlbum = BuildRemoteAlbum(); _trackedDownload = Builder.CreateNew() .With(c => c.State = TrackedDownloadState.Downloading) .With(c => c.DownloadItem = completed) .With(c => c.RemoteAlbum = remoteAlbum) .Build(); Mocker.GetMock() .SetupGet(c => c.Definition) .Returns(new DownloadClientDefinition { Id = 1, Name = "testClient" }); Mocker.GetMock() .Setup(c => c.Get(It.IsAny())) .Returns(Mocker.GetMock().Object); Mocker.GetMock() .Setup(c => c.ProvideImportItem(It.IsAny(), It.IsAny())) .Returns((DownloadClientItem item, DownloadClientItem previous) => item); Mocker.GetMock() .Setup(s => s.MostRecentForDownloadId(_trackedDownload.DownloadItem.DownloadId)) .Returns(new EntityHistory()); Mocker.GetMock() .Setup(s => s.GetArtist("Drone.S01E01.HDTV")) .Returns(remoteAlbum.Artist); } private RemoteAlbum BuildRemoteAlbum() { return new RemoteAlbum { Artist = new Artist(), Albums = new List { new Album { Id = 1 } } }; } private void GivenNoGrabbedHistory() { Mocker.GetMock() .Setup(s => s.MostRecentForDownloadId(_trackedDownload.DownloadItem.DownloadId)) .Returns((EntityHistory)null); } private void GivenArtistMatch() { Mocker.GetMock() .Setup(s => s.GetArtist(It.IsAny())) .Returns(_trackedDownload.RemoteAlbum.Artist); } private void GivenABadlyNamedDownload() { _trackedDownload.DownloadItem.DownloadId = "1234"; _trackedDownload.DownloadItem.Title = "Droned Pilot"; // Set a badly named download Mocker.GetMock() .Setup(s => s.MostRecentForDownloadId(It.Is(i => i == "1234"))) .Returns(new EntityHistory() { SourceTitle = "Droned S01E01" }); Mocker.GetMock() .Setup(s => s.GetArtist(It.IsAny())) .Returns((Artist)null); Mocker.GetMock() .Setup(s => s.GetArtist("Droned S01E01")) .Returns(BuildRemoteAlbum().Artist); } [TestCase(DownloadItemStatus.Downloading)] [TestCase(DownloadItemStatus.Failed)] [TestCase(DownloadItemStatus.Queued)] [TestCase(DownloadItemStatus.Paused)] [TestCase(DownloadItemStatus.Warning)] public void should_not_process_if_download_status_isnt_completed(DownloadItemStatus status) { _trackedDownload.DownloadItem.Status = status; Subject.Check(_trackedDownload); AssertNotReadyToImport(); } [Test] public void should_not_process_if_matching_history_is_not_found_and_no_category_specified() { _trackedDownload.DownloadItem.Category = null; GivenNoGrabbedHistory(); Subject.Check(_trackedDownload); AssertNotReadyToImport(); } [Test] public void should_process_if_matching_history_is_not_found_but_category_specified() { _trackedDownload.DownloadItem.Category = "tv"; GivenNoGrabbedHistory(); GivenArtistMatch(); Subject.Check(_trackedDownload); AssertReadyToImport(); } [Test] public void should_not_process_if_output_path_is_empty() { _trackedDownload.DownloadItem.OutputPath = default; Subject.Check(_trackedDownload); AssertNotReadyToImport(); } [Test] public void should_not_process_if_the_download_cannot_be_tracked_using_the_source_title_as_it_was_initiated_externally() { GivenABadlyNamedDownload(); _trackedDownload.RemoteAlbum.Artist = null; Mocker.GetMock() .Setup(s => s.MostRecentForDownloadId(It.Is(i => i == "1234"))); Subject.Check(_trackedDownload); AssertNotReadyToImport(); } [Test] public void should_not_process_when_there_is_a_title_mismatch() { _trackedDownload.RemoteAlbum.Artist = null; Mocker.GetMock() .Setup(s => s.GetArtist("Drone.S01E01.HDTV")) .Returns((Artist)null); Subject.Check(_trackedDownload); AssertNotReadyToImport(); } private void AssertNotReadyToImport() { _trackedDownload.State.Should().NotBe(TrackedDownloadState.ImportPending); } private void AssertReadyToImport() { _trackedDownload.State.Should().Be(TrackedDownloadState.ImportPending); } } }