You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Sonarr/NzbDrone.Core.Test/JobTests/BannerDownloadJobTest.cs

242 lines
8.4 KiB

using System.IO;
using System.Net;
using AutoMoq;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Providers.Jobs;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework;
using DiskProvider = NzbDrone.Core.Providers.Core.DiskProvider;
namespace NzbDrone.Core.Test.JobTests
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class BannerDownloadJobTest : TestBase
{
13 years ago
[SetUp]
public void Setup()
{
WithStrictMocker();
WithTempAsStartUpPath();
}
[Test]
public void BannerDownload_all()
{
//Setup
var fakeSeries = Builder<Series>.CreateListOfSize(10)
.Build();
var notification = new ProgressNotification("Banner Download");
13 years ago
Mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetAllSeries())
.Returns(fakeSeries);
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()));
13 years ago
Mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
13 years ago
Mocker.Resolve<BannerDownloadJob>().Start(notification, 0, 0);
//Assert
13 years ago
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)
.Random(2)
.With(s => s.BannerUrl = null)
.Build();
13 years ago
var notification = new ProgressNotification("Banner Download");
13 years ago
Mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetAllSeries())
.Returns(fakeSeries);
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()));
13 years ago
Mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
13 years ago
Mocker.Resolve<BannerDownloadJob>().Start(notification, 0, 0);
//Assert
13 years ago
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
var fakeSeries = Builder<Series>.CreateListOfSize(10)
.Build();
13 years ago
var pathProvider = Mocker.Resolve<PathProvider>();
var notification = new ProgressNotification("Banner Download");
13 years ago
Mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetAllSeries())
.Returns(fakeSeries);
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(pathProvider.BannerPath, "1.jpg")))
.Throws(new WebException());
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(pathProvider.BannerPath, "2.jpg")));
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(pathProvider.BannerPath, "3.jpg")))
.Throws(new WebException());
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(pathProvider.BannerPath, "4.jpg")));
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(pathProvider.BannerPath, "5.jpg")))
.Throws(new WebException());
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(pathProvider.BannerPath, "6.jpg")));
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(pathProvider.BannerPath, "7.jpg")))
.Throws(new WebException());
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(pathProvider.BannerPath, "8.jpg")));
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(pathProvider.BannerPath, "9.jpg")))
.Throws(new WebException());
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), Path.Combine(pathProvider.BannerPath, "10.jpg")));
13 years ago
Mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
13 years ago
Mocker.Resolve<BannerDownloadJob>().Start(notification, 0, 0);
//Assert
13 years ago
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");
13 years ago
Mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetAllSeries())
.Returns(fakeSeries);
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
.Throws(new WebException());
13 years ago
Mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
13 years ago
Mocker.Resolve<BannerDownloadJob>().Start(notification, 0, 0);
//Assert
13 years ago
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");
13 years ago
Mocker.GetMock<SeriesProvider>()
.Setup(c => c.GetSeries(1))
.Returns(fakeSeries);
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
.Throws(new WebException());
13 years ago
Mocker.GetMock<DiskProvider>()
.Setup(S => S.CreateDirectory(It.IsAny<string>()))
.Returns("");
//Act
13 years ago
Mocker.Resolve<BannerDownloadJob>().Start(notification, 1, 0);
//Assert
13 years ago
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");
13 years ago
Mocker.GetMock<HttpProvider>()
.Setup(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()))
.Throws(new WebException());
//Act
13 years ago
Mocker.Resolve<BannerDownloadJob>().DownloadBanner(notification, fakeSeries);
//Assert
13 years ago
Mocker.VerifyAllMocks();
Mocker.GetMock<HttpProvider>().Verify(s => s.DownloadFile(It.IsAny<string>(), It.IsAny<string>()),
Times.Once());
}
}
}