Skip to content

Commit

Permalink
Merge pull request #217 from SkillsFundingAgency/CON-3345_Migrate_tra…
Browse files Browse the repository at this point in the history
…nsfers_details_to_commitments_v2

CON-3345 Migrated transfer details to v2
  • Loading branch information
ChrisJWoodcock authored Jun 21, 2021
2 parents 73bff47 + 8503f68 commit 24e1fcf
Show file tree
Hide file tree
Showing 31 changed files with 1,168 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using AutoFixture.NUnit3;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Api.Client;
using SFA.DAS.CommitmentsV2.Api.Types.Requests;
using SFA.DAS.CommitmentsV2.Shared.Interfaces;
using SFA.DAS.EmployerCommitmentsV2.Web.Controllers;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.TransferRequest;
using SFA.DAS.Testing.AutoFixture;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.TransferRequestControllerTests
{
public class WhenCallingPostTransferDetailsForSender
{
[Test, MoqAutoData]
public async Task And_TransferDetailsForSender_Succeeds_Then_Redirect_To_Confirmation(
UpdateTransferApprovalForSenderRequest request,
TransferRequestForSenderViewModel viewModel,
[Frozen] Mock<IModelMapper> mockMapper,
TransferRequestController controller)
{
mockMapper
.Setup(mapper => mapper.Map<UpdateTransferApprovalForSenderRequest>(viewModel))
.ReturnsAsync(request);

var result = (await controller.TransferDetailsForSender(viewModel)) as RedirectToActionResult;

result.Should().NotBeNull();
result.ActionName.Should().Be("TransferConfirmation");
result.ControllerName.Should().Be("TransferRequest");
}

[Test, MoqAutoData]
public async Task And_TransferDetailsForSender_Succeeds_Then_Api_Called_To_Update(
Mock<ICommitmentsApiClient> mockCommitmentsApiClient,
Mock<ILogger<TransferRequestController>> mockLogger,
UpdateTransferApprovalForSenderRequest request,
TransferRequestForSenderViewModel viewModel,
[Frozen] Mock<IModelMapper> mockMapper)
{
mockMapper
.Setup(mapper => mapper.Map<UpdateTransferApprovalForSenderRequest>(viewModel))
.ReturnsAsync(request);

mockCommitmentsApiClient
.Setup(r => r.UpdateTransferRequestForSender(
It.IsAny<long>(),
It.IsAny<long>(),
It.IsAny<long>(),
It.IsAny<UpdateTransferApprovalForSenderRequest>(),
It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);

TransferRequestController controller = new TransferRequestController(mockCommitmentsApiClient.Object,
mockLogger.Object, mockMapper.Object);

var result = (await controller.TransferDetailsForSender(viewModel)) as RedirectToActionResult;

mockCommitmentsApiClient.Verify(m => m.UpdateTransferRequestForSender(
It.IsAny<long>(),
It.IsAny<long>(),
It.IsAny<long>(),
It.IsAny<UpdateTransferApprovalForSenderRequest>(),
It.IsAny<CancellationToken>()), Times.Once);
}

[Test, MoqAutoData]
public async Task And_TransferDetailsForSender_Fails_Then_Redirect_To_Error(
TransferRequestForSenderViewModel viewModel,
[Frozen] Mock<IModelMapper> mockMapper,
TransferRequestController controller)
{
mockMapper
.Setup(mapper => mapper.Map<UpdateTransferApprovalForSenderRequest>(viewModel))
.ThrowsAsync(new Exception("Some error"));

var result = (await controller.TransferDetailsForSender(viewModel)) as RedirectToActionResult;

result.Should().NotBeNull();
result.ActionName.Should().Be("Error");
result.ControllerName.Should().Be("Error");
result.RouteValues.Should().BeNull();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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.Models.TransferRequest;
using SFA.DAS.Testing.AutoFixture;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.TransferRequestControllerTests

{
public class WhenCallingTransferDetailsForReceiver
{
[Test, MoqAutoData]
public async Task Then_Returns_View_With_Correct_Model(
TransferRequestRequest request,
TransferRequestForReceiverViewModel viewModel,
[Frozen] Mock<IModelMapper> mockMapper,
TransferRequestController controller)
{
mockMapper
.Setup(mapper => mapper.Map<TransferRequestForReceiverViewModel>(request))
.ReturnsAsync(viewModel);

var result = await controller.TransferDetailsForReceiver(request) as ViewResult;

result.ViewName.Should().BeNull();
var model = result.Model as TransferRequestForReceiverViewModel;
model.Should().BeSameAs(viewModel);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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.Models.TransferRequest;
using SFA.DAS.Testing.AutoFixture;

namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.PaymentOrderControllerTests

{
public class WhenCallingTransferDetailsForSender
{
[Test, MoqAutoData]
public async Task Then_Returns_View_With_Correct_Model(
TransferRequestRequest request,
TransferRequestForSenderViewModel viewModel,
[Frozen] Mock<IModelMapper> mockMapper,
TransferRequestController controller)
{
mockMapper
.Setup(mapper => mapper.Map<TransferRequestForSenderViewModel>(request))
.ReturnsAsync(viewModel);

var result = await controller.TransferDetailsForSender(request) as ViewResult;

result.ViewName.Should().BeNull();
var model = result.Model as TransferRequestForSenderViewModel;
model.Should().BeSameAs(viewModel);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
using AutoFixture;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Api.Client;
using SFA.DAS.CommitmentsV2.Api.Types.Requests;
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.EmployerCommitmentsV2.Web.Mappers.TransferRequest;
using SFA.DAS.EmployerCommitmentsV2.Web.Models.TransferRequest;
using SFA.DAS.Encoding;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

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

private GetTransferRequestResponse _getTransferRequestResponse;
private TransferRequestRequest _request;

private TransferRequestForReceiverViewModelMapper _mapper;

private const long AccountIdFirst = 12;
private const long TransferRequestIdFirst = 34;
private const long ReceivingEmployerAccountIdFirst = 56;

[SetUp]
public void SetUp()
{
//Arrange
var autoFixture = new Fixture();
_request = autoFixture.Build<TransferRequestRequest>()
.With(x => x.AccountHashedId, $"A{AccountIdFirst}")
.With(x => x.TransferRequestHashedId, $"A{TransferRequestIdFirst}")
.Create();

_getTransferRequestResponse = autoFixture.Build<GetTransferRequestResponse>()
.With(x => x.ReceivingEmployerAccountId, ReceivingEmployerAccountIdFirst)
.With(x => x.TransferRequestId, TransferRequestIdFirst)
.Create();

_mockCommitmentsApiClient = new Mock<ICommitmentsApiClient>();
_mockCommitmentsApiClient.Setup(r => r.GetTransferRequestForReceiver(It.IsAny<long>(), It.IsAny<long>(), CancellationToken.None))
.ReturnsAsync(_getTransferRequestResponse);

_mockEncodingService = new Mock<IEncodingService>();

_mockEncodingService.Setup(t => t.Encode(It.IsAny<long>(), EncodingType.AccountId))
.Returns((long value, EncodingType encodingType) => $"A{value}");
_mockEncodingService.Setup(t => t.Encode(It.IsAny<long>(), EncodingType.PublicAccountId))
.Returns((long value, EncodingType encodingType) => $"P{value}");
_mockEncodingService.Setup(t => t.Encode(It.IsAny<long>(), EncodingType.TransferRequestId))
.Returns((long value, EncodingType encodingType) => $"T{value}");
_mockEncodingService.Setup(t => t.Encode(It.IsAny<long>(), EncodingType.CohortReference))
.Returns((long value, EncodingType encodingType) => $"C{value}");
_mockEncodingService.Setup(t => t.Decode(It.IsAny<string>(), It.IsAny<EncodingType>()))
.Returns((string value, EncodingType encodingType) => long.Parse(Regex.Replace(value, "[A-Za-z ]", "")));

_mapper = new TransferRequestForReceiverViewModelMapper(_mockCommitmentsApiClient.Object, _mockEncodingService.Object);
}

[Test]
public async Task GetTransferRequestForReceiverIsCalled()
{
//Act
var result = await _mapper.Map(_request);

//Assert
_mockCommitmentsApiClient.Verify(t => t.GetTransferRequestForReceiver(_request.AccountId, _request.TransferRequestId, It.IsAny<CancellationToken>()), Times.Once());
}

[Test]
public async Task TransferReceiverHashedAccountId_IsMapped()
{
//Act
var result = await _mapper.Map(_request);

//Assert
Assert.AreEqual($"A{_getTransferRequestResponse.ReceivingEmployerAccountId}", result.TransferReceiverHashedAccountId);
}

[Test]
public async Task TransferSenderPublicHashedAccountId_IsMapped()
{
//Act
var result = await _mapper.Map(_request);

//Assert
Assert.AreEqual($"P{_getTransferRequestResponse.SendingEmployerAccountId}", result.TransferSenderPublicHashedAccountId);
}

[Test]
public async Task TransferSenderName_IsMapped()
{
//Act
var result = await _mapper.Map(_request);

//Assert
Assert.AreEqual(_getTransferRequestResponse.TransferSenderName, result.TransferSenderName);
}

[Test]
public async Task HashedCohortReference_IsMapped()
{
//Act
var result = await _mapper.Map(_request);

//Assert
Assert.AreEqual($"C{_getTransferRequestResponse.CommitmentId}", result.HashedCohortReference);
}

[Test]
public async Task TransferApprovalStatusDesc_IsMapped()
{
//Act
var result = await _mapper.Map(_request);

//Assert
Assert.AreEqual(_getTransferRequestResponse.Status.ToString(), result.TransferApprovalStatusDesc);
}

[Test]
public async Task TransferApprovalSetBy_IsMapped()
{
//Act
var result = await _mapper.Map(_request);

//Assert
Assert.AreEqual(_getTransferRequestResponse.ApprovedOrRejectedByUserName, result.TransferApprovalSetBy);
}

[Test]
public async Task TransferApprovalSetOn_IsMapped()
{
//Act
var result = await _mapper.Map(_request);

//Assert
Assert.AreEqual(_getTransferRequestResponse.ApprovedOrRejectedOn, result.TransferApprovalSetOn);
}

[Test]
public async Task TotalCost_IsMapped()
{
//Act
var result = await _mapper.Map(_request);

//Assert
Assert.AreEqual(_getTransferRequestResponse.TransferCost, result.TotalCost);
}

[Test]
public async Task FundingCap_IsMapped()
{
//Act
var result = await _mapper.Map(_request);

//Assert
Assert.AreEqual(_getTransferRequestResponse.FundingCap, result.FundingCap);
}

[TestCase(TransferApprovalStatus.Pending, 1500.0, 12000, true)]
[TestCase(TransferApprovalStatus.Approved, 1500.0, 12000, true)]
[TestCase(TransferApprovalStatus.Pending, 12000.0, 12000, false)]
[TestCase(TransferApprovalStatus.Approved, 12000.0, 12000, false)]
[TestCase(TransferApprovalStatus.Pending, 13000.0, 12000, false)]
[TestCase(TransferApprovalStatus.Approved, 13000.0, 12000, false)]
[TestCase(TransferApprovalStatus.Rejected, 1500.0, 12000, false)]
[TestCase(TransferApprovalStatus.Rejected, 12000.0, 12000, false)]
[TestCase(TransferApprovalStatus.Rejected, 13000.0, 12000, false)]
public async Task TrainingName_IsMapped(TransferApprovalStatus transferApprovalStatus, decimal transferCost, int fundingCap, bool showFundingCapWarning)
{
// Arrange
_getTransferRequestResponse.Status = transferApprovalStatus;
_getTransferRequestResponse.TransferCost = transferCost;
_getTransferRequestResponse.FundingCap = fundingCap;

//Act
var result = await _mapper.Map(_request);

//Assert
Assert.AreEqual(showFundingCapWarning, result.ShowFundingCapWarning);
}
}
}
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="7.27.0" />
<PackageReference Include="SFA.DAS.CommitmentsV2.Shared" Version="7.25.0" />
<PackageReference Include="SFA.DAS.CommitmentsV2.Api.Client" Version="7.35.0" />
<PackageReference Include="SFA.DAS.CommitmentsV2.Shared" Version="7.35.0" />
<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
Expand Up @@ -7,6 +7,7 @@ public static class AuthorizationContextKeys
public const string AccountLegalEntityId = "AccountLegalEntityId";
public const string DraftApprenticeshipId = "DraftApprenticeshipId";
public const string DecodedTransferSenderId = "DecodedTransferSenderId";
public const string TransferRequestId = "TransferRequestId";
public const string ApprenticeshipId = "ApprenticeshipId";
}
}
Loading

0 comments on commit 24e1fcf

Please sign in to comment.