Skip to content

Commit

Permalink
Merge pull request #666 from SkillsFundingAgency/CV-405-CV-510-CV-511…
Browse files Browse the repository at this point in the history
…-Notifications-Provider-Send-Approve-TransferSender-Approve-Reject

Cv 405 cv 510 cv 511 notifications provider send approve transfer sender approve reject
  • Loading branch information
Najamuddin-Muhammad authored Feb 14, 2020
2 parents c59891c + 1e6f97e commit 772e65b
Show file tree
Hide file tree
Showing 105 changed files with 3,518 additions and 1,099 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ApproveTestsFixture()
Controller = new CohortController(Mediator.Object);
Request = AutoFixture.Create<ApproveCohortRequest>();

Mediator.Setup(m => m.Send(It.Is<ApproveCohortCommand>(c =>
Mediator.Setup(m => m.Send(It.Is<ApproveCohortCommand>(c =>
c.CohortId == CohortId &&
c.Message == Request.Message &&
c.UserInfo == Request.UserInfo), CancellationToken.None))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CreateEmptyCohortTests : FluentTest<CreateEmptyCohortTestsFixture>
public async Task WhenPostRequestReceived_ThenShouldReturnResponse()
{
var fixture = new CreateEmptyCohortTestsFixture();
var result= await fixture.Create();
var result = await fixture.Create();

fixture.VerifyResponse(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public CreateTestsFixture()
Controller = new CohortController(Mediator.Object);
Request = AutoFixture.Create<CreateCohortRequest>();
Result = AutoFixture.Create<AddCohortResult>();

Mediator
.Setup(m => m.Send(It.Is<AddCohortCommand>(c =>
c.AccountId == Request.AccountId &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public CreateWithOtherPartyTestsFixture()
Controller = new CohortController(Mediator.Object);
Request = AutoFixture.Create<CreateCohortWithOtherPartyRequest>();
Result = AutoFixture.Create<AddCohortResult>();

Mediator
.Setup(m => m.Send(It.Is<AddCohortWithOtherPartyCommand>(c =>
c.AccountId == Request.AccountId &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class GetTestsFixture
public GetCohortSummaryQueryResult GetCohortResult { get; }
public GetCohortsRequest GetCohortsRequest { get; }
public GetCohortsResult GetCohortsResult { get; }

public long AccountId = 1;
private const long CohortId = 123;

Expand All @@ -98,7 +98,7 @@ public GetTestsFixture()
GetCohortResult = AutoFixture.Create<GetCohortSummaryQueryResult>();
Mediator.Setup(m => m.Send(It.Is<GetCohortSummaryQuery>(q => q.CohortId == CohortId), CancellationToken.None)).ReturnsAsync(GetCohortResult);

GetCohortsRequest = AutoFixture.Build<GetCohortsRequest>().With(x=>x.AccountId, AccountId).Create();
GetCohortsRequest = AutoFixture.Build<GetCohortsRequest>().With(x => x.AccountId, AccountId).Create();
GetCohortsResult = AutoFixture.Create<GetCohortsResult>();
Mediator.Setup(m => m.Send(It.Is<GetCohortsQuery>(q => q.AccountId == AccountId), CancellationToken.None)).ReturnsAsync(GetCohortsResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public class SendTestsFixture
public SendCohortRequest Request { get; set; }

private const long CohortId = 123;

public SendTestsFixture()
{
AutoFixture = new Fixture();
Mediator = new Mock<IMediator>();
Controller = new CohortController(Mediator.Object);
Request = AutoFixture.Create<SendCohortRequest>();

Mediator.Setup(m => m.Send(It.Is<SendCohortCommand>(c =>
Mediator.Setup(m => m.Send(It.Is<SendCohortCommand>(c =>
c.CohortId == CohortId &&
c.Message == Request.Message &&
c.UserInfo == Request.UserInfo), CancellationToken.None))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using SFA.DAS.CommitmentsV2.Application.Commands.SendCohort;
using SFA.DAS.CommitmentsV2.Application.Queries.GetCohorts;
using SFA.DAS.CommitmentsV2.Application.Queries.GetCohortSummary;
using SFA.DAS.CommitmentsV2.Authentication;
using SFA.DAS.CommitmentsV2.Mapping;
using SFA.DAS.CommitmentsV2.Types;


Expand All @@ -22,7 +24,7 @@ namespace SFA.DAS.CommitmentsV2.Api.Controllers
public class CohortController : ControllerBase
{
private readonly IMediator _mediator;

public CohortController(IMediator mediator)
{
_mediator = mediator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.Extensions.Options;
using NServiceBus;
using SFA.DAS.CommitmentsV2.Configuration;
using SFA.DAS.CommitmentsV2.Extensions;
using SFA.DAS.CommitmentsV2.Messages.Commands;
using SFA.DAS.Configuration;
using SFA.DAS.Configuration.AzureTableStorage;
using SFA.DAS.NServiceBus.Configuration;
Expand Down Expand Up @@ -37,11 +39,11 @@ public static async Task Main()

if (isDevelopment)
{
endpointConfiguration.UseLearningTransport();
endpointConfiguration.UseLearningTransport(s => s.AddRouting());
}
else
{
endpointConfiguration.UseAzureServiceBusTransport(config.ServiceBusConnectionString);
endpointConfiguration.UseAzureServiceBusTransport(config.ServiceBusConnectionString, s => s.AddRouting());
}

var endpoint = await Endpoint.Start(endpointConfiguration);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NServiceBus;
using SFA.DAS.CommitmentsV2.Messages.Commands;
using SFA.DAS.CommitmentsV2.Messages.Events;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.EmployerAccounts.Messages.Events;
Expand All @@ -22,6 +24,7 @@ public async Task Run()
long accountId = 1001;
long accountLegalEntityId = 2061;
long cohortId = 186091;
UserInfo userInfo = new UserInfo { UserDisplayName = "Paul Graham", UserEmail = "paul.graham@test.com", UserId = "PG"};

ConsoleKey key = ConsoleKey.Escape;

Expand All @@ -39,9 +42,13 @@ public async Task Run()
Console.WriteLine("G - BulkUploadIntoCohortCompletedEvent");
Console.WriteLine("H - CohortAssignedToProviderEvent");
Console.WriteLine("I - CohortTransferApprovalRequestedEvent");
Console.WriteLine("M - ApprovedCohortReturnedToProviderEvent");
Console.WriteLine("N - CohortApprovedByEmployer");
Console.WriteLine("J - ApprovedCohortReturnedToProviderEvent");
Console.WriteLine("K - CohortApprovedByEmployer");
Console.WriteLine("L - SendEmailToEmployerCommand");
Console.WriteLine("M - RunHealthCheckCommand");
Console.WriteLine("O - CohortDeletedEvent");
Console.WriteLine("P - ApproveTransferRequestCommand");
Console.WriteLine("Q - RejectTransferRequestCommand");
Console.WriteLine("X - Exit");
Console.WriteLine("Press [Key] for Test Option");
key = Console.ReadKey().Key;
Expand Down Expand Up @@ -99,21 +106,41 @@ await _publisher.Publish(new AddedLegalEntityEvent { AccountId = accountId, Crea
Console.WriteLine();
Console.WriteLine($"Published {nameof(CohortTransferApprovalRequestedEvent)}");
break;
case ConsoleKey.M:
case ConsoleKey.J:
await _publisher.Publish(new ApprovedCohortReturnedToProviderEvent(cohortId, DateTime.Now));
Console.WriteLine();
Console.WriteLine($"Published {nameof(ApprovedCohortReturnedToProviderEvent)}");
break;
case ConsoleKey.N:
case ConsoleKey.K:
await _publisher.Publish(new CohortApprovedByEmployerEvent(cohortId, DateTime.Now));
Console.WriteLine();
Console.WriteLine($"Published {nameof(CohortApprovedByEmployerEvent)}");
break;
case ConsoleKey.L:
await _publisher.Send(new SendEmailToEmployerCommand(10003, "ABCDE", new Dictionary<string, string>(), "Test@test.com"), new SendOptions());
Console.WriteLine();
Console.WriteLine($"Sent {nameof(SendEmailToEmployerCommand)}");
break;
case ConsoleKey.M:
await _publisher.Send(new RunHealthCheckCommand(), new SendOptions());
Console.WriteLine();
Console.WriteLine($"Sent {nameof(RunHealthCheckCommand)}");
break;
case ConsoleKey.O:
await _publisher.Publish(new CohortDeletedEvent(cohortId, 22222, 33333, Party.None, DateTime.Now));
Console.WriteLine();
Console.WriteLine($"Published {nameof(CohortDeletedEvent)}");
break;
case ConsoleKey.P:
await _publisher.Send(new ApproveTransferRequestCommand(10004, DateTime.UtcNow, userInfo), new SendOptions());
Console.WriteLine();
Console.WriteLine($"Sent {nameof(ApproveTransferRequestCommand)}");
break;
case ConsoleKey.Q:
await _publisher.Send(new RejectTransferRequestCommand(10004, DateTime.UtcNow, userInfo), new SendOptions());
Console.WriteLine();
Console.WriteLine($"Sent {nameof(RejectTransferRequestCommand)}");
break;
}
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using AutoFixture;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Moq;
using NServiceBus;
using NUnit.Framework;
using SFA.DAS.CommitmentsV2.Data;
using SFA.DAS.CommitmentsV2.Domain.Entities;
using SFA.DAS.CommitmentsV2.MessageHandlers.CommandHandlers;
using SFA.DAS.CommitmentsV2.Messages.Commands;
using SFA.DAS.CommitmentsV2.Messages.Events;
using SFA.DAS.CommitmentsV2.Models;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.UnitOfWork.Context;

namespace SFA.DAS.CommitmentsV2.MessageHandlers.UnitTests.CommandHandlers
{
[TestFixture]
[Parallelizable(ParallelScope.All)]
public class ApproveTransferRequestCommandHandlerTests
{
[Test]
public void Handle_WhenHandlingTransferSenderApproveCohortCommand_ThenItShouldUpdateCohortAndTransferRequestWithApprovalAction()
{
var fixture = new ApproveTransferRequestCommandHandlerTestsFixture();
fixture.SetupTransfer().SetupTransferSenderApproveCohortCommand();

fixture.Handle();

fixture.VerifyTransferRequestApprovalPropertiesAreSet();
}

[Test]
public void Handle_WhenHandlingTransferSenderApproveCohortCommand_ThenItShouldPublishTransferRequestApprovedEvent()
{
var fixture = new ApproveTransferRequestCommandHandlerTestsFixture();
fixture.SetupTransfer().SetupTransferSenderApproveCohortCommand();

fixture.Handle();

fixture.VerifyTransferRequestApprovedEventIsPublished();
}

[Test]
public void Handle_WhenHandlingTransferSenderApproveCohortCommand_ThenItShouldPublishChangeTrackingEvents()
{
var fixture = new ApproveTransferRequestCommandHandlerTestsFixture();
fixture.SetupTransfer().SetupTransferSenderApproveCohortCommand();

fixture.Handle();

fixture.VerifyEntityIsBeingTracked();
}

[Test]
public void Handle_WhenHandlingTransferSenderApproveCohortCommandFails_ThenItShouldAnExceptionAndLogIt()
{
var fixture = new ApproveTransferRequestCommandHandlerTestsFixture();
fixture.SetupTransfer().SetupTransferSenderApproveCohortCommand(-1991);

fixture.Handle();

fixture.VerifyHasError();
}
}

public class ApproveTransferRequestCommandHandlerTestsFixture
{
public Mock<IMessageHandlerContext> MessageHandlerContext { get; set; }
public DraftApprenticeship ExistingApprenticeshipDetails { get; set; }
public UserInfo TransferSenderUserInfo { get; set; }
public DateTime Now { get; }
public ApproveTransferRequestCommandHandler Sut { get; set; }
public ApproveTransferRequestCommand TransferSenderApproveCohortCommand;
public ProviderCommitmentsDbContext Db { get; set; }
public Cohort Cohort { get; set; }
public TransferRequest TransferRequest { get; set; }
public UnitOfWorkContext UnitOfWorkContext { get; set; }
public Fixture Fixture { get; set; }
public FakeLogger<ApproveTransferRequestCommandHandler> Logger { get; set; }

public ApproveTransferRequestCommandHandlerTestsFixture()
{
UnitOfWorkContext = new UnitOfWorkContext();
MessageHandlerContext = new Mock<IMessageHandlerContext>();
Fixture = new Fixture();
Now = DateTime.UtcNow;

Db = new ProviderCommitmentsDbContext(new DbContextOptionsBuilder<ProviderCommitmentsDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.ConfigureWarnings(w => w.Throw(RelationalEventId.QueryClientEvaluationWarning))
.Options);
Logger = new FakeLogger<ApproveTransferRequestCommandHandler>();
Sut = new ApproveTransferRequestCommandHandler(new Lazy<ProviderCommitmentsDbContext>(() => Db), Logger);

Cohort = new Cohort(
new Provider(),
new AccountLegalEntity(),
null,
Party.Employer,
"",
new UserInfo()) {EmployerAccountId = 100, TransferSenderId = 99};

ExistingApprenticeshipDetails = new DraftApprenticeship(Fixture.Build<DraftApprenticeshipDetails>().Create(), Party.Provider);
Cohort.Apprenticeships.Add(ExistingApprenticeshipDetails);

Cohort.EditStatus = EditStatus.Both;
Cohort.TransferApprovalStatus = TransferApprovalStatus.Pending;
Cohort.TransferSenderId = 10900;

TransferSenderUserInfo = Fixture.Create<UserInfo>();
TransferRequest = new TransferRequest
{ Status = TransferApprovalStatus.Pending, Cost = 1000, Cohort = Cohort};
}

public ApproveTransferRequestCommandHandlerTestsFixture SetupTransferSenderApproveCohortCommand(long transferRequestId = 0)
{
if (transferRequestId == 0)
{
transferRequestId = TransferRequest.Id;
}

TransferSenderApproveCohortCommand = new ApproveTransferRequestCommand(transferRequestId, Now, TransferSenderUserInfo);

return this;
}

public ApproveTransferRequestCommandHandlerTestsFixture SetupTransfer()
{
Db.TransferRequests.Add(TransferRequest);
Db.SaveChanges();

return this;
}

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

public void VerifyTransferRequestApprovalPropertiesAreSet()
{
Assert.AreEqual(TransferRequest.Status, TransferApprovalStatus.Approved);
Assert.AreEqual(TransferRequest.TransferApprovalActionedOn, TransferSenderApproveCohortCommand.ApprovedOn);
Assert.AreEqual(TransferRequest.TransferApprovalActionedByEmployerName, TransferSenderUserInfo.UserDisplayName);
Assert.AreEqual(TransferRequest.TransferApprovalActionedByEmployerEmail, TransferSenderUserInfo.UserEmail);
}

public void VerifyHasError()
{
Assert.IsTrue(Logger.HasErrors);
}

public void VerifyTransferRequestApprovedEventIsPublished()
{
var list = UnitOfWorkContext.GetEvents().OfType<TransferRequestApprovedEvent>().ToList();

Assert.AreEqual(1,list.Count);
Assert.AreEqual(Cohort.Id, list[0].CohortId);
Assert.AreEqual(TransferRequest.Id, list[0].TransferRequestId);
Assert.AreEqual(TransferSenderUserInfo, list[0].UserInfo);
Assert.AreEqual(Now, list[0].ApprovedOn);
}

public void VerifyEntityIsBeingTracked()
{
var list = UnitOfWorkContext.GetEvents().OfType<EntityStateChangedEvent>().Where(x=>x.StateChangeType == UserAction.ApproveTransferRequest).ToList();

Assert.AreEqual(1, list.Count);

Assert.AreEqual(UserAction.ApproveTransferRequest, list[0].StateChangeType);
Assert.AreEqual(TransferRequest.Id, list[0].EntityId);
Assert.AreEqual(TransferSenderUserInfo.UserId, list[0].UpdatingUserId);
Assert.AreEqual(TransferSenderUserInfo.UserDisplayName, list[0].UpdatingUserName);
Assert.AreEqual(Party.TransferSender, list[0].UpdatingParty);
}
}
}
Loading

0 comments on commit 772e65b

Please sign in to comment.