Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FAI-2021 APIM Delete search alerts Dormant Account #1859

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using AutoFixture.NUnit3;
using FluentAssertions;
using FluentAssertions.Execution;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using SFA.DAS.FindApprenticeshipJobs.Api.Controllers;
using SFA.DAS.FindApprenticeshipJobs.Application.Queries.SavedSearch.GetCandidatesByActivity;
using SFA.DAS.Testing.AutoFixture;
using System.Net;

namespace SFA.DAS.FindApprenticeshipJobs.Api.UnitTests.Controllers
{
[TestFixture]
public class WhenGettingCandidatesByActivity
{
[Test, MoqAutoData]
public async Task Then_Candidates_Returned_From_Mediator(
int pageNumber,
int pageSize,
DateTime cutOffDateTime,
GetCandidateByActivityQueryResult mockQueryResult,
[Frozen] Mock<IMediator> mockMediator,
[Greedy] CandidatesController sut)
{
mockMediator.Setup(x => x.Send(It.IsAny<GetCandidateByActivityQuery>(), It.IsAny<CancellationToken>())).ReturnsAsync(mockQueryResult);

var actual = await sut.GetCandidatesByActivity(cutOffDateTime, pageNumber, pageSize, It.IsAny<CancellationToken>()) as ObjectResult;
var actualValue = actual!.Value as GetCandidateByActivityQueryResult;

using (new AssertionScope())
{
actual.StatusCode.Should().Be((int)HttpStatusCode.OK);
actual.Value.Should().BeOfType<GetCandidateByActivityQueryResult>();
actualValue!.Candidates.Should().BeEquivalentTo(mockQueryResult.Candidates);
}
}

[Test, MoqAutoData]
public async Task And_Exception_Returned_Then_Returns_Internal_Server_Error(
int pageNumber,
int pageSize,
DateTime cutOffDateTime,
GetCandidateByActivityQueryResult mockQueryResult,
[Frozen] Mock<IMediator> mockMediator,
[Greedy] CandidatesController sut)
{
mockMediator.Setup(x => x.Send(It.IsAny<GetCandidateByActivityQuery>(), It.IsAny<CancellationToken>())).ThrowsAsync(new InvalidOperationException());

var actual = await sut.GetCandidatesByActivity(cutOffDateTime, pageNumber, pageSize, It.IsAny<CancellationToken>()) as StatusCodeResult;

actual!.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using AutoFixture.NUnit3;
using FluentAssertions;
using FluentAssertions.Execution;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using SFA.DAS.FindApprenticeshipJobs.Api.Models;
using SFA.DAS.FindApprenticeshipJobs.Application.Commands.Candidates;
using SFA.DAS.Testing.AutoFixture;
using System.Net;

namespace SFA.DAS.FindApprenticeshipJobs.Api.UnitTests.Controllers
{
[TestFixture]
public class WhenPostingCandidateStatus
{
[Test, MoqAutoData]
public async Task Then_Returns_Post_Response(
string govIdentifier,
CandidateUpdateStatusRequest model,
[Frozen] Mock<IMediator> mediator,
[Greedy] Api.Controllers.CandidatesController controller)
{
var actual = await controller.UpdateStatus(govIdentifier, model);

using (new AssertionScope())
{
actual.Should().BeOfType<NoContentResult>();
}
}

[Test, MoqAutoData]
public async Task Then_If_An_Exception_Is_Thrown_Then_Internal_Server_Error_Response_Returned(
string govIdentifier,
CandidateUpdateStatusRequest model,
[Frozen] Mock<IMediator> mediator,
[Greedy] Api.Controllers.CandidatesController controller)
{
mediator.Setup(x => x.Send(It.Is<UpdateCandidateStatusCommand>(x => x.GovUkIdentifier == govIdentifier
&& (x.Email == model.Email)
&& x.Status == model.Status),
It.IsAny<CancellationToken>()))
.ThrowsAsync(new Exception());

var actual = await controller.UpdateStatus(govIdentifier, model) as StatusCodeResult;

Assert.That(actual, Is.Not.Null);
actual.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using SFA.DAS.FindApprenticeshipJobs.Application.Queries.SavedSearch.GetCandidatesByActivity;
using System.Net;
using SFA.DAS.FindApprenticeshipJobs.Application.Commands.Candidates;
using SFA.DAS.FindApprenticeshipJobs.Domain.Models;
using SFA.DAS.FindApprenticeshipJobs.Api.Models;

namespace SFA.DAS.FindApprenticeshipJobs.Api.Controllers
{
[ApiController]
[Route("[controller]")]
public class CandidatesController(
IMediator mediator,
ILogger<CandidatesController> logger) : ControllerBase
{
[HttpGet]
[Route("GetCandidatesByActivity")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.InternalServerError)]
public async Task<IActionResult> GetCandidatesByActivity(
[FromQuery] DateTime cutOffDateTime,
[FromQuery] int pageNumber = 1,
[FromQuery] int pageSize = 10,
CancellationToken cancellationToken = default)
{
logger.LogInformation("Get Candidates by activity invoked");

try
{
var result = await mediator.Send(new GetCandidateByActivityQuery(cutOffDateTime, pageNumber, pageSize),
cancellationToken);
return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Error invoking Get Candidates By Activity");
return new StatusCodeResult((int)HttpStatusCode.InternalServerError);
}
}

[HttpPost]
[Route("{govIdentifier}/status")]
public async Task<IActionResult> UpdateStatus(
[FromRoute] string govIdentifier,
[FromBody] CandidateUpdateStatusRequest request)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No cancellation token on this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved it as in comments

{
try
{
await mediator.Send(new UpdateCandidateStatusCommand
{
GovUkIdentifier = govIdentifier,
Email = request.Email,
Status = request.Status
});

return NoContent();
}
catch (Exception e)
{
logger.LogError(e, "Error attempting to update candidate status");
return StatusCode((int)HttpStatusCode.InternalServerError);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using SFA.DAS.FindApprenticeshipJobs.Domain.Models;

namespace SFA.DAS.FindApprenticeshipJobs.Api.Models
{
public class CandidateUpdateStatusRequest
{
public required string Email { get; set; }
public UserStatus Status { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using AutoFixture.NUnit3;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using SFA.DAS.FindApprenticeshipJobs.Application.Queries.SavedSearch.GetCandidatesByActivity;
using SFA.DAS.FindApprenticeshipJobs.InnerApi.Requests;
using SFA.DAS.FindApprenticeshipJobs.InnerApi.Responses;
using SFA.DAS.SharedOuterApi.Configuration;
using SFA.DAS.SharedOuterApi.Interfaces;
using SFA.DAS.Testing.AutoFixture;

namespace SFA.DAS.FindApprenticeshipJobs.UnitTests.Candidates
{
[TestFixture]
public class WhenHandlingGetCandidatesByActivityQuery
{
[Test]
[MoqAutoData]
public async Task Then_The_Candidates_Are_Returned(
GetCandidateByActivityQuery query,
GetCandidatesByActivityApiResponse mockCandidatesByActivityApiResponse,
[Frozen] Mock<ICandidateApiClient<CandidateApiConfiguration>> mockCandidateApiClient,
GetCandidateByActivityQueryHandler handler)
{
var expectedGetCandidatesByActivityApiRequest =
new GetCandidatesByActivityApiRequest(query.CutOffDateTime.ToString("O"), query.PageNumber, query.PageSize);

mockCandidateApiClient
.Setup(client => client.Get<GetCandidatesByActivityApiResponse>(
It.Is<GetCandidatesByActivityApiRequest>(c =>
c.GetUrl == expectedGetCandidatesByActivityApiRequest.GetUrl)))
.ReturnsAsync(mockCandidatesByActivityApiResponse);


var actual = await handler.Handle(query, CancellationToken.None);

actual.Should().NotBeNull();
actual.Candidates.Should().BeEquivalentTo(mockCandidatesByActivityApiResponse.Candidates);
actual.PageSize.Should().Be(mockCandidatesByActivityApiResponse.PageSize);
actual.PageIndex.Should().Be(mockCandidatesByActivityApiResponse.PageIndex);
actual.TotalPages.Should().Be(mockCandidatesByActivityApiResponse.TotalPages);
actual.TotalCount.Should().Be(mockCandidatesByActivityApiResponse.TotalCount);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using AutoFixture.NUnit3;
using Moq;
using NUnit.Framework;
using SFA.DAS.FindApprenticeshipJobs.Application.Commands.Candidates;
using SFA.DAS.FindApprenticeshipJobs.InnerApi.Requests;
using SFA.DAS.FindApprenticeshipJobs.InnerApi.Responses;
using SFA.DAS.SharedOuterApi.Configuration;
using SFA.DAS.SharedOuterApi.Interfaces;
using SFA.DAS.SharedOuterApi.Models;
using SFA.DAS.Testing.AutoFixture;
using System.Net;

namespace SFA.DAS.FindApprenticeshipJobs.UnitTests.Candidates
{
public class WhenHandlingUpdateCandidateStatusCommand
{
[Test, MoqAutoData]
public async Task Then_The_Put_Is_Sent_And_Data_Returned(
string govUkId,
UpdateCandidateStatusCommand command,
GetCandidateApiResponse getCandidateApiResponse,
PutCandidateApiResponse putCandidateApiResponse,
[Frozen] Mock<ICandidateApiClient<CandidateApiConfiguration>> mockApiClient,
UpdateCandidateStatusCommandHandler handler)
{
command.GovUkIdentifier = govUkId;
command.Email = getCandidateApiResponse.Email;
var expectedGetCandidateRequest = new GetCandidateApiRequest(govUkId);
mockApiClient.Setup(x => x.GetWithResponseCode<GetCandidateApiResponse>(
It.Is<GetCandidateApiRequest>(r => r.GetUrl == expectedGetCandidateRequest.GetUrl)))
.ReturnsAsync(new ApiResponse<GetCandidateApiResponse>(getCandidateApiResponse, HttpStatusCode.OK, string.Empty));


var expectedPutData = new PutCandidateApiRequestData
{
Email = command.Email,
Status = command.Status
};

var expectedRequest = new PutCandidateApiRequest(getCandidateApiResponse.Id, expectedPutData);

mockApiClient
.Setup(client => client.PutWithResponseCode<PutCandidateApiResponse>(
It.Is<PutCandidateApiRequest>(c =>
c.PutUrl == expectedRequest.PutUrl
&& ((PutCandidateApiRequestData)c.Data).Email == command.Email)))
.ReturnsAsync(new ApiResponse<PutCandidateApiResponse>(putCandidateApiResponse, HttpStatusCode.OK, string.Empty));


await handler.Handle(command, CancellationToken.None);

mockApiClient.Verify(client => client.PutWithResponseCode<PutCandidateApiResponse>(
It.Is<PutCandidateApiRequest>(c =>
c.PutUrl == expectedRequest.PutUrl
&& ((PutCandidateApiRequestData)c.Data).Email == command.Email)), Times.Once());
}

[Test, MoqAutoData]
public async Task And_Api_Returns_Null_Then_Put_Never_Called(
string govUkId,
UpdateCandidateStatusCommand command,
GetCandidateApiResponse getCandidateApiResponse,
PutCandidateApiResponse putCandidateApiResponse,
[Frozen] Mock<ICandidateApiClient<CandidateApiConfiguration>> mockApiClient,
UpdateCandidateStatusCommandHandler handler)
{
command.GovUkIdentifier = govUkId;
command.Email = getCandidateApiResponse.Email;
var expectedGetCandidateRequest = new GetCandidateApiRequest(govUkId);
mockApiClient.Setup(x => x.GetWithResponseCode<GetCandidateApiResponse>(
It.Is<GetCandidateApiRequest>(r => r.GetUrl == expectedGetCandidateRequest.GetUrl)))
.ReturnsAsync(new ApiResponse<GetCandidateApiResponse>(null!, HttpStatusCode.NotFound, string.Empty));


var expectedPutData = new PutCandidateApiRequestData
{
Email = command.Email,
Status = command.Status
};

var expectedRequest = new PutCandidateApiRequest(getCandidateApiResponse.Id, expectedPutData);

await handler.Handle(command, CancellationToken.None);

mockApiClient.Verify(client => client.PutWithResponseCode<PutCandidateApiResponse>(
It.Is<PutCandidateApiRequest>(c =>
c.PutUrl == expectedRequest.PutUrl
&& ((PutCandidateApiRequestData)c.Data).Email == command.Email)), Times.Never());
}

[Test, MoqAutoData]
public async Task And_Api_Returns_Different_Email_Then_Put_Never_Called(
string govUkId,
UpdateCandidateStatusCommand command,
GetCandidateApiResponse getCandidateApiResponse,
PutCandidateApiResponse putCandidateApiResponse,
[Frozen] Mock<ICandidateApiClient<CandidateApiConfiguration>> mockApiClient,
UpdateCandidateStatusCommandHandler handler)
{
command.GovUkIdentifier = govUkId;
var expectedGetCandidateRequest = new GetCandidateApiRequest(govUkId);
mockApiClient.Setup(x => x.GetWithResponseCode<GetCandidateApiResponse>(
It.Is<GetCandidateApiRequest>(r => r.GetUrl == expectedGetCandidateRequest.GetUrl)))
.ReturnsAsync(new ApiResponse<GetCandidateApiResponse>(null!, HttpStatusCode.NotFound, string.Empty));


var expectedPutData = new PutCandidateApiRequestData
{
Email = command.Email,
Status = command.Status
};

var expectedRequest = new PutCandidateApiRequest(getCandidateApiResponse.Id, expectedPutData);

await handler.Handle(command, CancellationToken.None);

mockApiClient.Verify(client => client.PutWithResponseCode<PutCandidateApiResponse>(
It.Is<PutCandidateApiRequest>(c =>
c.PutUrl == expectedRequest.PutUrl
&& ((PutCandidateApiRequestData)c.Data).Email == command.Email)), Times.Never());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using AutoFixture.NUnit3;
using FluentAssertions;
using NUnit.Framework;
using SFA.DAS.FindApprenticeshipJobs.InnerApi.Requests;

namespace SFA.DAS.FindApprenticeshipJobs.UnitTests.InnerApi
{
[TestFixture]
public class WhenBuildingGetCandidatesByActivityApiRequest
{
[Test, AutoData]
public void Then_The_Url_Is_Correctly_Built(string cutOffDateTime, int pageNumber, int pageSize)
{
var actual = new GetCandidatesByActivityApiRequest(cutOffDateTime, pageNumber, pageSize);

actual.GetUrl.Should().Be($"api/candidates/GetCandidatesByActivity?cutOffDateTime={cutOffDateTime}&pageNumber={pageNumber}&pageSize={pageSize}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using AutoFixture.NUnit3;
using FluentAssertions;
using NUnit.Framework;
using SFA.DAS.FindApprenticeshipJobs.InnerApi.Requests;

namespace SFA.DAS.FindApprenticeshipJobs.UnitTests.InnerApi;
public class WhenBuildingPutCandidateRequest
{
[Test, AutoData]
public void Then_The_Request_Url_Is_Correctly_Built(Guid candidateId, PutCandidateApiRequestData data)
{
var actual = new PutCandidateApiRequest(candidateId, data);

actual.PutUrl.Should().Be($"/api/candidates/{candidateId}");
}
}
Loading