Skip to content

Commit

Permalink
Merge branch 'refs/heads/main' into feature/187393-create-conversion-…
Browse files Browse the repository at this point in the history
…projects

# Conflicts:
#	src/Tests/Dfe.Complete.Application.Tests/QueryHandlers/Project/GetProjectByUrnQueryHandlerTests.cs
  • Loading branch information
zhodges-nimble committed Jan 10, 2025
2 parents 94ca7f2 + c8f952d commit 11da0cf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Frontend/Dfe.Complete/Validators/UrnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override ValidationResult IsValid(object value, ValidationContext vali

var result = sender.Send(new GetProjectByUrnQuery(new Urn(urn.ToInt())));

if (result.Result != null)
if (result.Result?.Value != null)
{
var errorMessage = $"A project with the urn: {urn} already exists";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Dfe.Complete.Application.Common.Models;
using DfE.CoreLibs.Caching.Helpers;
using Dfe.Complete.Application.Projects.Queries.GetProject;
using System.Linq.Expressions;
using Dfe.Complete.Domain.ValueObjects;

namespace Dfe.Complete.Application.Tests.QueryHandlers.Project
Expand All @@ -24,11 +25,9 @@ GetProjectByUrnQuery command
{
var now = DateTime.UtcNow;

var urn = 123456;

var project = Domain.Entities.Project.CreateConversionProject(
new ProjectId(Guid.NewGuid()),
new Domain.ValueObjects.Urn(urn),
command.Urn,
now,
now,
Domain.Enums.TaskType.Conversion,
Expand All @@ -50,10 +49,10 @@ GetProjectByUrnQuery command
null,
null);

var cacheKey = $"Project_{CacheKeyHelper.GenerateHashedCacheKey(urn.ToString())}";
var cacheKey = $"Project_{CacheKeyHelper.GenerateHashedCacheKey(command.Urn.Value.ToString())}";

// Arrange
mockProjectRepository.GetAsync()
mockProjectRepository.GetAsync(Arg.Any<Expression<Func<Domain.Entities.Project, bool>>>())
.Returns(project);

mockCacheService.GetOrAddAsync(
Expand All @@ -70,7 +69,46 @@ GetProjectByUrnQuery command
var result = await handler.Handle(command, default);

// Assert
//await mockProjectRepository.Received(1).GetAsync();
await mockProjectRepository.Received(1).GetAsync(Arg.Any<Expression<Func<Domain.Entities.Project, bool>>>());
Assert.True(result.IsSuccess == true);
Assert.True(result.Value?.Urn == command.Urn);
}


[Theory]
[CustomAutoData(typeof(DateOnlyCustomization))]
public async Task ONEONEHandle_ShouldGetAProjectByUrn_WhenCommandIsValid(
[Frozen] ICompleteRepository<Domain.Entities.Project> mockProjectRepository,
[Frozen] ICacheService<IMemoryCacheType> mockCacheService,
GetProjectByUrnQueryHandler handler,
GetProjectByUrnQuery command
)
{
var now = DateTime.UtcNow;

var cacheKey = $"Project_{CacheKeyHelper.GenerateHashedCacheKey(command.Urn.Value.ToString())}";

// Arrange
mockProjectRepository.GetAsync(Arg.Any<Expression<Func<Domain.Entities.Project?, bool>>>())
.Returns((Domain.Entities.Project?)null);

mockCacheService.GetOrAddAsync(
cacheKey,
Arg.Any<Func<Task<Result<Domain.Entities.Project?>>>>(),
Arg.Any<string>())
.Returns(callInfo =>
{
var callback = callInfo.ArgAt<Func<Task<Result<Domain.Entities.Project?>>>>(1);
return callback();
});

// Act
var result = await handler.Handle(command, default);

// Assert
await mockProjectRepository.Received(1).GetAsync(Arg.Any<Expression<Func<Domain.Entities.Project, bool>>>());
Assert.True(result.IsSuccess == true);
Assert.True(result.Value == null);
}
}
}

0 comments on commit 11da0cf

Please sign in to comment.