Skip to content

Commit

Permalink
Merge pull request #148 from SkillsFundingAgency/CON-2507-Show-employ…
Browse files Browse the repository at this point in the history
…er-a-request-submitted-confirmation

Con 2507 show employer a request submitted confirmation
  • Loading branch information
Najamuddin-Muhammad authored Nov 9, 2020
2 parents 80c4ce9 + f86edbd commit f3f01c7
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using AutoFixture;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Api.Client;
using SFA.DAS.CommitmentsV2.Shared.Interfaces;
using SFA.DAS.EmployerCommitmentsV2.Web.Controllers;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice;
using SFA.DAS.EmployerUrlHelper;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.ApprenticeControllerTests
{
public class WhenCallingChangeProviderRequestedConfirmationPageTests
{
private WhenCallingChangeProviderRequestedConfirmationPageTestsFixture _fixture;

[SetUp]
public void Arrange()
{
_fixture = new WhenCallingChangeProviderRequestedConfirmationPageTestsFixture();

}

[Test]
public async Task ThenViewIsReturned()
{
var result = await _fixture.ChangeProviderRequested();

_fixture.VerifyViewModel(result);
}
}

public class WhenCallingChangeProviderRequestedConfirmationPageTestsFixture
{
private readonly ChangeProviderRequestedConfirmationRequest _request;
private readonly ChangeProviderRequestedConfirmationViewModel _viewModel;

private Mock<IModelMapper> _mockMapper;

private ApprenticeController _controller;

public WhenCallingChangeProviderRequestedConfirmationPageTestsFixture()
{
var autoFixture = new Fixture();
_request = autoFixture.Create<ChangeProviderRequestedConfirmationRequest>();
_viewModel = autoFixture.Create<ChangeProviderRequestedConfirmationViewModel>();

_mockMapper = new Mock<IModelMapper>();
_mockMapper.Setup(m => m.Map<ChangeProviderRequestedConfirmationViewModel>(_request))
.ReturnsAsync(_viewModel);

_controller = new ApprenticeController(_mockMapper.Object,
Mock.Of<ICookieStorageService<IndexRequest>>(),
Mock.Of<ICommitmentsApiClient>(),
Mock.Of<ILinkGenerator>());
}

public async Task<IActionResult> ChangeProviderRequested()
{
return await _controller.ChangeProviderRequested(_request);
}

public void VerifyViewModel(IActionResult actionResult)
{
var result = actionResult as ViewResult;
var viewModel = result.Model;

Assert.IsInstanceOf<ChangeProviderRequestedConfirmationViewModel>(viewModel);

var changeProviderRequestedViewModel = viewModel as ChangeProviderRequestedConfirmationViewModel;

Assert.AreEqual(_viewModel, changeProviderRequestedViewModel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void VerifyRedirectsToSentAction(IActionResult result)
{
var redirect = (RedirectToRouteResult)result;

Assert.AreEqual(RouteNames.Sent, redirect.RouteName);
Assert.AreEqual(RouteNames.ChangeProviderRequestedConfirmation, redirect.RouteName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using AutoFixture;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Api.Client;
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.EmployerCommitmentsV2.Web.Mappers.Apprentice;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice;
using SFA.DAS.Testing.AutoFixture;
using System.Threading;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Mappers.Apprentice
{
public class ChangeProviderRequestedConfirmationViewModelMapperTests
{

private Mock<ICommitmentsApiClient> _mockCommitmentsApi;
private GetApprenticeshipResponse _apprenticeshipResponse;
private GetProviderResponse _providerResponse;

private ChangeProviderRequestedConfirmationViewModelMapper _mapper;

[SetUp]
public void Arrange()
{
var autoFixture = new Fixture();

_apprenticeshipResponse = autoFixture.Build<GetApprenticeshipResponse>()
.With(a => a.FirstName, "FirstName")
.With(a => a.LastName, "LastName")
.Create();

_providerResponse = autoFixture.Build<GetProviderResponse>()
.With(p => p.Name, "Test Provider")
.Create();

_mockCommitmentsApi = new Mock<ICommitmentsApiClient>();

_mockCommitmentsApi.Setup(c => c.GetApprenticeship(It.IsAny<long>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(_apprenticeshipResponse);
_mockCommitmentsApi.Setup(c => c.GetProvider(It.IsAny<long>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(_providerResponse);

_mapper = new ChangeProviderRequestedConfirmationViewModelMapper(_mockCommitmentsApi.Object);
}

[Test, MoqAutoData]
public async Task ApprenticeshipHashedId_IsMapped(ChangeProviderRequestedConfirmationRequest request)
{
var result = await _mapper.Map(request);

Assert.AreEqual(request.ApprenticeshipHashedId, result.ApprenticeshipHashedId);
}

[Test, MoqAutoData]
public async Task AccountHashedId_IsMapped(ChangeProviderRequestedConfirmationRequest request)
{
var result = await _mapper.Map(request);

Assert.AreEqual(request.AccountHashedId, result.AccountHashedId);
}

[Test, MoqAutoData]
public async Task GetApprenticeshipIsCalled(ChangeProviderRequestedConfirmationRequest request)
{
var result = await _mapper.Map(request);

_mockCommitmentsApi.Verify(c => c.GetApprenticeship(It.IsAny<long>(), It.IsAny<CancellationToken>()), Times.Once);
}

[Test, MoqAutoData]
public async Task ApprenticeName_IsMapped(ChangeProviderRequestedConfirmationRequest request)
{
var result = await _mapper.Map(request);

Assert.AreEqual($"{_apprenticeshipResponse.FirstName} {_apprenticeshipResponse.LastName}", result.ApprenticeName);
}

[Test, MoqAutoData]
public async Task ProviderName_IsMapped(ChangeProviderRequestedConfirmationRequest request)
{
var result = await _mapper.Map(request);

Assert.AreEqual(_providerResponse.Name, result.ProviderName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using FluentValidation.TestHelper;
using NUnit.Framework;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice;
using SFA.DAS.EmployerCommitmentsV2.Web.Validators;
using System;
using System.Linq.Expressions;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Validators
{
[TestFixture]
public class ChangeProviderRequestedConfirmationValidatorTests
{
[TestCase(1, true)]
[TestCase(default(long), false)]
public void ThenValidatesProviderId(long providerId, bool expectedValid)
{
var request = new ChangeProviderRequestedConfirmationRequest { ProviderId = providerId};

AssertValidationResult(x => x.ProviderId, request, expectedValid);
}

[TestCase(1, true)]
[TestCase(default(long), false)]
public void ThenValidatesApprenticeshipId(long apprenticeshipId, bool expectedValid)
{
var request = new ChangeProviderRequestedConfirmationRequest { ApprenticeshipId = apprenticeshipId};

AssertValidationResult(x => x.ApprenticeshipId, request, expectedValid);
}

[TestCase("", false)]
[TestCase(" ", false)]
[TestCase("testString", true)]
[TestCase(null, false)]
public void ThenValidatesAccountHashedId(string accountHashedId, bool expectedValid)
{
var request = new ChangeProviderRequestedConfirmationRequest { AccountHashedId = accountHashedId };

AssertValidationResult(x => x.AccountHashedId, request, expectedValid);
}

[TestCase("", false)]
[TestCase(" ", false)]
[TestCase("testString", true)]
[TestCase(null, false)]
public void ThenValidatesApprenticeshipHashedId(string apprenticeshipHashedId, bool expectedValid)
{
var request = new ChangeProviderRequestedConfirmationRequest { ApprenticeshipHashedId = apprenticeshipHashedId };

AssertValidationResult(x => x.ApprenticeshipHashedId, request, expectedValid);
}


private void AssertValidationResult<T>(Expression<Func<ChangeProviderRequestedConfirmationRequest, T>> property, ChangeProviderRequestedConfirmationRequest instance, bool expectedValid)
{
var validator = new ChangeProviderRequestedConfirmationValidator();

if (expectedValid)
{
validator.ShouldNotHaveValidationErrorFor(property, instance);
}
else
{
validator.ShouldHaveValidationErrorFor(property, instance);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,33 @@ public async Task<IActionResult> EditEndDate(EditEndDateViewModel viewModel)
return Redirect(url);
}

[Route("{apprenticeshipHashedId}/details/changestatus")]
[DasAuthorize(EmployerFeature.ManageApprenticesV2)]
[HttpGet]
public async Task<IActionResult> ChangeStatus(ChangeStatusRequest request)
{
var viewModel = await _modelMapper.Map<ChangeStatusRequestViewModel>(request);
return View(viewModel);
}

[Route("{apprenticeshipHashedId}/details/changestatus")]
[DasAuthorize(EmployerFeature.ManageApprenticesV2)]
[HttpPost]
public IActionResult ChangeStatus(ChangeStatusRequestViewModel viewModel)
{
switch (viewModel.SelectedStatusChange)
{
case ChangeStatusType.Pause:
return RedirectToAction(nameof(PauseApprenticeship), new { viewModel.AccountHashedId, viewModel.ApprenticeshipHashedId });
case ChangeStatusType.Stop:
return Redirect(_linkGenerator.WhenToApplyStopApprentice(viewModel.AccountHashedId, viewModel.ApprenticeshipHashedId));
case ChangeStatusType.Resume:
return RedirectToAction(nameof(ResumeApprenticeship), new { viewModel.AccountHashedId, viewModel.ApprenticeshipHashedId });
default:
return Redirect(_linkGenerator.ApprenticeDetails(viewModel.AccountHashedId, viewModel.ApprenticeshipHashedId));
}
}

[Route("{apprenticeshipHashedId}/details/changing-training-provider", Name = RouteNames.ChangeProviderInform)]
[DasAuthorize(EmployerFeature.ChangeOfProvider)]
public async Task<IActionResult> ChangeProviderInform(ChangeProviderInformRequest request)
Expand Down Expand Up @@ -140,47 +167,21 @@ public IActionResult SendRequestNewTrainingProvider(SendNewTrainingProviderViewM
{
if (request.Confirm.Value)
{
return RedirectToRoute(RouteNames.Sent);
return RedirectToRoute(RouteNames.ChangeProviderRequestedConfirmation, new { request.AccountHashedId, request.ApprenticeshipHashedId, request.ProviderId });
}

return Redirect(_linkGenerator.ApprenticeDetails(request.AccountHashedId, request.ApprenticeshipHashedId));
}

[Route("{apprenticeshipHashedId}/change-provider/sent", Name = RouteNames.Sent)]
public IActionResult Sent()
{
// Place holder to display information sent.
return View();
}

[Route("{apprenticeshipHashedId}/details/changestatus")]
[DasAuthorize(EmployerFeature.ManageApprenticesV2)]
[HttpGet]
public async Task<IActionResult> ChangeStatus(ChangeStatusRequest request)
[Route("{apprenticeshipHashedId}/change-provider/change-provider-requested/{providerId}", Name = RouteNames.ChangeProviderRequestedConfirmation)]
[DasAuthorize(EmployerFeature.ChangeOfProvider)]
public async Task<IActionResult> ChangeProviderRequested(ChangeProviderRequestedConfirmationRequest request)
{
var viewModel = await _modelMapper.Map<ChangeStatusRequestViewModel>(request);
var viewModel = await _modelMapper.Map<ChangeProviderRequestedConfirmationViewModel>(request);

return View(viewModel);
}

[Route("{apprenticeshipHashedId}/details/changestatus")]
[DasAuthorize(EmployerFeature.ManageApprenticesV2)]
[HttpPost]
public IActionResult ChangeStatus(ChangeStatusRequestViewModel viewModel)
{
switch (viewModel.SelectedStatusChange)
{
case ChangeStatusType.Pause:
return RedirectToAction(nameof(PauseApprenticeship), new { viewModel.AccountHashedId, viewModel.ApprenticeshipHashedId });
case ChangeStatusType.Stop:
return Redirect(_linkGenerator.WhenToApplyStopApprentice(viewModel.AccountHashedId, viewModel.ApprenticeshipHashedId));
case ChangeStatusType.Resume:
return RedirectToAction(nameof(ResumeApprenticeship), new { viewModel.AccountHashedId, viewModel.ApprenticeshipHashedId });
default:
return Redirect(_linkGenerator.ApprenticeDetails(viewModel.AccountHashedId, viewModel.ApprenticeshipHashedId));
}
}

[Route("{apprenticeshipHashedId}/details/pause")]
[DasAuthorize(EmployerFeature.ManageApprenticesV2)]
[HttpGet]
Expand All @@ -201,7 +202,7 @@ public async Task<IActionResult> PauseApprenticeship(PauseRequestViewModel viewM

await _commitmentsApiClient.PauseApprenticeship(pauseRequest, CancellationToken.None);
}

return Redirect(_linkGenerator.ApprenticeDetails(viewModel.AccountHashedId, viewModel.ApprenticeshipHashedId));
}

Expand All @@ -228,5 +229,6 @@ public async Task<IActionResult> ResumeApprenticeship(ResumeRequestViewModel vie

return Redirect(_linkGenerator.ApprenticeDetails(viewModel.AccountHashedId, viewModel.ApprenticeshipHashedId));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using SFA.DAS.CommitmentsV2.Api.Client;
using SFA.DAS.CommitmentsV2.Shared.Interfaces;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerCommitmentsV2.Web.Mappers.Apprentice
{
public class ChangeProviderRequestedConfirmationViewModelMapper : IMapper<ChangeProviderRequestedConfirmationRequest, ChangeProviderRequestedConfirmationViewModel>
{
private readonly ICommitmentsApiClient _client;

public ChangeProviderRequestedConfirmationViewModelMapper(ICommitmentsApiClient client)
{
_client = client;
}

public async Task<ChangeProviderRequestedConfirmationViewModel> Map(ChangeProviderRequestedConfirmationRequest source)
{
var getProviderTask = _client.GetProvider(source.ProviderId);
var getApprenticeshipTask = _client.GetApprenticeship(source.ApprenticeshipId);

await Task.WhenAll(getProviderTask, getApprenticeshipTask);

var result = new ChangeProviderRequestedConfirmationViewModel
{
ApprenticeshipHashedId = source.ApprenticeshipHashedId,
AccountHashedId = source.AccountHashedId,
ProviderName = getProviderTask.Result.Name,
ApprenticeName = $"{getApprenticeshipTask.Result.FirstName} {getApprenticeshipTask.Result.LastName}"
};

return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public EnterNewTrainingProviderViewModelMapper(ICommitmentsApiClient client)
public async Task<EnterNewTrainingProviderViewModel> Map(EnterNewTrainingProviderRequest source)
{
var providersResponse = await _client.GetAllProviders();

var result = new EnterNewTrainingProviderViewModel
{
AccountHashedId = source.AccountHashedId,
Expand Down
Loading

0 comments on commit f3f01c7

Please sign in to comment.