Skip to content

Commit

Permalink
Merge pull request #709 from SkillsFundingAgency/CV-684-LevyNoneLevy-…
Browse files Browse the repository at this point in the history
…flag

Cv 684 levy none levy flag
  • Loading branch information
Najamuddin-Muhammad authored May 4, 2020
2 parents 2832925 + df737ff commit f0db2fd
Show file tree
Hide file tree
Showing 31 changed files with 321 additions and 532 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
namespace SFA.DAS.CommitmentsV2.Api.Types.Responses
using SFA.DAS.CommitmentsV2.Types;

namespace SFA.DAS.CommitmentsV2.Api.Types.Responses
{
public class AccountLegalEntityResponse
{
public long AccountId { get; set; }
public long MaLegalEntityId { get; set; }
public string AccountName { get; set; }
public string LegalEntityName { get; set; }
public ApprenticeshipEmployerType LevyStatus { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace SFA.DAS.CommitmentsV2.Api.Types.Responses
using SFA.DAS.CommitmentsV2.Types;

namespace SFA.DAS.CommitmentsV2.Api.Types.Responses
{
public class AccountResponse
{
public long AccountId { get; set; }
public bool HasCohorts { get; set; }
public bool HasApprenticeships { get; set; }
public ApprenticeshipEmployerType LevyStatus { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public sealed class GetCohortResponse
public bool IsApprovedByEmployer { get; set; }
public bool IsApprovedByProvider { get; set; }
public bool IsCompleteForEmployer { get; set; }
public ApprenticeshipEmployerType LevyStatus { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public void VerifyResult()
var response = (AccountResponse) objectResult.Value;

Assert.AreEqual(MediatorQueryResult.AccountId, response.AccountId);
Assert.AreEqual(MediatorQueryResult.HasApprenticeships, response.HasApprenticeships);
Assert.AreEqual(MediatorQueryResult.HasCohorts, response.HasCohorts);
Assert.AreEqual(MediatorQueryResult.LevyStatus, response.LevyStatus);
}

public void VerifyApprovedProviderResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using SFA.DAS.CommitmentsV2.Api.Controllers;
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.CommitmentsV2.Application.Queries.GetAccountLegalEntity;
using SFA.DAS.CommitmentsV2.Types;

namespace SFA.DAS.CommitmentsV2.Api.UnitTests.Controllers
{
Expand Down Expand Up @@ -41,7 +42,7 @@ public async Task GetAccountLegalEntity_WithValidModelAndExistingId_ShouldResult

// arrange
var fixtures = new AccountLegalEntityControllerTestFixtures()
.SetQueryResponse(accountLegalEntityId, new GetAccountLegalEntityQueryResult { AccountId = 1, MaLegalEntityId = 234, AccountName = "AccountName", LegalEntityName = "ABC" });
.SetQueryResponse(accountLegalEntityId, new GetAccountLegalEntityQueryResult { AccountId = 1, MaLegalEntityId = 234, AccountName = "AccountName", LegalEntityName = "ABC", LevyStatus = ApprenticeshipEmployerType.Levy });

// act
var response = await fixtures.CallControllerMethod(accountLegalEntityId);
Expand All @@ -55,6 +56,7 @@ public async Task GetAccountLegalEntity_WithValidModelAndExistingId_ShouldResult
Assert.AreEqual(234, model.MaLegalEntityId);
Assert.AreEqual("AccountName", model.AccountName);
Assert.AreEqual("ABC", model.LegalEntityName);
Assert.AreEqual(ApprenticeshipEmployerType.Levy, model.LevyStatus);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ await TestAsync(
v.LatestMessageCreatedByEmployer == f.GetCohortResult.LatestMessageCreatedByEmployer &&
v.LatestMessageCreatedByProvider == f.GetCohortResult.LatestMessageCreatedByProvider &&
v.IsApprovedByEmployer == f.GetCohortResult.IsApprovedByEmployer &&
v.IsApprovedByProvider == f.GetCohortResult.IsApprovedByProvider));
v.IsApprovedByProvider == f.GetCohortResult.IsApprovedByProvider &&
v.LevyStatus == f.GetCohortResult.LevyStatus));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.CommitmentsV2.Application.Queries.GetAccountSummary;
using SFA.DAS.CommitmentsV2.Application.Queries.GetApprovedProviders;
using SFA.DAS.CommitmentsV2.Types;

namespace SFA.DAS.CommitmentsV2.Api.Controllers
{
Expand Down Expand Up @@ -32,8 +33,7 @@ public async Task<IActionResult> GetAccount(long accountId)
return Ok(new AccountResponse
{
AccountId = employer.AccountId,
HasApprenticeships = employer.HasApprenticeships,
HasCohorts = employer.HasCohorts
LevyStatus = employer.LevyStatus
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using SFA.DAS.CommitmentsV2.Api.Extensions;
using SFA.DAS.CommitmentsV2.Api.Types.Responses;
using SFA.DAS.CommitmentsV2.Application.Queries.GetAccountLegalEntity;
using SFA.DAS.CommitmentsV2.Types;

namespace SFA.DAS.CommitmentsV2.Api.Controllers
{
Expand Down Expand Up @@ -51,7 +52,8 @@ public async Task<IActionResult> GetAccountLegalEntity(long accountLegalEntityId
AccountId = employer.AccountId,
MaLegalEntityId = employer.MaLegalEntityId,
AccountName = employer.AccountName,
LegalEntityName = employer.LegalEntityName
LegalEntityName = employer.LegalEntityName,
LevyStatus = employer.LevyStatus
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public async Task<IActionResult> Get(long cohortId)
LatestMessageCreatedByProvider = result.LatestMessageCreatedByProvider,
IsApprovedByEmployer = result.IsApprovedByEmployer,
IsApprovedByProvider = result.IsApprovedByProvider,
IsCompleteForEmployer = result.IsCompleteForEmployer
IsCompleteForEmployer = result.IsCompleteForEmployer,
LevyStatus = result.LevyStatus
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
<PackageReference Include="SFA.DAS.Configuration.AzureTableStorage" Version="3.0.77" />
<PackageReference Include="SFA.DAS.EmployerAccounts.Messages" Version="1.3.2504" />
<PackageReference Include="SFA.DAS.EmployerFinance.Messages" Version="1.6.2887" />
<PackageReference Include="SFA.DAS.Payments.ProviderPayments.Messages" Version="0.0.69" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.EmployerAccounts.Messages.Events;
using SFA.DAS.EmployerAccounts.Types.Models;
using SFA.DAS.EmployerFinance.Messages.Events;
using SFA.DAS.Payments.ProviderPayments.Messages;

namespace SFA.DAS.CommitmentsV2.MessageHandlers.TestHarness
Expand Down Expand Up @@ -51,6 +52,7 @@ public async Task Run()
Console.WriteLine("O - CohortDeletedEvent");
Console.WriteLine("P - ApproveTransferRequestCommand");
Console.WriteLine("Q - RejectTransferRequestCommand");
Console.WriteLine("S - LevyAddedToAccount");
Console.WriteLine("X - Exit");
Console.WriteLine("Press [Key] for Test Option");
key = Console.ReadKey().Key;
Expand Down Expand Up @@ -147,6 +149,11 @@ await _publisher.Publish(new AddedLegalEntityEvent { AccountId = accountId, Crea
Console.WriteLine();
Console.WriteLine($"Sent {nameof(RejectTransferRequestCommand)}");
break;
case ConsoleKey.S:
await _publisher.Publish(new LevyAddedToAccount { AccountId = accountId, Amount = 10, Created = DateTime.UtcNow });
Console.WriteLine();
Console.WriteLine($"Sent {nameof(LevyAddedToAccount)}");
break;
}
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using MediatR;
using Microsoft.Extensions.Logging;
using Moq;
using NServiceBus;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Application.Commands.UpdateLevyStatusToLevy;
using SFA.DAS.CommitmentsV2.MessageHandlers.EventHandlers;
using SFA.DAS.EmployerFinance.Messages.Events;

namespace SFA.DAS.CommitmentsV2.MessageHandlers.UnitTests.EventHandlers
{
[TestFixture]
[Parallelizable(ParallelScope.All)]
public class LevyAddedToAccountEventHandlerTests
{
[Test]
public async Task Handle_WhenLevyAddedToAccountIsRaised_LevyStatusIsSetToLevy()
{
var f = new LevyAddedToAccountEventHandlerTestsFixture();
await f.Handle();
f.VerifyUpdateLevyStatusToLevyCommandSent();
}
}

public class LevyAddedToAccountEventHandlerTestsFixture
{
public Mock<IMediator> Mediator { get; set; }
public LevyAddedToAccountEventHandler Sut;
public LevyAddedToAccount LevyAddedToAccount;

public LevyAddedToAccountEventHandlerTestsFixture()
{
var autoFixture = new Fixture();
Mediator = new Mock<IMediator>();
LevyAddedToAccount = autoFixture.Create<LevyAddedToAccount>();

Sut = new LevyAddedToAccountEventHandler(Mediator.Object, Mock.Of<ILogger<LevyAddedToAccountEventHandler>>());
}

public Task Handle()
{
return Sut.Handle(LevyAddedToAccount, Mock.Of<IMessageHandlerContext>());
}

public void VerifyUpdateLevyStatusToLevyCommandSent()
{
Mediator.Verify(x =>
x.Send(It.Is<UpdateLevyStatusToLevyCommand>(p => p.AccountId == LevyAddedToAccount.AccountId),
It.IsAny<CancellationToken>()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using MediatR;
using Microsoft.Extensions.Logging;
using NServiceBus;
using SFA.DAS.CommitmentsV2.Application.Commands.UpdateLevyStatusToLevy;
using SFA.DAS.EmployerFinance.Messages.Events;
using System.Threading.Tasks;

namespace SFA.DAS.CommitmentsV2.MessageHandlers.EventHandlers
{
public class LevyAddedToAccountEventHandler : IHandleMessages<LevyAddedToAccount>
{
private readonly IMediator _mediator;
private readonly ILogger<LevyAddedToAccountEventHandler> _logger;

public LevyAddedToAccountEventHandler(IMediator mediator, ILogger<LevyAddedToAccountEventHandler> logger)
{
_mediator = mediator;
_logger = logger;
}

public async Task Handle(LevyAddedToAccount message, IMessageHandlerContext context)
{
_logger.LogInformation($"LevyAddedToAccount event received for Account {message.AccountId}");
await _mediator.Send(new UpdateLevyStatusToLevyCommand { AccountId = message.AccountId });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<PackageReference Include="SFA.DAS.Account.Api.Client" Version="1.6.2276" />
<PackageReference Include="SFA.DAS.Configuration.AzureTableStorage" Version="3.0.77" />
<PackageReference Include="SFA.DAS.EmployerAccounts.Messages" Version="1.3.2504" />
<PackageReference Include="SFA.DAS.EmployerFinance.Messages" Version="1.6.2887" />
<PackageReference Include="SFA.DAS.NLog.Targets.Redis" Version="1.2.1" />
<PackageReference Include="SFA.DAS.Notifications.Messages" Version="3.0.0" />
<PackageReference Include="SFA.DAS.Payments.ProviderPayments.Messages" Version="0.0.69" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Application.Commands.UpdateLevyStatusToLevy;
using SFA.DAS.CommitmentsV2.Data;
using SFA.DAS.CommitmentsV2.Models;
using SFA.DAS.CommitmentsV2.Types;

namespace SFA.DAS.CommitmentsV2.UnitTests.Application.Commands
{
[TestFixture]
[Parallelizable(ParallelScope.None)]
public class UpdateLevyStatusToLevyCommandHandlerTests
{
[Test]
public void Handle_WhenHandlingCommand_ThenShouldUpdateTheLevyStatus()
{
var f = new UpdateLevyStatusToLevyCommandHandlerTestsFixture();
f.SetAccount()
.Handle();

Assert.IsTrue(f.IsValid(ApprenticeshipEmployerType.Levy));
}

[Test]
public void Handle_WhenHandlingCommand_AndAccountNotFound_ThenShouldnotUpdateTheLevyStatus()
{
var f = new UpdateLevyStatusToLevyCommandHandlerTestsFixture();
f.SetAccount();
f.Command.AccountId = 2;
f.Handle();

Assert.IsTrue(f.IsValid(ApprenticeshipEmployerType.NonLevy));
}
}

public class UpdateLevyStatusToLevyCommandHandlerTestsFixture
{
public IFixture AutoFixture { get; set; }
public UpdateLevyStatusToLevyCommand Command { get; set; }
public Mock<ProviderCommitmentsDbContext> Db { get; set; }
public IRequestHandler<UpdateLevyStatusToLevyCommand> Handler { get; set; }
public long AccountId { get; set; }

public UpdateLevyStatusToLevyCommandHandlerTestsFixture()
{
AutoFixture = new Fixture();
AccountId = 1;
Command = new UpdateLevyStatusToLevyCommand { AccountId = AccountId };
Db = new Mock<ProviderCommitmentsDbContext>(new DbContextOptionsBuilder<ProviderCommitmentsDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options) { CallBase = true };
Handler = new UpdateLevyStatusToLevyCommandHandler(new Lazy<ProviderCommitmentsDbContext>(() => Db.Object), Mock.Of<ILogger<UpdateLevyStatusToLevyCommandHandler>>());

AutoFixture.Behaviors.Add(new OmitOnRecursionBehavior());
}

public Task Handle()
{
return Handler.Handle(Command, CancellationToken.None);
}

public UpdateLevyStatusToLevyCommandHandlerTestsFixture SetAccount()
{
var account = new Account(AccountId, "", "", "", DateTime.UtcNow);

Db.Object.Accounts.Add(account);
Db.Object.SaveChanges();

return this;
}

public bool IsValid(ApprenticeshipEmployerType levyStatus)
{
return levyStatus == Db.Object.Accounts.First().LevyStatus;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,30 @@
using SFA.DAS.CommitmentsV2.Application.Queries.GetAccountLegalEntity;
using SFA.DAS.CommitmentsV2.Data;
using SFA.DAS.CommitmentsV2.Models;
using SFA.DAS.CommitmentsV2.Types;

namespace SFA.DAS.CommitmentsV2.UnitTests.Application.Queries.GetAccountLegalEntity
{
[TestFixture]
public class GetAccountLegalEntityHandlerTests
{
[Test]
public async Task Handle_WithSpecifiedId_ShouldSetIsValidCorrectly()
[TestCase(123,456,987, ApprenticeshipEmployerType.Levy)]
[TestCase(1234, 4567, 9870, ApprenticeshipEmployerType.NonLevy)]
public async Task Handle_WithSpecifiedId_ShouldSetIsValidCorrectly(long accountId, long accountLegalEntityId, long maLegalEntityId, ApprenticeshipEmployerType levyStatus)
{
const long accountId = 123;
const long accountLegalEntityId = 456;
const long maLegalEntityId = 987;

// arrange
var fixtures = new GetEmployerHandlerTestFixtures()
.AddAccountWithLegalEntities(accountId, "Account123", accountLegalEntityId, maLegalEntityId, "LegalEntity456");
.AddAccountWithLegalEntities(accountId, "Account123", accountLegalEntityId, maLegalEntityId, "LegalEntity456", levyStatus);

// act
var response = await fixtures.GetResponse(new GetAccountLegalEntityQuery {AccountLegalEntityId = accountLegalEntityId });

// Assert
Assert.IsNotNull(response);
Assert.AreEqual(response.AccountId, accountId);
Assert.AreEqual(response.MaLegalEntityId, maLegalEntityId);
Assert.AreEqual(response.AccountName, "Account123");
Assert.AreEqual(accountId, response.AccountId);
Assert.AreEqual(maLegalEntityId, response.MaLegalEntityId);
Assert.AreEqual("Account123", response.AccountName);
Assert.AreEqual(levyStatus, response.LevyStatus);
}
}

Expand All @@ -62,9 +61,9 @@ public GetEmployerHandlerTestFixtures()
public List<Account> SeedAccounts { get; }

public GetEmployerHandlerTestFixtures AddAccountWithLegalEntities(long accountId, string accountName,
long accountLegalEntityId, long maLegalEntityId, string name)
long accountLegalEntityId, long maLegalEntityId, string name, ApprenticeshipEmployerType levyStatus)
{
var account = new Account(accountId, "PRI123", "PUB123", accountName, DateTime.Now);
var account = new Account(accountId, "PRI123", "PUB123", accountName, DateTime.Now) { LevyStatus = levyStatus };

account.AddAccountLegalEntity(accountLegalEntityId, maLegalEntityId, "ABC456", "PUB456",
name, OrganisationType.Charities, "My address", DateTime.Now);
Expand Down
Loading

0 comments on commit f0db2fd

Please sign in to comment.