using System.Collections.Generic; using System.IO; using System.Linq; using FizzWare.NBuilder; using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; using NzbDrone.Core.Organizer; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Music; using NzbDrone.Core.Music.Commands; using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.MusicTests { [TestFixture] public class MoveArtistServiceFixture : CoreTest { private Artist _artist; private MoveArtistCommand _command; private BulkMoveArtistCommand _bulkCommand; [SetUp] public void Setup() { _artist = Builder .CreateNew() .Build(); _command = new MoveArtistCommand { ArtistId = 1, SourcePath = @"C:\Test\Music\Artist".AsOsAgnostic(), DestinationPath = @"C:\Test\Music2\Artist".AsOsAgnostic() }; _bulkCommand = new BulkMoveArtistCommand { Artist = new List { new BulkMoveArtist { ArtistId = 1, SourcePath = @"C:\Test\Music\Artist".AsOsAgnostic() } }, DestinationRootFolder = @"C:\Test\Music2".AsOsAgnostic() }; Mocker.GetMock() .Setup(s => s.GetArtist(It.IsAny())) .Returns(_artist); } private void GivenFailedMove() { Mocker.GetMock() .Setup(s => s.TransferFolder(It.IsAny(), It.IsAny(), TransferMode.Move, true)) .Throws(); } [Test] public void should_log_error_when_move_throws_an_exception() { GivenFailedMove(); Subject.Execute(_command); ExceptionVerification.ExpectedErrors(1); } [Test] public void should_revert_artist_path_on_error() { GivenFailedMove(); Subject.Execute(_command); ExceptionVerification.ExpectedErrors(1); Mocker.GetMock() .Verify(v => v.UpdateArtist(It.IsAny()), Times.Once()); } [Test] public void should_use_destination_path() { Subject.Execute(_command); Mocker.GetMock() .Verify(v => v.TransferFolder(_command.SourcePath, _command.DestinationPath, TransferMode.Move, It.IsAny()), Times.Once()); Mocker.GetMock() .Verify(v => v.GetArtistFolder(It.IsAny(), null), Times.Never()); } [Test] public void should_build_new_path_when_root_folder_is_provided() { var artistFolder = "Artist"; var expectedPath = Path.Combine(_bulkCommand.DestinationRootFolder, artistFolder); Mocker.GetMock() .Setup(s => s.GetArtistFolder(It.IsAny(), null)) .Returns(artistFolder); Subject.Execute(_bulkCommand); Mocker.GetMock() .Verify(v => v.TransferFolder(_bulkCommand.Artist.First().SourcePath, expectedPath, TransferMode.Move, It.IsAny()), Times.Once()); } } }