You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
6.3 KiB
225 lines
6.3 KiB
using API_LoL_Project.Controllers;
|
|
using API_LoL_Project.Controllers.Request;
|
|
using API_LoL_Project.Controllers.Response;
|
|
using API_LoL_Project.Controllers.version2;
|
|
using DTO;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Model;
|
|
using Shared;
|
|
using StubLib;
|
|
using System.Net;
|
|
|
|
namespace Test_Api
|
|
{
|
|
[TestClass]
|
|
public class ChampionControllerTest
|
|
{
|
|
public readonly ChampionsController championCtrl;
|
|
private readonly ILogger<ChampionsController> logger = new NullLogger<ChampionsController>();
|
|
|
|
public readonly IDataManager stubMgr;
|
|
public ChampionControllerTest()
|
|
{
|
|
stubMgr = new StubData();
|
|
|
|
championCtrl = new ChampionsController(stubMgr, logger);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TestGetChampions()
|
|
{
|
|
var totalcountTest = await stubMgr.ChampionsMgr.GetNbItems();
|
|
|
|
// Arrange
|
|
var request = new PageRequest
|
|
{
|
|
index = 0,
|
|
count = 10,
|
|
orderingPropertyName = "Name",
|
|
descending = false
|
|
};
|
|
|
|
// Create some test data
|
|
var champions = new List<Champion>
|
|
{
|
|
|
|
new Champion("Brand", ChampionClass.Mage),
|
|
new Champion("Caitlyn", ChampionClass.Marksman),
|
|
new Champion("Cassiopeia", ChampionClass.Mage)
|
|
};
|
|
foreach (var c in champions)
|
|
{
|
|
await stubMgr.ChampionsMgr.AddItem(c);
|
|
}
|
|
|
|
// Act
|
|
var getResult = await championCtrl.Get(request);
|
|
|
|
// Assert
|
|
|
|
Assert.IsInstanceOfType(getResult.Result, typeof(OkObjectResult));
|
|
var objectRes = getResult.Result as OkObjectResult;
|
|
Assert.AreEqual(200, objectRes.StatusCode);
|
|
Assert.IsNotNull(objectRes);
|
|
|
|
var pageResponse = objectRes.Value as PageResponse<ChampionDTO>;
|
|
Assert.IsNotNull(pageResponse);
|
|
Assert.IsInstanceOfType(pageResponse, typeof(PageResponse<ChampionDTO>));
|
|
Assert.AreEqual(totalcountTest + champions.Count, pageResponse.TotalCount);
|
|
Assert.AreEqual(totalcountTest + champions.Count, pageResponse.Data.Count());
|
|
|
|
var endpoints = pageResponse.Data.Select(r => r.Links);
|
|
foreach (var endpointList in endpoints)
|
|
{
|
|
Assert.AreEqual(4, endpointList.Count());
|
|
Assert.IsTrue(endpointList.Any(e => e.Href.Contains("/api/[controller]/")));
|
|
}
|
|
|
|
var championsDTO = pageResponse.Data.Select(r => r.Data);
|
|
foreach (var c in championsDTO)
|
|
{
|
|
Assert.IsInstanceOfType(c, typeof(ChampionDTO));
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TestGetChampions_When_Count_Index_Too_High()
|
|
{
|
|
|
|
var totalcountTest = await stubMgr.ChampionsMgr.GetNbItems();
|
|
|
|
|
|
var request = new PageRequest
|
|
{
|
|
index = 999,
|
|
count = 9,
|
|
orderingPropertyName = "Name",
|
|
descending = false
|
|
};
|
|
// Act
|
|
var getResult = await championCtrl.Get(request);
|
|
// Assert
|
|
var badRequestResult = getResult.Result as BadRequestObjectResult;
|
|
Assert.IsNotNull(badRequestResult);
|
|
Assert.AreEqual($"To many object is asked the max is : {totalcountTest}", badRequestResult.Value);
|
|
}
|
|
|
|
|
|
[TestMethod]
|
|
public async Task TestGetBadRequest_WhenNoChampionsFound()
|
|
{
|
|
|
|
// need to be empty
|
|
List<Champion> champions = new()
|
|
{
|
|
new Champion("Akali", ChampionClass.Assassin),
|
|
new Champion("Aatrox", ChampionClass.Fighter),
|
|
new Champion("Ahri", ChampionClass.Mage),
|
|
new Champion("Akshan", ChampionClass.Marksman),
|
|
new Champion("Bard", ChampionClass.Support),
|
|
new Champion("Alistar", ChampionClass.Tank),
|
|
};
|
|
foreach(var c in champions)
|
|
{
|
|
stubMgr.ChampionsMgr.DeleteItem(c);
|
|
}
|
|
|
|
|
|
var request = new PageRequest
|
|
{
|
|
index = 0,
|
|
count = 10,
|
|
orderingPropertyName = "Name",
|
|
descending = false
|
|
};
|
|
|
|
|
|
// Act
|
|
|
|
var getResult = await championCtrl.Get(request);
|
|
Console.WriteLine(getResult);
|
|
// Assert
|
|
var badRequestResult = getResult.Result as BadRequestObjectResult;
|
|
Assert.IsNotNull(badRequestResult);
|
|
Assert.AreEqual("To many object is asked the max is : 0", badRequestResult.Value);
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
[TestMethod]
|
|
public async Task TestPostChampions()
|
|
{
|
|
|
|
var getResult = await championCtrl.Get();
|
|
Console.WriteLine(getResult);
|
|
var objectRes = getResult as OkObjectResult;
|
|
|
|
Assert.AreEqual(200, objectRes.StatusCode);
|
|
|
|
Assert.IsNotNull(objectRes);
|
|
|
|
var champions = objectRes?.Value as IEnumerable<ChampionDTO>;
|
|
|
|
Assert.IsNotNull(champions);
|
|
|
|
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TestUpdateChampions()
|
|
{
|
|
|
|
var getResult = await championCtrl.Get();
|
|
Console.WriteLine(getResult);
|
|
var objectRes = getResult as OkObjectResult;
|
|
|
|
Assert.AreEqual(200, objectRes.StatusCode);
|
|
|
|
Assert.IsNotNull(objectRes);
|
|
|
|
var champions = objectRes?.Value as IEnumerable<ChampionDTO>;
|
|
|
|
Assert.IsNotNull(champions);
|
|
|
|
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TestDeleteChampions()
|
|
{
|
|
|
|
var getResult = await championCtrl.Get();
|
|
Console.WriteLine(getResult);
|
|
var objectRes = getResult as OkObjectResult;
|
|
|
|
Assert.AreEqual(200, objectRes.StatusCode);
|
|
|
|
Assert.IsNotNull(objectRes);
|
|
|
|
var champions = objectRes?.Value as IEnumerable<ChampionDTO>;
|
|
|
|
Assert.IsNotNull(champions);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
}
|
|
} |