Esempio n. 1
0
        public async Task IfAnimeDoesNotExistAnErrorIsPassedBackToTheCaller()
        {
            // arrange
            const int malId   = 13127;
            var       fixture = new RequestProcessorFixture();

            var document    = new HtmlDocument();
            var path        = AppDomain.CurrentDomain.BaseDirectory;
            var examplePath = Path.Combine(path, "PageExamples", $"{malId}.html");

            using (var htmlFile = File.Open(examplePath, FileMode.Open))
            {
                document.Load(htmlFile);
            }

            fixture.PageRetrieverMock
            .Setup(t => t.RetrieveHtmlPageAsync(MalRouteBuilder.AnimeUrl(malId)))
            .ReturnsAsync(new HtmlDocumentRetrievalWrapper(HttpStatusCode.NotFound, true, document));

            var sut = fixture.Instance;

            // act
            var result = await sut.GetAnime(malId);

            // assert
            result.ResponseStatusCode.Should().Be(HttpStatusCode.NotFound);
            result.Success.Should().BeFalse();
        }
Esempio n. 2
0
        public void FailedHttpRetrievalDoesNotCauseAnException()
        {
            // arrange
            const int malId   = 13127;
            var       fixture = new RequestProcessorFixture();

            var document    = new HtmlDocument();
            var path        = AppDomain.CurrentDomain.BaseDirectory;
            var examplePath = Path.Combine(path, "PageExamples", $"{malId}.html");

            using (var htmlFile = File.Open(examplePath, FileMode.Open))
            {
                document.Load(htmlFile);
            }

            fixture.PageRetrieverMock
            .Setup(t => t.RetrieveHtmlPageAsync(MalRouteBuilder.AnimeUrl(malId)))
            .ReturnsAsync(new HtmlDocumentRetrievalWrapper(HttpStatusCode.NotFound, true, document));

            var sut = fixture.Instance;
            var act = new Action(() => sut.GetAnime(malId).Wait());

            // act
            // assert
            act.ShouldNotThrow <Exception>();
        }
Esempio n. 3
0
        public void RetrievingAnimeWithUsernameAndPasswordPopulatesTheUserFields()
        {
            // arrange
            const int    animeId     = 11757;
            var          fixture     = new RequestProcessorFixture(animeId);
            const string user        = "******";
            const string pass        = "******";
            var          document    = new HtmlDocument();
            var          path        = AppDomain.CurrentDomain.BaseDirectory;
            var          examplePath = Path.Combine(path, "PageExamples", $"{animeId}LoggedIn.html");

            using (var htmlFile = File.Open(examplePath, FileMode.Open))
            {
                document.Load(htmlFile);
            }

            fixture.PageRetrieverMock
            .Setup(t => t.RetrieveHtmlPageAsync(MalRouteBuilder.AnimeUrl(animeId), user, pass))
            .ReturnsAsync(new HtmlDocumentRetrievalWrapper(HttpStatusCode.OK, true, document));

            var sut = fixture.Instance;

            // act
            var result = sut.GetAnime(animeId, user, pass).Result;

            // assert
            fixture.PageRetrieverMock.Verify(t => t.RetrieveHtmlPageAsync(MalRouteBuilder.AnimeCharacterUrl(animeId)),
                                             Times.Once);
            result.ResponseData.UserScore.Should().Be(10);
            result.ResponseData.UserWatchedEpisodes.Should().Be(25);
            result.ResponseData.UserWatchedStatus.Should().Be("Completed");
        }
Esempio n. 4
0
        public void RetrievingAnimeCorrectlyRetrievesTheSynopsis()
        {
            // arrange
            const int animeId     = 11757;
            var       fixture     = new RequestProcessorFixture(animeId);
            var       document    = new HtmlDocument();
            var       path        = AppDomain.CurrentDomain.BaseDirectory;
            var       examplePath = Path.Combine(path, "PageExamples", $"{animeId}LoggedIn.html");

            using (var htmlFile = File.Open(examplePath, FileMode.Open))
            {
                document.Load(htmlFile);
            }

            fixture.PageRetrieverMock
            .Setup(t => t.RetrieveHtmlPageAsync(MalRouteBuilder.AnimeUrl(animeId)))
            .ReturnsAsync(new HtmlDocumentRetrievalWrapper(HttpStatusCode.OK, true, document));

            var sut = fixture.Instance;

            // act
            var result = sut.GetAnime(animeId).Result;

            // assert
            result.ResponseData.Synopsis.Should().NotBeNullOrEmpty();
        }
Esempio n. 5
0
        public void LoadingInvalidDataCorrectlyMarksTheItemAsError()
        {
            // arrange
            const int animeId = 11757;
            var       fixture = new RequestProcessorFixture(animeId);

            fixture.PageRetrieverMock
            .Setup(t => t.RetrieveHtmlPageAsync(MalRouteBuilder.AnimeUrl(animeId)))
            .Throws(new Exception("Cannot load"));

            var sut = fixture.Instance;

            // act
            var result = sut.GetAnime(animeId).Result;

            // assert
            result.Exception.Should().NotBeNull();
            result.ResponseData.ErrorOccured.Should().BeTrue();
            result.ResponseData.ErrorMessage.Should().Be("Cannot load");
        }
Esempio n. 6
0
        /// <summary>
        /// Retrieve an anime from MAL
        /// </summary>
        /// <param name="id">MAL Id</param>
        /// <param name="loginDetails">Username and password for retrieving user information. Pass null to retrieve pulbic page</param>
        /// <returns>Anime instance</returns>
        private async Task <RetrievalWrapper <Anime> > DoAnimeRetrieval(int id, Tuple <string, string> loginDetails)
        {
            var anime = new Anime();

            try
            {
                var animePageTask = loginDetails == null
                    ? _pageRetriever.RetrieveHtmlPageAsync(MalRouteBuilder.AnimeUrl(id))
                    : _pageRetriever.RetrieveHtmlPageAsync(MalRouteBuilder.AnimeUrl(id), loginDetails.Item1,
                                                           loginDetails.Item2);

                var characterTask = _pageRetriever.RetrieveHtmlPageAsync(MalRouteBuilder.AnimeCharacterUrl(id));

                var animeResponse = await animePageTask;
                if (animeResponse.ResponseStatusCode == null)
                {
                    throw animeResponse.Exception;
                }

                if (!new HttpResponseMessage(animeResponse.ResponseStatusCode.Value)
                    .IsSuccessStatusCode)
                {
                    anime.ErrorOccured = true;
                    anime.ErrorMessage =
                        $"Status code {animeResponse.ResponseStatusCode.Value} does not indicate success";
                    return(new RetrievalWrapper <Anime>(animeResponse.ResponseStatusCode.Value, false, anime));
                }

                var characterResponse = await characterTask;

                var animeDoc     = animeResponse.Document;
                var characterDoc = characterResponse.Document;

                anime
                .RetrieveAnimeId(animeDoc)
                .RetrieveAnimeTitle(animeDoc)
                .RetrieveAlternativeTitles(animeDoc)
                .RetrieveSynopsis(animeDoc)
                .RetrieveImage(animeDoc)
                .RetrieveType(animeDoc)
                .RetrieveEpisodes(animeDoc)
                .RetrieveStatus(animeDoc)
                .RetrieveAirDates(animeDoc)
                .RetrieveRating(animeDoc)
                .RetrieveRank(animeDoc)
                .RetrievePopularity(animeDoc)
                .RetrieveScore(animeDoc)
                .RetrieveMemberCount(animeDoc)
                .RetrieveFavotireCount(animeDoc)
                .RetrieveGenres(animeDoc)
                .RetrieveInfoUrls(animeDoc)
                .RetrieveRelatedAnime(animeDoc)
                .PopulateCharacterAndSeiyuuInformation(characterDoc);

                if (loginDetails != null)
                {
                    anime
                    .RetrieveUserScore(animeDoc)
                    .RetrieveUserEpisode(animeDoc)
                    .RetrieveUserStatus(animeDoc);
                }

                // TODO - Add sanity check

                return(new RetrievalWrapper <Anime>(animeResponse.ResponseStatusCode.Value, animeResponse.Success,
                                                    anime));
            }
            catch (Exception exception)
            {
                anime.ErrorOccured = true;
                anime.ErrorMessage = exception.Message;
                return(new RetrievalWrapper <Anime>(exception, anime));
            }
        }