using System; using System.Collections.Generic; using System.Linq; using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; using NzbDrone.Common.Extensions; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles.Events; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Organizer; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Music; using NzbDrone.Test.Common; using System.IO; namespace NzbDrone.Core.Test.MediaFiles.TrackFileMovingServiceTests { [TestFixture] public class MoveTrackFileFixture : CoreTest { private Artist _artist; private TrackFile _trackFile; private LocalTrack _localtrack; [SetUp] public void Setup() { _artist = Builder.CreateNew() .With(s => s.Path = @"C:\Test\Music\Artist".AsOsAgnostic()) .Build(); _trackFile = Builder.CreateNew() .With(f => f.Path = null) .With(f => f.Path = Path.Combine(_artist.Path, @"Album\File.mp3")) .Build(); _localtrack = Builder.CreateNew() .With(l => l.Artist = _artist) .With(l => l.Tracks = Builder.CreateListOfSize(1).Build().ToList()) .Build(); Mocker.GetMock() .Setup(s => s.BuildTrackFileName(It.IsAny>(), It.IsAny(), It.IsAny(), It.IsAny(), null, null)) .Returns("File Name"); Mocker.GetMock() .Setup(s => s.BuildTrackFilePath(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(@"C:\Test\Music\Artist\Album\File Name.mp3".AsOsAgnostic()); Mocker.GetMock() .Setup(s => s.BuildAlbumPath(It.IsAny(), It.IsAny())) .Returns(@"C:\Test\Music\Artist\Album".AsOsAgnostic()); var rootFolder = @"C:\Test\Music\".AsOsAgnostic(); Mocker.GetMock() .Setup(s => s.FolderExists(rootFolder)) .Returns(true); Mocker.GetMock() .Setup(s => s.FileExists(It.IsAny())) .Returns(true); } [Test] public void should_catch_UnauthorizedAccessException_during_folder_inheritance() { WindowsOnly(); Mocker.GetMock() .Setup(s => s.InheritFolderPermissions(It.IsAny())) .Throws(); Subject.MoveTrackFile(_trackFile, _localtrack); } [Test] public void should_catch_InvalidOperationException_during_folder_inheritance() { WindowsOnly(); Mocker.GetMock() .Setup(s => s.InheritFolderPermissions(It.IsAny())) .Throws(); Subject.MoveTrackFile(_trackFile, _localtrack); } [Test] public void should_notify_on_artist_folder_creation() { Subject.MoveTrackFile(_trackFile, _localtrack); Mocker.GetMock() .Verify(s => s.PublishEvent(It.Is(p => p.ArtistFolder.IsNotNullOrWhiteSpace())), Times.Once()); } [Test] public void should_notify_on_album_folder_creation() { Subject.MoveTrackFile(_trackFile, _localtrack); Mocker.GetMock() .Verify(s => s.PublishEvent(It.Is(p => p.AlbumFolder.IsNotNullOrWhiteSpace())), Times.Once()); } [Test] public void should_not_notify_if_artist_folder_already_exists() { Mocker.GetMock() .Setup(s => s.FolderExists(_artist.Path)) .Returns(true); Subject.MoveTrackFile(_trackFile, _localtrack); Mocker.GetMock() .Verify(s => s.PublishEvent(It.Is(p => p.ArtistFolder.IsNotNullOrWhiteSpace())), Times.Never()); } } }