@ -2,15 +2,19 @@ using API_LoL_Project.Controllers;
using API_LoL_Project.Controllers.Request ;
using API_LoL_Project.Controllers.Request ;
using API_LoL_Project.Controllers.Response ;
using API_LoL_Project.Controllers.Response ;
using API_LoL_Project.Controllers.version2 ;
using API_LoL_Project.Controllers.version2 ;
using API_LoL_Project.Middleware ;
using DTO ;
using DTO ;
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.AspNetCore.Routing ;
using Microsoft.Extensions.Logging ;
using Microsoft.Extensions.Logging ;
using Microsoft.Extensions.Logging.Abstractions ;
using Microsoft.Extensions.Logging.Abstractions ;
using Model ;
using Model ;
using Shared ;
using Shared ;
using StubLib ;
using StubLib ;
using System.Collections.ObjectModel ;
using System.Net ;
using System.Net ;
using System.Xml.Linq ;
namespace Test_Api
namespace Test_Api
{
{
@ -29,7 +33,7 @@ namespace Test_Api
}
}
[TestMethod]
[TestMethod]
public async Task TestGet Champions( )
public async Task TestGet Succes Champions( )
{
{
var totalcountTest = await stubMgr . ChampionsMgr . GetNbItems ( ) ;
var totalcountTest = await stubMgr . ChampionsMgr . GetNbItems ( ) ;
@ -147,79 +151,249 @@ namespace Test_Api
Assert . AreEqual ( "To many object is asked the max is : 0" , badRequestResult . Value ) ;
Assert . AreEqual ( "To many object is asked the max is : 0" , badRequestResult . Value ) ;
}
}
[TestMethod]
public async Task TestGetChampionByName_ExistingName ( )
{
// Arrange
var championName = "lopalinda" ;
var champion = new Champion ( championName , ChampionClass . Assassin ) ;
await stubMgr . ChampionsMgr . AddItem ( champion ) ;
// Act
var getResult = await championCtrl . GetChampionsByName ( championName ) ;
/ *
// Assert
[TestMethod]
/ * Assert . IsInstanceOfType ( getResult , typeof ( OkObjectResult ) ) ;
public async Task TestPostChampions ( )
var objectRes = getResult . Result as OkObjectResult ;
{
Assert . AreEqual ( 200 , objectRes . StatusCode ) ;
Assert . IsInstanceOfType ( okResult . Value , typeof ( IEnumerable < LolResponse < ChampionFullDTO > > ) ) ;
Assert . IsNotNull ( objectRes ) ; * /
var getResult = await championCtrl . Get ( ) ;
Console . WriteLine ( getResult ) ;
var objectRes = getResult as OkObjectResult ;
Assert . AreEqual ( 200 , objectRes . StatusCode ) ;
Assert . IsInstanceOfType ( getResult . Result , typeof ( OkObjectResult ) ) ;
var okResult = getResult . Result as OkObjectResult ;
Assert . IsInstanceOfType ( okResult . Value , typeof ( IEnumerable < LolResponse < ChampionFullDTO > > ) ) ;
var responseList = okResult . Value as IEnumerable < LolResponse < ChampionFullDTO > > ;
Assert . AreEqual ( 1 , responseList . Count ( ) ) ;
var response = responseList . First ( ) ;
Assert . AreEqual ( champion . Name , response . Data . Name ) ;
}
[TestMethod]
public async Task TestGetChampionsByName_ReturnsBadRequest_WhenNameIsEmpty ( )
{
// Arrange
var name = "" ;
Assert . IsNotNull ( objectRes ) ;
// Act
var result = await championCtrl . GetChampionsByName ( name ) ;
var champions = objectRes ? . Value as IEnumerable < ChampionDTO > ;
// Assert
Assert . IsInstanceOfType ( result . Result , typeof ( BadRequestObjectResult ) ) ;
var badRequestResult = result . Result as BadRequestObjectResult ;
Assert . AreEqual ( "Can not get champions without the name (is empty)" , badRequestResult . Value ) ;
}
[TestMethod]
public async Task TestGetChampionsByName_ReturnsBadRequest_WhenNoChampionsFound ( )
{
// Arrange
var name = "NonExistentChampionName" ;
// Act
var result = await championCtrl . GetChampionsByName ( name ) ;
Assert . IsNotNull ( champions ) ;
// Assert
Assert . IsInstanceOfType ( result . Result , typeof ( NotFoundObjectResult ) ) ;
var badRequestResult = result . Result as NotFoundObjectResult ;
Assert . AreEqual ( "No chamions found with this name: NonExistentChampionNamein the dataContext" , badRequestResult . Value ) ;
}
}
/ * [ TestMethod ]
public async Task TestPostChampionsSucess ( )
{
var championName = "bhbhb" ;
var expectedChampion = new ChampionFullDTO ( )
{
Characteristics = new ReadOnlyDictionary < string , int > ( new Dictionary < string , int > ( ) {
{ "health" , 500 } ,
{ "attack damage" , 60 } ,
{ "ability power" , 0 }
} ) ,
Name = championName ,
Bio = "Garen is a strong and noble warrior, the pride of Demacia." ,
Class = ChampionClass . Fighter ,
Icon = "https://example.com/garen-icon.png" ,
LargeImage = new ImageDTO ( ) { base64 = "xyz" } ,
Skins = new List < SkinDto > ( ) {
new SkinDto ( ) {
Name = "Classic Garen" ,
Description = "The default skin for Garen." ,
Icon = "https://example.com/garen-classic-icon.png" ,
Price = 1350 ,
LargeImage = new ImageDTO ( ) { base64 = "xyz" }
} ,
new SkinDto ( ) {
Name = "God-King Garen" ,
Description = "Garen as a divine being." ,
Icon = "https://example.com/garen-god-king-icon.png" ,
Price = 1820 ,
LargeImage = new ImageDTO ( ) { base64 = "xyz" }
}
} ,
Skills = new List < Skill > ( ) {
new Skill ( "Decisive Strike" , SkillType . Basic , "Garen's next attack strikes a vital area of his foe, dealing bonus damage and silencing them." ) ,
new Skill ( "Courage" , SkillType . Basic , "Garen passively increases his armor and magic resistance by killing enemies. He may also activate this ability to gain additional defensive statistics." ) ,
new Skill ( "Judgment" , SkillType . Basic , "Garen rapidly spins his sword around his body, dealing physical damage to nearby enemies and reducing the duration of all incoming crowd control effects." ) ,
new Skill ( "Demacian Justice" , SkillType . Ultimate , "Garen calls upon the might of Demacia to deal a finishing blow to an enemy champion, dealing massive damage based on their missing health." )
}
} ;
var rep = await championCtrl . Post ( expectedChampion ) ;
[TestMethod]
Assert . AreEqual ( 200 , objectRes . StatusCode ) ;
public async Task TestUpdateChampions ( )
{
var getResult = await championCtrl . Get ( ) ;
Assert . IsNotNull ( objectRes ) ;
Console . WriteLine ( getResult ) ;
var objectRes = getResult as OkObjectResult ;
Assert . AreEqual ( 200 , objectRes . StatusCode ) ;
var champions = objectRes ? . Value as IEnumerable < ChampionDTO > ;
Assert . IsNotNull ( objectRes ) ;
Assert . IsNotNull ( champion s) ;
var champions = objectRes ? . Value as IEnumerable < ChampionDTO > ;
Assert . IsNotNull ( champions ) ;
}
* /
[TestMethod]
public async Task TestPost_ReturnsCreatedAndSavedChampion ( )
{
// Arrange
var expectedChampion = new ChampionFullDTO ( )
{
Characteristics = new ReadOnlyDictionary < string , int > ( new Dictionary < string , int > ( ) {
{ "health" , 500 } ,
{ "attack damage" , 60 } ,
{ "ability power" , 0 }
} ) ,
Name = "iuhbbhs" ,
Bio = "Garen is a strong and noble warrior, the pride of Demacia." ,
Class = ChampionClass . Fighter ,
Icon = "https://example.com/garen-icon.png" ,
LargeImage = new ImageDTO ( ) { base64 = "xyz" } ,
Skins = new List < SkinDto > ( ) {
new SkinDto ( ) {
Name = "Classic Garen" ,
Description = "The default skin for Garen." ,
Icon = "https://example.com/garen-classic-icon.png" ,
Price = 1350 ,
LargeImage = new ImageDTO ( ) { base64 = "xyz" }
} ,
new SkinDto ( ) {
Name = "God-King Garen" ,
Description = "Garen as a divine being." ,
Icon = "https://example.com/garen-god-king-icon.png" ,
Price = 1820 ,
LargeImage = new ImageDTO ( ) { base64 = "xyz" }
}
} ,
Skills = new List < Skill > ( ) {
new Skill ( "Decisive Strike" , SkillType . Basic , "Garen's next attack strikes a vital area of his foe, dealing bonus damage and silencing them." ) ,
new Skill ( "Courage" , SkillType . Basic , "Garen passively increases his armor and magic resistance by killing enemies. He may also activate this ability to gain additional defensive statistics." ) ,
new Skill ( "Judgment" , SkillType . Basic , "Garen rapidly spins his sword around his body, dealing physical damage to nearby enemies and reducing the duration of all incoming crowd control effects." ) ,
new Skill ( "Demacian Justice" , SkillType . Ultimate , "Garen calls upon the might of Demacia to deal a finishing blow to an enemy champion, dealing massive damage based on their missing health." )
}
} ;
// Act
var postResult = await championCtrl . Post ( expectedChampion ) ;
}
// Assert
Assert . IsInstanceOfType ( postResult , typeof ( CreatedAtActionResult ) ) ;
var objectRes = postResult as CreatedAtActionResult ;
Assert . AreEqual ( 201 , objectRes . StatusCode ) ;
Assert . IsNotNull ( objectRes ) ;
Assert . AreEqual ( nameof ( championCtrl . Get ) , objectRes . ActionName ) ;
var createdChampion = objectRes . Value as ChampionDTO ;
Assert . IsNotNull ( createdChampion ) ;
Assert . AreEqual ( expectedChampion . Name , createdChampion . Name ) ;
Assert . AreEqual ( expectedChampion . Class , createdChampion . Class ) ;
}
[TestMethod]
[TestMethod]
public async Task TestDeleteChampions ( )
public async Task TestPutChampion ( )
{
{
// Arrange
var championName = "kakarot" ;
var updatedChampion = new ChampionDTO
{
Name = championName ,
Class = ChampionClass . Mage ,
Icon = "dsodm" ,
Bio = "totto" ,
} ;
var getResult = await championCtrl . Get ( ) ;
await stubMgr . ChampionsMgr . AddItem ( new Champion ( championName , ChampionClass . Assassin ) ) ;
Console . WriteLine ( getResult ) ;
// Act
var objectRes = getResult as OkObjectResult ;
var result = await championCtrl . Put ( championName , updatedChampion ) ;
Assert . AreEqual ( 200 , objectRes . StatusCode ) ;
// Assert
Assert . IsInstanceOfType ( result , typeof ( OkObjectResult ) ) ;
var champion = await stubMgr . ChampionsMgr . GetItemsByName ( championName , 0 , 1 ) ;
Assert . AreEqual ( updatedChampion . Name , champion . First ( ) . Name ) ;
Assert . AreEqual ( updatedChampion . Class , champion . First ( ) . Class ) ;
Assert . AreEqual ( updatedChampion . Bio , champion . First ( ) . Bio ) ;
}
Assert . IsNotNull ( objectRes ) ;
[TestMethod]
public async Task TestDeleteChampion ( )
{
// Arrange
var championToDelete = new Champion ( "Yasuo" , ChampionClass . Fighter ) ;
await stubMgr . ChampionsMgr . AddItem ( championToDelete ) ;
var champions = objectRes ? . Value as IEnumerable < ChampionDTO > ;
// Act
var result = await championCtrl . Delete ( championToDelete . Name ) ;
Assert . IsNotNull ( champions ) ;
// Assert
Assert . IsInstanceOfType ( result , typeof ( OkResult ) ) ;
Assert . AreEqual ( 200 , ( result as OkResult ) . StatusCode ) ;
var championsAfterDelete = await stubMgr . ChampionsMgr . GetItems ( 0 , await stubMgr . ChampionsMgr . GetNbItems ( ) ) ;
Assert . IsFalse ( championsAfterDelete . Any ( c = > c . Name = = championToDelete . Name ) ) ;
}
}
[TestMethod]
public async Task TestDeleteChampionWithInvalidName ( )
{
// Arrange
var invalidName = "Invalid Name" ;
// Act
var result = await championCtrl . Delete ( invalidName ) ;
// Assert
Assert . IsInstanceOfType ( result , typeof ( NotFoundObjectResult ) ) ;
Assert . AreEqual ( 404 , ( result as NotFoundObjectResult ) . StatusCode ) ;
Assert . AreEqual ( $"No chamions found with {invalidName} cannot delete" , ( result as NotFoundObjectResult ) . Value ) ;
}
[TestMethod]
public async Task TestDeleteChampionWithEmptyName ( )
{
// Arrange
var emptyName = string . Empty ;
// Act
var result = await championCtrl . Delete ( emptyName ) ;
// Assert
Assert . IsInstanceOfType ( result , typeof ( BadRequestObjectResult ) ) ;
Assert . AreEqual ( 400 , ( result as BadRequestObjectResult ) . StatusCode ) ;
Assert . AreEqual ( "Can not delelte champions without the name (is empty)" , ( result as BadRequestObjectResult ) . Value ) ;
}
* /
}
}
}
}