Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
145d5fe
PV2-2849 draft controller + endpoint
AkshayBanifatsyevichChugh Mar 28, 2022
e3b6ac9
PV2-2849 initial GetApprenticeshipStatistics query added
AkshayBanifatsyevichChugh Mar 31, 2022
8cf2d0a
PV2-2849 GetApprenticeshipStatisticsResponse api response dto
AkshayBanifatsyevichChugh Mar 31, 2022
a6cdf1d
PV2-2849 initial controller logic
AkshayBanifatsyevichChugh Mar 31, 2022
612b5da
PV2-2849 ApprenticeshipStatisticsController GetStatistics action impl…
AkshayBanifatsyevichChugh Apr 1, 2022
62f587f
PV2-2849 more GetApprenticeshipStatisticsQuery code
AkshayBanifatsyevichChugh Apr 1, 2022
118c1f3
PV2-2849 extended existing ApprenticeshipStatusSummaryService
AkshayBanifatsyevichChugh Apr 4, 2022
78dd62b
PV2-2849 GetApprenticeshipStatisticsQueryHandler implementation
AkshayBanifatsyevichChugh Apr 4, 2022
bbb573e
PV2-2849 Mapper implementation
AkshayBanifatsyevichChugh Apr 4, 2022
66713c3
PV2-2849 controller empty line removed
AkshayBanifatsyevichChugh Apr 4, 2022
99a65ec
PV2-2849 renamed response properties
AkshayBanifatsyevichChugh Apr 5, 2022
c47a7b2
PV2-2849 correcting GetApprenticeshipStatisticsFor method
AkshayBanifatsyevichChugh Apr 5, 2022
47ab1d0
PV2-2849 controller final implementation + unit tests
AkshayBanifatsyevichChugh Apr 5, 2022
c3a986e
PV2-2849 GetApprenticeshipStatisticsQueryValidator unit tests
AkshayBanifatsyevichChugh Apr 5, 2022
56dc480
PV2-2849 GetApprenticeshipStatisticsHandler unit tests
AkshayBanifatsyevichChugh Apr 5, 2022
fd15490
PV2-2849 ApprenticeshipStatisticsController unit tests amended
AkshayBanifatsyevichChugh Apr 6, 2022
4581629
PV2-2849 GetApprenticeshipStatisticsResponseMapper unit tests
AkshayBanifatsyevichChugh Apr 6, 2022
b86507c
PV2-2849 WhenGettingApprenticeshipStatistics additional unit tests
AkshayBanifatsyevichChugh Apr 6, 2022
619c5e4
Merge branch 'master' of https://github.com/SkillsFundingAgency/das-c…
AkshayBanifatsyevichChugh Apr 6, 2022
35ac13d
PV2-2849 ApprenticeshipStatusSummaryService now awaiting each query
AkshayBanifatsyevichChugh Apr 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace SFA.DAS.CommitmentsV2.Api.Types.Responses
{
public class GetApprenticeshipStatisticsResponse
{
public long Approved { get; set; }
public long Paused { get; set; }
public long Stopped { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using FluentAssertions;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Api.Controllers;
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.CommitmentsV2.Application.Queries.GetApprenticeshipStatistics;
using SFA.DAS.CommitmentsV2.Shared.Interfaces;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;

namespace SFA.DAS.CommitmentsV2.Api.UnitTests.Controllers
{
[TestFixture]
public class ApprenticeshipStatisticsControllerTests
{
private Fixture _fixture;
private int _lastNumberOfDays;
private Mock<IMediator> _mediatorMock;
private Mock<IModelMapper> _modelMapperMock;
private GetApprenticeshipStatisticsQueryResult _getApprenticeshipStatisticsQueryResult;
private GetApprenticeshipStatisticsResponse _getApprenticeshipStatisticsResponse;
private ApprenticeshipStatisticsController _sut;

[SetUp]
public void SetUp()
{
_fixture = new Fixture();
_lastNumberOfDays = _fixture.Create<int>();
_mediatorMock = new Mock<IMediator>();
_modelMapperMock = new Mock<IModelMapper>();
_getApprenticeshipStatisticsQueryResult = _fixture.Freeze<GetApprenticeshipStatisticsQueryResult>();
_getApprenticeshipStatisticsResponse = _fixture.Freeze<GetApprenticeshipStatisticsResponse>();

_sut = new ApprenticeshipStatisticsController(_mediatorMock.Object, _modelMapperMock.Object);

_mediatorMock
.Setup(x => x.Send(It.Is<GetApprenticeshipStatisticsQuery>(x => x.LastNumberOfDays == _lastNumberOfDays),
CancellationToken.None))
.ReturnsAsync(_getApprenticeshipStatisticsQueryResult);

_modelMapperMock
.Setup(x => x.Map<GetApprenticeshipStatisticsResponse>(_getApprenticeshipStatisticsQueryResult))
.ReturnsAsync(_getApprenticeshipStatisticsResponse);
}

[Test]
public async Task WhenCallingGetStatistics_ThenSendsMediatorQueryCorrectly()
{
//Act
await _sut.GetStatistics(_lastNumberOfDays);

//Assert
_mediatorMock.Verify(x => x.Send(It.Is<GetApprenticeshipStatisticsQuery>(x => x.LastNumberOfDays == _lastNumberOfDays), default), Times.Once);
}

[Test]
public async Task WhenCallingGetStatistics_AndQueryReturnsNull_ThenReturnsNotFound()
{
//Arrange
_mediatorMock
.Setup(x => x.Send(
It.Is<GetApprenticeshipStatisticsQuery>(x => x.LastNumberOfDays == _lastNumberOfDays),
CancellationToken.None))
.Returns(Task.FromResult<GetApprenticeshipStatisticsQueryResult>(null));

//Act
var result = await _sut.GetStatistics(_lastNumberOfDays);

//Assert
Assert.True(result is NotFoundResult);
}

[Test]
public async Task WhenCallingGetStatistics_ThenMapsResultToResponseDto()
{
//Act
var result = await _sut.GetStatistics(_lastNumberOfDays);

//Assert
_modelMapperMock.Verify(x => x.Map<GetApprenticeshipStatisticsResponse>(_getApprenticeshipStatisticsQueryResult));
}

[Test]
public async Task WhenCallingGetStatistics_ThenGeneratesOkResponse()
{
//Act
var result = await _sut.GetStatistics(_lastNumberOfDays) as OkObjectResult;
var resultResponse = result?.Value as GetApprenticeshipStatisticsResponse;

//Assert
result.Should().NotBeNull();
resultResponse.Should().NotBeNull();
resultResponse.Paused.Should().Be(_getApprenticeshipStatisticsResponse.Paused);
resultResponse.Stopped.Should().Be(_getApprenticeshipStatisticsResponse.Stopped);
resultResponse.Approved.Should().Be(_getApprenticeshipStatisticsResponse.Approved);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.CommitmentsV2.Application.Queries.GetApprenticeshipStatistics;
using SFA.DAS.CommitmentsV2.Shared.Interfaces;
using System.Threading.Tasks;

namespace SFA.DAS.CommitmentsV2.Api.Controllers
{
[ApiController]
[Authorize]
[Route("api/apprenticeshipstatistics")]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please change this to "api/v1"

public class ApprenticeshipStatisticsController : ControllerBase
{
private readonly IMediator _mediator;
private readonly IModelMapper _modelMapper;

public ApprenticeshipStatisticsController(IMediator mediator, IModelMapper modelMapper)
{
_mediator = mediator;
_modelMapper = modelMapper;
}

[HttpGet]
[Route("/stats")]
public async Task<IActionResult> GetStatistics(int lastNumberOfDays)
{
var result = await _mediator.Send(new GetApprenticeshipStatisticsQuery { LastNumberOfDays = lastNumberOfDays });

if (result == null)
{
return NotFound();
}

var response = await _modelMapper.Map<GetApprenticeshipStatisticsResponse>(result);

return Ok(response);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Application.Queries.GetApprenticeshipStatistics;
using SFA.DAS.CommitmentsV2.Domain.Interfaces;

namespace SFA.DAS.CommitmentsV2.UnitTests.Application.Queries.GetApprenticeshipStatisticsTests
{
[TestFixture]
public class GetApprenticeshipStatisticsHandlerTests
{
private GetApprenticeshipStatisticsQuery _query;
private GetApprenticeshipStatisticsQueryResult _result;
private Mock<IApprenticeshipStatusSummaryService> _apprenticeshipStatusSummaryServiceMock;
private GetApprenticeshipStatisticsQueryHandler _sut;

[SetUp]
public void SetUp()
{
_query = new GetApprenticeshipStatisticsQuery();
_result = new GetApprenticeshipStatisticsQueryResult();

_apprenticeshipStatusSummaryServiceMock = new Mock<IApprenticeshipStatusSummaryService>();
_apprenticeshipStatusSummaryServiceMock
.Setup(x => x.GetApprenticeshipStatisticsFor(_query.LastNumberOfDays))
.ReturnsAsync(_result);

_sut = new GetApprenticeshipStatisticsQueryHandler(_apprenticeshipStatusSummaryServiceMock.Object);
}

[Test]
public async Task WhenHandling_ThenCallsApprenticeshipStatusSummaryServiceWithCorrectValues()
{
//Arrange
_query.LastNumberOfDays = 30;

//Act
await _sut.Handle(_query, default);

//Assert
_apprenticeshipStatusSummaryServiceMock.Verify(x => x.GetApprenticeshipStatisticsFor(_query.LastNumberOfDays), Times.Once);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using FluentAssertions;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Application.Queries.GetApprenticeshipStatistics;

namespace SFA.DAS.CommitmentsV2.UnitTests.Application.Queries.GetApprenticeshipStatisticsTests
{
[TestFixture]
public class GetApprenticeshipStatisticsValidatorTests
{
[TestCase(-10, false)]
[TestCase(0, false)]
[TestCase(10, true)]
public void WhenCallingValidate_ThenCorrectlyValidatesLastNumberOfDays(int lastNumberOfDays,
bool expectedValid)
{
//Arrange
var validator = new GetApprenticeshipStatisticsQueryValidator();

//Act
var result = validator.Validate(new GetApprenticeshipStatisticsQuery { LastNumberOfDays = lastNumberOfDays });

//Assert
result.IsValid.Should().Be(expectedValid);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using AutoFixture;
using FluentAssertions;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Application.Queries.GetApprenticeshipStatistics;
using SFA.DAS.CommitmentsV2.Mapping.ResponseMappers;
using System.Threading.Tasks;

namespace SFA.DAS.CommitmentsV2.UnitTests.Mapping.ResponseMappers
{
[TestFixture]
public class GetApprenticeshipStatisticsResponseMapperTests
{
private Fixture _fixture;
private GetApprenticeshipStatisticsQueryResult _queryResult;
private GetApprenticeshipStatisticsResponseMapper _sut;

[SetUp]
public void SetUp()
{
_fixture = new Fixture();
_queryResult = _fixture.Create<GetApprenticeshipStatisticsQueryResult>();
_sut = new GetApprenticeshipStatisticsResponseMapper();
}

[Test]
public async Task WhenMapIsCalled_ThenResponseIsMappedCorrectly()
{
//Act
var response = await _sut.Map(_queryResult);

//Assert
response.Paused.Should().Be(_queryResult.PausedApprenticeshipCount);
response.Approved.Should().Be(_queryResult.ApprovedApprenticeshipCount);
response.Stopped.Should().Be(_queryResult.StoppedApprenticeshipCount);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using AutoFixture;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;
Expand All @@ -10,13 +15,8 @@
using SFA.DAS.CommitmentsV2.Services;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.Testing.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace SFA.DAS.CommitmentsV2.UnitTests.Services
namespace SFA.DAS.CommitmentsV2.UnitTests.Services.ApprenticeshipStatusSummaryTests
{
[TestFixture]
[Parallelizable(ParallelScope.None)]
Expand Down
Loading