public async Task RequestServicesCommandHandler_Handle_RecordsProcessed() { var availabilityService = new Mock <IAvailabilityService>(); availabilityService.Setup(e => e.Request(It.IsAny <string>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new AvailabilityResponseModel(HttpStatusCode.OK, 22, "{}")); var availabilityProcessor = new AvailabilityProcessor(_availabilityRecordRepository, availabilityService.Object); var requestServicesCommandHandler = new RequestServicesCommandHandler(_availabilityContext, availabilityProcessor); var availabilityRecord = new AvailabilityRecord(Guid.NewGuid(), "test name", "http://google.com", 200, null, 2); await _availabilityRecordRepository.AddAsync(availabilityRecord, CancellationToken.None); await _availabilityRecordRepository.UnitOfWork.SaveEntitiesAsync(CancellationToken.None); await requestServicesCommandHandler.Handle(new RequestServicesCommand(), CancellationToken.None); availabilityRecord = await _availabilityRecordRepository .GetById(availabilityRecord.Id, CancellationToken.None) .ConfigureAwait(false); Assert.Single(availabilityRecord.AvailabilityLogs); Assert.Equal(200, availabilityRecord.AvailabilityLogs.First().StatusCode); Assert.Equal(22, availabilityRecord.AvailabilityLogs.First().ResponseTime); Assert.Equal("{}", availabilityRecord.AvailabilityLogs.First().Body); }
public async Task AvailabilityProcessor_ProcessAvailabilityRecord_RecordProcessed() { var availabilityRecord = new AvailabilityRecord(Guid.NewGuid(), "test name", "http://google.com/", 200, null, 2); await _availabilityRecordRepository.AddAsync(availabilityRecord, CancellationToken.None); await _availabilityRecordRepository.UnitOfWork.SaveEntitiesAsync(CancellationToken.None); var availabilityService = new Mock <IAvailabilityService>(); availabilityService.Setup(e => e.Request(It.IsAny <string>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new AvailabilityResponseModel(HttpStatusCode.OK, 65, "{}")); var availabilityProcessor = new AvailabilityProcessor(_availabilityRecordRepository, availabilityService.Object); await availabilityProcessor.ProcessAvailabilityRecord(availabilityRecord.Id, CancellationToken.None); availabilityRecord = await _availabilityRecordRepository.GetById(availabilityRecord.Id, CancellationToken.None); Assert.Single(availabilityRecord.AvailabilityLogs); Assert.Equal(200, availabilityRecord.AvailabilityLogs.First().StatusCode); Assert.Equal("{}", availabilityRecord.AvailabilityLogs.First().Body); Assert.Equal(65, availabilityRecord.AvailabilityLogs.First().ResponseTime); }
public async Task <List <AvailabilityListItemDto> > Handle(GetAvailabilityListItemsByIdsCommand request, CancellationToken cancellationToken) { var result = new List <AvailabilityRecord>(); foreach (var id in request.Ids) { result.Add(await _availabilityRecordRepository.GetById(id, cancellationToken)); } return(result.Select(_mapper.Map <AvailabilityListItemDto>).ToList()); }
public async Task Handle(DeleteAvailabilityRecordCommand notification, CancellationToken cancellationToken) { var accountId = _claimConverter.GetAccountId(notification.ClaimsPrincipal); var availabilityRecord = await _availabilityRecordRepository.GetById(notification.Id, cancellationToken) .ConfigureAwait(false); if (availabilityRecord.AccountId != accountId) { throw new InvalidOperationException("Operation not allowed"); } _availabilityRecordRepository.Remove(availabilityRecord); await _availabilityRecordRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken) .ConfigureAwait(false); }
public async Task <AvailabilityRecordDto> Handle(GetAvailabilityRecordByIdCommand request, CancellationToken cancellationToken) { var record = await _availabilityRecordRepository.GetById(request.Id, cancellationToken) .ConfigureAwait(false); var dto = _mapper.Map <AvailabilityRecordDto>(record); if (dto.AvailabilityLogs != null && dto.AvailabilityLogs.Count > 0) { dto.AvailabilityLogs = dto.AvailabilityLogs .OrderByDescending(e => e.CreatedAt) .ToList(); } return(dto); }
public async Task ProcessAvailabilityRecord(Guid recordId, CancellationToken cancellationToken) { var availabilityRecord = await _availabilityRecordRepository.GetById(recordId, cancellationToken) .ConfigureAwait(false); var responseModel = await _availabilityService.Request(availabilityRecord.Url, cancellationToken); availabilityRecord.AppendLog((int)responseModel.HttpStatusCode, responseModel.Body, responseModel.ResponseTime); availabilityRecord.ClearOutdatedLogs(); _availabilityRecordRepository.Update(availabilityRecord); await _availabilityRecordRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken) .ConfigureAwait(false); }
public async Task Handle(UpdateAvailabilityRecordCommand notification, CancellationToken cancellationToken) { var accountId = _claimConverter.GetAccountId(notification.ClaimsPrincipal); var availabilityRecord = await _availabilityRecordRepository.GetById(notification.Id, cancellationToken) .ConfigureAwait(false); if (availabilityRecord.AccountId != accountId) { throw new InvalidOperationException("Invalid account id"); } availabilityRecord.UpdateName(notification.Name); availabilityRecord.UpdateUrl(notification.Url); availabilityRecord.UpdateExpectedResponse(notification.ExpectedResponse); availabilityRecord.UpdateExpectedStatusCode(notification.ExpectedStatusCode); availabilityRecord.UpdateLogLifetimeThresholdInHours(notification.LogLifetimeThresholdInHours); _availabilityRecordRepository.Update(availabilityRecord); await _availabilityRecordRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); }