diff --git a/Sources/BowlingApi/Controllers/JoueurController.cs b/Sources/BowlingApi/Controllers/JoueurController.cs index 34a5eac..3aafde3 100644 --- a/Sources/BowlingApi/Controllers/JoueurController.cs +++ b/Sources/BowlingApi/Controllers/JoueurController.cs @@ -42,7 +42,7 @@ public class JoueurController:Controller { try { - if(name == null) + if(String.IsNullOrWhiteSpace(name)) return BadRequest("Le nom du joueur est obligatoire"); var result = _joueurService.GetDataWithName(name).Result; diff --git a/Sources/BowlingApi/Controllers/WeatherForecastController.cs b/Sources/BowlingApi/Controllers/WeatherForecastController.cs deleted file mode 100644 index 86d28a5..0000000 --- a/Sources/BowlingApi/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace BowlingApi.Controllers; - -[ApiController] -[Route("[controller]")] -public class WeatherForecastController : ControllerBase -{ - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } -} \ No newline at end of file diff --git a/Sources/BowlingApi/WeatherForecast.cs b/Sources/BowlingApi/WeatherForecast.cs deleted file mode 100644 index 5fff4e9..0000000 --- a/Sources/BowlingApi/WeatherForecast.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace BowlingApi; - -public class WeatherForecast -{ - public DateOnly Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } -} \ No newline at end of file diff --git a/Sources/GraphQL Project/GraphQL Project.csproj b/Sources/GraphQL Project/GraphQL Project.csproj index a574737..acdda21 100644 --- a/Sources/GraphQL Project/GraphQL Project.csproj +++ b/Sources/GraphQL Project/GraphQL Project.csproj @@ -8,6 +8,7 @@ + diff --git a/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj b/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj index ad62438..e6b217e 100644 --- a/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj +++ b/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj @@ -27,4 +27,10 @@ + + + ..\..\..\..\..\.nuget\packages\fluentassertions\6.9.0\lib\net6.0\FluentAssertions.dll + + + diff --git a/Sources/Tests/BowlingAPITest/TestJoueurController.cs b/Sources/Tests/BowlingAPITest/TestJoueurController.cs index 44bd48d..ee48a9b 100644 --- a/Sources/Tests/BowlingAPITest/TestJoueurController.cs +++ b/Sources/Tests/BowlingAPITest/TestJoueurController.cs @@ -1,4 +1,5 @@ using DTOs; +using FluentAssertions; namespace BowlingAPITest; @@ -8,15 +9,22 @@ public class TestController public async void Get_ShouldReturnOkResult() { // Arrange + var joueur1 = new JoueurDTO { Pseudo = "John Doe" }; + var joueur2 = new JoueurDTO { Pseudo = "Jane Smith" }; + var joueurs = GetTestItems(); var mockService = new Mock(); - mockService.Setup(service => service.GetAll()).ReturnsAsync(new List()); + mockService.Setup(service => service.GetAll()).ReturnsAsync(joueurs); var controller = new JoueurController(mockService.Object); // Act - var result = await controller.Get(); + var result= await controller.Get() as OkObjectResult; + var value = result.Value as List; // Assert - Assert.IsType(result); + result.Should().NotBeNull(); + value.Should().NotBeNull(); + result.StatusCode.Should().Be(200); + value.Should().BeEquivalentTo(joueurs); } @@ -47,18 +55,160 @@ public class TestController return testItems; } - // [Fact] - // public async void GetById_ShouldReturnNotFound() - // { - // // Arrange - // var mockService = new Mock(); - // mockService.Setup(service => service.Get(1)).ReturnsAsync((JoueurDTO)null); - // var controller = new JoueurController(mockService.Object); - // - // // Act - // var result = await controller.Get(1); - // - // // Assert - // Assert.IsType(result); - // } + [Fact] + public async Task Get_With_Invalid_Name_Should_Return_BadRequest() + { + // Arrange + var joueurController = new JoueurController(null); + + // Act + var result = await joueurController.Get(null); + + // Assert + result.Should().BeOfType(); + var badRequestResult = result as BadRequestObjectResult; + badRequestResult.Value.Should().Be("Le nom du joueur est obligatoire"); + } + + [Fact] + public async Task Get_With_Valid_Name_Should_Return_Ok_With_Joueur() + { + // Arrange + var joueur = new JoueurDTO { Id = 1, Pseudo = "John Doe" }; + var joueurServiceMock = new Mock(); + joueurServiceMock.Setup(x => x.GetDataWithName("John Doe")).ReturnsAsync(joueur); + var joueurController = new JoueurController(joueurServiceMock.Object); + + // Act + var result = await joueurController.Get("John Doe"); + + // Assert + result.Should().BeOfType(); + var okResult = result as OkObjectResult; + okResult.Value.Should().BeEquivalentTo(joueur); + } + + [Fact] + public async Task Post_With_Invalid_Joueur_Should_Return_BadRequest() + { + // Arrange + var joueurController = new JoueurController(null); + + // Act + var result = await joueurController.Post(null); + + // Assert + result.Should().BeOfType>(); + var actionResult = result as ActionResult; + actionResult.Result.Should().BeOfType(); + var badRequestResult = actionResult.Result as BadRequestObjectResult; + badRequestResult.Value.Should().Be("Le joueur est obligatoire"); + } + + [Fact] + public async Task Post_With_Valid_Joueur_Should_Return_Created_With_Joueur() + { + // Arrange + var joueur = new JoueurDTO { Id = 1, Pseudo = "John Doe" }; + var joueurServiceMock = new Mock(); + joueurServiceMock.Setup(x => x.Add(joueur)).ReturnsAsync(joueur); + var joueurController = new JoueurController(joueurServiceMock.Object); + + // Act + var result = await joueurController.Post(joueur); + + // Assert + result.Should().BeOfType>(); + var actionResult = result as ActionResult; + actionResult.Result.Should().BeOfType(); + var createdResult = actionResult.Result as CreatedAtActionResult; + createdResult.Value.Should().BeEquivalentTo(joueur); + } + + [Fact] + public async Task Put_With_Invalid_Joueur_Should_Return_BadRequest() + { + // Arrange + var joueurController = new JoueurController(null); + + // Act + var result = await joueurController.Put(null, null); + + // Assert + result.Should().BeOfType>(); + var actionResult = result as ActionResult; + actionResult.Result.Should().BeOfType(); + var badRequestResult = actionResult.Result as BadRequestObjectResult; + badRequestResult.Value.Should().Be("Le joueur est obligatoire"); + } + + [Fact] + public async Task Put_With_Valid_Joueur_Should_Return_Ok_With_Joueur() + { + // Arrange + var joueur = new JoueurDTO { Id = 1, Pseudo = "John Doe" }; + var joueurServiceMock = new Mock(); + joueurServiceMock.Setup(x => x.Update(joueur)).ReturnsAsync(true); + var joueurController = new JoueurController(joueurServiceMock.Object); + + // Act + var result = await joueurController.Put(joueur.Pseudo, joueur); + + // Assert + result.Should().BeOfType>(); + var actionResult = result as ActionResult; + actionResult.Result.Should().BeOfType(); + } + + //test Get_ShouldReturnNotFound + [Fact] + public async Task Get_ShouldReturnNotFound() + { + // Arrange + var mockService = new Mock(); + mockService.Setup(service => service.GetAll()).ReturnsAsync((List)null); + var controller = new JoueurController(mockService.Object); + + // Act + var result = await controller.Get(); + + // Assert + result.Should().BeOfType(); + } + + [Fact] + public async Task Get_White_Name_ShouldReturnNotFound() + { + // Arrange + + var joueur2 = new JoueurDTO { Pseudo = "Jane Smith" }; + var mockService = new Mock(); + mockService.Setup(service => service.GetDataWithName("Jane Smith")).ReturnsAsync(joueur2); + var controller = new JoueurController(mockService.Object); + + // Act + var result = await controller.Get("John Doe"); + + // Assert + result.Should().BeOfType(); + } + + //test Get_ShouldReturn InternalServerError + [Fact] + public async Task Get_ShouldReturnInternalServerError() + { + // Arrange + var mockService = new Mock(); + mockService.Setup(service => service.GetAll()).ThrowsAsync(new Exception()); + var controller = new JoueurController(mockService.Object); + + // Act + var result = await controller.Get() as ObjectResult; + + // Assert + result.Should().BeOfType(); + result.StatusCode.Should().Be(500); + } + + } \ No newline at end of file