public async Task Handle_WhenHandlingCommand_StoppingApprenticeship_CreatesAddHistoyEvent() { // Arrange var apprenticeship = await SetupApprenticeship(); var stopDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1); var command = new StopApprenticeshipCommand(apprenticeship.Cohort.EmployerAccountId, apprenticeship.Id, stopDate, false, new UserInfo()); // Act await _handler.Handle(command, new CancellationToken()); // Simulate Unit of Work contex transaction ending in http request. await _dbContext.SaveChangesAsync(); // Assert var historyEvent = _unitOfWorkContext.GetEvents().OfType <EntityStateChangedEvent>().First(e => e.EntityId == apprenticeship.Id); historyEvent.EntityType.Should().Be("Apprenticeship"); historyEvent.StateChangeType.Should().Be(UserAction.StopApprenticeship); var definition = new { StopDate = DateTime.MinValue, MadeRedundant = true, PaymentStatus = PaymentStatus.Active }; var historyState = JsonConvert.DeserializeAnonymousType(historyEvent.UpdatedState, definition); historyState.StopDate.Should().Be(stopDate); historyState.MadeRedundant.Should().Be(false); historyState.PaymentStatus.Should().Be(PaymentStatus.Withdrawn); }
public async Task Handle_WhenHandlingCommand_WithInvalidData_ThenShouldThrowException() { // Arrange var command = new StopApprenticeshipCommand(0, 0, DateTime.MinValue, false, new UserInfo()); StopApprenticeshipCommandValidator sut = new StopApprenticeshipCommandValidator(); // Act var result = await sut.ValidateAsync(command, new CancellationToken()); // Assert result.Errors.Should().SatisfyRespectively( first => { first.PropertyName.Should().Be("AccountId"); first.ErrorMessage.Should().Be("The Account Id must be positive"); }, second => { second.PropertyName.Should().Be("ApprenticeshipId"); second.ErrorMessage.Should().Be("The ApprenticeshipId must be positive"); }, third => { third.PropertyName.Should().Be("UserInfo"); third.ErrorMessage.Should().Be("The User Info supplied must not be null and contain a UserId"); }, fourth => { fourth.PropertyName.Should().Be("StopDate"); fourth.ErrorMessage.Should().Be("The StopDate must be supplied"); }); }
public async Task Handle_WhenHandlingCommand_StoppingApprenticeship_ThenShouldSendProviderEmail(string hashedAppId) { // Arrange var apprenticeship = await SetupApprenticeship(); var fixture = new Fixture(); apprenticeship.Cohort.ProviderId = fixture.Create <long>(); _encodingService.Setup(a => a.Encode(apprenticeship.Id, EncodingType.ApprenticeshipId)).Returns(hashedAppId); var stopDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1); var templateName = "ProviderApprenticeshipStopNotification"; var tokenUrl = $"{apprenticeship.Cohort.ProviderId}/apprentices/manage/{hashedAppId}/details"; var tokens = new Dictionary <string, string> { { "EMPLOYER", apprenticeship.Cohort.AccountLegalEntity.Name }, { "APPRENTICE", apprenticeship.ApprenticeName }, { "DATE", stopDate.ToString("dd/MM/yyyy") }, { "URL", tokenUrl }, }; var command = new StopApprenticeshipCommand(apprenticeship.Cohort.EmployerAccountId, apprenticeship.Id, stopDate, false, new UserInfo()); // Act await _handler.Handle(command, new CancellationToken()); // Assert _nserviceBusContext.Verify(s => s.Send(It.Is <SendEmailToProviderCommand>(x => x.ProviderId == apprenticeship.Cohort.ProviderId && x.Template == templateName && VerifyTokens(x.Tokens, tokens)), It.IsAny <SendOptions>())); }
public void Setup() { _validator = new ApprenticeshipStatusChangeCommandValidator(); _exampleCommand = new StopApprenticeshipCommand { AccountId = 1L, ApprenticeshipId = 444L }; }
public async Task Handle_WhenHandlingCommand_WithApprenticeshipWaitingToStart_WithStopDateNotEqualStartDate_ThenShouldThrowDomainException() { // Arrange var apprenticeship = await SetupApprenticeship(startDate : DateTime.UtcNow.AddMonths(2)); var command = new StopApprenticeshipCommand(apprenticeship.Cohort.EmployerAccountId, apprenticeship.Id, DateTime.UtcNow, false, new UserInfo()); // Act var exception = Assert.ThrowsAsync <DomainException>(async() => await _handler.Handle(command, new CancellationToken())); // Assert exception.DomainErrors.Should().BeEquivalentTo(new { PropertyName = "stopDate", ErrorMessage = $"Invalid stop date. Date should be value of start date if training has not started." }); }
public async Task Handle_WhenHandlingCommand_WithInvalidApprenticeshipForStop_PaymentStatusCompleted_ThenShouldThrowDomainException(PaymentStatus paymentStatus) { // Arrange var apprenticeship = await SetupApprenticeship(paymentStatus : paymentStatus); var command = new StopApprenticeshipCommand(apprenticeship.Cohort.EmployerAccountId, apprenticeship.Id, DateTime.UtcNow, false, new UserInfo()); // Act var exception = Assert.ThrowsAsync <DomainException>(async() => await _handler.Handle(command, new CancellationToken())); // Assert exception.DomainErrors.Should().BeEquivalentTo(new { PropertyName = "PaymentStatus", ErrorMessage = "Apprenticeship must be Active or Paused. Unable to stop apprenticeship" }); }
public async Task Handle_WhenHandlingCommand_WhenValidatingApprenticeship_WithStopDateInPast_ThenShouldThrowDomainException() { // Arrange var stopDate = DateTime.UtcNow.AddMonths(-3); var apprenticeship = await SetupApprenticeship(); var command = new StopApprenticeshipCommand(apprenticeship.Cohort.EmployerAccountId, apprenticeship.Id, stopDate, false, new UserInfo()); // Act var exception = Assert.ThrowsAsync <DomainException>(async() => await _handler.Handle(command, new CancellationToken())); // Assert exception.DomainErrors.Should().BeEquivalentTo(new { PropertyName = "stopDate", ErrorMessage = $"Invalid Stop Date. Stop date cannot be before the apprenticeship has started." }); }
public async Task Handle_WhenHandlingCommand_WithMismatchedAccountId_ThenShouldThrowDomainException() { // Arrange var apprenticeship = await SetupApprenticeship(); var incorrectAccountId = apprenticeship.Cohort.EmployerAccountId - 1; var command = new StopApprenticeshipCommand(incorrectAccountId, apprenticeship.Id, DateTime.UtcNow, false, new UserInfo()); // Act var exception = Assert.ThrowsAsync <DomainException>(async() => await _handler.Handle(command, new CancellationToken())); // Assert exception.DomainErrors.Should().BeEquivalentTo(new { PropertyName = "accountId", ErrorMessage = $"Employer {command.AccountId} not authorised to access commitment {apprenticeship.Cohort.Id}, expected employer {apprenticeship.Cohort.EmployerAccountId}" }); }
public override void SetUp() { base.SetUp(); ExampleValidRequest = new StopApprenticeshipCommand { AccountId = 111L, ApprenticeshipId = 444L, DateOfChange = DateTime.UtcNow.Date.AddMonths(6), UserName = "******" }; TestApprenticeship = new Apprenticeship { Id = 444L, CommitmentId = 123L, PaymentStatus = PaymentStatus.Active, StartDate = DateTime.UtcNow.Date.AddMonths(6) }; MockCurrentDateTime.SetupGet(x => x.Now) .Returns(DateTime.UtcNow); MockApprenticeshipRespository.Setup(x => x.GetApprenticeship(It.Is <long>(y => y == ExampleValidRequest.ApprenticeshipId))) .ReturnsAsync(TestApprenticeship); MockApprenticeshipRespository.Setup(x => x.UpdateApprenticeshipStatus( It.Is <long>(c => c == TestApprenticeship.CommitmentId), It.Is <long>(a => a == ExampleValidRequest.ApprenticeshipId), It.Is <PaymentStatus>(s => s == PaymentStatus.Withdrawn))) .Returns(Task.FromResult(new object())); MockDataLockRepository.Setup(x => x.GetDataLocks(ExampleValidRequest.ApprenticeshipId, false)) .ReturnsAsync(new List <DataLockStatus>()); MockDataLockRepository.Setup(x => x.ResolveDataLock(It.IsAny <IEnumerable <long> >())) .Returns(Task.CompletedTask); MockCommitmentRespository.Setup(x => x.GetCommitmentById( It.Is <long>(c => c == TestApprenticeship.CommitmentId))) .ReturnsAsync(new Commitment { Id = 123L, EmployerAccountId = ExampleValidRequest.AccountId }); }
public async Task Handle_WhenHandlingCommand_StoppingApprenticeship_ThenShouldResolveDataLocks() { // Arrange var apprenticeship = await SetupApprenticeship(); var stopDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1); var command = new StopApprenticeshipCommand(apprenticeship.Cohort.EmployerAccountId, apprenticeship.Id, stopDate, false, new UserInfo()); // Act await _handler.Handle(command, new CancellationToken()); // Simulate Unit of Work contex transaction ending in http request. await _dbContext.SaveChangesAsync(); // Assert var dataLockAssertion = await _confirmationDbContext.DataLocks.Where(s => s.ApprenticeshipId == apprenticeship.Id).ToListAsync(); dataLockAssertion.Should().HaveCount(4); dataLockAssertion.Where(s => s.IsResolved).Should().HaveCount(2); }
public async Task Handle_WhenHandlingCommand_StoppingApprenticeship_ThenShouldPublishApprenticeshipStoppedEvent() { // Arrange var apprenticeship = await SetupApprenticeship(); var stopDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1); var command = new StopApprenticeshipCommand(apprenticeship.Cohort.EmployerAccountId, apprenticeship.Id, stopDate, false, new UserInfo()); // Act await _handler.Handle(command, new CancellationToken()); // Assert var stoppedEvent = _unitOfWorkContext.GetEvents().OfType <ApprenticeshipStoppedEvent>().First(); stoppedEvent.Should().BeEquivalentTo(new ApprenticeshipStoppedEvent { AppliedOn = _currentDateTime.Object.UtcNow, ApprenticeshipId = apprenticeship.Id, StopDate = stopDate }); }
public async Task Handle_WhenHandlingCommand_StoppingApprenticeship_ThenShouldUpdateDatabaseRecord() { // Arrange var apprenticeship = await SetupApprenticeship(); var stopDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1); var command = new StopApprenticeshipCommand(apprenticeship.Cohort.EmployerAccountId, apprenticeship.Id, stopDate, false, new UserInfo()); // Act await _handler.Handle(command, new CancellationToken()); // Simulate Unit of Work contex transaction ending in http request. await _dbContext.SaveChangesAsync(); // Assert var apprenticeshipAssertion = await _confirmationDbContext.Apprenticeships.FirstAsync(a => a.Id == apprenticeship.Id); apprenticeshipAssertion.StopDate.Should().Be(stopDate); apprenticeshipAssertion.MadeRedundant.Should().Be(false); apprenticeshipAssertion.PaymentStatus.Should().Be(PaymentStatus.Withdrawn); }
public async Task Handle_WhenHandlingCommand_WithInvalidCallingParty_ThenShouldThrowDomainException(StopApprenticeshipCommand command) { // Arrange var apprenticeship = await SetupApprenticeship(Party.Provider); // Act var exception = Assert.ThrowsAsync <DomainException>(async() => await _handler.Handle(command, new CancellationToken())); // Assert exception.DomainErrors.Should().BeEquivalentTo(new { ErrorMessage = "StopApprenticeship is restricted to Employers only - Provider is invalid" }); }