Esempio n. 1
0
        public async Task GenerateBoosterAsync_Success()
        {
            // arrange
            const string CARD_NAME = "cardname1";

            _mockRateLimit.Setup(x => x.IsTurnedOn).Returns(false);

            _mockHeaderManager.Setup(x => x.Update(It.IsAny <IReadOnlyNameValueList <string> >()));
            _mockHeaderManager.Setup(x => x.Get <int>(ResponseHeader.TotalCount)).Returns(2000);
            _mockHeaderManager.Setup(x => x.Get <int>(ResponseHeader.PageSize)).Returns(1000);

            var cardDto = new CardDto()
            {
                Name = CARD_NAME
            };
            var rootCardList = new RootCardListDto()
            {
                Cards = new List <CardDto> {
                    cardDto
                },
            };

            _mockModelMapper.Setup(x => x.MapCard(It.IsAny <CardDto>())).Returns(new Card()
            {
                Name = CARD_NAME
            });

            using var httpTest = new HttpTest();
            httpTest.RespondWithJson(rootCardList);

            var service = new SetService(
                _mockHeaderManager.Object,
                _mockModelMapper.Object,
                ApiVersion.V1,
                _mockRateLimit.Object);

            // act
            var result = await service.GenerateBoosterAsync("12345");

            // assert
            Assert.True(result.IsSuccess);
            _mockRepository.VerifyAll();
        }
        public async Task GenerateBoosterAsyncTest()
        {
            var cards = new List <CardDto>()
            {
                new CardDto()
                {
                    Artist       = "artist1",
                    Border       = "border1",
                    Cmc          = 111,
                    Colors       = new string[] { "blue", "pink" },
                    Flavor       = "flavor1",
                    ForeignNames = new ForeignNameDto[] { new ForeignNameDto()
                                                          {
                                                              Language = "english", MultiverseId = 222, Name = "name2"
                                                          } },
                    Hand       = 222,
                    Id         = "12345",
                    ImageUrl   = new System.Uri("http://fake/url"),
                    Layout     = "layout1",
                    Legalities = new LegalityDto[] { new LegalityDto()
                                                     {
                                                         Format = "format2", LegalityName = "legality name 2"
                                                     } },
                    Life         = 333,
                    Loyalty      = "loyalty",
                    ManaCost     = "500",
                    MultiverseId = 444,
                    Name         = "name1",
                    Names        = new string[] { "name2", "name3" },
                    Number       = "600",
                    OriginalText = "original text",
                    OriginalType = "original type",
                    Power        = "9000",
                    Printings    = new string[] { "printing1", "printing2" },
                    Rarity       = "rare",
                    ReleaseDate  = "2010, 1, 1",
                    Reserved     = true,
                    Rulings      = new RulingDto[] { new RulingDto()
                                                     {
                                                         Date = "2010, 2, 2", Text = "text2"
                                                     } },
                    Set         = "set1",
                    SetName     = "set name 1",
                    Source      = "source",
                    Starter     = true,
                    SubTypes    = new string[] { "subtype1", "subtype2" },
                    SuperTypes  = new string[] { "supertype1", "supertype2" },
                    Text        = "text3",
                    Timeshifted = false,
                    Toughness   = "tough",
                    Type        = "type2",
                    Types       = new string[] { "type1", "type2" },
                    Variations  = new int[] { 1, 2, 3 },
                    Watermark   = "watermark"
                },
                new CardDto()
                {
                    Artist       = "artist1",
                    Border       = "border1",
                    Cmc          = 111,
                    Colors       = new string[] { "blue", "pink" },
                    Flavor       = "flavor1",
                    ForeignNames = new ForeignNameDto[] { new ForeignNameDto()
                                                          {
                                                              Language = "english", MultiverseId = 222, Name = "name2"
                                                          } },
                    Hand       = 222,
                    Id         = "12345",
                    ImageUrl   = new System.Uri("http://fake/url"),
                    Layout     = "layout1",
                    Legalities = new LegalityDto[] { new LegalityDto()
                                                     {
                                                         Format = "format2", LegalityName = "legality name 2"
                                                     } },
                    Life         = 333,
                    Loyalty      = "loyalty",
                    ManaCost     = "500",
                    MultiverseId = 444,
                    Name         = "name1",
                    Names        = new string[] { "name2", "name3" },
                    Number       = "600",
                    OriginalText = "original text",
                    OriginalType = "original type",
                    Power        = "9000",
                    Printings    = new string[] { "printing1", "printing2" },
                    Rarity       = "rare",
                    ReleaseDate  = "2010, 1, 1",
                    Reserved     = true,
                    Rulings      = new RulingDto[] { new RulingDto()
                                                     {
                                                         Date = "2010, 2, 2", Text = "text2"
                                                     } },
                    Set         = "set1",
                    SetName     = "set name 1",
                    Source      = "source",
                    Starter     = true,
                    SubTypes    = new string[] { "subtype1", "subtype2" },
                    SuperTypes  = new string[] { "supertype1", "supertype2" },
                    Text        = "text3",
                    Timeshifted = false,
                    Toughness   = "tough",
                    Type        = "type2",
                    Types       = new string[] { "type1", "type2" },
                    Variations  = new int[] { 1, 2, 3 },
                    Watermark   = "watermark"
                }
            };

            // Test the All method.
            var moqAdapter = new Mock <IMtgApiServiceAdapter>();

            moqAdapter
            .SetupSequence(x => x.WebGetAsync <RootCardListDto>(It.IsAny <Uri>()))
            .Throws <ArgumentNullException>()
            .Throws(new MtgApiException <BadRequestException>("bad request"))
            .Throws(new MtgApiException <ForbiddenException>("forbidden"))
            .Throws(new MtgApiException <InternalServerErrorException>("server error"))
            .Throws(new MtgApiException <NotFoundException>("not found"))
            .Throws(new MtgApiException <ServiceUnavailableException>("unavailable"))
            .Throws <Exception>()
            .ReturnsAsync(new RootCardListDto()
            {
                Cards = cards
            });

            var service = new SetService(moqAdapter.Object, ApiVersion.V1_0, false);

            var result = await service.GenerateBoosterAsync("ktk");

            Assert.False(result.IsSuccess);
            Assert.Equal("Value cannot be null.", result.Exception.Message);

            result = await service.GenerateBoosterAsync("ktk");

            Assert.False(result.IsSuccess);
            Assert.Equal("bad request", result.Exception.Message);

            result = await service.GenerateBoosterAsync("ktk");

            Assert.False(result.IsSuccess);
            Assert.Equal("forbidden", result.Exception.Message);

            result = await service.GenerateBoosterAsync("ktk");

            Assert.False(result.IsSuccess);
            Assert.Equal("server error", result.Exception.Message);

            result = await service.GenerateBoosterAsync("ktk");

            Assert.False(result.IsSuccess);
            Assert.Equal("not found", result.Exception.Message);

            result = await service.GenerateBoosterAsync("ktk");

            Assert.False(result.IsSuccess);
            Assert.Equal("unavailable", result.Exception.Message);

            result = await service.GenerateBoosterAsync("ktk");

            Assert.False(result.IsSuccess);
            Assert.IsType <Exception>(result.Exception);

            result = await service.GenerateBoosterAsync("ktk");

            Assert.True(result.IsSuccess);
            Assert.Null(result.Exception);
            Assert.NotNull(result.Value);
        }