From b8c589238d34c0a69c419bf3072c95b35c3cbd69 Mon Sep 17 00:00:00 2001 From: Nguyen Quang Chien Date: Mon, 29 May 2023 21:05:21 +0700 Subject: [PATCH 1/2] add: integration test --- .../Folders/Commands/EnableFolderTests.cs | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 tests/Application.Tests.Integration/Folders/Commands/EnableFolderTests.cs diff --git a/tests/Application.Tests.Integration/Folders/Commands/EnableFolderTests.cs b/tests/Application.Tests.Integration/Folders/Commands/EnableFolderTests.cs new file mode 100644 index 00000000..62f0d165 --- /dev/null +++ b/tests/Application.Tests.Integration/Folders/Commands/EnableFolderTests.cs @@ -0,0 +1,90 @@ +using Application.Common.Exceptions; +using Application.Folders.Commands; +using Domain.Entities; +using FluentAssertions; +using Xunit; + +namespace Application.Tests.Integration.Folders.Commands; + +public class EnableFolderTests : BaseClassFixture +{ + public EnableFolderTests(CustomApiFactory apiFactory) : base(apiFactory) + { + } + + [Fact] + public async Task ShouldEnableFolder_WhenThatFolderExistsAndIsDisabled() + { + // Arrange + var department = CreateDepartment(); + var folder = CreateFolder(); + var locker = CreateLocker(folder); + var room = CreateRoom(department, locker); + folder.IsAvailable = false; + await AddAsync(room); + + var command = new EnableFolder.Command() + { + FolderId = folder.Id, + }; + + // Act + var result = await SendAsync(command); + + // Assert + result.IsAvailable.Should().BeTrue(); + + // Cleanup + Remove(folder); + Remove(locker); + Remove(room); + Remove(await FindAsync(department.Id)); + } + + [Fact] + public async Task ShouldThrowKeyNotFoundException_WhenThatFolderDoesNotExist() + { + // Arrange + var command = new EnableFolder.Command() + { + FolderId = Guid.NewGuid(), + }; + + // Act + var action = async () => await SendAsync(command); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Folder does not exist."); + } + + [Fact] + public async Task ShouldThrowConflictException_WhenFolderIsAlreadyAvailable() + { + // Arrange + var department = CreateDepartment(); + var folder = CreateFolder(); + folder.IsAvailable = true; + var locker = CreateLocker(folder); + var room = CreateRoom(department, locker); + await AddAsync(room); + + var command = new EnableFolder.Command() + { + FolderId = folder.Id, + }; + + // Act + var action = async () => await SendAsync(command); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Folder has already been enabled."); + + // Cleanup + Remove(folder); + Remove(locker); + Remove(room); + Remove(await FindAsync(department.Id)); + } +} \ No newline at end of file From ac79e15a19509546aebac7b26487066f663a0c28 Mon Sep 17 00:00:00 2001 From: kaitoz11 <43519768+kaitoz11@users.noreply.github.com> Date: Thu, 1 Jun 2023 20:35:28 +0700 Subject: [PATCH 2/2] implementation for enable folder --- .../Folders/Commands/EnableFolder.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/Application/Folders/Commands/EnableFolder.cs b/src/Application/Folders/Commands/EnableFolder.cs index 33e3cb23..2cd66702 100644 --- a/src/Application/Folders/Commands/EnableFolder.cs +++ b/src/Application/Folders/Commands/EnableFolder.cs @@ -1,5 +1,9 @@ +using Application.Common.Exceptions; +using Application.Common.Interfaces; using Application.Common.Models.Dtos.Physical; +using AutoMapper; using MediatR; +using Microsoft.EntityFrameworkCore; namespace Application.Folders.Commands; @@ -9,4 +13,37 @@ public record Command : IRequest { public Guid FolderId { get; init; } } + + public class CommandHandler : IRequestHandler + { + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + + public CommandHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task Handle(Command request, CancellationToken cancellationToken) + { + var folder = await _context.Folders + .FirstOrDefaultAsync(x => x.Id.Equals(request.FolderId), cancellationToken); + + if (folder is null) + { + throw new KeyNotFoundException("Folder does not exist."); + } + + if (folder.IsAvailable) + { + throw new ConflictException("Folder has already been enabled."); + } + + folder.IsAvailable = true; + var result = _context.Folders.Update(folder); + await _context.SaveChangesAsync(cancellationToken); + return _mapper.Map(result.Entity); + } + } } \ No newline at end of file