public async Task AllAsync_NullCards_SuccessWithEmptyList() { // arrange _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); using var httpTest = new HttpTest(); httpTest.RespondWithJson(new RootCardListDto()); var service = new CardService( _mockHeaderManager.Object, _mockModelMapper.Object, ApiVersion.V1, _mockRateLimit.Object); // act var result = await service.AllAsync(); // assert Assert.True(result.IsSuccess); Assert.Empty(result.Value); _mockRepository.VerifyAll(); }
public async Task AllAsync_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 CardService( _mockHeaderManager.Object, _mockModelMapper.Object, ApiVersion.V1, _mockRateLimit.Object); // act var result = await service.AllAsync(); // assert Assert.True(result.IsSuccess); _mockRepository.VerifyAll(); }
public async Task Reset_Success() { 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 }, }; using var httpTest = new HttpTest(); httpTest.RespondWithJson(rootCardList); _mockModelMapper.Setup(x => x.MapCard(It.IsAny <CardDto>())).Returns(new Card()); var service = new CardService( _mockHeaderManager.Object, _mockModelMapper.Object, ApiVersion.V1, _mockRateLimit.Object); service.Where(x => x.Name, "test"); // act service.Reset(); // assert await service.AllAsync(); httpTest.ShouldHaveCalled("https://api.magicthegathering.io/v1/cards"); }
public async Task Where_AddsQueryParameters_Success() { const string NAME = "name1"; const string LANGUAGE = "English"; _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 rootCardList = new RootCardListDto() { Cards = new List <CardDto> { new CardDto() }, }; using var httpTest = new HttpTest(); httpTest.RespondWithJson(rootCardList); _mockModelMapper.Setup(x => x.MapCard(It.IsAny <CardDto>())).Returns(new Card()); var service = new CardService( _mockHeaderManager.Object, _mockModelMapper.Object, ApiVersion.V1, _mockRateLimit.Object); // act service .Where(x => x.Name, NAME) .Where(x => x.Language, LANGUAGE); // assert await service.AllAsync(); httpTest .ShouldHaveCalled("https://api.magicthegathering.io/v1/cards*") .WithQueryParams("name", "language"); }
public async Task AllAsync_NullCardListDto_Failure() { // arrange _mockRateLimit.Setup(x => x.IsTurnedOn).Returns(false); _mockHeaderManager.Setup(x => x.Update(It.IsAny <IReadOnlyNameValueList <string> >())); using var httpTest = new HttpTest(); httpTest.RespondWithJson(null); var service = new CardService( _mockHeaderManager.Object, _mockModelMapper.Object, ApiVersion.V1, _mockRateLimit.Object); // act var result = await service.AllAsync(); // assert Assert.False(result.IsSuccess); _mockRepository.VerifyAll(); }
public async Task AllAsyncTest() { 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 CardService(moqAdapter.Object, ApiVersion.V1_0, false); service = service.Where(x => x.Name, "name1"); var result = await service.AllAsync(); Assert.False(result.IsSuccess); Assert.Equal("Value cannot be null.", result.Exception.Message); result = await service.AllAsync(); Assert.False(result.IsSuccess); Assert.Equal("bad request", result.Exception.Message); result = await service.AllAsync(); Assert.False(result.IsSuccess); Assert.Equal("forbidden", result.Exception.Message); result = await service.AllAsync(); Assert.False(result.IsSuccess); Assert.Equal("server error", result.Exception.Message); result = await service.AllAsync(); Assert.False(result.IsSuccess); Assert.Equal("not found", result.Exception.Message); result = await service.AllAsync(); Assert.False(result.IsSuccess); Assert.Equal("unavailable", result.Exception.Message); result = await service.AllAsync(); Assert.False(result.IsSuccess); Assert.IsType <Exception>(result.Exception); result = await service.AllAsync(); Assert.True(result.IsSuccess); Assert.Null(result.Exception); Assert.NotNull(result.Value); }