using System; using System.IO; using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; using NzbDrone.Common.Extensions; using NzbDrone.Core.Books; 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.Test.Common; namespace NzbDrone.Core.Test.MediaFiles.TrackFileMovingServiceTests { [TestFixture] public class MoveTrackFileFixture : CoreTest { private Author _author; private BookFile _trackFile; private LocalBook _localtrack; [SetUp] public void Setup() { _author = Builder.CreateNew() .With(s => s.Path = @"C:\Test\Music\Author".AsOsAgnostic()) .Build(); _trackFile = Builder.CreateNew() .With(f => f.Path = null) .With(f => f.Path = Path.Combine(_author.Path, @"Book\File.mp3")) .Build(); _localtrack = Builder.CreateNew() .With(l => l.Author = _author) .With(l => l.Book = Builder.CreateNew().Build()) .Build(); Mocker.GetMock() .Setup(s => s.BuildBookFileName(It.IsAny(), It.IsAny(), It.IsAny(), null, null)) .Returns("File Name"); Mocker.GetMock() .Setup(s => s.BuildBookFilePath(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(@"C:\Test\Music\Author\Book\File Name.mp3".AsOsAgnostic()); Mocker.GetMock() .Setup(s => s.BuildBookPath(It.IsAny())) .Returns(@"C:\Test\Music\Author\Book".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.MoveBookFile(_trackFile, _localtrack); } [Test] public void should_catch_InvalidOperationException_during_folder_inheritance() { WindowsOnly(); Mocker.GetMock() .Setup(s => s.InheritFolderPermissions(It.IsAny())) .Throws(); Subject.MoveBookFile(_trackFile, _localtrack); } [Test] public void should_notify_on_author_folder_creation() { Subject.MoveBookFile(_trackFile, _localtrack); Mocker.GetMock() .Verify(s => s.PublishEvent(It.Is(p => p.AuthorFolder.IsNotNullOrWhiteSpace())), Times.Once()); } [Test] public void should_notify_on_book_folder_creation() { Subject.MoveBookFile(_trackFile, _localtrack); Mocker.GetMock() .Verify(s => s.PublishEvent(It.Is(p => p.BookFolder.IsNotNullOrWhiteSpace())), Times.Once()); } [Test] public void should_not_notify_if_author_folder_already_exists() { Mocker.GetMock() .Setup(s => s.FolderExists(_author.Path)) .Returns(true); Subject.MoveBookFile(_trackFile, _localtrack); Mocker.GetMock() .Verify(s => s.PublishEvent(It.Is(p => p.AuthorFolder.IsNotNullOrWhiteSpace())), Times.Never()); } } }