using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using FluentAssertions; using FluentValidation.Results; using Moq; using Newtonsoft.Json; using NUnit.Framework; using NzbDrone.Core.Books; using NzbDrone.Core.Books.Commands; using NzbDrone.Core.Configuration; using NzbDrone.Core.ImportLists.Exclusions; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles.BookImport; using NzbDrone.Core.MediaFiles.BookImport.Aggregation; using NzbDrone.Core.MediaFiles.BookImport.Aggregation.Aggregators; using NzbDrone.Core.MediaFiles.BookImport.Identification; using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.MetadataSource; using NzbDrone.Core.MetadataSource.BookInfo; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Profiles.Metadata; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.MediaFiles.BookImport.Identification { [TestFixture] public class IdentificationServiceFixture : DbTest { private AuthorService _authorService; private AddAuthorService _addAuthorService; private RefreshAuthorService _refreshAuthorService; private IdentificationService _Subject; [SetUp] public void SetUp() { UseRealHttp(); // Resolve all the parts we need Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); Mocker.GetMock().Setup(x => x.Exists(It.IsAny())).Returns(true); _authorService = Mocker.Resolve(); Mocker.SetConstant(_authorService); Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); _addAuthorService = Mocker.Resolve(); Mocker.SetConstant(Mocker.Resolve()); _refreshAuthorService = Mocker.Resolve(); Mocker.GetMock().Setup(x => x.Validate(It.IsAny())).Returns(new ValidationResult()); Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); // set up the augmenters var aggregators = new List> { Mocker.Resolve() }; Mocker.SetConstant>>(aggregators); Mocker.SetConstant(Mocker.Resolve()); _Subject = Mocker.Resolve(); } private void GivenMetadataProfile(MetadataProfile profile) { Mocker.GetMock().Setup(x => x.Get(profile.Id)).Returns(profile); } private List GivenAuthors(List authors) { var outp = new List(); for (var i = 0; i < authors.Count; i++) { var meta = authors[i].MetadataProfile; meta.Id = i + 1; GivenMetadataProfile(meta); outp.Add(GivenAuthor(authors[i].Author, meta.Id)); } return outp; } private Author GivenAuthor(string foreignAuthorId, int metadataProfileId) { var author = _addAuthorService.AddAuthor(new Author { Metadata = new AuthorMetadata { ForeignAuthorId = foreignAuthorId }, Path = @"c:\test".AsOsAgnostic(), MetadataProfileId = metadataProfileId }); var command = new RefreshAuthorCommand { AuthorId = author.Id, Trigger = CommandTrigger.Unspecified }; _refreshAuthorService.Execute(command); return _authorService.FindById(foreignAuthorId); } public static class IdTestCaseFactory { // for some reason using Directory.GetFiles causes nUnit to error private static string[] files = { "FilesWithMBIds.json", "PreferMissingToBadMatch.json", "InconsistentTyposInBook.json", "SucceedWhenManyBooksHaveSameTitle.json", "PenalizeUnknownMedia.json", "CorruptFile.json", "FilesWithoutTags.json" }; public static IEnumerable TestCases { get { foreach (var file in files) { yield return new TestCaseData(file).SetName($"should_match_tracks_{file.Replace(".json", "")}"); } } } } // these are slow to run so only do so manually [Explicit] [TestCaseSource(typeof(IdTestCaseFactory), "TestCases")] public void should_match_tracks(string file) { var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Files", "Identification", file); var testcase = JsonConvert.DeserializeObject(File.ReadAllText(path)); var authors = GivenAuthors(testcase.LibraryAuthors); var specifiedAuthor = authors.SingleOrDefault(x => x.Metadata.Value.ForeignAuthorId == testcase.Author); var idOverrides = new IdentificationOverrides { Author = specifiedAuthor }; var tracks = testcase.Tracks.Select(x => new LocalBook { Path = x.Path.AsOsAgnostic(), FileTrackInfo = x.FileTrackInfo }).ToList(); var config = new ImportDecisionMakerConfig { NewDownload = testcase.NewDownload, SingleRelease = testcase.SingleRelease, IncludeExisting = false }; var result = _Subject.Identify(tracks, idOverrides, config); result.Should().HaveCount(testcase.ExpectedMusicBrainzReleaseIds.Count); } } }