using System; using System.Collections.Generic; using System.Net; using System.ServiceModel.Syndication; using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Common; using NzbDrone.Core.Configuration; using NzbDrone.Core.Indexers; using NzbDrone.Core.Model; using NzbDrone.Core.Providers; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common.AutoMoq; namespace NzbDrone.Core.Test.Indexers { [TestFixture] public class IndexerServiceTest : CoreTest { [Test] public void should_insert_indexer_in_repository_when_it_doesnt_exist() { Mocker.SetConstant>(new List { Mocker.Resolve() }); Subject.Init(); Mocker.GetMock() .Verify(v => v.Insert(It.IsAny()), Times.Once()); } [Test] public void getEnabled_should_not_return_any_when_no_indexers_are_enabled() { Mocker.SetConstant>(new List { Mocker.Resolve() }); Mocker.GetMock() .Setup(s => s.All()) .Returns(new List {new Indexer {Id = 1, Type = "", Enable = false, Name = "Fake Indexer"}}); Subject.GetEnabledIndexers().Should().BeEmpty(); } [Test] public void Init_indexer_should_enable_indexer_that_is_enabled_by_default() { Mocker.SetConstant>(new List { Mocker.Resolve() }); Subject.Init(); Mocker.GetMock() .Verify(v => v.Insert(It.Is(indexer => indexer.Enable)), Times.Once()); Mocker.GetMock() .Verify(v => v.Insert(It.Is(indexer => !indexer.Enable)), Times.Never()); } [Test] public void Init_indexer_should_not_enable_indexer_that_is_not_enabled_by_default() { Mocker.SetConstant>(new List { Mocker.Resolve() }); Subject.Init(); Mocker.GetMock() .Verify(v => v.Insert(It.Is(indexer => indexer.Enable)), Times.Never()); Mocker.GetMock() .Verify(v => v.Insert(It.Is(indexer => !indexer.Enable)), Times.Once()); } } public class MockIndexer : IndexerBase { public MockIndexer(HttpProvider httpProvider, IConfigService configService) : base(httpProvider, configService) { } protected override string[] Urls { get { return new[] { "www.google.com" }; } } public override bool IsConfigured { get { return true; } } protected override NetworkCredential Credentials { get { return null; } } protected override IEnumerable GetEpisodeSearchUrls(string seriesTitle, int seasonNumber, int episodeNumber) { throw new NotImplementedException(); } protected override IEnumerable GetDailyEpisodeSearchUrls(string seriesTitle, DateTime date) { throw new NotImplementedException(); } protected override IEnumerable GetSeasonSearchUrls(string seriesTitle, int seasonNumber) { throw new NotImplementedException(); } protected override IEnumerable GetPartialSeasonSearchUrls(string seriesTitle, int seasonNumber, int episodeWildcard) { throw new NotImplementedException(); } public override string Name { get { return "Mocked Indexer"; } } protected override string NzbDownloadUrl(SyndicationItem item) { return item.Links[0].Uri.ToString(); } protected override string NzbInfoUrl(SyndicationItem item) { return item.Links[0].Uri.ToString(); } } public class TestUrlIndexer : IndexerBase { public TestUrlIndexer(HttpProvider httpProvider, IConfigService configService) : base(httpProvider, configService) { } public override string Name { get { return "All Urls"; } } protected override string[] Urls { get { return new[] { "http://rss.nzbs.com/rss.php?cat=TV" }; } } public override bool IsConfigured { get { return true; } } protected override IEnumerable GetEpisodeSearchUrls(string seriesTitle, int seasonNumber, int episodeNumber) { throw new NotImplementedException(); } protected override IEnumerable GetDailyEpisodeSearchUrls(string seriesTitle, DateTime date) { throw new NotImplementedException(); } protected override IEnumerable GetSeasonSearchUrls(string seriesTitle, int seasonNumber) { throw new NotImplementedException(); } protected override IEnumerable GetPartialSeasonSearchUrls(string seriesTitle, int seasonNumber, int episodeWildcard) { throw new NotImplementedException(); } protected override string NzbDownloadUrl(SyndicationItem item) { return "http://google.com"; } protected override string NzbInfoUrl(SyndicationItem item) { return "http://google.com"; } } public class CustomParserIndexer : IndexerBase { public CustomParserIndexer(HttpProvider httpProvider, IConfigService configService) : base(httpProvider, configService) { } public override string Name { get { return "Custom parser"; } } protected override string[] Urls { get { return new[] { "http://www.google.com" }; } } public override bool IsConfigured { get { return true; } } protected override IEnumerable GetEpisodeSearchUrls(string seriesTitle, int seasonNumber, int episodeNumber) { throw new NotImplementedException(); } protected override IEnumerable GetDailyEpisodeSearchUrls(string seriesTitle, DateTime date) { throw new NotImplementedException(); } protected override IEnumerable GetSeasonSearchUrls(string seriesTitle, int seasonNumber) { throw new NotImplementedException(); } protected override IEnumerable GetPartialSeasonSearchUrls(string seriesTitle, int seasonNumber, int episodeWildcard) { throw new NotImplementedException(); } protected override string NzbDownloadUrl(SyndicationItem item) { return "http://www.google.com"; } protected override string NzbInfoUrl(SyndicationItem item) { return "http://www.google.com"; } protected override EpisodeParseResult CustomParser(SyndicationItem item, EpisodeParseResult currentResult) { if (currentResult == null) currentResult = new EpisodeParseResult(); currentResult.Language = LanguageType.Finnish; return currentResult; } } public class NotConfiguredIndexer : IndexerBase { public NotConfiguredIndexer(HttpProvider httpProvider, IConfigService configService) : base(httpProvider, configService) { } public override string Name { get { return "NotConfiguredIndexer"; } } protected override string[] Urls { get { return new[] { "http://rss.nzbs.com/rss.php?cat=TV" }; } } public override bool IsConfigured { get { return false; } } protected override IEnumerable GetEpisodeSearchUrls(string seriesTitle, int seasonNumber, int episodeNumber) { throw new NotImplementedException(); } protected override IEnumerable GetDailyEpisodeSearchUrls(string seriesTitle, DateTime date) { throw new NotImplementedException(); } protected override IEnumerable GetSeasonSearchUrls(string seriesTitle, int seasonNumber) { throw new NotImplementedException(); } protected override IEnumerable GetPartialSeasonSearchUrls(string seriesTitle, int seasonNumber, int episodeWildcard) { throw new NotImplementedException(); } protected override string NzbDownloadUrl(SyndicationItem item) { throw new NotImplementedException(); } protected override string NzbInfoUrl(SyndicationItem item) { throw new NotImplementedException(); } } public class DefaultEnabledIndexer : IndexerBase { public DefaultEnabledIndexer(HttpProvider httpProvider, IConfigService configService) : base(httpProvider, configService) { } protected override string[] Urls { get { return new[] { "www.google.com" }; } } public override bool IsConfigured { get { return true; } } protected override NetworkCredential Credentials { get { return null; } } protected override IEnumerable GetEpisodeSearchUrls(string seriesTitle, int seasonNumber, int episodeNumber) { throw new NotImplementedException(); } protected override IEnumerable GetDailyEpisodeSearchUrls(string seriesTitle, DateTime date) { throw new NotImplementedException(); } protected override IEnumerable GetSeasonSearchUrls(string seriesTitle, int seasonNumber) { throw new NotImplementedException(); } protected override IEnumerable GetPartialSeasonSearchUrls(string seriesTitle, int seasonNumber, int episodeWildcard) { throw new NotImplementedException(); } public override string Name { get { return "Mocked Indexer"; } } protected override string NzbDownloadUrl(SyndicationItem item) { return item.Links[0].Uri.ToString(); } protected override string NzbInfoUrl(SyndicationItem item) { return item.Links[1].Uri.ToString(); } public override bool EnabledByDefault { get { return true; } } } }