diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunesController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunesController.cs index b5225f3..787689a 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunesController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunesController.cs @@ -1,28 +1,28 @@ using ApiLol.Mapper; using DTO; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; using Model; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 -namespace ApiLol.Controllers -{ - [Route("api/[controller]")] - [ApiController] - public class RunesController : ControllerBase +namespace ApiLol.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class RunesController : ControllerBase { - private readonly IDataManager _manager; - private readonly ILogger _logger; - - public RunesController(IDataManager dataManager, ILogger logger) - { - _logger = logger; - this._manager = dataManager; - } - - - // GET: api/ + private readonly IDataManager _manager; + private readonly ILogger _logger; + + public RunesController(IDataManager dataManager, ILogger logger) + { + _logger = logger; + this._manager = dataManager; + } + + + // GET: api/ [HttpGet] public async Task Get([FromQuery] PageRequest pageRequest) { @@ -51,7 +51,7 @@ namespace ApiLol.Controllers dtos = (await _manager.RunesMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending)) .Select(x => x.ToDto()); } - return Ok(new { Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal }); + return Ok(new PageResponse{ Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal }); } catch (Exception error) @@ -59,11 +59,11 @@ namespace ApiLol.Controllers _logger.LogError(error.Message); return BadRequest(error.Message); } - } - - // GET api//5 - [HttpGet("{name}")] - public async Task Get(string name) + } + + // GET api//5 + [HttpGet("{name}")] + public async Task Get(string name) { _logger.LogInformation("method {Action} - RUNE call with {name}", nameof(Get), name); try @@ -81,12 +81,12 @@ namespace ApiLol.Controllers { _logger.LogError(error.Message); return BadRequest(error.Message); - } - } - - // POST api/ + } + } + + // POST api/ [HttpPost] - public async Task Post([FromBody] RuneDto rune) + public async Task Post([FromBody] RuneDto rune) { _logger.LogInformation("method {Action} - RUNE call with {item}", nameof(Post), rune); try @@ -103,7 +103,7 @@ namespace ApiLol.Controllers { _logger.LogError(error.Message); return BadRequest(error.Message); - } + } } // PUT api//5 @@ -135,11 +135,11 @@ namespace ApiLol.Controllers _logger.LogError(error.Message); return BadRequest(error.Message); } - } - - // DELETE api//5 + } + + // DELETE api//5 [HttpDelete("{name}")] - public async Task Delete(string name) + public async Task Delete(string name) { _logger.LogInformation("method {Action} - RUNE call with {name}", nameof(Delete), name); try @@ -156,7 +156,7 @@ namespace ApiLol.Controllers { _logger.LogError(error.Message); return BadRequest(error.Message); - } + } } [HttpGet("/countRunes")] @@ -174,4 +174,4 @@ namespace ApiLol.Controllers } } } -} +} diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/SkinsController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/SkinsController.cs index 7b9f0fd..8353d0b 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/SkinsController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/SkinsController.cs @@ -50,7 +50,7 @@ namespace ApiLol.Controllers dtos = (await _manager.SkinsMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending)) .Select(x => x.ToDtoC()); } - return Ok(new { Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal }); + return Ok(new PageResponse { Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal }); } catch (Exception error) diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v1/ChampionsController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v1/ChampionsController.cs index c565858..8cd63ab 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v1/ChampionsController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v1/ChampionsController.cs @@ -68,7 +68,7 @@ namespace ApiLol.Controllers.v1 } [HttpGet("/countChampions")] - public async Task> GetCountChampions() + public async Task GetCountChampions() { try { diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v2/ChampionsController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v2/ChampionsController.cs index 457cb91..7b67006 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v2/ChampionsController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v2/ChampionsController.cs @@ -81,7 +81,7 @@ namespace ApiLol.Controllers.v2 dtos = (await _manager.ChampionsMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending)) .Select(x => x.ToDto()); } - return Ok(new { Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal }); + return Ok(new PageResponse{ Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal }); } catch (Exception error) { diff --git a/src/EntityFramework_LoL/Sources/DTO/PageResponse.cs b/src/EntityFramework_LoL/Sources/DTO/PageResponse.cs new file mode 100644 index 0000000..c2d0fc4 --- /dev/null +++ b/src/EntityFramework_LoL/Sources/DTO/PageResponse.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DTO +{ + public class PageResponse + { + public IEnumerable Data { get; set; } + public int index { get; set; } + public int count { get; set; } + public int total { get; set; } + + } +} diff --git a/src/EntityFramework_LoL/Sources/Tests/ApiTests/ChampionsControllerTestV1.cs b/src/EntityFramework_LoL/Sources/Tests/ApiTests/ChampionsControllerTestV1.cs new file mode 100644 index 0000000..641fc9a --- /dev/null +++ b/src/EntityFramework_LoL/Sources/Tests/ApiTests/ChampionsControllerTestV1.cs @@ -0,0 +1,171 @@ +using ApiLol.Controllers.v1; +using DTO; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging.Abstractions; +using StubLib; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ApiTests +{ + [TestClass] + public class ChampionsControllerTestV1 + { + private readonly StubData stub; + private readonly ChampionsController champs; + public ChampionsControllerTestV1() + { + stub = new StubData(); + champs = new ChampionsController(stub, new NullLogger()); + } + + [TestMethod] + public async Task TestGetChampions() + { + //Arrange + + //Act + var total = await stub.ChampionsMgr.GetNbItems(); + var champion = await champs.Get(new PageRequest(){ index = 0, count = total }); + + //Assert + var objectResult = champion as OkObjectResult; + Assert.IsNotNull(objectResult); + + var champions = objectResult?.Value as IEnumerable; + Assert.IsNotNull(champions); + + Assert.AreEqual(champions.Count(), total); + + } + + [TestMethod] + public async Task TestPostChampion() + { + //Arange + var ChampionDto = new ChampionDto + { + Name = "Sylas", + Bio = "Good", + Class = ChampionClassDto.Tank, + Icon = "", + Image = new LargeImageDto() { Base64 = "" }, + Skins = new List() + }; + + //Act + var championsResult = await champs.Post(ChampionDto); + + //Assert + var objectResult = championsResult as CreatedAtActionResult; + Assert.IsNotNull(objectResult); + + var champions = objectResult?.Value as ChampionDto; + Assert.IsNotNull(champions); + + } + + [TestMethod] + public async Task TestCountChampion() + { + //Arange + var ChampionDto = new ChampionDto + { + Name = "Sylas", + Bio = "Good", + Class = ChampionClassDto.Tank, + Icon = "", + Image = new LargeImageDto() { Base64 = "" }, + Skins = new List() + }; + + //Act + var oldTotal = await stub.ChampionsMgr.GetNbItems(); + var oldResult = await champs.GetCountChampions(); + await champs.Post(ChampionDto); + + var objectResult = oldResult as OkObjectResult; + Assert.IsNotNull(objectResult); + + var newTotal = await stub.ChampionsMgr.GetNbItems(); + var newResult = await champs.GetCountChampions(); + + //Assert + var objectResultOld = oldResult as OkObjectResult; + Assert.IsNotNull(objectResultOld); + + var objectResultNew = newResult as OkObjectResult; + Assert.IsNotNull(objectResultNew); + + Assert.AreEqual(objectResultOld.Value, oldTotal); + Assert.AreNotEqual(objectResultOld.Value, newTotal); + + Assert.AreEqual(objectResultNew.Value, newTotal); + Assert.AreNotEqual(objectResultNew.Value, oldTotal); + + + } + + [TestMethod] + public async Task TestPutChampion() + { + //Arange + var ChampionDto = new ChampionDto + { + Name = "Sylas", + Bio = "Good", + Class = ChampionClassDto.Tank, + Icon = "", + Image = new LargeImageDto() { Base64 = "" }, + Skins = new List() + }; + var ChampionDtoPut = new ChampionDto + { + Name = "Sylas", + Bio = "Bad", + Class = ChampionClassDto.Tank, + Icon = "", + Image = new LargeImageDto() { Base64 = "" }, + Skins = new List() + }; + + //Act + await champs.Post(ChampionDto); + var championsResult = await champs.Put(ChampionDto.Name, ChampionDtoPut); + + //Assert + var objectResult = championsResult as OkObjectResult; + Assert.IsNotNull(objectResult); + + var champions = objectResult?.Value as ChampionDto; + Assert.IsNotNull(champions); + + Assert.AreNotEqual(ChampionDto.Bio, champions.Bio); + Assert.AreEqual(ChampionDtoPut.Bio, champions.Bio); + + } + + [TestMethod] + public async Task TestDeleteChampion() + { + //Arange + + + //Act + var total = await stub.ChampionsMgr.GetNbItems(); + var championsResult = await champs.Delete("Akali"); + + //Assert + var objectResult = championsResult as OkObjectResult; + Assert.IsNotNull(objectResult); + + Assert.AreEqual(objectResult.Value, true); + Assert.AreNotEqual(await stub.ChampionsMgr.GetNbItems(), total); + + } + + } +} diff --git a/src/EntityFramework_LoL/Sources/Tests/ApiTests/ChampionsControllerTest.cs b/src/EntityFramework_LoL/Sources/Tests/ApiTests/ChampionsControllerTestV2.cs similarity index 79% rename from src/EntityFramework_LoL/Sources/Tests/ApiTests/ChampionsControllerTest.cs rename to src/EntityFramework_LoL/Sources/Tests/ApiTests/ChampionsControllerTestV2.cs index 8613e57..39366e8 100644 --- a/src/EntityFramework_LoL/Sources/Tests/ApiTests/ChampionsControllerTest.cs +++ b/src/EntityFramework_LoL/Sources/Tests/ApiTests/ChampionsControllerTestV2.cs @@ -9,11 +9,11 @@ using StubLib; namespace ApiTests { [TestClass] - public class ChampionsControllerTest + public class ChampionsControllerTestV2 { private readonly StubData stub; private readonly ChampionsController champs; - public ChampionsControllerTest() + public ChampionsControllerTestV2() { stub = new StubData(); champs = new ChampionsController(stub, new NullLogger()); @@ -39,6 +39,29 @@ namespace ApiTests } + [TestMethod] + public async Task TestGetV3Champions() + { + //Arrange + + //Act + var total = await stub.ChampionsMgr.GetNbItems(); + var champion = await champs.GetV3(new PageRequest()); + + //Assert + var objectResult = champion as OkObjectResult; + Assert.IsNotNull(objectResult); + + var skinsResult = objectResult?.Value as PageResponse; + Assert.IsNotNull(skinsResult); + + var result = skinsResult?.Data as IEnumerable; + Assert.IsNotNull(result); + + Assert.AreEqual(result.Count(), total); + + } + [TestMethod] public async Task TestPostChampion() { diff --git a/src/EntityFramework_LoL/Sources/Tests/ApiTests/RunesControllerTest.cs b/src/EntityFramework_LoL/Sources/Tests/ApiTests/RunesControllerTest.cs new file mode 100644 index 0000000..2bdedb4 --- /dev/null +++ b/src/EntityFramework_LoL/Sources/Tests/ApiTests/RunesControllerTest.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ApiTests +{ + [TestClass] + public class RunesControllerTest + { + } +} diff --git a/src/EntityFramework_LoL/Sources/Tests/ApiTests/SkinsControllerTest.cs b/src/EntityFramework_LoL/Sources/Tests/ApiTests/SkinsControllerTest.cs index 1cc4c36..26a0f59 100644 --- a/src/EntityFramework_LoL/Sources/Tests/ApiTests/SkinsControllerTest.cs +++ b/src/EntityFramework_LoL/Sources/Tests/ApiTests/SkinsControllerTest.cs @@ -1,126 +1,140 @@ -using ApiLol.Controllers; -using DTO; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging.Abstractions; -using StubLib; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ApiTests -{ - [TestClass] - public class SkinsControllerTest - { - private readonly StubData stub; - private readonly SkinsController skins; - public SkinsControllerTest() - { - stub = new StubData(); - skins = new SkinsController(stub, new NullLogger()); - } - -/* [TestMethod] - public async Task TestGetSkins() - { - //Arrange - - //Act - var total = await stub.SkinsMgr.GetNbItems(); - var skin = await skins.Get(new PageRequest()); - - //Assert - var objectResult = skin as OkObjectResult; - Assert.IsNotNull(objectResult); - - var skinsResult = objectResult?.Value as IEnumerable; - Assert.IsNotNull(skinsResult); - - Assert.AreEqual(skinsResult.Count(), total); - - }*/ - -/* [TestMethod] - public async Task TestPostSkin() - { - //Arange - var SkinDto = new SkinDtoC - { - Name = "Project", - ChampionName = "Aatrox" - }; - - //Act - var skinsResult = await skins.Post(SkinDto); - - //Assert - var objectResult = skinsResult as CreatedAtActionResult; - Assert.IsNotNull(objectResult); - - var champions = objectResult?.Value as Ski; - Assert.IsNotNull(champions); - - }*/ - -/* [TestMethod] - public async Task TestPutSkin() - { - //Arange - var ChampionDto = new ChampionDto - { - Name = "Sylas", - Bio = "Good", - Class = ChampionClassDto.Tank, - Icon = "", - Image = new LargeImageDto() { Base64 = "" }, - Skins = new List() - }; - var ChampionDtoPut = new ChampionDto - { - Name = "Sylas", - Bio = "Bad", - Class = ChampionClassDto.Tank, - Icon = "", - Image = new LargeImageDto() { Base64 = "" }, - Skins = new List() - }; - - //Act - await champs.Post(ChampionDto); - var championsResult = await champs.Put(ChampionDto.Name, ChampionDtoPut); - - //Assert - var objectResult = championsResult as OkObjectResult; - Assert.IsNotNull(objectResult); - - var champions = objectResult?.Value as ChampionDto; - Assert.IsNotNull(champions); - - Assert.AreNotEqual(ChampionDto.Bio, champions.Bio); - Assert.AreEqual(ChampionDtoPut.Bio, champions.Bio); - - } - - [TestMethod] - public async Task TestDeleteChampion() - { - //Arange - - - //Act - var total = await stub.ChampionsMgr.GetNbItems(); - var championsResult = await champs.Delete("Akali"); - - //Assert - var objectResult = championsResult as OkObjectResult; - Assert.IsNotNull(objectResult); - - Assert.AreEqual(objectResult.Value, true); - Assert.AreNotEqual(await stub.ChampionsMgr.GetNbItems(), total); - - }*/ - - } -} +using ApiLol.Controllers; +using DTO; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging.Abstractions; +using StubLib; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ApiTests +{ + [TestClass] + public class SkinsControllerTest + { + private readonly StubData stub; + private readonly SkinsController skins; + public SkinsControllerTest() + { + stub = new StubData(); + skins = new SkinsController(stub, new NullLogger()); + } + + [TestMethod] + public async Task TestGetSkins() + { + //Arrange + + //Act + var total = await stub.SkinsMgr.GetNbItems(); + var skin = await skins.Get(new PageRequest()); + + //Assert + var objectResult = skin as OkObjectResult; + Assert.IsNotNull(objectResult); + + var skinsResult = objectResult.Value as PageResponse; + Assert.IsNotNull(skinsResult); + + var result = skinsResult.Data as IEnumerable; + Assert.IsNotNull(result); + + Assert.AreEqual(result.Count(), total); + Assert.AreEqual(total, skinsResult.total); + + } + + [TestMethod] + public async Task TestPostSkin() + { + //Arange + var SkinDto = new SkinDtoC + { + Name = "Project", + Description = "Test", + Icon = "", + Image = new LargeImageDto(), + Price = 900, + ChampionName = "aatrox" + }; + + //Act + var total = await stub.SkinsMgr.GetNbItems(); + var skinsResult = await skins.Post(SkinDto); + + //Assert + var objectResult = skinsResult as CreatedAtActionResult; + Assert.IsNotNull(objectResult); + + var isSkinDto = objectResult?.Value as SkinDtoC; + Assert.IsNotNull(isSkinDto); + + Assert.AreEqual(total+1, await stub.SkinsMgr.GetNbItems()); + + } + + [TestMethod] + public async Task TestPutSkin() + { + //Arange + var SkinDto = new SkinDtoC + { + Name = "Project", + Description = "Test", + Icon = "", + Image = new LargeImageDto(), + Price = 900, + ChampionName = "aatrox" + }; + var SkinDtoPut = new SkinDtoC + { + Name = "Project", + Description = "ForTestTest", + Icon = "", + Image = new LargeImageDto(), + Price = 850, + ChampionName = "aatrox" + }; + + //Act + await skins.Post(SkinDto); + var skinsResult = await skins.Put(SkinDto.Name, SkinDtoPut); + + //Assert + var objectResult = skinsResult as OkObjectResult; + Assert.IsNotNull(objectResult); + + var skin = objectResult?.Value as SkinDtoC; + Assert.IsNotNull(skin); + + Assert.AreNotEqual(SkinDto.Description, skin.Description); + Assert.AreNotEqual(SkinDto.Price, skin.Price); + + Assert.AreEqual(SkinDtoPut.Description, skin.Description); + Assert.AreEqual(SkinDtoPut.Price, skin.Price); + + } + + [TestMethod] + public async Task TestDeleteChampion() + { + //Arange + + + //Act + var total = await stub.SkinsMgr.GetNbItems(); + var skinsResult = await skins.Delete("Stinger"); + + //Assert + var objectResult = skinsResult as OkObjectResult; + Assert.IsNotNull(objectResult); + + Assert.AreEqual(objectResult.Value, true); + Assert.AreNotEqual(await stub.SkinsMgr.GetNbItems(), total); + + } + + } +} diff --git a/src/EntityFramework_LoL/Sources/Tests/UT_EF/ChampionsTest.cs b/src/EntityFramework_LoL/Sources/Tests/UT_EF/ChampionsTest.cs index 6e44013..5fcf6b7 100644 --- a/src/EntityFramework_LoL/Sources/Tests/UT_EF/ChampionsTest.cs +++ b/src/EntityFramework_LoL/Sources/Tests/UT_EF/ChampionsTest.cs @@ -124,5 +124,26 @@ namespace UT_EF } } + + [Fact] + public void Test_DataSeeder() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "DataSeeder_Test_Champion_database") + .Options; + + + using (var context = new LolDbContext(options)) + { + DataSeeder.SeedData(context); + string nameToFind = "hecarim"; + ChampionClassEntity type = ChampionClassEntity.Assassin; + Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count()); + Assert.Equal(1, context.Champions.Where(c => c.Class == type).Count()); + Assert.Equal(3, context.Champions.Count()); + + } + + } } } \ No newline at end of file