Пример #1
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");
        }
Пример #2
0
        public void LoadingCharacterInformationWorksCorrectly()
        {
            // arrange
            const int characterId = 36828;
            var       fixture     = new RequestProcessorFixture();

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

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

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

            var sut = fixture.Instance;

            // act
            var retrievalWrapper = sut.DoCharacterRetrieval(characterId).Result;

            // assert
            retrievalWrapper.Success.Should().BeTrue();
            retrievalWrapper.ResponseStatusCode.Should().Be(HttpStatusCode.OK);
            retrievalWrapper.ResponseData.Name.Should().Be("Asuna Yuuki (結城 明日奈 / アスナ)");
            retrievalWrapper.ResponseData.Id.Should().Be(characterId);
        }
Пример #3
0
            public RequestProcessorFixture(int malId)
                : this()
            {
                var characterDocument = new HtmlDocument();
                var path        = AppDomain.CurrentDomain.BaseDirectory;
                var examplePath = Path.Combine(path, "PageExamples", $"{malId}CharacterInfo.html");

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

                PageRetrieverMock
                .Setup(t => t.RetrieveHtmlPageAsync(MalRouteBuilder.AnimeCharacterUrl(malId)))
                .ReturnsAsync(new HtmlDocumentRetrievalWrapper(HttpStatusCode.OK, true, characterDocument));
            }
Пример #4
0
        public void LoadingCharacterWithAnErrorCorrectlyMarksItAsBroken()
        {
            // arrange
            const int characterId = 36828;
            var       fixture     = new RequestProcessorFixture();

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

            var sut = fixture.Instance;

            // act
            var retrievalWrapper = sut.DoCharacterRetrieval(characterId).Result;

            // assert
            retrievalWrapper.Exception.Should().NotBeNull();
            retrievalWrapper.ResponseData.ErrorOccured.Should().BeTrue();
            retrievalWrapper.ResponseData.ErrorMessage.Should().Be("Cannot load");
        }
Пример #5
0
        /// <summary>
        /// Retrieve a Character from MAL
        /// </summary>
        /// <param name="characterId">Character Id</param>
        /// <returns>Populated Character</returns>
        public async Task <RetrievalWrapper <Character> > DoCharacterRetrieval(int characterId)
        {
            var character = new Character
            {
                Id  = characterId,
                Url = MalRouteBuilder.AnimeCharacterUrl(characterId)
            };

            try
            {
                var characterResponse = await _pageRetriever.RetrieveHtmlPageAsync(character.Url);

                if (characterResponse.ResponseStatusCode == null)
                {
                    throw characterResponse.Exception;
                }
                var characterDoc = characterResponse.Document;

                character
                .RetrieveCharacterName(characterDoc)
                .RetrieveCharacterImage(characterDoc)
                .RetrieveFavoriteCount(characterDoc)
                .RetrieveBiography(characterDoc)
                .RetrieveAnimeography(characterDoc)
                .RetrieveMangaograhy(characterDoc)
                .RetrieveSeiyuu(characterDoc);
                return(new RetrievalWrapper <Character>(characterResponse.ResponseStatusCode.Value,
                                                        characterResponse.Success,
                                                        character));
            }
            catch (Exception exception)
            {
                character.ErrorOccured = true;
                character.ErrorMessage = exception.Message;
                return(new RetrievalWrapper <Character>(exception, character));
            }
        }
Пример #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));
            }
        }