public ExportJobTaskTests() { _cancellationToken = _cancellationTokenSource.Token; _exportJobRecord = new ExportJobRecord( new Uri("https://localhost/ExportJob/"), "Patient", "hash"); _fhirOperationDataStore.UpdateExportJobAsync(_exportJobRecord, _weakETag, _cancellationToken).Returns(x => { _lastExportJobOutcome = new ExportJobOutcome(_exportJobRecord, _weakETag); return(_lastExportJobOutcome); }); _secretStore.GetSecretAsync(Arg.Any <string>(), _cancellationToken).Returns(x => new SecretWrapper(x.ArgAt <string>(0), "{\"destinationType\": \"in-memory\"}")); _exportDestinationClientFactory.Create("in-memory").Returns(_inMemoryDestinationClient); _resourceToByteArraySerializer.Serialize(Arg.Any <ResourceWrapper>()).Returns(x => Encoding.UTF8.GetBytes(x.ArgAt <ResourceWrapper>(0).ResourceId)); _exportJobTask = new ExportJobTask( () => _fhirOperationDataStore.CreateMockScope(), _secretStore, Options.Create(_exportJobConfiguration), () => _searchService.CreateMockScope(), _resourceToByteArraySerializer, _exportDestinationClientFactory, NullLogger <ExportJobTask> .Instance); }
public async Task GivenConnectingToDestinationFails_WhenExecuted_ThenJobStatusShouldBeUpdatedToFailed() { // Setup export destination client. string connectionFailure = "failedToConnectToDestination"; IExportDestinationClient mockExportDestinationClient = Substitute.For <IExportDestinationClient>(); mockExportDestinationClient.ConnectAsync(Arg.Any <string>(), Arg.Any <CancellationToken>(), Arg.Any <string>()) .Returns <Task>(x => throw new DestinationConnectionException(connectionFailure, HttpStatusCode.BadRequest)); _exportDestinationClientFactory.Create("in-memory").Returns(mockExportDestinationClient); var exportJobTask = new ExportJobTask( () => _fhirOperationDataStore.CreateMockScope(), _secretStore, Options.Create(_exportJobConfiguration), () => _searchService.CreateMockScope(), _resourceToByteArraySerializer, _exportDestinationClientFactory, NullLogger <ExportJobTask> .Instance); await exportJobTask.ExecuteAsync(_exportJobRecord, _weakETag, _cancellationToken); Assert.NotNull(_lastExportJobOutcome); Assert.Equal(OperationStatus.Failed, _lastExportJobOutcome.JobRecord.Status); Assert.Equal(connectionFailure, _lastExportJobOutcome.JobRecord.FailureDetails.FailureReason); Assert.Equal(HttpStatusCode.BadRequest, _lastExportJobOutcome.JobRecord.FailureDetails.FailureStatusCode); }
public async Task GivenStorageAccountConnectionChanged_WhenExecuted_ThenJobStatusShouldBeUpdatedToFailed() { string connectionFailure = "Storage account connection string was updated during an export job."; var exportJobRecordWithChangedConnection = new ExportJobRecord( new Uri("https://localhost/ExportJob/"), "Patient", "hash", since: PartialDateTime.MinValue, storageAccountConnectionHash: Microsoft.Health.Core.Extensions.StringExtensions.ComputeHash("different connection"), storageAccountUri: _exportJobConfiguration.StorageAccountUri); SetupExportJobRecordAndOperationDataStore(exportJobRecordWithChangedConnection); var exportJobTask = new ExportJobTask( () => _fhirOperationDataStore.CreateMockScope(), Options.Create(_exportJobConfiguration), () => _searchService.CreateMockScope(), _resourceToByteArraySerializer, _inMemoryDestinationClient, NullLogger <ExportJobTask> .Instance); await exportJobTask.ExecuteAsync(_exportJobRecord, _weakETag, _cancellationToken); Assert.NotNull(_lastExportJobOutcome); Assert.Equal(OperationStatus.Failed, _lastExportJobOutcome.JobRecord.Status); Assert.Equal(connectionFailure, _lastExportJobOutcome.JobRecord.FailureDetails.FailureReason); Assert.Equal(HttpStatusCode.BadRequest, _lastExportJobOutcome.JobRecord.FailureDetails.FailureStatusCode); }
public ExportJobTaskTests() { _cancellationToken = _cancellationTokenSource.Token; _fhirOperationDataStore.UpdateExportJobAsync(_exportJobRecord, _weakETag, _cancellationToken).Returns(x => new ExportJobOutcome(_exportJobRecord, _weakETag)); _exportJobTask = new ExportJobTask( _fhirOperationDataStore, NullLogger <ExportJobTask> .Instance); }
public ExportJobTaskTests() { _cancellationToken = _cancellationTokenSource.Token; SetupExportJobRecordAndOperationDataStore(); _resourceToByteArraySerializer.Serialize(Arg.Any <ResourceWrapper>()).Returns(x => Encoding.UTF8.GetBytes(x.ArgAt <ResourceWrapper>(0).ResourceId)); _exportJobTask = new ExportJobTask( () => _fhirOperationDataStore.CreateMockScope(), Options.Create(_exportJobConfiguration), () => _searchService.CreateMockScope(), _resourceToByteArraySerializer, _inMemoryDestinationClient, NullLogger <ExportJobTask> .Instance); }
public async Task GivenStorageAccountConnectionDidNotChange_WhenExecuted_ThenJobShouldBeCompleted() { ExportJobConfiguration exportJobConfiguration = new ExportJobConfiguration(); exportJobConfiguration.StorageAccountConnection = "connection"; exportJobConfiguration.StorageAccountUri = string.Empty; var exportJobRecordWithConnection = new ExportJobRecord( new Uri("https://localhost/ExportJob/"), "Patient", "hash", since: PartialDateTime.MinValue, storageAccountConnectionHash: Microsoft.Health.Core.Extensions.StringExtensions.ComputeHash(exportJobConfiguration.StorageAccountConnection), storageAccountUri: exportJobConfiguration.StorageAccountUri); SetupExportJobRecordAndOperationDataStore(exportJobRecordWithConnection); var exportJobTask = new ExportJobTask( () => _fhirOperationDataStore.CreateMockScope(), Options.Create(exportJobConfiguration), () => _searchService.CreateMockScope(), _resourceToByteArraySerializer, _inMemoryDestinationClient, NullLogger <ExportJobTask> .Instance); _searchService.SearchAsync( Arg.Any <string>(), Arg.Any <IReadOnlyList <Tuple <string, string> > >(), _cancellationToken) .Returns(x => CreateSearchResult()); DateTimeOffset endTimestamp = DateTimeOffset.UtcNow; using (Mock.Property(() => ClockResolver.UtcNowFunc, () => endTimestamp)) { await exportJobTask.ExecuteAsync(_exportJobRecord, _weakETag, _cancellationToken); } Assert.NotNull(_lastExportJobOutcome); Assert.Equal(OperationStatus.Completed, _lastExportJobOutcome.JobRecord.Status); Assert.Equal(endTimestamp, _lastExportJobOutcome.JobRecord.EndTime); }
public async Task GivenStorageAccountUriChanged_WhenExecuted_ThenRecordsAreSentToOldStorageAccount() { var exportJobRecordWithChangedConnection = new ExportJobRecord( new Uri("https://localhost/ExportJob/"), "Patient", "hash", since: PartialDateTime.MinValue, storageAccountConnectionHash: Microsoft.Health.Core.Extensions.StringExtensions.ComputeHash(_exportJobConfiguration.StorageAccountConnection), storageAccountUri: "origionalUri"); SetupExportJobRecordAndOperationDataStore(exportJobRecordWithChangedConnection); ExportJobConfiguration configurationWithUri = new ExportJobConfiguration(); configurationWithUri.StorageAccountUri = "newUri"; IExportDestinationClient mockDestinationClient = Substitute.For <IExportDestinationClient>(); ExportJobConfiguration capturedConfiguration = null; mockDestinationClient.ConnectAsync( Arg.Do <ExportJobConfiguration>(arg => capturedConfiguration = arg), Arg.Any <CancellationToken>(), Arg.Any <string>()) .Returns(x => { return(Task.CompletedTask); }); var exportJobTask = new ExportJobTask( () => _fhirOperationDataStore.CreateMockScope(), Options.Create(_exportJobConfiguration), () => _searchService.CreateMockScope(), _resourceToByteArraySerializer, mockDestinationClient, NullLogger <ExportJobTask> .Instance); await exportJobTask.ExecuteAsync(_exportJobRecord, _weakETag, _cancellationToken); Assert.Equal(exportJobRecordWithChangedConnection.StorageAccountUri, capturedConfiguration.StorageAccountUri); }
public ExportJobTaskTests(FhirStorageTestsFixture fixture) { _fhirOperationDataStore = fixture.OperationDataStore; _exportJobTask = new ExportJobTask(_fhirOperationDataStore, NullLogger <ExportJobTask> .Instance); }
public async Task GivenAnExportJobToResume_WhenExecuted_ThenItShouldExportAllRecordsAsExpected() { // We are using the SearchService to throw an exception in order to simulate the export job task // "crashing" while in the middle of the process. _exportJobConfiguration.NumberOfPagesPerCommit = 2; int numberOfCalls = 0; int numberOfSuccessfulPages = 2; _searchService.SearchAsync( Arg.Any <string>(), Arg.Any <IReadOnlyList <Tuple <string, string> > >(), _cancellationToken) .Returns(x => { int count = numberOfCalls; if (count == numberOfSuccessfulPages) { throw new Exception(); } numberOfCalls++; return(CreateSearchResult( new[] { new ResourceWrapper( count.ToString(CultureInfo.InvariantCulture), "1", "Patient", new RawResource("data", Core.Models.FhirResourceFormat.Json), null, DateTimeOffset.MinValue, false, null, null, null), }, continuationToken: "ct")); }); await _exportJobTask.ExecuteAsync(_exportJobRecord, _weakETag, _cancellationToken); string exportedIds = _inMemoryDestinationClient.GetExportedData(new Uri(PatientFileName, UriKind.Relative)); Assert.Equal("01", exportedIds); Assert.NotNull(_exportJobRecord.Progress); // We create a new export job task here to simulate the worker picking up the "old" export job record // and resuming the export process. The export destination client contains data that has // been committed up until the "crash". _inMemoryDestinationClient = new InMemoryExportDestinationClient(); _exportDestinationClientFactory.Create("in-memory").Returns(_inMemoryDestinationClient); var secondExportJobTask = new ExportJobTask( () => _fhirOperationDataStore.CreateMockScope(), _secretStore, Options.Create(_exportJobConfiguration), () => _searchService.CreateMockScope(), _resourceToByteArraySerializer, _exportDestinationClientFactory, NullLogger <ExportJobTask> .Instance); numberOfSuccessfulPages = 5; await secondExportJobTask.ExecuteAsync(_exportJobRecord, _weakETag, _cancellationToken); exportedIds = _inMemoryDestinationClient.GetExportedData(new Uri(PatientFileName, UriKind.Relative)); Assert.Equal("23", exportedIds); }