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; namespace NzbDrone.Core.Test.JobTests { [TestFixture] // ReSharper disable InconsistentNaming public class BannerDownloadJobTest : TestBase { [SetUp] public void Setup() { WithStrictMocker(); WithTempAsStartUpPath(); } [Test] public void BannerDownload_all() { //Setup var fakeSeries = Builder.CreateListOfSize(10) .Build(); var notification = new ProgressNotification("Banner Download"); Mocker.GetMock() .Setup(c => c.GetAllSeries()) .Returns(fakeSeries); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), It.IsAny())); Mocker.GetMock() .Setup(S => S.CreateDirectory(It.IsAny())) .Returns(""); //Act Mocker.Resolve().Start(notification, 0, 0); //Assert Mocker.VerifyAllMocks(); Mocker.GetMock().Verify(s => s.DownloadFile(It.IsAny(), It.IsAny()), Times.Exactly(fakeSeries.Count)); } [Test] public void BannerDownload_some_null_BannerUrl() { //Setup var fakeSeries = Builder.CreateListOfSize(10) .Random(2) .With(s => s.BannerUrl = null) .Build(); var notification = new ProgressNotification("Banner Download"); Mocker.GetMock() .Setup(c => c.GetAllSeries()) .Returns(fakeSeries); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), It.IsAny())); Mocker.GetMock() .Setup(S => S.CreateDirectory(It.IsAny())) .Returns(""); //Act Mocker.Resolve().Start(notification, 0, 0); //Assert Mocker.VerifyAllMocks(); Mocker.GetMock().Verify(s => s.DownloadFile(It.IsAny(), It.IsAny()), Times.Exactly(8)); } [Test] public void BannerDownload_some_failed_download() { //Setup var fakeSeries = Builder.CreateListOfSize(10) .Build(); var pathProvider = Mocker.Resolve(); var notification = new ProgressNotification("Banner Download"); Mocker.GetMock() .Setup(c => c.GetAllSeries()) .Returns(fakeSeries); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "1.jpg"))) .Throws(new WebException()); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "2.jpg"))); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "3.jpg"))) .Throws(new WebException()); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "4.jpg"))); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "5.jpg"))) .Throws(new WebException()); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "6.jpg"))); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "7.jpg"))) .Throws(new WebException()); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "8.jpg"))); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "9.jpg"))) .Throws(new WebException()); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "10.jpg"))); Mocker.GetMock() .Setup(S => S.CreateDirectory(It.IsAny())) .Returns(""); //Act Mocker.Resolve().Start(notification, 0, 0); //Assert Mocker.VerifyAllMocks(); Mocker.GetMock().Verify(s => s.DownloadFile(It.IsAny(), It.IsAny()), Times.Exactly(fakeSeries.Count)); } [Test] public void BannerDownload_all_failed_download() { //Setup var fakeSeries = Builder.CreateListOfSize(10) .Build(); var notification = new ProgressNotification("Banner Download"); Mocker.GetMock() .Setup(c => c.GetAllSeries()) .Returns(fakeSeries); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), It.IsAny())) .Throws(new WebException()); Mocker.GetMock() .Setup(S => S.CreateDirectory(It.IsAny())) .Returns(""); //Act Mocker.Resolve().Start(notification, 0, 0); //Assert Mocker.VerifyAllMocks(); Mocker.GetMock().Verify(s => s.DownloadFile(It.IsAny(), It.IsAny()), Times.Exactly(fakeSeries.Count)); } [Test] public void BannerDownload_single_banner() { //Setup var fakeSeries = Builder.CreateNew() .With(s => s.SeriesId = 1) .Build(); var notification = new ProgressNotification("Banner Download"); Mocker.GetMock() .Setup(c => c.GetSeries(1)) .Returns(fakeSeries); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), It.IsAny())) .Throws(new WebException()); Mocker.GetMock() .Setup(S => S.CreateDirectory(It.IsAny())) .Returns(""); //Act Mocker.Resolve().Start(notification, 1, 0); //Assert Mocker.VerifyAllMocks(); Mocker.GetMock().Verify(s => s.DownloadFile(It.IsAny(), It.IsAny()), Times.Once()); } [Test] public void Download_Banner() { //Setup var fakeSeries = Builder.CreateNew() .With(s => s.SeriesId = 1) .Build(); var notification = new ProgressNotification("Banner Download"); Mocker.GetMock() .Setup(s => s.DownloadFile(It.IsAny(), It.IsAny())) .Throws(new WebException()); //Act Mocker.Resolve().DownloadBanner(notification, fakeSeries); //Assert Mocker.VerifyAllMocks(); Mocker.GetMock().Verify(s => s.DownloadFile(It.IsAny(), It.IsAny()), Times.Once()); } } }