using System.Net; using AsbCloudApp.Data; using AsbCloudDb.Model; using AsbCloudDb.Model.ProcessMaps; using AsbCloudWebApi.IntegrationTests.Clients; using AsbCloudWebApi.IntegrationTests.TestFakers; using Mapster; using Microsoft.EntityFrameworkCore; using Xunit; namespace AsbCloudWebApi.IntegrationTests.Controllers; public class AdminDepositControllerTests : BaseIntegrationTest { protected IAdminDepositClient client; public AdminDepositControllerTests(WebAppFactoryFixture factory) : base(factory) { client = factory.GetAuthorizedHttpClient(); } [Fact] public async Task InsertAsync_ReturnsSuccess_WhenNewItemIsValid() { //arrange var dbset = dbContext.Set(); dbset.RemoveRange(dbset); dbContext.SaveChanges(); var expectedDto = EntitiesFaker.Deposit.Generate().Adapt(); //act var response = await client.InsertAsync(expectedDto); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.True(response.Content > 0); var entity = await dbContext.Deposits.FirstOrDefaultAsync(d => d.Id == response.Content); var actualDto = entity?.Adapt(); var excludeProps = new[] { nameof(DepositBaseDto.Id) }; MatchHelper.Match(expectedDto, actualDto, excludeProps); } [Fact] public async Task InsertRangeAsync_ReturnsSuccess_WhenAllNewItemsIsValid() { //arrange var dbset = dbContext.Set(); dbset.RemoveRange(dbset); dbContext.SaveChanges(); var dto = EntitiesFaker.Deposit.Generate().Adapt(); //act var responce = await client.InsertRangeAsync(new[] { dto }); //assert Assert.Equal(HttpStatusCode.OK, responce.StatusCode); Assert.Equal(1, responce.Content); var entity = await dbContext.Deposits.OrderBy(d => d.Id).LastOrDefaultAsync(); var deposit = entity?.Adapt(); var excludeProps = new[] { nameof(DepositBaseDto.Id) }; MatchHelper.Match(dto, deposit, excludeProps); } [Fact] public async Task UpdateAsync_ReturnsBadRequest_WhenUpdatedItemHasInvalidId() { //arrange var dto = EntitiesFaker.Deposit.Generate().Adapt(); //act var response = await client.UpdateAsync(dto); //assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); } [Fact] public async Task UpdateAsync_ReturnsSuccess_WhenUpdatedItemIsValid() { //arrange var dto = EntitiesFaker.Deposit.Generate().Adapt(); var insertResponse = await client.InsertAsync(dto); dto.Id = insertResponse.Content; dto.Caption = "Test"; dto.Latitude = 50; dto.Longitude = 50; dto.Timezone = new SimpleTimezoneDto { IsOverride = true, Hours = 12 }; //act var response = await client.UpdateAsync(dto); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.True(response.Content > 0); var entity = await dbContext.Deposits.FirstOrDefaultAsync(d => d.Id == response.Content); var deposit = entity?.Adapt(); MatchHelper.Match(dto, deposit); } [Fact] public async Task GetOrDefaultAsync_ReturnsSuccess_WhenIdIsValid() { //arrange var entity = await dbContext.Deposits.FirstAsync(); var expected = entity.Adapt(); //act var response = await client.GetOrDefaultAsync(entity.Id); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); MatchHelper.Match(expected, response.Content); } [Fact] public async Task GetOrDefaultAsync_ReturnsNoContent_WhenIdIsInvalid() { //arrange const int id = 0; //act var responce = await client.GetOrDefaultAsync(id); //assert Assert.Equal(HttpStatusCode.NoContent, responce.StatusCode); } [Fact] public async Task GetAllAsync_ReturnsSuccess() { //act var response = await client.GetAllAsync(); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); var expectedCount = await dbContext.Deposits.CountAsync(); Assert.Equal(expectedCount, response.Content.Count()); var entity = await dbContext.Deposits.FirstOrDefaultAsync(); var dto = entity?.Adapt(); MatchHelper.Match(dto, response.Content.FirstOrDefault()); } [Fact] public async Task DeleteAsync_ReturnsSuccess_WhenIdIsValid() { //arrange var dto = EntitiesFaker.Deposit.Generate().Adapt(); var insertResponse = await client.InsertAsync(dto); var id = insertResponse.Content; //act var response = await client.DeleteAsync(id); //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.True(response.Content > 0); var entity = await dbContext.Deposits.FirstOrDefaultAsync(d => d.Id == dto.Id); Assert.Null(entity); } [Fact] public async Task DeleteAsync_ReturnsNoContent_WhenIdIsInvalid() { //arrange const int id = 0; //act var response = await client.DeleteAsync(id); //assert Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); } }