PatientValidator { @NotNull @VisibleForTesting static List<ValidationFinding> validateTreatments(@NotNull String patientIdentifier, @NotNull List<BiopsyTreatmentData> treatments) { List<ValidationFinding> findings = Lists.newArrayList(); List<BiopsyTreatmentData> matchedTreatments = treatments.stream().filter(treatment -> treatment.biopsyId() != null).collect(Collectors.toList()); matchedTreatments.forEach(treatment -> findings.addAll(validateTreatmentData(patientIdentifier, treatment))); Collections.sort(matchedTreatments); if (matchedTreatments.size() > 1) { for (int index = 1; index < matchedTreatments.size(); index++) { BiopsyTreatmentData currentTreatment = matchedTreatments.get(index); LocalDate startDate = currentTreatment.startDate(); LocalDate previousTreatmentEnd = matchedTreatments.get(index - 1).endDate(); if (startDate != null && (previousTreatmentEnd == null || startDate.isBefore(previousTreatmentEnd))) { findings.add(ValidationFinding.of(ECRF_LEVEL, patientIdentifier, "Subsequent treatment starts before the end of previous treatment", currentTreatment.formStatus())); } } List<BiopsyTreatmentData> nonFinishedTreatments = matchedTreatments.stream() .filter(treatment -> treatment.startDate() != null) .filter(treatment -> treatment.endDate() == null) .collect(Collectors.toList()); if (nonFinishedTreatments.size() > 1) { nonFinishedTreatments.forEach(treatment -> findings.add(ValidationFinding.of(ECRF_LEVEL, patientIdentifier, "End of at least 1 non-final treatment is missing", treatment.formStatus()))); } } return findings; }  @NotNull static List<ValidationFinding> validatePatient(@NotNull Patient patient);  }
@Test public void reportsMissingTreatmentGiven() { List<ValidationFinding> findings = PatientValidator.validateTreatments(PATIENT_IDENTIFIER, Lists.newArrayList(TREATMENT_GIVEN_NULL)); assertEquals(1, findings.size()); }
@Test public void reportsEmptyTreatmentGiven() { List<ValidationFinding> findings = PatientValidator.validateTreatments(PATIENT_IDENTIFIER, Lists.newArrayList(TREATMENT_GIVEN_NO_DATA)); assertEquals(2, findings.size()); }
@Test public void reportsTreatmentGivenNoWithData() { List<ValidationFinding> findings = PatientValidator.validateTreatments(PATIENT_IDENTIFIER, Lists.newArrayList(TREATMENT_NOT_GIVEN_DATA)); assertEquals(1, findings.size()); }
@Test public void reportsTreatmentGivenGibberish() { List<ValidationFinding> findings = PatientValidator.validateTreatments(PATIENT_IDENTIFIER, Lists.newArrayList(TREATMENT_GIVEN_GIBBERISH)); assertEquals(1, findings.size()); }
@Test public void reportsMissingRadioTherapy() { List<ValidationFinding> findings = PatientValidator.validateTreatments(PATIENT_IDENTIFIER, Lists.newArrayList(TREATMENT_RADIO_THERAPY_NULL)); assertEquals(1, findings.size()); }
@Test public void reportsDrugFindingsForTreatment() { List<ValidationFinding> findings = PatientValidator.validateTreatments(PATIENT_IDENTIFIER, Lists.newArrayList(TREATMENT_WRONG_DRUG_DATA)); assertEquals(4, findings.size()); findings.stream().map(ValidationFinding::patientIdentifier).forEach(id -> assertEquals(PATIENT_IDENTIFIER, id)); }
@Test public void reportsWrongTreatmentTimeline() { List<ValidationFinding> findings = PatientValidator.validateTreatments(PATIENT_IDENTIFIER, Lists.newArrayList(TREATMENT_JAN_MAR, TREATMENT_JAN_JAN, TREATMENT_JAN_FEB)); assertEquals(1, findings.size()); }
@Test public void reportsWrongTreatmentTimelineOngoing() { List<ValidationFinding> findings = PatientValidator.validateTreatments(PATIENT_IDENTIFIER, Lists.newArrayList(TREATMENT_JAN_FEB, TREATMENT_JAN_ONGOING)); assertEquals(1, findings.size()); }
@Test public void reportsTwoOngoingTreatments() { List<ValidationFinding> findings = PatientValidator.validateTreatments(PATIENT_IDENTIFIER, Lists.newArrayList(TREATMENT_JAN_ONGOING, TREATMENT_FEB_ONGOING)); assertEquals(3, findings.size()); }