public async Task GetPlaylistVideosAsyncTest()
        {
            // Mock
            var httpMock       = new Mock <ISafeHttpClient>(MockBehavior.Strict);
            var youTubeService = new YouTubeService(httpMock.Object, config);

            httpMock.Setup(x => x.GetAsync <YouTube.OnlyIdResponse>(It.Is <string>(s => checkApiCall(s, "playlistItems", null))))
            .ReturnsAsync(_mockOnlyIdResponse);
            httpMock.Setup(x => x.GetAsync <YouTube.VideoResponse>(It.Is <string>(s => checkApiCall(s, "videos", null))))
            .ReturnsAsync(new YouTube.VideoResponse
            {
                Items = new List <YouTube.Video> {
                    _mockVideoResponse.Items[0],
                    _mockVideoResponse.Items[1],
                    _mockVideoResponse.Items[2]
                }
            });

            // Run
            var videos = await youTubeService.GetPlaylistVideosAsync("1");

            // Assert
            Assert.AreEqual(3, videos.Count, "First page retrieved");
            Assert.AreEqual(_mockOnlyIdResponse.Items[0].Id, videos[0].Id);
            Assert.AreEqual(_mockOnlyIdResponse.Items[1].Id, videos[1].Id);
            Assert.AreEqual(_mockOnlyIdResponse.Items[2].Id, videos[2].Id);

            // Verify
            httpMock.VerifyAll();
        }
Beispiel #2
0
        public async Task TestPlayList()
        {
            var service = new YouTubeService(new SafeHttpClient(), new Configuration.YouTubeConfiguration {
                ApiKey = _apiKey
            });

            var channelId = await service.GetChannelIdAsync(channelName);

            Assert.IsNotNull(channelId);

            var playlistId = await service.GetPlaylistIdAsync(channelId, playlistName);

            Assert.IsNotNull(playlistId);

            var progressCounter   = 0;
            var progressIndicator = new Progress <int>(i => Interlocked.Increment(ref progressCounter));
            var playlistItems     = await service.GetPlaylistVideosAsync(playlistId, progressIndicator);

            Assert.IsNotNull(playlistItems);
            Assert.IsNotEmpty(playlistItems);
            Assert.Greater(progressCounter, 0);

            TestContext.WriteLine($"Playlist items: {playlistItems.Count}");
            foreach (var item in playlistItems)
            {
                Assert.IsNotNull(item?.Id);
            }
        }
        public void GetPlaylistVideosAsyncWithCancellationTokenTest()
        {
            // Mock
            var ctSource       = new CancellationTokenSource();
            var httpMock       = new Mock <ISafeHttpClient>(MockBehavior.Strict);
            var youTubeService = new YouTubeService(httpMock.Object, config);

            httpMock.Setup(x => x.GetAsync <YouTube.OnlyIdResponse>(It.Is <string>(s => checkApiCall(s, "playlistItems", null))))
            .Callback(() => ctSource.Cancel())
            .ReturnsAsync(_mockOnlyIdResponse);

            // Assert
            Assert.ThrowsAsync <TaskCanceledException>(async() => await youTubeService.GetPlaylistVideosAsync("1", ctSource.Token));

            // Verify
            httpMock.VerifyAll();
        }
        public async Task GetPlaylistVideosAsyncNotFoundTest()
        {
            // Mock
            var httpMock       = new Mock <ISafeHttpClient>(MockBehavior.Strict);
            var youTubeService = new YouTubeService(httpMock.Object, config);

            httpMock.Setup(x => x.GetAsync <YouTube.OnlyIdResponse>(It.Is <string>(s => checkApiCall(s, "playlistItems", null))))
            .ReturnsAsync(null as YouTube.OnlyIdResponse);

            // Run
            var videos = await youTubeService.GetPlaylistVideosAsync("1");

            // Assert
            Assert.IsEmpty(videos);

            // Verify
            httpMock.VerifyAll();
        }
        public async Task GetPlaylistVideosAsyncWithProgressTest()
        {
            var progressCalled    = 0;
            var progressIndicator = new Progress <int>(i => Interlocked.Increment(ref progressCalled));

            // Mock
            var httpMock       = new Mock <ISafeHttpClient>(MockBehavior.Strict);
            var youTubeService = new YouTubeService(httpMock.Object, config);

            httpMock.SetupSequence(x => x.GetAsync <YouTube.OnlyIdResponse>(It.Is <string>(s => checkApiCall(s, "playlistItems", null))))
            .ReturnsAsync(_mockOnlyIdResponse)
            .ReturnsAsync(_mockOnlyIdResponseSecondPage);
            httpMock.Setup(x => x.GetAsync <YouTube.VideoResponse>(It.Is <string>(s => checkApiCall(s, "videos", null))))
            .ReturnsAsync(new YouTube.VideoResponse
            {
                Items = new List <YouTube.Video> {
                    _mockVideoResponse.Items[0],
                    _mockVideoResponse.Items[1],
                    _mockVideoResponse.Items[2],
                    _mockVideoResponseSecondPage.Items[0]
                }
            });

            // Run
            var videos = await youTubeService.GetPlaylistVideosAsync("1", progressIndicator);

            // Wait for progress
            Thread.Sleep(10);

            // Assert
            Assert.AreEqual(4, videos.Count);
            Assert.Greater(progressCalled, 0);

            // Verify
            httpMock.VerifyAll();
        }