Skip to content

Commit

Permalink
Merge pull request #145 from SkillsFundingAgency/CON-2506-send-reques…
Browse files Browse the repository at this point in the history
…t-new-training-provider

Con 2506 send request new training provider
  • Loading branch information
Najamuddin-Muhammad authored Nov 4, 2020
2 parents ad3ce10 + 2044e33 commit bb4139d
Show file tree
Hide file tree
Showing 18 changed files with 639 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using AutoFixture;
using AutoFixture.NUnit3;
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 SFA.DAS.Testing.AutoFixture;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.ApprenticeControllerTests
{
public class WhenCallingGetSendRequestNewTrainingProviderTests
{
WhenCallingGetSendRequestNewTrainingProviderTestsFixture _fixture;

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

[Test]
public async Task Then_The_View_Is_Returned()
{
var actionResult = await _fixture.SendRequestNewTrainingProvider();

_fixture.VerifyViewModel(actionResult);
}
}

public class WhenCallingGetSendRequestNewTrainingProviderTestsFixture
{
private readonly Mock<IModelMapper> _modelMapper;
private ApprenticeController _controller;
private SendNewTrainingProviderViewModel _expectedViewModel;
private SendNewTrainingProviderRequest _request;

public WhenCallingGetSendRequestNewTrainingProviderTestsFixture()
{
var autoFixture = new Fixture();
_expectedViewModel = autoFixture.Create<SendNewTrainingProviderViewModel>();
_request = autoFixture.Create<SendNewTrainingProviderRequest>();


_modelMapper = new Mock<IModelMapper>();
_modelMapper
.Setup(mapper => mapper.Map<SendNewTrainingProviderViewModel>(_request))
.ReturnsAsync(_expectedViewModel);

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

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

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

Assert.IsInstanceOf<SendNewTrainingProviderViewModel>(viewModel);
var sendNewTrainingProviderViewModel = (SendNewTrainingProviderViewModel)viewModel;

Assert.AreEqual(_expectedViewModel, sendNewTrainingProviderViewModel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public WhenPostingEnterNewTrainingProviderFixture()

_mockMapper = new Mock<IModelMapper>();
_mockMapper.Setup(m => m.Map<SendNewTrainingProviderRequest>(_viewModel))
.ReturnsAsync(new SendNewTrainingProviderRequest { AccountHashedId = _viewModel.AccountHashedId, ApprenticeshipHashedId = _viewModel.ApprenticeshipHashedId, Ukprn = _viewModel.Ukprn });
.ReturnsAsync(new SendNewTrainingProviderRequest { AccountHashedId = _viewModel.AccountHashedId, ApprenticeshipHashedId = _viewModel.ApprenticeshipHashedId, ProviderId = _viewModel.Ukprn });

_mockLinkGenerator = new Mock<ILinkGenerator>();
_mockLinkGenerator.Setup(x => x.CommitmentsLink(It.IsAny<string>())).Returns<string>(s => s);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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.EmployerCommitmentsV2.Web.RouteValues;
using SFA.DAS.EmployerUrlHelper;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.ApprenticeControllerTests
{
public class WhenPostingSendRequestNewTrainingProviderTests
{
private WhenPostingSendRequestNewTrainingProviderTestsFixture _fixture;

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

[Test]
public void VerifyRedirectsToApprenticeDetailsPage()
{
_fixture.SetConfirm(false);

var result = _fixture.SendRequestNewTrainingProvider();

_fixture.VerifyRedirectsToApprenticeDetailsPage(result);
}

[Test]
public void VerifyRedirectsToSentAction()
{
_fixture.SetConfirm(true);

var result = _fixture.SendRequestNewTrainingProvider();

_fixture.VerifyRedirectsToSentAction(result);
}
}

public class WhenPostingSendRequestNewTrainingProviderTestsFixture
{
private readonly Mock<IModelMapper> _modelMapper;
private readonly Mock<ICommitmentsApiClient> _commitmentsApiClient;
private readonly Mock<ILinkGenerator> _linkGenerator;
private ApprenticeController _controller;
private SendNewTrainingProviderViewModel _viewModel;

public WhenPostingSendRequestNewTrainingProviderTestsFixture()
{
var autoFixture = new Fixture();
_viewModel = autoFixture.Create<SendNewTrainingProviderViewModel>();

_commitmentsApiClient = new Mock<ICommitmentsApiClient>();
_modelMapper = new Mock<IModelMapper>();

_linkGenerator = new Mock<ILinkGenerator>();
_linkGenerator.Setup(x => x.CommitmentsLink(It.IsAny<string>())).Returns<string>(s => s);

_controller = new ApprenticeController(Mock.Of<IModelMapper>(),
Mock.Of<ICookieStorageService<IndexRequest>>(),
_commitmentsApiClient.Object,
_linkGenerator.Object);
}

public IActionResult SendRequestNewTrainingProvider()
{
return _controller.SendRequestNewTrainingProvider(_viewModel);
}

public WhenPostingSendRequestNewTrainingProviderTestsFixture SetConfirm(bool confirm)
{
_viewModel.Confirm = confirm;
return this;
}

public void VerifyRedirectsToApprenticeDetailsPage(IActionResult result)
{
var redirect = (RedirectResult)result;
redirect.WithUrl($"accounts/{_viewModel.AccountHashedId}/apprentices/manage/{_viewModel.ApprenticeshipHashedId}/details");
}

public void VerifyRedirectsToSentAction(IActionResult result)
{
var redirect = (RedirectToRouteResult)result;

Assert.AreEqual(RouteNames.Sent, redirect.RouteName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task Ukprn_IsMapped(EnterNewTrainingProviderViewModel viewModel)
{
var result = await _mapper.Map(viewModel);

Assert.AreEqual(viewModel.Ukprn, result.Ukprn);
Assert.AreEqual(viewModel.Ukprn, result.ProviderId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using AutoFixture;
using Microsoft.Extensions.Logging;
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 System.Threading;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Mappers.Apprentice
{
public class SendNewTrainingProviderViewModelMapperTests
{
private Mock<ICommitmentsApiClient> _commitmentApiClient;
private GetApprenticeshipResponse _apprenticeshipResponse;
private GetProviderResponse _providerResponse;
private SendNewTrainingProviderViewModelMapper _sut;
private SendNewTrainingProviderRequest _request;

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

_apprenticeshipResponse = autoFixture.Build<GetApprenticeshipResponse>()
.Create();
_providerResponse = autoFixture.Build<GetProviderResponse>()
.Create();
_request = autoFixture.Build<SendNewTrainingProviderRequest>()
.Create();


_commitmentApiClient = new Mock<ICommitmentsApiClient>();
_commitmentApiClient.Setup(a => a.GetApprenticeship(It.IsAny<long>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(_apprenticeshipResponse);

_commitmentApiClient.Setup(a => a.GetProvider(It.IsAny<long>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(_providerResponse);

_sut = new SendNewTrainingProviderViewModelMapper(_commitmentApiClient.Object, Mock.Of<ILogger<SendNewTrainingProviderViewModelMapper>>());
}

[Test]
public async Task ApprenticeshipHashedId_IsMapped()
{
var result = await _sut.Map(_request);

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

[Test]
public async Task AccountHashedId_IsMapped()
{
var result = await _sut.Map(_request);

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

[Test]
public async Task EmployerName_IsMapped()
{
var result = await _sut.Map(_request);

Assert.AreEqual(_apprenticeshipResponse.EmployerName, result.EmployerName);
}

[Test]
public async Task ApprenticeName_IsMapped()
{
var result = await _sut.Map(_request);

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

[Test]
public async Task OldProviderName_IsMapped()
{
var result = await _sut.Map(_request);

Assert.AreEqual(_apprenticeshipResponse.ProviderName, result.OldProviderName);
}

[Test]
public async Task NewProviderName_IsMapped()
{
var result = await _sut.Map(_request);

Assert.AreEqual(_providerResponse.Name, result.NewProviderName);
}

[Test]
public async Task ProviderId_IsMapped()
{
var result = await _sut.Map(_request);

Assert.AreEqual(_request.ProviderId, result.ProviderId);
}

[Test]
public async Task ApprenticeshipStatus_IsMapped()
{
var result = await _sut.Map(_request);

Assert.AreEqual(_apprenticeshipResponse.Status, result.ApprenticeshipStatus);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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 SendNewTrainingProviderRequestValidatorTests
{
[TestCase("5143541", true)]
[TestCase(" ", false)]
[TestCase("", false)]
[TestCase(null, false)]
public void ThenAccountHashedIdIsValidated(string accountHashedId, bool expectedValid)
{
var request = new SendNewTrainingProviderRequest() { AccountHashedId = accountHashedId };
AssertValidationResult(x => x.AccountHashedId, request, expectedValid);
}
[TestCase("", false)]
[TestCase("31", true)]
[TestCase(" ", false)]
[TestCase(null, false)]
public void ThenApprenticeshipHashedIdIsValidated(string apprenticeshipHashedId, bool expectedValid)
{
var request = new SendNewTrainingProviderRequest() { ApprenticeshipHashedId = apprenticeshipHashedId };
AssertValidationResult(x => x.ApprenticeshipHashedId, request, expectedValid);
}

[TestCase(default(long), false)]
[TestCase(-2342, false)]
[TestCase(234, true)]
public void ThenProviderIsValidated(long providerId, bool expectedValid)
{
var request = new SendNewTrainingProviderRequest() { ProviderId = providerId };
AssertValidationResult(x => x.ProviderId, request, expectedValid);
}

[TestCase(default(long), false)]
[TestCase(-2342, false)]
[TestCase(234, true)]
public void ThenApprenticeshipIdIsValidated(long apprenticeshipId, bool expectedValid)
{
var request = new SendNewTrainingProviderRequest() { ApprenticeshipId = apprenticeshipId };
AssertValidationResult(x => x.ApprenticeshipId, request, expectedValid);
}

[TestCase(default(long), false)]
[TestCase(-2342, false)]
[TestCase(234, true)]
public void ThenAccountIdIsValidated(long accountId, bool expectedValid)
{
var request = new SendNewTrainingProviderRequest() { AccountId = accountId };
AssertValidationResult(x => x.AccountId, request, expectedValid);
}

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

if (expectedValid)
{
validator.ShouldNotHaveValidationErrorFor(property, instance);
}
else
{
validator.ShouldHaveValidationErrorFor(property, instance);
}
}
}
}
Loading

0 comments on commit bb4139d

Please sign in to comment.