public async void TestArtist() { var mockContext = new Mock <PersonContext>(); var mockRepo = new Mock <Repository <Albums> >(); var mockFact = new Mock <IHttpClientFactory>(); HttpResponseMessage mockResp = new HttpResponseMessage(); // Music constructor var music = new ITunesService(mockFact.Object, mockContext.Object); // Key HttpClient setup var mockHandler = new Mock <HttpMessageHandler>(); mockFact.Setup(x => x.CreateClient("")).Returns(new HttpClient(mockHandler.Object)); // Mock SendAsync mockHandler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()). ReturnsAsync(mockResp); mockResp.Content = new StringContent("{\"resultCount\":1, \"results\" :[{\"artistId\":1, \"collectionId\":2,\"collectionName\":\"album\"}]}", Encoding.UTF8, "text/javascript"); var ret = await music.GetArtist("zztop"); Assert.Equal(1, ret.ResultCount); }
/// <summary> /// Queries the iTunes service for podcast entries. /// </summary> /// <param name="count">Number of podcasts to fetch.</param> /// <param name="iTunesId">Optional ID for filtering.</param> /// <param name="allowExplicit">Filters child-friendly content.</param> /// <returns></returns> private async Task <IEnumerable <DomainModels.Podcast> > GetiTunesPodcastsAsync(int count, int?iTunesId, string isoCountryCode, bool allowExplicit) { var iTunesPodcasts = await ITunesService.GetTopPodcastsAsync(count, isoCountryCode, allowExplicit, iTunesId); return(iTunesPodcasts.Select(p => new DomainModels.Podcast() { Title = p.Name, RssUrl = p.RssUrl, ImageUrl = p.ImageUrl })); }
// Dispose(bool disposing) executes in two distinct scenarios. // If disposing equals true, the method has been called directly // or indirectly by a user's code. Managed and unmanaged resources // can be disposed. // If disposing equals false, the method has been called by the // runtime from inside the finalizer and you should not reference // other objects. Only unmanaged resources can be disposed. protected override void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (this.m_isDisposed == false) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { if (this.m_tunesService != null) { this.m_tunesService = null; } } this.m_isDisposed = true; } base.Dispose(disposing); }
public async void TestAlbum() { var mockContext = new Mock <PersonContext>(); var mockRepo = new Mock <Repository <Albums> >(); var mockFact = new Mock <IHttpClientFactory>(); HttpResponseMessage mockResp = new HttpResponseMessage(); // Music constructor var music = new ITunesService(mockFact.Object, mockContext.Object); // Key HttpClient setup var mockHandler = new Mock <HttpMessageHandler>(); mockFact.Setup(x => x.CreateClient("")).Returns(new HttpClient(mockHandler.Object)); // Mock SendAsync mockHandler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()). ReturnsAsync(mockResp); // Return from itunes HTTP call mockResp.Content = new StringContent("{\"resultCount\":1, \"results\" :[{\"artistId\":1, \"collectionId\":2,\"collectionName\":\"album\"}]}", Encoding.UTF8, "text/javascript"); // Mock repository var mockSet = new Mock <DbSet <Albums> >(); var aList = new List <Albums>(); IQueryable <Albums> empty = aList.AsQueryable(); // Return an actual queryable list. mockSet.As <IQueryable <Albums> >().Setup(m => m.Provider).Returns(empty.Provider); mockSet.As <IQueryable <Albums> >().Setup(m => m.Expression).Returns(empty.Expression); mockSet.As <IQueryable <Albums> >().Setup(m => m.ElementType).Returns(empty.ElementType); mockSet.As <IQueryable <Albums> >().Setup(m => m.GetEnumerator()).Returns(empty.GetEnumerator()); mockSet.Setup(d => d.Add(It.IsAny <Albums>())).Callback <Albums>((s) => aList.Add(s)); mockContext.Setup(m => m.Albums).Returns(mockSet.Object); var ret = await music.GetAlbums("zztop"); Assert.Equal(1, ret.ResultCount); Assert.Single(aList); // Verify correct object stored. mockSet.Verify(x => x.Add(It.Is <Albums>(a => a.ArtistId == 1 && a.CollectionId == 2 && a.CollectionName == "album"))); }
/// <summary> /// Queries sources for the specified search term. /// </summary> /// <param name="query">Value to search for.</param> /// <param name="limit">Number of items to return.</param> /// <param name="allowExplicit">Filters child-friendly content.</param> /// <param name="countryCode">Country to fetch data for.</param> /// <returns></returns> public async Task <IEnumerable <DomainModels.Podcast> > SearchPodcastsAsync(string query, int limit, bool alowExplicit, string countryCode) { var cacheTerm = query + alowExplicit; if (!Cache.TryGetValue(cacheTerm, out IEnumerable <DomainModels.Podcast> podcasts)) { var fetchedPodcasts = new List <DomainModels.Podcast>(); var iTunesPodcasts = await ITunesService.SearchPodcastsAsync(query, limit, countryCode, alowExplicit); var dbPodcasts = DbContext.Podcasts.Where(c => c.Active && c.Title.Contains(query)).Take(limit); if (!alowExplicit) { dbPodcasts = dbPodcasts.Where(c => c.IsExplicit == false); } fetchedPodcasts.AddRange(dbPodcasts.Select(p => new DomainModels.Podcast { Title = p.Title, RssUrl = p.RSS, ImageUrl = p.ImageUrl })); fetchedPodcasts.AddRange(iTunesPodcasts.Select(p => new DomainModels.Podcast { Title = p.Name, RssUrl = p.RssUrl, ImageUrl = p.ImageUrl })); podcasts = fetchedPodcasts; var cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddHours(1)); Cache.Set(cacheTerm, podcasts, cacheEntryOptions); } return(podcasts); }
public TunesController() { this.m_tunesService = new BSE.Tunes.Entities.TunesBusinessObject(); }
public FilesController() { this.m_fileProvider = new FileProvider(); this.m_tunesService = new BSE.Tunes.Entities.TunesBusinessObject(); this.m_impersonationUser = Settings.ImpersonationUser; }