-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
179 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...emisation.Service.UnitTest/Commands/ProjectGroup/DeleteProjectGroupCommandHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using AutoFixture; | ||
using Dfe.Academies.Academisation.Core; | ||
using Dfe.Academies.Academisation.Domain.ProjectGroupsAggregate; | ||
using Dfe.Academies.Academisation.Domain.SeedWork; | ||
using Dfe.Academies.Academisation.Service.Commands.ProjectGroup; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Dfe.Academies.Academisation.Service.UnitTest.Commands.ProjectGroup | ||
{ | ||
public class DeleteProjectGroupCommandHandlerTests | ||
{ | ||
private readonly MockRepository _mockRepository; | ||
private readonly Mock<IProjectGroupRepository> _mockProjectGroupRepository; | ||
private readonly Mock<ILogger<DeleteProjectGroupCommandHandler>> _mocklogger; | ||
private readonly Fixture _fixture = new(); | ||
private readonly CancellationToken _cancellationToken; | ||
private readonly DeleteProjectGroupCommandHandler _deleteProjectGroupCommandHandler; | ||
|
||
public DeleteProjectGroupCommandHandlerTests() | ||
{ | ||
_mockRepository = new MockRepository(MockBehavior.Strict); | ||
_cancellationToken = CancellationToken.None; | ||
_mockProjectGroupRepository = _mockRepository.Create<IProjectGroupRepository>(); | ||
_mocklogger = new Mock<ILogger<DeleteProjectGroupCommandHandler>>(); | ||
|
||
var mockContext = new Mock<IUnitOfWork>(); | ||
_mockProjectGroupRepository.Setup(x => x.UnitOfWork).Returns(mockContext.Object); | ||
|
||
_deleteProjectGroupCommandHandler = new DeleteProjectGroupCommandHandler( | ||
_mockProjectGroupRepository.Object, | ||
_mocklogger.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_ProjectGroupDoesNotExists_ReturnsNotFoundCommandResult() | ||
{ | ||
// Arrange | ||
var request = new DeleteProjectGroupCommand("Reference"); | ||
_mockProjectGroupRepository.Setup(x => x.GetByReferenceNumberAsync(request.GroupReferenceNumber, _cancellationToken)).ReturnsAsync((Domain.ProjectGroupsAggregate.ProjectGroup?)null); | ||
|
||
// Act | ||
var result = await _deleteProjectGroupCommandHandler.Handle( | ||
request, | ||
_cancellationToken); | ||
|
||
// Assert | ||
var notFoundCommandResult = Assert.IsType<NotFoundCommandResult>(result); | ||
_mockProjectGroupRepository.Verify(x => x.Update(It.IsAny<Domain.ProjectGroupsAggregate.ProjectGroup>()), Times.Never()); | ||
_mockProjectGroupRepository.Verify(x => x.GetByReferenceNumberAsync(request.GroupReferenceNumber, _cancellationToken), Times.Once()); | ||
_mockProjectGroupRepository.Verify(x => x.UnitOfWork.SaveChangesAsync(It.Is<CancellationToken>(x => x == _cancellationToken)), Times.Never()); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_ValidRequestWithNoRemovedConversions_ReturnsSuccess() | ||
{ | ||
// Arrange | ||
var expectedProjectGroup = _fixture.Create<Domain.ProjectGroupsAggregate.ProjectGroup>(); | ||
expectedProjectGroup.SetProjectReference(1); | ||
var request = new DeleteProjectGroupCommand(expectedProjectGroup.ReferenceNumber!); | ||
_mockProjectGroupRepository.Setup(x => x.Update(It.IsAny<Domain.ProjectGroupsAggregate.ProjectGroup>())); | ||
_mockProjectGroupRepository.Setup(x => x.GetByReferenceNumberAsync(request.GroupReferenceNumber, _cancellationToken)).ReturnsAsync(expectedProjectGroup); | ||
_mockProjectGroupRepository.Setup(x => x.Delete(expectedProjectGroup)); | ||
|
||
// Act | ||
var result = await _deleteProjectGroupCommandHandler.Handle( | ||
request, | ||
_cancellationToken); | ||
|
||
// Assert | ||
var commandSuccessResult = Assert.IsType<CommandSuccessResult>(result); | ||
_mockProjectGroupRepository.Verify(x => x.GetByReferenceNumberAsync(request.GroupReferenceNumber, _cancellationToken), Times.Once); | ||
_mockProjectGroupRepository.Verify(x => x.Delete(expectedProjectGroup), Times.Once); | ||
_mockProjectGroupRepository.Verify(x => x.UnitOfWork.SaveChangesAsync(_cancellationToken), Times.Once); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
Dfe.Academies.Academisation.Service/Commands/ProjectGroup/DeleteProjectGroupCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Dfe.Academies.Academisation.Core; | ||
using MediatR; | ||
|
||
namespace Dfe.Academies.Academisation.Service.Commands.ProjectGroup | ||
{ | ||
public class DeleteProjectGroupCommand(string referenceNumber) : IRequest<CommandResult> | ||
{ | ||
public string GroupReferenceNumber { get; set; } = referenceNumber; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...Academies.Academisation.Service/Commands/ProjectGroup/DeleteProjectGroupCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Dfe.Academies.Academisation.Core; | ||
using Dfe.Academies.Academisation.Domain.ProjectGroupsAggregate; | ||
using MediatR; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Dfe.Academies.Academisation.Service.Commands.ProjectGroup | ||
{ | ||
public class DeleteProjectGroupCommandHandler(IProjectGroupRepository projectGroupRepository, ILogger<DeleteProjectGroupCommandHandler> logger) : IRequestHandler<DeleteProjectGroupCommand, CommandResult> | ||
{ | ||
public async Task<CommandResult> Handle(DeleteProjectGroupCommand message, CancellationToken cancellationToken) | ||
{ | ||
logger.LogInformation("Setting project group with reference number:{value}", message.GroupReferenceNumber); | ||
|
||
var projectGroup = await projectGroupRepository.GetByReferenceNumberAsync(message.GroupReferenceNumber, cancellationToken); | ||
if (projectGroup == null) | ||
{ | ||
logger.LogError("Project group is not found with reference number:{value}", message.GroupReferenceNumber); | ||
return new NotFoundCommandResult(); | ||
} | ||
|
||
projectGroupRepository.Delete(projectGroup); | ||
await projectGroupRepository.UnitOfWork.SaveChangesAsync(cancellationToken); | ||
|
||
return new CommandSuccessResult(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters