Skip to content

Commit

Permalink
Merge pull request #100 from SkillsFundingAgency/MF-431-manage-appren…
Browse files Browse the repository at this point in the history
…tices-table

MF-431 manage apprentices table
  • Loading branch information
dashton82 authored Mar 12, 2020
2 parents 52187ee + bc0b121 commit 89aafb8
Show file tree
Hide file tree
Showing 38 changed files with 2,493 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.IO;
using System.Threading.Tasks;
using AutoFixture.NUnit3;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Shared.ActionResults;
using SFA.DAS.CommitmentsV2.Shared.Interfaces;
using SFA.DAS.EmployerCommitmentsV2.Web.Controllers;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice;
using SFA.DAS.Testing.AutoFixture;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.ApprenticeControllerTests
{
public class DownloadApprenticesTests
{
[Test, MoqAutoData]
public async Task ThenTheFileContentIsSetCorrectly(
DownloadRequest request,
string expectedFileName,
[Frozen] Mock<IModelMapper> csvMapper,
ApprenticeController controller)
{
//Arrange
var expectedCsvContent = new DownloadViewModel
{
Name = expectedFileName,
Content = new MemoryStream()
};
csvMapper.Setup(x =>
x.Map<DownloadViewModel>(request))
.ReturnsAsync(expectedCsvContent);

//Act
var actual = await controller.Download(request);

var actualFileResult = actual as FileResult;

//Assert
Assert.IsNotNull(actualFileResult);
Assert.AreEqual(expectedCsvContent.Name, actualFileResult.FileDownloadName);
Assert.AreEqual(expectedCsvContent.ContentType, actualFileResult.ContentType);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,194 @@
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using AutoFixture.NUnit3;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Shared.Interfaces;
using SFA.DAS.EmployerCommitmentsV2.Web.Controllers;
using SFA.DAS.EmployerCommitmentsV2.Web.Cookies;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice;
using SFA.DAS.Testing.AutoFixture;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.ApprenticeControllerTests
{
[TestFixture]
public class ManageApprenticesTests
{
private ApprenticeController _controller;
[Test, MoqAutoData]
public async Task ThenTheMappedViewModelIsReturned(
IndexRequest request,
IndexViewModel expectedViewModel,
[Frozen] Mock<IModelMapper> apprenticeshipMapper,
ApprenticeController controller)
{
//Arrange
apprenticeshipMapper
.Setup(mapper => mapper.Map<IndexViewModel>(request))
.ReturnsAsync(expectedViewModel);

//Act
var result = await controller.Index(request) as ViewResult;
var actualModel = result.Model as IndexViewModel;

//Assert
actualModel.Should().BeEquivalentTo(expectedViewModel);
}

[Test, MoqAutoData]
public async Task Then_SortedByHeaderClassName_Set(
IndexRequest request,
IndexViewModel expectedViewModel,
[Frozen] Mock<IModelMapper> apprenticeshipMapper,
ApprenticeController controller)
{
//Arrange
expectedViewModel.FilterModel.ReverseSort = false;
apprenticeshipMapper
.Setup(mapper => mapper.Map<IndexViewModel>(request))
.ReturnsAsync(expectedViewModel);

//Act
var result = await controller.Index(request) as ViewResult;
var actualModel = result.Model as IndexViewModel;

//Assert
actualModel.SortedByHeaderClassName.Should().EndWith("das-table__sort--asc");
}

[Test, MoqAutoData]
public async Task Then_Will_Update_Search_Details_To_Cookies(
IndexRequest request,
IndexViewModel expectedViewModel,
[Frozen] Mock<IModelMapper> apprenticeshipMapper,
[Frozen] Mock<ICookieStorageService<IndexRequest>> cookieService,
ApprenticeController controller)
{
//Arrange
request.FromSearch = false;

apprenticeshipMapper
.Setup(mapper => mapper.Map<IndexViewModel>(It.IsAny<IndexRequest>()))
.ReturnsAsync(expectedViewModel);

//Act
await controller.Index(request);

//Assert
cookieService.Verify(x => x.Update(CookieNames.ManageApprentices, request, It.IsAny<int>()));
cookieService.Verify(x => x.Get(It.IsAny<string>()), Times.Never);
}

[Test, MoqAutoData]
public async Task Then_Will_Get_Saved_Search_Details_From_Cookies(
IndexRequest request,
IndexRequest savedRequest,
IndexViewModel expectedViewModel,
[Frozen] Mock<IModelMapper> apprenticeshipMapper,
[Frozen] Mock<ICookieStorageService<IndexRequest>> cookieService,
ApprenticeController controller)
{
//Arrange
request.FromSearch = true;

[SetUp]
public void Setup()
apprenticeshipMapper
.Setup(mapper => mapper.Map<IndexViewModel>(It.IsAny<IndexRequest>()))
.ReturnsAsync(expectedViewModel);

cookieService
.Setup(x => x.Get(CookieNames.ManageApprentices))
.Returns(savedRequest);

//Act
await controller.Index(request);

//Assert
cookieService.Verify(x => x.Get(CookieNames.ManageApprentices));
}

[Test, MoqAutoData]
public async Task Then_Will_Used_Saved_Search_Details_From_Cookies(
IndexRequest request,
IndexRequest savedRequest,
IndexViewModel expectedViewModel,
[Frozen] Mock<IModelMapper> apprenticeshipMapper,
[Frozen] Mock<ICookieStorageService<IndexRequest>> cookieService,
ApprenticeController controller)
{
_controller = new ApprenticeController();
//Arrange
request.FromSearch = true;

apprenticeshipMapper
.Setup(mapper => mapper.Map<IndexViewModel>(It.IsAny<IndexRequest>()))
.ReturnsAsync(expectedViewModel);

cookieService
.Setup(x => x.Get(CookieNames.ManageApprentices))
.Returns(savedRequest);

//Act
await controller.Index(request);

//Assert
apprenticeshipMapper.Verify(mapper => mapper.Map<IndexViewModel>(savedRequest));
}

[Test]
public void AndIsCalledWithInvalidModelStateThenBadResponseShouldBeReturned()
[Test, MoqAutoData]
public async Task Then_Will_Update_Saved_Search_Details_If_From_Cookies(
IndexRequest request,
IndexRequest savedRequest,
IndexViewModel expectedViewModel,
[Frozen] Mock<IModelMapper> apprenticeshipMapper,
[Frozen] Mock<ICookieStorageService<IndexRequest>> cookieService,
ApprenticeController controller)
{
//Arrange
_controller.ModelState.AddModelError("test", "test");
request.FromSearch = true;

apprenticeshipMapper
.Setup(mapper => mapper.Map<IndexViewModel>(It.IsAny<IndexRequest>()))
.ReturnsAsync(expectedViewModel);

cookieService
.Setup(x => x.Get(CookieNames.ManageApprentices))
.Returns(savedRequest);

//Act
var result = _controller.Index("");
await controller.Index(request);

//Assert
Assert.IsInstanceOf<BadRequestObjectResult>(result);
cookieService.Verify(x => x.Update(
CookieNames.ManageApprentices, It.IsAny<IndexRequest>(), It.IsAny<int>()), Times.Never);
}

[Test]
public void ThenTheAccountHashedIdIsPassedToTheViewModel()
[Test, MoqAutoData]
public async Task Then_Will_Used_Current_Search_Details_If_Saved_Details_Not_Available(
IndexRequest request,
IndexViewModel expectedViewModel,
[Frozen] Mock<IModelMapper> apprenticeshipMapper,
[Frozen] Mock<ICookieStorageService<IndexRequest>> cookieService,
ApprenticeController controller)
{
//Arrange
var expectedAccountHashedId = "TEST";
request.FromSearch = true;

apprenticeshipMapper
.Setup(mapper => mapper.Map<IndexViewModel>(It.IsAny<IndexRequest>()))
.ReturnsAsync(expectedViewModel);

cookieService
.Setup(x => x.Get(CookieNames.ManageApprentices))
.Returns((IndexRequest)null);

//Act
var result = _controller.Index(expectedAccountHashedId);
var view = ((ViewResult) result).Model as ManageApprenticesViewModel;
await controller.Index(request);

//Assert
Assert.AreEqual(expectedAccountHashedId, view.AccountHashedId);
apprenticeshipMapper.Verify(mapper => mapper.Map<IndexViewModel>(request));

cookieService.Verify(x => x.Update(
CookieNames.ManageApprentices, It.IsAny<IndexRequest>(), It.IsAny<int>()), Times.Once);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public void EmployerHome_BuildsPathCorrectly(string accountHashedId)
Assert.AreEqual($"{_fixture.UsersLink}accounts/{accountHashedId}/teams", url);
}

[Test, AutoData]
public void ApprenticeDetails_BuildsPathCorrectly(
string accountHashedId,
string hashedApprenticeshipId)
{
var url = _fixture.Sut.ApprenticeDetails(accountHashedId, hashedApprenticeshipId);
Assert.AreEqual($"{_fixture.CommitmentsLink}accounts/{accountHashedId}/apprentices/manage/{hashedApprenticeshipId}/details", url);
}

}

public class ILinkGeneratorExtensionsTestsFixture
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System.Linq;
using System.Threading.Tasks;
using AutoFixture.NUnit3;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.CommitmentsV2.Shared.Extensions;
using SFA.DAS.EmployerCommitmentsV2.Web.Mappers.Apprentice;
using SFA.DAS.Encoding;
using SFA.DAS.Testing.AutoFixture;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Mappers.Apprentice
{
public class ApprenticeshipDetailsToViewModelMapperTests
{
[Test, MoqAutoData]
public async Task Then_Maps_ApprenticeshipId(
GetApprenticeshipsResponse.ApprenticeshipDetailsResponse source,
string encodedApprenticeshipId,
[Frozen] Mock<IEncodingService> mockEncodingService,
ApprenticeshipDetailsToViewModelMapper mapper)
{
mockEncodingService
.Setup(service => service.Encode(source.Id, EncodingType.ApprenticeshipId))
.Returns(encodedApprenticeshipId);

var result = await mapper.Map(source);

result.EncodedApprenticeshipId.Should().Be(encodedApprenticeshipId);
}

[Test, MoqAutoData]
public async Task Then_Maps_ApprenticeName(
GetApprenticeshipsResponse.ApprenticeshipDetailsResponse source,
ApprenticeshipDetailsToViewModelMapper mapper)
{
var result = await mapper.Map(source);

result.ApprenticeName.Should().Be($"{source.FirstName} {source.LastName}");
}

[Test, MoqAutoData]
public async Task Then_Maps_ProviderName(
GetApprenticeshipsResponse.ApprenticeshipDetailsResponse source,
ApprenticeshipDetailsToViewModelMapper mapper)
{
var result = await mapper.Map(source);

result.ProviderName.Should().Be(source.ProviderName);
}

[Test, MoqAutoData]
public async Task Then_Maps_CourseName(
GetApprenticeshipsResponse.ApprenticeshipDetailsResponse source,
ApprenticeshipDetailsToViewModelMapper mapper)
{
var result = await mapper.Map(source);

result.CourseName.Should().Be(source.CourseName);
}

[Test, MoqAutoData]
public async Task Then_Maps_PlannedStartDate(
GetApprenticeshipsResponse.ApprenticeshipDetailsResponse source,
ApprenticeshipDetailsToViewModelMapper mapper)
{
var result = await mapper.Map(source);

result.PlannedStartDate.Should().Be(source.StartDate);
}

[Test, MoqAutoData]
public async Task Then_Maps_PlannedEndDate(
GetApprenticeshipsResponse.ApprenticeshipDetailsResponse source,
ApprenticeshipDetailsToViewModelMapper mapper)
{
var result = await mapper.Map(source);

result.PlannedEndDate.Should().Be(source.EndDate);
}

[Test, MoqAutoData]
public async Task Then_Maps_ApprenticeshipStatus(
GetApprenticeshipsResponse.ApprenticeshipDetailsResponse source,
ApprenticeshipDetailsToViewModelMapper mapper)
{
var result = await mapper.Map(source);

result.Status.Should().Be(source.ApprenticeshipStatus.GetDescription());
}

[Test, MoqAutoData]
public async Task Then_Maps_Alerts(
GetApprenticeshipsResponse.ApprenticeshipDetailsResponse source,
ApprenticeshipDetailsToViewModelMapper mapper)
{
var alertStrings = source.Alerts.Select(x => x.GetDescription());

var result = await mapper.Map(source);

result.Alerts.Should().BeEquivalentTo(alertStrings);
}
}
}
Loading

0 comments on commit 89aafb8

Please sign in to comment.