using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using FizzWare.NBuilder; using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Core.MediaCover; using NzbDrone.Core.Movies; using NzbDrone.Core.Movies.Events; using NzbDrone.Core.Test.Framework; namespace NzbDrone.Core.Test.MediaCoverTests { [TestFixture] public class MediaCoverServiceFixture : CoreTest { private Movie _movie; [SetUp] public void Setup() { Mocker.SetConstant(new AppFolderInfo(Mocker.Resolve())); _movie = Builder.CreateNew() .With(v => v.Id = 2) .With(v => v.MovieMetadata.Value.Images = new List { new MediaCover.MediaCover(MediaCoverTypes.Poster, "") }) .Build(); Mocker.GetMock().Setup(m => m.GetMovie(It.Is(id => id == _movie.Id))).Returns(_movie); } [Test] public void should_convert_cover_urls_to_local() { var covers = new List { new MediaCover.MediaCover { CoverType = MediaCoverTypes.Banner } }; var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Files", "Media", "H264_sample.mp4"); var fileInfo = new FileInfo(path); Mocker.GetMock() .Setup(c => c.GetFileInfo(It.IsAny())) .Returns(fileInfo); Subject.ConvertToLocalUrls(12, covers); covers.Single().Url.Should().Be($"/MediaCover/12/banner.jpg?lastWrite={fileInfo.LastWriteTimeUtc.Ticks}"); } [Test] public void should_convert_media_urls_to_local_without_time_if_file_doesnt_exist() { var covers = new List { new MediaCover.MediaCover { CoverType = MediaCoverTypes.Banner } }; var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Files", "Media", "NonExistant.mp4"); var fileInfo = new FileInfo(path); Mocker.GetMock() .Setup(c => c.GetFileInfo(It.IsAny())) .Returns(fileInfo); Subject.ConvertToLocalUrls(12, covers); covers.Single().Url.Should().Be("/MediaCover/12/banner.jpg"); } [Test] public void should_resize_covers_if_main_downloaded() { Mocker.GetMock() .Setup(v => v.AlreadyExists(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(false)); Mocker.GetMock() .Setup(v => v.FileExists(It.IsAny())) .Returns(true); Subject.HandleAsync(new MovieUpdatedEvent(_movie)); Mocker.GetMock() .Verify(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(2)); } [Test] public void should_resize_covers_if_missing() { Mocker.GetMock() .Setup(v => v.AlreadyExists(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(true)); Mocker.GetMock() .Setup(v => v.FileExists(It.IsAny())) .Returns(false); Subject.HandleAsync(new MovieUpdatedEvent(_movie)); Mocker.GetMock() .Verify(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(2)); } [Test] public void should_not_resize_covers_if_exists() { Mocker.GetMock() .Setup(v => v.AlreadyExists(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(true)); Mocker.GetMock() .Setup(v => v.FileExists(It.IsAny())) .Returns(true); Mocker.GetMock() .Setup(v => v.GetFileSize(It.IsAny())) .Returns(1000); Subject.HandleAsync(new MovieUpdatedEvent(_movie)); Mocker.GetMock() .Verify(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); } [Test] public void should_resize_covers_if_existing_is_empty() { Mocker.GetMock() .Setup(v => v.AlreadyExists(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(true)); Mocker.GetMock() .Setup(v => v.FileExists(It.IsAny())) .Returns(true); Mocker.GetMock() .Setup(v => v.GetFileSize(It.IsAny())) .Returns(0); Subject.HandleAsync(new MovieUpdatedEvent(_movie)); Mocker.GetMock() .Verify(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(2)); } [Test] public void should_log_error_if_resize_failed() { Mocker.GetMock() .Setup(v => v.AlreadyExists(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(true)); Mocker.GetMock() .Setup(v => v.FileExists(It.IsAny())) .Returns(false); Mocker.GetMock() .Setup(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny())) .Throws(); Subject.HandleAsync(new MovieUpdatedEvent(_movie)); Mocker.GetMock() .Verify(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(2)); } } }