public ApprenticeshipCompletedEventTestsFixture() { MessageHandlerContext = new Mock <IMessageHandlerContext>(); MockGetApprenticeship = new Mock <IGetApprenticeshipService>(); MockLogger = new Mock <ILogger <ApprenticeshipCompletedEventHandler> >(); MockApprenticeshipCompletionDateUpdatedEventHandler = new Mock <IApprenticeshipCompletedEventHandler>(); Fixture = new Fixture(); Db = new ForecastingDbContext(new DbContextOptionsBuilder <ForecastingDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .EnableSensitiveDataLogging() .Options); Commitment = Fixture.Create <Commitments>(); Commitment.Id = CommitmentId = 101; Commitment.ActualEndDate = null; Commitment.Status = Status.LiveOrWaitingToStart; Commitment.ApprenticeshipId = 1; Db.Commitment.Add(Commitment); ApprenticeshipCompletedEvent = Fixture.Create <ApprenticeshipCompletedEvent>(); ApprenticeshipCompletedEvent.ApprenticeshipId = Commitment.ApprenticeshipId; var configuration = new MapperConfiguration(cfg => cfg.AddProfile <AutoMapperProfile>()); var mapper = new Mapper(configuration); Sut = new ApprenticeshipCompletedEventHandler(Db, MockGetApprenticeship.Object, MockLogger.Object); Db.SaveChanges(); }
public async Task Handle(ApprenticeshipCompletedEvent message) { try { var selectedApprenticeship = _forecastingDbContext.Commitment.FirstOrDefault(x => x.ApprenticeshipId == message.ApprenticeshipId); if (selectedApprenticeship == null) { selectedApprenticeship = await _getApprenticeshipService.GetApprenticeshipDetails(message.ApprenticeshipId); _forecastingDbContext.Commitment.Add(selectedApprenticeship); } selectedApprenticeship.UpdatedDateTime = DateTime.UtcNow; selectedApprenticeship.Status = Status.Completed; selectedApprenticeship.ActualEndDate = message.CompletionDate; await _forecastingDbContext.SaveChangesAsync(); } catch (CommitmentsApiModelException commitmentException) { _logger.LogError(commitmentException, $"Apprenticeship Completed function Failure to retrieve ApprenticeshipId: [{message.ApprenticeshipId}]"); throw; } catch (Exception ex) { _logger.LogError(ex, $"Apprenticeship Completed function Failed for ApprenticeshipId: [{message.ApprenticeshipId}] "); throw; } }
public async Task Run( [NServiceBusTrigger(Endpoint = "SFA.DAS.Fcast.ApprenticeshipCompletedEvent")] ApprenticeshipCompletedEvent message) { _logger.LogInformation($"Apprenticeship Completed function Begin at: [{DateTime.UtcNow}] UTC, event with ApprenticeshipId: [{message.ApprenticeshipId}]."); await _apprenticeshipCompletedEventHandler.Handle(message); _logger.LogInformation($"Apprenticeship Completed function Finished at: [{DateTime.UtcNow}] UTC, event with ApprenticeshipId: [{message.ApprenticeshipId}]."); }
public ApprenticeshipCompletedEventTestsFixture() { MockLogger = new Mock <ILogger <ApprenticeshipCompletedFunction> >(); MockpprenticeshipCompletedEventHandler = new Mock <IApprenticeshipCompletedEventHandler>(); Fixture = new Fixture(); ApprenticeshipCompletedEvent = Fixture.Create <ApprenticeshipCompletedEvent>(); Sut = new ApprenticeshipCompletedFunction(MockpprenticeshipCompletedEventHandler.Object, MockLogger.Object); }