Fixed: DownloadEpisodesScan api command regressed during a refactoring causing nzbToMedia to fail.

This commit is contained in:
Taloth Saldono 2015-01-22 01:29:52 +01:00
parent d67811b50a
commit 2f048f4217
3 changed files with 122 additions and 3 deletions

View File

@ -6,10 +6,13 @@ using NUnit.Framework;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.Download.TrackedDownloads;
using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.Commands; using NzbDrone.Core.MediaFiles.Commands;
using NzbDrone.Core.MediaFiles.EpisodeImport; using NzbDrone.Core.MediaFiles.EpisodeImport;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.MediaFiles namespace NzbDrone.Core.Test.MediaFiles
@ -18,6 +21,9 @@ namespace NzbDrone.Core.Test.MediaFiles
public class DownloadedEpisodesCommandServiceFixture : CoreTest<DownloadedEpisodesCommandService> public class DownloadedEpisodesCommandServiceFixture : CoreTest<DownloadedEpisodesCommandService>
{ {
private string _droneFactory = "c:\\drop\\".AsOsAgnostic(); private string _droneFactory = "c:\\drop\\".AsOsAgnostic();
private string _downloadFolder = "c:\\drop_other\\Show.S01E01\\".AsOsAgnostic();
private TrackedDownload _trackedDownload;
[SetUp] [SetUp]
public void Setup() public void Setup()
@ -31,6 +37,33 @@ namespace NzbDrone.Core.Test.MediaFiles
Mocker.GetMock<IDownloadedEpisodesImportService>() Mocker.GetMock<IDownloadedEpisodesImportService>()
.Setup(v => v.ProcessRootFolder(It.IsAny<DirectoryInfo>())) .Setup(v => v.ProcessRootFolder(It.IsAny<DirectoryInfo>()))
.Returns(new List<ImportResult>()); .Returns(new List<ImportResult>());
Mocker.GetMock<IDownloadedEpisodesImportService>()
.Setup(v => v.ProcessPath(It.IsAny<string>(), It.IsAny<Series>(), It.IsAny<DownloadClientItem>()))
.Returns(new List<ImportResult>());
var downloadItem = Builder<DownloadClientItem>.CreateNew()
.With(v => v.DownloadId = "sab1")
.With(v => v.Status = DownloadItemStatus.Downloading)
.Build();
var remoteEpisode = Builder<RemoteEpisode>.CreateNew()
.With(v => v.Series = new Series())
.Build();
_trackedDownload = new TrackedDownload
{
DownloadItem = downloadItem,
RemoteEpisode = remoteEpisode,
State = TrackedDownloadStage.Downloading
};
}
private void GivenValidQueueItem()
{
Mocker.GetMock<ITrackedDownloadService>()
.Setup(s => s.Find("sab1"))
.Returns(_trackedDownload);
} }
[Test] [Test]
@ -51,6 +84,42 @@ namespace NzbDrone.Core.Test.MediaFiles
Mocker.GetMock<IDownloadedEpisodesImportService>().Verify(c => c.ProcessRootFolder(It.IsAny<DirectoryInfo>()), Times.Never()); Mocker.GetMock<IDownloadedEpisodesImportService>().Verify(c => c.ProcessRootFolder(It.IsAny<DirectoryInfo>()), Times.Never());
ExceptionVerification.ExpectedWarns(1); ExceptionVerification.ExpectedWarns(1);
} }
[Test]
public void should_ignore_downloadclientid_if_path_is_not_specified()
{
Subject.Execute(new DownloadedEpisodesScanCommand() { DownloadClientId = "sab1" });
Mocker.GetMock<IDownloadedEpisodesImportService>().Verify(c => c.ProcessRootFolder(It.IsAny<DirectoryInfo>()), Times.Once());
}
[Test]
public void should_process_folder_if_downloadclientid_is_not_specified()
{
Subject.Execute(new DownloadedEpisodesScanCommand() { Path = _downloadFolder });
Mocker.GetMock<IDownloadedEpisodesImportService>().Verify(c => c.ProcessPath(It.IsAny<string>(), null, null), Times.Once());
}
[Test]
public void should_process_folder_with_downloadclientitem_if_available()
{
GivenValidQueueItem();
Subject.Execute(new DownloadedEpisodesScanCommand() { Path = _downloadFolder, DownloadClientId = "sab1" });
Mocker.GetMock<IDownloadedEpisodesImportService>().Verify(c => c.ProcessPath(_downloadFolder, _trackedDownload.RemoteEpisode.Series, _trackedDownload.DownloadItem), Times.Once());
}
[Test]
public void should_process_folder_without_downloadclientitem_if_not_available()
{
Subject.Execute(new DownloadedEpisodesScanCommand() { Path = _downloadFolder, DownloadClientId = "sab1" });
Mocker.GetMock<IDownloadedEpisodesImportService>().Verify(c => c.ProcessPath(_downloadFolder, null, null), Times.Once());
ExceptionVerification.ExpectedWarns(1);
}
} }
} }

View File

@ -14,5 +14,9 @@ namespace NzbDrone.Core.MediaFiles.Commands
} }
public Boolean SendUpdates { get; set; } public Boolean SendUpdates { get; set; }
// Properties used by third-party apps, do not modify.
public String Path { get; set; }
public String DownloadClientId { get; set; }
} }
} }

View File

@ -4,7 +4,10 @@ using System.IO;
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Download;
using NzbDrone.Core.Download.TrackedDownloads;
using NzbDrone.Core.MediaFiles.Commands; using NzbDrone.Core.MediaFiles.Commands;
using NzbDrone.Core.MediaFiles.EpisodeImport; using NzbDrone.Core.MediaFiles.EpisodeImport;
using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Commands;
@ -14,16 +17,19 @@ namespace NzbDrone.Core.MediaFiles
public class DownloadedEpisodesCommandService : IExecute<DownloadedEpisodesScanCommand> public class DownloadedEpisodesCommandService : IExecute<DownloadedEpisodesScanCommand>
{ {
private readonly IDownloadedEpisodesImportService _downloadedEpisodesImportService; private readonly IDownloadedEpisodesImportService _downloadedEpisodesImportService;
private readonly ITrackedDownloadService _trackedDownloadService;
private readonly IDiskProvider _diskProvider; private readonly IDiskProvider _diskProvider;
private readonly IConfigService _configService; private readonly IConfigService _configService;
private readonly Logger _logger; private readonly Logger _logger;
public DownloadedEpisodesCommandService(IDownloadedEpisodesImportService downloadedEpisodesImportService, public DownloadedEpisodesCommandService(IDownloadedEpisodesImportService downloadedEpisodesImportService,
ITrackedDownloadService trackedDownloadService,
IDiskProvider diskProvider, IDiskProvider diskProvider,
IConfigService configService, IConfigService configService,
Logger logger) Logger logger)
{ {
_downloadedEpisodesImportService = downloadedEpisodesImportService; _downloadedEpisodesImportService = downloadedEpisodesImportService;
_trackedDownloadService = trackedDownloadService;
_diskProvider = diskProvider; _diskProvider = diskProvider;
_configService = configService; _configService = configService;
_logger = logger; _logger = logger;
@ -48,9 +54,49 @@ namespace NzbDrone.Core.MediaFiles
return _downloadedEpisodesImportService.ProcessRootFolder(new DirectoryInfo(downloadedEpisodesFolder)); return _downloadedEpisodesImportService.ProcessRootFolder(new DirectoryInfo(downloadedEpisodesFolder));
} }
private List<ImportResult> ProcessFolder(DownloadedEpisodesScanCommand message)
{
if (!_diskProvider.FolderExists(message.Path))
{
_logger.Warn("Folder specified for import scan [{0}] doesn't exist.", message.Path);
return new List<ImportResult>();
}
if (message.DownloadClientId.IsNotNullOrWhiteSpace())
{
var trackedDownload = _trackedDownloadService.Find(message.DownloadClientId);
if (trackedDownload != null)
{
_logger.Debug("External directory scan request for known download {0}. [{1}]", message.DownloadClientId, message.Path);
return _downloadedEpisodesImportService.ProcessPath(message.Path, trackedDownload.RemoteEpisode.Series, trackedDownload.DownloadItem);
}
else
{
_logger.Warn("External directory scan request for unknown download {0}, attempting normal import. [{1}]", message.DownloadClientId, message.Path);
return _downloadedEpisodesImportService.ProcessPath(message.Path);
}
}
return _downloadedEpisodesImportService.ProcessPath(message.Path);
}
public void Execute(DownloadedEpisodesScanCommand message) public void Execute(DownloadedEpisodesScanCommand message)
{ {
var importResults = ProcessDroneFactoryFolder(); List<ImportResult> importResults;
if (message.Path.IsNotNullOrWhiteSpace())
{
importResults = ProcessFolder(message);
}
else
{
importResults = ProcessDroneFactoryFolder();
}
if (importResults == null || importResults.All(v => v.Result != ImportResultType.Imported)) if (importResults == null || importResults.All(v => v.Result != ImportResultType.Imported))
{ {
// Atm we don't report it as a command failure, coz that would cause the download to be failed. // Atm we don't report it as a command failure, coz that would cause the download to be failed.