Sonarr/NzbDrone.Core.Test/JobTests/BannerDownloadJobTest.cs

216 lines
7.2 KiB
C#
Raw Normal View History

using System.IO;
using System.Net;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
2011-12-02 01:33:17 +00:00
using NzbDrone.Core.Jobs;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.JobTests
{
[TestFixture]
// ReSharper disable InconsistentNaming
2011-11-13 07:27:16 +00:00
public class BannerDownloadJobTest : CoreTest
{
2011-11-03 23:23:54 +00:00
[SetUp]
public void Setup()
{
WithStrictMocker();
2011-11-13 07:27:16 +00:00
WithTempAsAppPath();
2011-11-03 23:23:54 +00:00
}
[Test]
public void BannerDownload_all()
{
//Setup
var fakeSeries = Builder<Series>.CreateListOfSize(10)
.Build();
var notification = new ProgressNotification("Banner Download");
2011-11-03 23:23:54 +00:00
Mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetAllSeries())
.Returns(fakeSeries);
2011-11-03 23:23:54 +00:00
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()));
2011-11-03 23:23:54 +00:00
Mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
2011-11-03 23:23:54 +00:00
Mocker.Resolve<BannerDownloadJob>().Start(notification, 0, 0);
//Assert
2011-11-03 23:23:54 +00:00
Mocker.VerifyAllMocks();
Mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Exactly(fakeSeries.Count));
}
[Test]
public void BannerDownload_some_null_BannerUrl()
{
//Setup
var fakeSeries = Builder<Series>.CreateListOfSize(10)
2011-10-23 05:39:14 +00:00
.Random(2)
2011-10-18 21:46:06 +00:00
.With(s => s.BannerUrl = null)
.Build();
2011-11-03 23:23:54 +00:00
var notification = new ProgressNotification("Banner Download");
2011-11-03 23:23:54 +00:00
Mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetAllSeries())
.Returns(fakeSeries);
2011-11-03 23:23:54 +00:00
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()));
2011-11-03 23:23:54 +00:00
Mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
2011-11-03 23:23:54 +00:00
Mocker.Resolve<BannerDownloadJob>().Start(notification, 0, 0);
//Assert
2011-11-03 23:23:54 +00:00
Mocker.VerifyAllMocks();
Mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Exactly(8));
}
[Test]
public void BannerDownload_some_failed_download()
{
//Setup
2011-11-13 07:27:16 +00:00
var fakeSeries = Builder<Series>.CreateListOfSize(4)
.Build();
2011-11-13 07:27:16 +00:00
var bannerPath = Mocker.GetMock<EnviromentProvider>().Object.GetBannerPath();
var notification = new ProgressNotification("Banner Download");
2011-11-03 23:23:54 +00:00
Mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetAllSeries())
.Returns(fakeSeries);
2011-11-03 23:23:54 +00:00
Mocker.GetMock<HttpProvider>()
2011-11-13 07:27:16 +00:00
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(bannerPath, "1.jpg")))
.Throws(new WebException());
2011-11-03 23:23:54 +00:00
Mocker.GetMock<HttpProvider>()
2011-11-13 07:27:16 +00:00
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(bannerPath, "2.jpg")));
2011-11-03 23:23:54 +00:00
Mocker.GetMock<HttpProvider>()
2011-11-13 07:27:16 +00:00
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(bannerPath, "3.jpg")))
.Throws(new WebException());
2011-11-03 23:23:54 +00:00
Mocker.GetMock<HttpProvider>()
2011-11-13 07:27:16 +00:00
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(bannerPath, "4.jpg")));
2011-11-03 23:23:54 +00:00
Mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
2011-11-03 23:23:54 +00:00
Mocker.Resolve<BannerDownloadJob>().Start(notification, 0, 0);
//Assert
2011-11-03 23:23:54 +00:00
Mocker.VerifyAllMocks();
Mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Exactly(fakeSeries.Count));
}
[Test]
public void BannerDownload_all_failed_download()
{
//Setup
var fakeSeries = Builder<Series>.CreateListOfSize(10)
.Build();
var notification = new ProgressNotification("Banner Download");
2011-11-03 23:23:54 +00:00
Mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetAllSeries())
.Returns(fakeSeries);
2011-11-03 23:23:54 +00:00
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
.Throws(new WebException());
2011-11-03 23:23:54 +00:00
Mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
2011-11-03 23:23:54 +00:00
Mocker.Resolve<BannerDownloadJob>().Start(notification, 0, 0);
//Assert
2011-11-03 23:23:54 +00:00
Mocker.VerifyAllMocks();
Mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Exactly(fakeSeries.Count));
}
[Test]
public void BannerDownload_single_banner()
{
//Setup
var fakeSeries = Builder<Series>.CreateNew()
.With(s => s.SeriesId = 1)
.Build();
var notification = new ProgressNotification("Banner Download");
2011-11-03 23:23:54 +00:00
Mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetSeries(1))
.Returns(fakeSeries);
2011-11-03 23:23:54 +00:00
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
.Throws(new WebException());
2011-11-03 23:23:54 +00:00
Mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
2011-11-03 23:23:54 +00:00
Mocker.Resolve<BannerDownloadJob>().Start(notification, 1, 0);
//Assert
2011-11-03 23:23:54 +00:00
Mocker.VerifyAllMocks();
Mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Once());
}
[Test]
public void Download_Banner()
{
//Setup
var fakeSeries = Builder<Series>.CreateNew()
.With(s => s.SeriesId = 1)
.Build();
var notification = new ProgressNotification("Banner Download");
2011-11-03 23:23:54 +00:00
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
.Throws(new WebException());
//Act
2011-11-03 23:23:54 +00:00
Mocker.Resolve<BannerDownloadJob>().DownloadBanner(notification, fakeSeries);
//Assert
2011-11-03 23:23:54 +00:00
Mocker.VerifyAllMocks();
Mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Once());
}
}
}