using System; using System.Collections.Generic; using System.Linq; using FizzWare.NBuilder; using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Http; using NzbDrone.Core.MediaCover; using NzbDrone.Core.Music; using NzbDrone.Core.Music.Events; using NzbDrone.Core.Test.Framework; namespace NzbDrone.Core.Test.MediaCoverTests { [TestFixture] public class MediaCoverServiceFixture : CoreTest { private Artist _artist; private Album _album; private HttpResponse _httpResponse; [SetUp] public void Setup() { Mocker.SetConstant(new AppFolderInfo(Mocker.Resolve())); _artist = Builder.CreateNew() .With(v => v.Id = 2) .With(v => v.Metadata.Value.Images = new List { new MediaCover.MediaCover(MediaCoverTypes.Poster, "") }) .Build(); _album = Builder.CreateNew() .With(v => v.Id = 4) .With(v => v.Images = new List { new MediaCover.MediaCover(MediaCoverTypes.Cover, "") }) .Build(); _httpResponse = new HttpResponse(null, new HttpHeader(), ""); Mocker.GetMock().Setup(c => c.Head(It.IsAny())).Returns(_httpResponse); } [TestCase(".png")] [TestCase(".jpg")] public void should_convert_cover_urls_to_local(string extension) { var covers = new List { new MediaCover.MediaCover { Url = "http://dummy.com/test" + extension, CoverType = MediaCoverTypes.Banner } }; Mocker.GetMock().Setup(c => c.FileGetLastWrite(It.IsAny())) .Returns(new DateTime(1234)); Mocker.GetMock().Setup(c => c.FileExists(It.IsAny())) .Returns(true); Subject.ConvertToLocalUrls(12, MediaCoverEntity.Artist, covers); covers.Single().Url.Should().Be("/MediaCover/12/banner" + extension + "?lastWrite=1234"); } [TestCase(".png")] [TestCase(".jpg")] public void convert_to_local_url_should_not_change_extension(string extension) { var covers = new List { new MediaCover.MediaCover { Url = "http://dummy.com/test" + extension, CoverType = MediaCoverTypes.Banner } }; Mocker.GetMock().Setup(c => c.FileGetLastWrite(It.IsAny())) .Returns(new DateTime(1234)); Mocker.GetMock().Setup(c => c.FileExists(It.IsAny())) .Returns(true); Subject.ConvertToLocalUrls(12, MediaCoverEntity.Artist, covers); covers.Single().Extension.Should().Be(extension); } [TestCase(".png")] [TestCase(".jpg")] public void should_convert_album_cover_urls_to_local(string extension) { var covers = new List { new MediaCover.MediaCover { Url = "http://dummy.com/test" + extension, CoverType = MediaCoverTypes.Disc } }; Mocker.GetMock().Setup(c => c.FileGetLastWrite(It.IsAny())) .Returns(new DateTime(1234)); Mocker.GetMock().Setup(c => c.FileExists(It.IsAny())) .Returns(true); Subject.ConvertToLocalUrls(6, MediaCoverEntity.Album, covers); covers.Single().Url.Should().Be("/MediaCover/Albums/6/disc" + extension + "?lastWrite=1234"); } [TestCase(".png")] [TestCase(".jpg")] public void should_convert_media_urls_to_local_without_time_if_file_doesnt_exist(string extension) { var covers = new List { new MediaCover.MediaCover { Url = "http://dummy.com/test" + extension, CoverType = MediaCoverTypes.Banner } }; Subject.ConvertToLocalUrls(12, MediaCoverEntity.Artist, covers); covers.Single().Url.Should().Be("/MediaCover/12/banner" + extension); } [Test] public void should_resize_covers_if_main_downloaded() { Mocker.GetMock() .Setup(v => v.AlreadyExists(It.IsAny(), It.IsAny(), It.IsAny())) .Returns(false); Mocker.GetMock() .Setup(v => v.GetAlbumsByArtist(It.IsAny())) .Returns(new List { _album }); Mocker.GetMock() .Setup(v => v.FileExists(It.IsAny())) .Returns(true); Subject.HandleAsync(new ArtistRefreshCompleteEvent(_artist)); 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(), It.IsAny())) .Returns(true); Mocker.GetMock() .Setup(v => v.GetAlbumsByArtist(It.IsAny())) .Returns(new List { _album }); Mocker.GetMock() .Setup(v => v.FileExists(It.IsAny())) .Returns(false); Subject.HandleAsync(new ArtistRefreshCompleteEvent(_artist)); 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(), It.IsAny())) .Returns(true); Mocker.GetMock() .Setup(v => v.FileExists(It.IsAny())) .Returns(true); Mocker.GetMock() .Setup(v => v.GetAlbumsByArtist(It.IsAny())) .Returns(new List { _album }); Mocker.GetMock() .Setup(v => v.GetFileSize(It.IsAny())) .Returns(1000); Subject.HandleAsync(new ArtistRefreshCompleteEvent(_artist)); 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(), It.IsAny())) .Returns(true); Mocker.GetMock() .Setup(v => v.FileExists(It.IsAny())) .Returns(true); Mocker.GetMock() .Setup(v => v.GetAlbumsByArtist(It.IsAny())) .Returns(new List { _album }); Mocker.GetMock() .Setup(v => v.GetFileSize(It.IsAny())) .Returns(0); Subject.HandleAsync(new ArtistRefreshCompleteEvent(_artist)); 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(), It.IsAny())) .Returns(true); Mocker.GetMock() .Setup(v => v.FileExists(It.IsAny())) .Returns(false); Mocker.GetMock() .Setup(v => v.GetAlbumsByArtist(It.IsAny())) .Returns(new List { _album }); Mocker.GetMock() .Setup(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny())) .Throws(); Subject.HandleAsync(new ArtistRefreshCompleteEvent(_artist)); Mocker.GetMock() .Verify(v => v.Resize(It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(2)); } } }