Skip to content

Commit aba0515

Browse files
committed
Fixed issue with case ordering leading to random test failures
1 parent a6b1a73 commit aba0515

2 files changed

Lines changed: 47 additions & 22 deletions

File tree

sormas-backend/src/main/java/de/symeda/sormas/backend/externalmessage/labmessage/AutomaticLabMessageProcessor.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -314,20 +314,44 @@ protected void handlePickOrCreateEntry(
314314
automaticAssignmentSampleDate,
315315
automaticSampleAssignmentThreshold);
316316

317-
318317
CaseSelectionDto caseToAssignTo = null;
319318

320-
// special case for LATENT_TUBERCULOSIS: if there are more caseUuids and we have tuberculosis cases we need to give priority to the tuberculosis cases
321-
if (disease == Disease.LATENT_TUBERCULOSIS && autoAssignCaseUuids.size() > 1) {
322-
// sort the cases by disease giving priority to tuberculosis cases and take the first one (the one with the highest priority and most recent report date)
323-
caseToAssignTo = similarCases.stream()
324-
.filter(c -> autoAssignCaseUuids.contains(c.getUuid()))
325-
.sorted(Comparator.comparing((CaseSelectionDto c) -> c.getDisease() == Disease.TUBERCULOSIS ? 0 : 1)
326-
.thenComparing((CaseSelectionDto c) -> c.getReportDate(), Comparator.reverseOrder()))
327-
.findFirst()
328-
.orElse(null);
329-
} else {
330-
caseToAssignTo = similarCases.stream().filter(c -> autoAssignCaseUuids.contains(c.getUuid())).findFirst().orElse(null);
319+
if (!autoAssignCaseUuids.isEmpty()) {
320+
// special case for LATENT_TUBERCULOSIS: if there are more caseUuids and we have tuberculosis cases we need to give priority to the tuberculosis cases
321+
if (disease == Disease.LATENT_TUBERCULOSIS && autoAssignCaseUuids.size() > 1) {
322+
// sort the cases by disease giving priority to tuberculosis cases and take the first one (the one with the highest priority and most recent report date)
323+
// we can't use the autoAssignCaseUuids order because it may have at position 0 a case with LATENT_TUBERCULOSIS and we need to prioritize TUBERCULOSIS cases
324+
caseToAssignTo = similarCases.stream()
325+
.filter(c -> autoAssignCaseUuids.contains(c.getUuid()))
326+
.sorted(
327+
Comparator.comparing((CaseSelectionDto c) -> c.getDisease() == Disease.TUBERCULOSIS ? 0 : 1)
328+
.thenComparing((CaseSelectionDto c) -> c.getReportDate(), Comparator.reverseOrder()))
329+
.findFirst()
330+
.orElse(null);
331+
} else {
332+
// autoAssignCaseUuids contains at least one element (checked above)
333+
// autoAssignCaseUuids is ordered by date descending, so the first element is the most recent case
334+
final String autoAssignMostRecentCaseUuid = autoAssignCaseUuids.get(0);
335+
caseToAssignTo = similarCases.stream().filter(c -> autoAssignMostRecentCaseUuid.equals(c.getUuid())).findFirst().orElse(null);
336+
}
337+
338+
if (logger.isDebugEnabled()) {
339+
logger.debug(
340+
"Similar cases : {}",
341+
similarCases.stream()
342+
.map(c -> String.format("%s(%s,%s)", c.getUuid(), c.getDisease().getName(), c.getReportDate()))
343+
.collect(Collectors.joining(";")));
344+
logger.debug("Similar case uuids: {}", similarCaseUuids);
345+
logger.debug(
346+
"Selected case: {}",
347+
caseToAssignTo == null
348+
? "null"
349+
: String.format(
350+
"%s(%s,%s)",
351+
caseToAssignTo.getUuid(),
352+
caseToAssignTo.getDisease().getName(),
353+
caseToAssignTo.getReportDate()));
354+
}
331355
}
332356

333357
if (caseToAssignTo == null) {

sormas-backend/src/test/java/de/symeda/sormas/backend/externalmessage/labmessage/AutomaticLabMessageProcessorTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Date;
2828
import java.util.List;
2929
import java.util.concurrent.ExecutionException;
30-
import java.util.concurrent.TimeUnit;
3130
import java.util.function.Consumer;
3231
import java.util.stream.Collectors;
3332

@@ -301,8 +300,8 @@ public void testThresholdAgainstSampleDate() throws ExecutionException, Interrup
301300

302301
@ParameterizedTest
303302
@CsvSource({
304-
"lu, 2010010100774"
305-
})
303+
"null, null",
304+
"lu, 2010010100774" })
306305
public void testProcessWithExistingPersonAndCaseWithBySampleDate(final String countryLocale, final String personalHealthId)
307306
throws ExecutionException, InterruptedException {
308307

@@ -345,15 +344,13 @@ public void testProcessWithExistingPersonAndCaseWithBySampleDate(final String co
345344
sample.setSampleDateTime(DateHelper.subtractDays(new Date(), 11));
346345
getSampleFacade().saveSample(sample);
347346

348-
TimeUnit.SECONDS.sleep(1);
349-
350347
// STEP 2: Process the message again now with a set threshold but with a sample date that is too old
351348
final int sampleCountBeforeStep2 = getSampleFacade().getAllActiveSamplesAfter(new Date(0)).size();
352349
result = runFlow(externalMessage);
353350
assertThat(result.getStatus(), is(DONE));
354351
assertThat(externalMessage.getStatus(), is(ExternalMessageStatus.PROCESSED));
355352
final int sampleCountAfterStep2 = getSampleFacade().getAllActiveSamplesAfter(new Date(0)).size();
356-
353+
357354
final String step2ResultCaseUuid = result.getData().getCase().getUuid();
358355

359356
assertThat("Sample count should have incresed, new sample for new case.", sampleCountAfterStep2, is(greaterThan(sampleCountBeforeStep2)));
@@ -368,8 +365,6 @@ public void testProcessWithExistingPersonAndCaseWithBySampleDate(final String co
368365
sample.setSampleDateTime(DateHelper.subtractDays(new Date(), 5));
369366
getSampleFacade().saveSample(sample);
370367

371-
TimeUnit.SECONDS.sleep(2);
372-
373368
// STEP 3: Process the message again now with a set threshold but with a sample date that is valid
374369
final int sampleCountBeforeStep3 = getSampleFacade().getAllActiveSamplesAfter(new Date(0)).size();
375370
result = runFlow(externalMessage);
@@ -380,8 +375,14 @@ public void testProcessWithExistingPersonAndCaseWithBySampleDate(final String co
380375

381376
cases = getCaseFacade().getAllAfter(new Date(0));
382377
assertThat("Case count should not have increased, sample date theshold was set in valid range", cases, hasSize(2));
383-
assertThat("Sample count should have incresed, new sample for existing case.", sampleCountAfterStep3, is(greaterThan(sampleCountBeforeStep3)));
384-
assertThat("Case UUID should be the same as the result of the previous step (result should be added to existing case).", step3ResultCaseUuid, is(step2ResultCaseUuid));
378+
assertThat(
379+
"Sample count should have incresed, new sample for existing case.",
380+
sampleCountAfterStep3,
381+
is(greaterThan(sampleCountBeforeStep3)));
382+
assertThat(
383+
"Case UUID should be the same as the result of the previous step (result should be added to existing case).",
384+
step3ResultCaseUuid,
385+
is(step2ResultCaseUuid));
385386

386387
persons = getPersonFacade().getAllAfter(new Date(0));
387388
assertThat(persons, hasSize(1));

0 commit comments

Comments
 (0)