Skip to content

Commit

Permalink
Merge pull request #146 from SkillsFundingAgency/CON-2505-CoP-Enter-t…
Browse files Browse the repository at this point in the history
…he-new-training-provider

CON-2505 CoP enter the new training provider
  • Loading branch information
Najamuddin-Muhammad authored Nov 4, 2020
2 parents 6d50c64 + e99f62d commit ad3ce10
Show file tree
Hide file tree
Showing 22 changed files with 609 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.ApprenticeControllerTests
{
public class WhenCallingGetEnterNewTrainingProviderTests
{
WhenCallingGetEnterNewTrainingProviderTestsFixture _fixture;

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

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

_fixture.VerifyViewModel(result);
}
}

public class WhenCallingGetEnterNewTrainingProviderTestsFixture
{
private readonly Mock<IModelMapper> _mockMapper;

private EnterNewTrainingProviderRequest _request;
private EnterNewTrainingProviderViewModel _viewModel;

private ApprenticeController _controller;

public WhenCallingGetEnterNewTrainingProviderTestsFixture()
{
var autoFixture = new Fixture();
_request = autoFixture.Create<EnterNewTrainingProviderRequest>();
_viewModel = autoFixture.Create<EnterNewTrainingProviderViewModel>();

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

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

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

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

Assert.IsInstanceOf<EnterNewTrainingProviderViewModel>(viewModel);

var enterNewTrainingProviderViewModel = (EnterNewTrainingProviderViewModel)viewModel;

Assert.AreEqual(_viewModel, enterNewTrainingProviderViewModel);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.ApprenticeControllerTests
{
public class WhenPostingEnterNewTrainingProvider
{
private WhenPostingEnterNewTrainingProviderFixture _fixture;

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

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

_fixture.VerifyRedirectsToSendNewTrainingProviderRequest(result);
}
}

public class WhenPostingEnterNewTrainingProviderFixture
{
private readonly Mock<IModelMapper> _mockMapper;
private readonly Mock<ILinkGenerator> _mockLinkGenerator;
private readonly EnterNewTrainingProviderViewModel _viewModel;

private readonly ApprenticeController _controller;

public WhenPostingEnterNewTrainingProviderFixture()
{
var autoFixture = new Fixture();

_viewModel = autoFixture.Create<EnterNewTrainingProviderViewModel>();

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

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

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

}

public async Task<IActionResult> EnterNewTrainingProvider()
{
return await _controller.EnterNewTrainingProvider(_viewModel);
}

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

Assert.AreEqual(RouteNames.SendRequestNewTrainingProvider, redirect.RouteName);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Mappers.Apprentice
{
public class EnterNewTrainingProviderViewModelMapperTests
{
private Mock<ICommitmentsApiClient> _mockCommitmentsApiClient;

private EnterNewTrainingProviderViewModelMapper _mapper;

[SetUp]
public void Arrange()
{
_mockCommitmentsApiClient = new Mock<ICommitmentsApiClient>();

_mockCommitmentsApiClient.Setup(m => m.GetAllProviders(It.IsAny<CancellationToken>()))
.ReturnsAsync(MockGetAllProvidersResponse());

_mapper = new EnterNewTrainingProviderViewModelMapper(_mockCommitmentsApiClient.Object);
}

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

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

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

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

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

_mockCommitmentsApiClient.Verify(m => m.GetAllProviders(It.IsAny<CancellationToken>()), Times.Once());
}

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

Assert.AreEqual(3, result.Providers.Count);
}
private GetAllProvidersResponse MockGetAllProvidersResponse()
{
return new GetAllProvidersResponse
{
Providers = new List<Provider>
{
new Provider { Ukprn = 10000001, Name = "Provider 1" },
new Provider { Ukprn = 10000002, Name = "Provider 2" },
new Provider { Ukprn = 10000003, Name = "Provider 3" }
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using NUnit.Framework;
using SFA.DAS.EmployerCommitmentsV2.Web.Mappers.Apprentice;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice;
using SFA.DAS.Testing.AutoFixture;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Mappers.Apprentice
{
public class SendNewTrainingProviderRequestMapperTests
{
private SendNewTrainingProviderRequestMapper _mapper;

[SetUp]
public void Arrange()
{
_mapper = new SendNewTrainingProviderRequestMapper();
}

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

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

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

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

[Test, MoqAutoData]
public async Task Ukprn_IsMapped(EnterNewTrainingProviderViewModel viewModel)
{
var result = await _mapper.Map(viewModel);

Assert.AreEqual(viewModel.Ukprn, result.Ukprn);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="SFA.DAS.CommitmentsV2.Api.Client" Version="4.1.1736" />
<PackageReference Include="SFA.DAS.CommitmentsV2.Shared" Version="4.1.1736" />
<PackageReference Include="SFA.DAS.CommitmentsV2.Api.Client" Version="4.2.1" />
<PackageReference Include="SFA.DAS.CommitmentsV2.Shared" Version="4.2.1" />
<PackageReference Include="SFA.DAS.Testing" Version="3.0.22" />
<PackageReference Include="SFA.DAS.Testing.AutoFixture" Version="3.0.121" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using NUnit.Framework;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice;
using SFA.DAS.EmployerCommitmentsV2.Web.Validators;
using SFA.DAS.Testing.AutoFixture;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Validators
{
public class EnterNewTrainingProviderViewModelValidatorTests
{
private const string ExpectedUkprnErrorMessage = "Select a training provider";
private EnterNewTrainingProviderViewModelValidator _validator;

[SetUp]
public void Arrange()
{
_validator = new EnterNewTrainingProviderViewModelValidator();
}

[Test, MoqAutoData]
public void WhenValidatingNewTrainingProvider_AndAnUkprnIsNotGiven_ThenValidatorReturnsInvalid(EnterNewTrainingProviderViewModel viewModel)
{
viewModel.Ukprn = 0;
var result = _validator.Validate(viewModel);

Assert.False(result.IsValid);
Assert.AreEqual(ExpectedUkprnErrorMessage, result.Errors.First().ErrorMessage);
}

[Test, MoqAutoData]
public void WhenValidatingNewTrainingProvider_AndAnUkprnIsGiven_ThenValidatorReturnsValid(EnterNewTrainingProviderViewModel viewModel)
{
var result = _validator.Validate(viewModel);

Assert.True(result.IsValid);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,46 @@ public async Task<IActionResult> EditEndDate(EditEndDateViewModel viewModel)
public async Task<IActionResult> ChangeProviderInform(ChangeProviderInformRequest request)
{
var viewModel = await _modelMapper.Map<ChangeProviderInformViewModel>(request);

return View(viewModel);
}

// Placeholder for CON-2516 - url not specified yet
[Route("{apprenticeshipHashedId}/details/stopped-error", Name = RouteNames.ApprenticeNotStoppedError)]
[Route("{apprenticeshipHashedId}/change-provider/stopped-error", Name = RouteNames.ApprenticeNotStoppedError)]
[DasAuthorize(EmployerFeature.ChangeOfProvider)]
public IActionResult ApprenticeNotStoppedError()
{
return View();
}

// Placeholder for CON-2505
[Route("{apprenticeshipHashedId}/details/enter-new-training-provider-name-or-reference-number", Name = RouteNames.EnterNewTrainingProvider)]
[HttpGet]
[Route("{apprenticeshipHashedId}/change-provider/enter-new-training-provider-name-or-reference-number", Name = RouteNames.EnterNewTrainingProvider)]
[DasAuthorize(EmployerFeature.ChangeOfProvider)]
public IActionResult EnterNewTrainingProvider()
public async Task<IActionResult> EnterNewTrainingProvider(EnterNewTrainingProviderRequest request)
{
return View();
var viewModel = await _modelMapper.Map<EnterNewTrainingProviderViewModel>(request);

return View(viewModel);
}

[HttpPost]
[Route("{apprenticeshipHashedId}/change-provider/enter-new-training-provider-name-or-reference-number", Name = RouteNames.EnterNewTrainingProvider)]
[DasAuthorize(EmployerFeature.ChangeOfProvider)]
public async Task<IActionResult> EnterNewTrainingProvider(EnterNewTrainingProviderViewModel viewModel)
{
var request = await _modelMapper.Map<SendNewTrainingProviderRequest>(viewModel);

return RedirectToRoute(RouteNames.SendRequestNewTrainingProvider, new { request.AccountHashedId, request.ApprenticeshipHashedId, request.Ukprn });
}

// Placeholder for CON-2506
[Route("{apprenticeshipHashedId}/change-provider/send-request-new-training-provider", Name = RouteNames.SendRequestNewTrainingProvider)]
[DasAuthorize(EmployerFeature.ChangeOfProvider)]
public async Task<IActionResult> SendRequestNewTrainingProvider(SendNewTrainingProviderRequest request)
{
var viewModel = await _modelMapper.Map<SendNewTrainingProviderViewModel>(request);

return View(viewModel);
}

[Route("{apprenticeshipHashedId}/details/changestatus")]
Expand Down
Loading

0 comments on commit ad3ce10

Please sign in to comment.