Files
monorepo/LCU.API.Collections.RepoApi-old/LCU.API.Collections.RepoApiTests/Controllers/CollateralControllerTest.cs
T
2026-04-20 14:47:24 -05:00

125 lines
3.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using LCU.API.Collections.RepoApi.Controllers;
using LCU.API.Collections.RepoApi.Services;
using LCU.Shared.Collections.RepoApiLib.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Xunit;
namespace LCU.API.Collections.RepoApiTests.Controllers
{
public class CollateralControllerTest
{
private readonly Mock<ICollateralService> mockCollateralService;
private readonly CollateralController controller;
private readonly Mock<IHttpContextAccessor> mockHttpContextAccessor;
public CollateralControllerTest()
{
mockCollateralService = new Mock<ICollateralService>();
mockHttpContextAccessor = new Mock<IHttpContextAccessor>();
controller = new CollateralController(mockCollateralService.Object,
mockHttpContextAccessor.Object);
}
// Get tests
[Fact]
public async void Get_OK()
{
// arrange
var collateral = new List<Collateral>
{
new Collateral()
{
CollateralId = 1234567,
RepoId = 2345678
}
};
mockCollateralService.Setup(x => x.ListAll()).ReturnsAsync(collateral);
// act
var result = await controller.Get();
Assert.NotNull(result);
var okResult = result as OkObjectResult;
// assert
Assert.NotNull(okResult);
Assert.Equal(200, okResult.StatusCode);
var okValue = okResult.Value as IEnumerable<Collateral>;
Assert.NotNull(okValue);
Assert.True(collateral.SequenceEqual(okValue));
}
[Fact]
public async void GetById_OK()
{
// arrange
var repos = new List<Collateral>
{
new Collateral()
{
CollateralId = 1234567,
RepoId = 2345678
}
};
mockCollateralService.Setup(x => x.Get(It.IsAny<int>())).ReturnsAsync(repos.First());
// act
var result = await controller.GetById(1234567);
Assert.NotNull(result);
var okResult = result as OkObjectResult;
// assert
Assert.NotNull(okResult);
Assert.Equal(200, okResult.StatusCode);
var okValue = okResult.Value as Collateral;
Assert.NotNull(okValue);
Assert.Equal(2345678, okValue.RepoId);
}
// Post tests
[Fact]
public async void Post_OK()
{
// arrange
var postCollateral = new Collateral()
{
CollateralId = 1234567,
RepoId = 2345678
};
// act
var result = await controller.Post(postCollateral);
Assert.NotNull(result);
var okResult = result as OkObjectResult;
// assert
Assert.NotNull(okResult);
Assert.Equal(200, okResult.StatusCode);
}
[Fact]
public async void Post_Bad_Request()
{
// arrange
var postCollateral = new Collateral()
{
CollateralId = 1234567,
};
controller.ModelState.AddModelError("Message", "The message field is required.");
// act
var result = await controller.Post(postCollateral);
Assert.NotNull(result);
var badRequestResult = result as BadRequestObjectResult;
// assert
Assert.NotNull(badRequestResult);
Assert.Equal(StatusCodes.Status400BadRequest, badRequestResult.StatusCode);
}
}
}