private void ValidateEvent(UpdatedPermissionsEvent updateEvent) { if (updateEvent == null) { throw new ArgumentException( "Event cannot be null", nameof(UpdatedPermissionsEvent)); } if (updateEvent.AccountId.Equals(default(long))) { throw new ArgumentException( "Account ID must be set in event", nameof(updateEvent.AccountId)); } if (updateEvent.AccountLegalEntityId.Equals(default(long))) { throw new ArgumentException( "Account legal entity ID must be set in event", nameof(updateEvent.AccountLegalEntityId)); } if (updateEvent.Ukprn.Equals(default(long))) { throw new ArgumentException( "UKPRN must be set in event", nameof(updateEvent.Ukprn)); } }
public static async Task Run( [NServiceBusTrigger(EndPoint = QueueNames.UpdatedProviderPermissions)] UpdatedPermissionsEvent updateEvent, [Inject] ILogger <UpdatedPermissionsEvent> logger, [Inject] IProviderPermissionsUpdatedHandler providerPermissionsUpdatedHandler) { logger.LogInformation($"NServiceBus AddedAccountProvider trigger function executed at: {DateTime.Now}"); await providerPermissionsUpdatedHandler.Handle(updateEvent); }
public void And_All_Required_Fields_Then_Is_Valid( UpdatedPermissionsEvent permissionsEvent, UpdatedPermissionsEventValidator validator) { //Act var result = validator.Validate(permissionsEvent); //Assert result.Should().BeTrue(); }
private static ProviderPermission Map(UpdatedPermissionsEvent updateEvent) { return(new ProviderPermission { AccountId = updateEvent.AccountId, AccountLegalEntityId = updateEvent.AccountLegalEntityId, ProviderId = updateEvent.Ukprn, CanCreateCohort = updateEvent.GrantedOperations?.Contains(Operation.CreateCohort) ?? false }); }
public async Task Then_Calls_Service( [ArrangeUpdatedPermissionsEvent(Operation = Operation.CreateCohort)] UpdatedPermissionsEvent updatedEvent, [Frozen] Mock <IUpdatedPermissionsEventValidator> mockValidator, [Frozen] Mock <IProviderPermissionService> mockPermissionsService, ProviderPermissionsUpdatedHandler handler) { await handler.Handle(updatedEvent); //Assert mockPermissionsService.Verify(s => s.AddProviderPermission(updatedEvent), Times.Once); }
public async Task Handle(UpdatedPermissionsEvent updateEvent) { if (!_validator.Validate(updateEvent)) { _logger.LogWarning( $"Cannot process provider permission event due to validation failure " + $"Account ID: {updateEvent?.AccountId}, Ukprn: {updateEvent?.Ukprn}, Account legal entity ID:{updateEvent?.AccountLegalEntityId}"); return; } await _service.AddProviderPermission(updateEvent); }
public void Customize(IFixture fixture) { var permissionEvent = new UpdatedPermissionsEvent( 10, 11, 12, 13, 14, Guid.NewGuid(), "*****@*****.**", "Test", "Tester", new HashSet <Operation> { _operation }, DateTime.Now); fixture.Inject(permissionEvent); }
public async Task And_CanCreateCohort_Then_Adds_Provider_To_Search_Index( [ArrangeUpdatedPermissionsEvent(Operation = Operation.CreateCohort)] UpdatedPermissionsEvent updatedEvent, [Frozen] Mock <IReservationService> mockReservationsService, ProviderPermissionService service) { await service.AddProviderPermission(updatedEvent); mockReservationsService.Verify(reservationService => reservationService.AddProviderToSearchIndex((uint)updatedEvent.Ukprn, updatedEvent.AccountLegalEntityId), Times.Once); }
public async Task And_Validation_Error_Then_No_Further_Processing( [ArrangeUpdatedPermissionsEvent(Operation = Operation.CreateCohort)] UpdatedPermissionsEvent updatedEvent, [Frozen] Mock <IUpdatedPermissionsEventValidator> mockValidator, [Frozen] Mock <IProviderPermissionService> mockPermissionsService, ProviderPermissionsUpdatedHandler handler) { mockValidator .Setup(validator => validator.Validate(updatedEvent)) .Returns(false); await handler.Handle(updatedEvent); mockPermissionsService.Verify(service => service.AddProviderPermission(It.IsAny <UpdatedPermissionsEvent>()), Times.Never); }
public void And_No_Ukprn_Then_Not_Valid( UpdatedPermissionsEventValidator validator) { //Arrange var permissionEvent = new UpdatedPermissionsEvent( 1, 11, 12, 13, 0, Guid.NewGuid(), "*****@*****.**", "Test", "Tester", new HashSet <Operation> { Operation.Recruitment }, DateTime.Now); //Act var result = validator.Validate(permissionEvent); //Assert result.Should().BeFalse(); }
public async Task Then_Message_Handler_Called() { //Arrange var handler = new Mock <IProviderPermissionsUpdatedHandler>(); var message = new UpdatedPermissionsEvent( 10, 11, 12, 13, 14, Guid.NewGuid(), "*****@*****.**", "Test", "Tester", new HashSet <Operation>(), DateTime.Now); //Act await HandleProviderPermissionUpdatedEvent.Run( message, Mock.Of <ILogger <UpdatedPermissionsEvent> >(), handler.Object); //Assert handler.Verify(s => s.Handle(message), Times.Once); }
public async Task ThenSetsCanCreateCohortToFalseIfPermissionIsNull_And_Updates_Index() { //Arrange var permissionEvent = new UpdatedPermissionsEvent( 10, 11, 12, 13, 14, Guid.NewGuid(), "*****@*****.**", "Test", "Tester", null, DateTime.Now); //Act await _service.AddProviderPermission(permissionEvent); //Assert _repo.Verify(s => s.Add(It.Is <Domain.Entities.ProviderPermission>(p => !p.CanCreateCohort)), Times.Once); _reservationIndexService.Verify( x => x.DeleteProviderFromSearchIndex(Convert.ToUInt32(permissionEvent.Ukprn), permissionEvent.AccountLegalEntityId), Times.Once); }
public async Task ThenCallsRepositoryToAddPermissionToDatabase_And_Does_Not_Call_Delete_On_Index() { //Arrange var permissionEvent = new UpdatedPermissionsEvent( 10, 11, 12, 13, 14, Guid.NewGuid(), "*****@*****.**", "Test", "Tester", new HashSet <Operation> { Operation.CreateCohort }, DateTime.Now); //Act await _service.AddProviderPermission(permissionEvent); //Assert _repo.Verify(s => s.Add(It.Is <Domain.Entities.ProviderPermission>(p => p.AccountId.Equals(permissionEvent.AccountId) && p.AccountLegalEntityId.Equals(permissionEvent.AccountLegalEntityId) && p.ProviderId.Equals(permissionEvent.Ukprn) && p.CanCreateCohort)), Times.Once); _reservationIndexService.Verify(x => x.DeleteProviderFromSearchIndex(It.IsAny <uint>(), It.IsAny <long>()), Times.Never); }
public async Task Run() { var connectionString = NServiceBus.AzureFunction.Infrastructure.EnvironmentVariables.NServiceBusConnectionString; var endpointConfiguration = new EndpointConfiguration("SFA.DAS.Reservations.TestConsole") .UseAzureServiceBusTransport(connectionString, r => { r.RouteToEndpoint(typeof(UpdatedPermissionsEvent), QueueNames.UpdatedProviderPermissions); }) .UseErrorQueue() .UseInstallers() .UseMessageConventions() .UseNewtonsoftJsonSerializer(); var endpointInstance = await Endpoint.Start(endpointConfiguration) .ConfigureAwait(false); var command = string.Empty; do { var message = new UpdatedPermissionsEvent( 10, 11, 12, 13, 14, Guid.NewGuid(), "*****@*****.**", "Test", "Tester", new HashSet <Operation>(), DateTime.Now); await endpointInstance.Publish(message); Console.WriteLine("Message sent..."); Console.WriteLine("Enter 'q' to exit..." + Environment.NewLine); command = Console.ReadLine(); } while (!command.Equals("q")); await endpointInstance.Stop() .ConfigureAwait(false); }
public async Task AddProviderPermission(UpdatedPermissionsEvent updateEvent) { try { var permission = Map(updateEvent); await _permissionRepository.Add(permission); if (!permission.CanCreateCohort) { await _reservationService.DeleteProviderFromSearchIndex( Convert.ToUInt32(permission.ProviderId), permission.AccountLegalEntityId); } else { await _reservationService.AddProviderToSearchIndex( (uint)permission.ProviderId, permission.AccountLegalEntityId); } } catch (DbUpdateException e) { if (e.GetBaseException() is SqlException sqlException && (sqlException.Number == UniqueConstraintViolation || sqlException.Number == UniqueKeyViolation)) { _logger.LogWarning( $"ProviderPermissionService: Rolling back adding permission for ProviderId:[{updateEvent.Ukprn}], AccountLegalEntityId:[{updateEvent.AccountLegalEntityId}] - item already exists."); } } catch (Exception ex) { _logger.LogWarning($"Error updating index for ProviderId:[{updateEvent.Ukprn}], AccountLegalEntityId:[{updateEvent.AccountLegalEntityId}]", ex); } }
public bool Validate(UpdatedPermissionsEvent updatedPermissionsEvent) { if (updatedPermissionsEvent.AccountId.Equals(default))