Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,73 @@ private ImportProcessingJobDefinition GetInputData()
return inputData;
}

[Fact]
public async Task GivenImportInput_WhenConstraintViolationOccurs_ThenImportShouldAbort()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is valid, but what is missing is E2E test.

{
ImportProcessingJobDefinition inputData = GetInputData();

IImportResourceLoader loader = Substitute.For<IImportResourceLoader>();
IImporter importer = Substitute.For<IImporter>();
IImportErrorStore importErrorStore = Substitute.For<IImportErrorStore>();
IImportErrorStoreFactory importErrorStoreFactory = Substitute.For<IImportErrorStoreFactory>();
RequestContextAccessor<IFhirRequestContext> contextAccessor = Substitute.For<RequestContextAccessor<IFhirRequestContext>>();
ILoggerFactory loggerFactory = new NullLoggerFactory();
IMediator mediator = Substitute.For<IMediator>();
IAuditLogger auditLogger = Substitute.For<IAuditLogger>();
IQueueClient queueClient = Substitute.For<IQueueClient>();

loader.LoadResources(Arg.Any<string>(), Arg.Any<long>(), Arg.Any<int>(), Arg.Any<string>(), Arg.Any<ImportMode>(), Arg.Any<CancellationToken>())
.Returns(callInfo =>
{
Channel<ImportResource> resourceChannel = Channel.CreateUnbounded<ImportResource>();

Task loadTask = Task.Run(async () =>
{
try
{
// Simulate loading a resource
var wrapper = new ResourceWrapper("id", "1", "Patient", null, null, null, null, null);
var resource = new ImportResource(0, 0, 100, false, false, false, wrapper);
await resourceChannel.Writer.WriteAsync(resource);
}
finally
{
resourceChannel.Writer.Complete();
}
});

return (resourceChannel, loadTask);
});

// Simulate constraint violation by throwing InvalidOperationException with constraint violation message
importer.Import(Arg.Any<Channel<ImportResource>>(), Arg.Any<IImportErrorStore>(), Arg.Any<ImportMode>(), Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns<ImportProcessingProgress>(callInfo =>
{
throw new InvalidOperationException("Import aborted due to constraint violation. See error logs for details.");
});

importErrorStoreFactory.InitializeAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(importErrorStore);

ImportProcessingJob job = new ImportProcessingJob(
mediator,
queueClient,
loader,
importer,
importErrorStoreFactory,
contextAccessor,
loggerFactory,
auditLogger);

var jobInfo = GetJobInfo(inputData, null);
jobInfo.Id = 1;
jobInfo.GroupId = 1;
jobInfo.Status = JobStatus.Running;

// Verify that import aborts with constraint violation
await Assert.ThrowsAsync<JobExecutionException>(() => job.ExecuteAsync(jobInfo, CancellationToken.None));
}

private static JobInfo GetJobInfo(ImportProcessingJobDefinition data, ImportProcessingJobResult result)
{
var jobInfo = new JobInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ public async Task<ImportProcessingProgress> Import(Channel<ImportResource> input
var validResources = resources.Where(r => string.IsNullOrEmpty(r.ImportError)).ToList();
var newErrors = await _store.ImportResourcesAsync(validResources, importMode, allowNegativeVersions, eventualConsistency, cancellationToken);
errors.AddRange(newErrors);

// Abort import on first constraint violation to avoid wasting compute resources
// Constraint violations are identified by checking if the error message contains constraint-related keywords
if (newErrors.Any(e => e.Contains("constraint violation", StringComparison.OrdinalIgnoreCase) ||
e.Contains("Database constraint", StringComparison.OrdinalIgnoreCase)))
{
_logger.LogWarning("Constraint violation detected during import. Aborting further processing to conserve resources.");
throw new InvalidOperationException("Import aborted due to constraint violation. See error logs for details.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear how invalid operation can translate to bad request

}

var totalBytes = resources.Sum(_ => (long)_.Length);
resources.Clear();
return (validResources.Count - newErrors.Count, totalBytes);
Expand Down
Loading