public async Task ExportDataBlockCallback_ReturnsNullIfInferenceRequestCannotBeFound() { var service = new DicomWebExportService( _loggerFactory.Object, _httpClientFactory.Object, _serviceScopeFactory.Object, _logger.Object, _configuration, _storageInfoProvider.Object); var tasks = ExportServiceBaseTest.GenerateTaskResponse(1); _resultsService.Setup(p => p.GetPendingJobs(It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <int>())).Returns(Task.FromResult(tasks)); _resultsService.Setup(p => p.ReportSuccess(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(true)); _resultsService.Setup(p => p.ReportFailure(It.IsAny <Guid>(), It.IsAny <bool>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(true)); _payloadsApi.Setup(p => p.Download(It.IsAny <string>(), It.IsAny <string>())) .Returns(Task.FromResult(new PayloadFile { Name = tasks.First().Uris.First(), Data = InstanceGenerator.GenerateDicomData() })); _inferenceRequestStore.Setup(p => p.Get(It.IsAny <string>(), It.IsAny <string>())).Returns((InferenceRequest)null); var dataflowCompleted = new ManualResetEvent(false); service.ReportActionStarted += (sender, args) => { dataflowCompleted.Set(); }; await service.StartAsync(_cancellationTokenSource.Token); dataflowCompleted.WaitOne(5000); _resultsService.Verify( p => p.GetPendingJobs( _configuration.Value.Dicom.Scu.ExportSettings.Agent, It.IsAny <CancellationToken>(), 10), Times.Once()); _payloadsApi.Verify(p => p.Download(tasks.First().PayloadId, tasks.First().Uris.First()), Times.AtLeastOnce()); _logger.VerifyLogging($"The specified job cannot be found in the inference request store and will not be exported.", LogLevel.Error, Times.AtLeastOnce()); _logger.VerifyLogging($"Task {tasks.First().TaskId} marked as failure and will not be retried.", LogLevel.Warning, Times.AtLeastOnce()); await StopAndVerify(service); }
public async Task CompletesDataflow(HttpStatusCode httpStatusCode) { var service = new DicomWebExportService( _loggerFactory.Object, _httpClientFactory.Object, _serviceScopeFactory.Object, _logger.Object, _configuration, _storageInfoProvider.Object); var url = "http://my-dicom-web.site"; var inferenceRequest = new InferenceRequest(); inferenceRequest.OutputResources.Add(new RequestOutputDataResource { Interface = InputInterfaceType.DicomWeb, ConnectionDetails = new DicomWebConnectionDetails { AuthId = "token", AuthType = ConnectionAuthType.Bearer, Uri = url } }); var tasks = ExportServiceBaseTest.GenerateTaskResponse(1); _resultsService.Setup(p => p.GetPendingJobs(It.IsAny <string>(), It.IsAny <CancellationToken>(), It.IsAny <int>())).Returns(Task.FromResult(tasks)); _resultsService.Setup(p => p.ReportSuccess(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(true)); _resultsService.Setup(p => p.ReportFailure(It.IsAny <Guid>(), It.IsAny <bool>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(true)); _payloadsApi.Setup(p => p.Download(It.IsAny <string>(), It.IsAny <string>())) .Returns(Task.FromResult(new PayloadFile { Name = tasks.First().Uris.First(), Data = InstanceGenerator.GenerateDicomData() })); _inferenceRequestStore.Setup(p => p.Get(It.IsAny <string>(), It.IsAny <string>())).Returns(inferenceRequest); var response = new HttpResponseMessage(httpStatusCode); response.Content = new StringContent("result"); _handlerMock = new Mock <HttpMessageHandler>(); _handlerMock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(response); _httpClientFactory.Setup(p => p.CreateClient(It.IsAny <string>())) .Returns(new HttpClient(_handlerMock.Object)); var dataflowCompleted = new ManualResetEvent(false); service.ReportActionStarted += (sender, args) => { dataflowCompleted.Set(); }; await service.StartAsync(_cancellationTokenSource.Token); dataflowCompleted.WaitOne(5000); _resultsService.Verify( p => p.GetPendingJobs( _configuration.Value.Dicom.Scu.ExportSettings.Agent, It.IsAny <CancellationToken>(), 10), Times.Once()); _payloadsApi.Verify(p => p.Download(tasks.First().PayloadId, tasks.First().Uris.First()), Times.AtLeastOnce()); _logger.VerifyLogging($"Exporting data to {inferenceRequest.OutputResources.First().ConnectionDetails.Uri}.", LogLevel.Debug, Times.AtLeastOnce()); if (httpStatusCode == HttpStatusCode.OK) { _logger.VerifyLogging($"Task marked as successful.", LogLevel.Information, Times.AtLeastOnce()); } else { _logger.VerifyLogging($"Failed to export data to DICOMweb destination.", LogLevel.Error, Times.AtLeastOnce()); _logger.VerifyLoggingMessageBeginsWith("Task marked as failed with failure rate=", LogLevel.Warning, Times.AtLeastOnce()); } _handlerMock.Protected().Verify( "SendAsync", Times.Exactly(1), ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Post && req.RequestUri.ToString().StartsWith($"{url}/studies/")), ItExpr.IsAny <CancellationToken>()); await StopAndVerify(service); }