|
1 | 1 | package com.lantanagroup.link.measureeval.services; |
2 | 2 |
|
3 | 3 | import ca.uhn.fhir.context.FhirContext; |
| 4 | +import ch.qos.logback.classic.Level; |
| 5 | +import ch.qos.logback.classic.Logger; |
| 6 | +import ch.qos.logback.classic.LoggerContext; |
| 7 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 8 | +import ch.qos.logback.core.read.ListAppender; |
4 | 9 | import com.azure.core.util.BinaryData; |
5 | 10 | import com.azure.storage.blob.BlobClient; |
6 | 11 | import com.azure.storage.blob.BlobContainerClient; |
7 | 12 | import com.lantanagroup.link.measureeval.entities.PatientReportingEvaluationStatus; |
8 | 13 | import com.lantanagroup.link.shared.entities.ReportScheduleModel; |
9 | 14 | import com.lantanagroup.link.shared.exceptions.ValidationException; |
10 | 15 | import com.lantanagroup.link.shared.services.ReportClient; |
| 16 | +import org.hl7.fhir.r4.model.Condition; |
11 | 17 | import org.hl7.fhir.r4.model.MeasureReport; |
| 18 | +import org.hl7.fhir.r4.model.Observation; |
12 | 19 | import org.hl7.fhir.r4.model.Patient; |
13 | 20 | import org.hl7.fhir.r4.model.Reference; |
| 21 | +import org.junit.jupiter.api.AfterEach; |
14 | 22 | import org.junit.jupiter.api.BeforeEach; |
15 | 23 | import org.junit.jupiter.api.Test; |
16 | 24 | import org.mockito.ArgumentCaptor; |
| 25 | +import org.slf4j.LoggerFactory; |
17 | 26 |
|
18 | 27 | import java.util.List; |
19 | 28 |
|
@@ -142,6 +151,99 @@ void testStorePatientInBlobStorageWithContainedResources() { |
142 | 151 | assertTrue(content.contains("MeasureReport/mr1")); |
143 | 152 | } |
144 | 153 |
|
| 154 | + @Test |
| 155 | + void testStorePatientInBlobStorageWithDuplicateIdPartAcrossTypes() { |
| 156 | + PatientReportingEvaluationStatus status = new PatientReportingEvaluationStatus(); |
| 157 | + status.setPatientId("patient1"); |
| 158 | + PatientReportingEvaluationStatus.Report report = new PatientReportingEvaluationStatus.Report(); |
| 159 | + report.setReportTrackingId("track1"); |
| 160 | + report.setReportType("type1"); |
| 161 | + status.setReports(List.of(report)); |
| 162 | + |
| 163 | + ReportScheduleModel schedule = new ReportScheduleModel(); |
| 164 | + schedule.setPayloadRootUri("https://storage.com/test-container/root/"); |
| 165 | + when(reportClient.getReportSchedule("track1")).thenReturn(schedule); |
| 166 | + |
| 167 | + MeasureReport measureReport = new MeasureReport(); |
| 168 | + measureReport.setId("mr1"); |
| 169 | + |
| 170 | + // Two contained resources of different types sharing the same ID part. |
| 171 | + // Previously this threw IllegalStateException from Collectors.toMap. |
| 172 | + Condition condition = new Condition(); |
| 173 | + condition.setId("#LCR-A"); |
| 174 | + measureReport.addContained(condition); |
| 175 | + |
| 176 | + Observation observation = new Observation(); |
| 177 | + observation.setId("#LCR-A"); |
| 178 | + measureReport.addContained(observation); |
| 179 | + |
| 180 | + blobStorageService.storePatientInBlobStorage(status, report, measureReport); |
| 181 | + |
| 182 | + ArgumentCaptor<BinaryData> contentCaptor = ArgumentCaptor.forClass(BinaryData.class); |
| 183 | + verify(blobClient).upload(contentCaptor.capture(), eq(true)); |
| 184 | + String content = contentCaptor.getValue().toString(); |
| 185 | + |
| 186 | + assertTrue(content.contains("Condition/A")); |
| 187 | + assertTrue(content.contains("Observation/A")); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + void testStorePatientInBlobStorageWithAmbiguousUntypedReferenceIsSkippedAndLogged() { |
| 192 | + Logger blobStorageServiceLogger = |
| 193 | + (Logger) LoggerFactory.getLogger(BlobStorageService.class); |
| 194 | + ListAppender<ILoggingEvent> logAppender = new ListAppender<>(); |
| 195 | + logAppender.setContext((LoggerContext) LoggerFactory.getILoggerFactory()); |
| 196 | + logAppender.start(); |
| 197 | + blobStorageServiceLogger.addAppender(logAppender); |
| 198 | + |
| 199 | + try { |
| 200 | + PatientReportingEvaluationStatus status = new PatientReportingEvaluationStatus(); |
| 201 | + status.setPatientId("patient1"); |
| 202 | + PatientReportingEvaluationStatus.Report report = new PatientReportingEvaluationStatus.Report(); |
| 203 | + report.setReportTrackingId("track1"); |
| 204 | + report.setReportType("type1"); |
| 205 | + status.setReports(List.of(report)); |
| 206 | + |
| 207 | + ReportScheduleModel schedule = new ReportScheduleModel(); |
| 208 | + schedule.setPayloadRootUri("https://storage.com/test-container/root/"); |
| 209 | + when(reportClient.getReportSchedule("track1")).thenReturn(schedule); |
| 210 | + |
| 211 | + MeasureReport measureReport = new MeasureReport(); |
| 212 | + measureReport.setId("mr1"); |
| 213 | + |
| 214 | + Condition condition = new Condition(); |
| 215 | + condition.setId("#LCR-A"); |
| 216 | + measureReport.addContained(condition); |
| 217 | + |
| 218 | + Observation observation = new Observation(); |
| 219 | + observation.setId("#LCR-A"); |
| 220 | + measureReport.addContained(observation); |
| 221 | + |
| 222 | + // Untyped contained reference to the ambiguous ID part - cannot be resolved deterministically. |
| 223 | + measureReport.setSubject(new Reference("#LCR-A")); |
| 224 | + |
| 225 | + blobStorageService.storePatientInBlobStorage(status, report, measureReport); |
| 226 | + |
| 227 | + ArgumentCaptor<BinaryData> contentCaptor = ArgumentCaptor.forClass(BinaryData.class); |
| 228 | + verify(blobClient).upload(contentCaptor.capture(), eq(true)); |
| 229 | + String content = contentCaptor.getValue().toString(); |
| 230 | + |
| 231 | + // First line is the "MeasureReport/<id>" marker, second is the MeasureReport JSON itself. |
| 232 | + String measureReportJson = content.split("\n")[1]; |
| 233 | + MeasureReport parsed = fhirContext.newJsonParser().parseResource(MeasureReport.class, measureReportJson); |
| 234 | + |
| 235 | + // The ambiguous reference is left unresolved rather than guessing which resource it means. |
| 236 | + assertEquals("#LCR-A", parsed.getSubject().getReference()); |
| 237 | + |
| 238 | + boolean warningLogged = logAppender.list.stream() |
| 239 | + .anyMatch(event -> event.getLevel() == Level.WARN |
| 240 | + && event.getFormattedMessage().contains("LCR-A")); |
| 241 | + assertTrue(warningLogged, "Expected a warning to be logged for the ambiguous contained reference"); |
| 242 | + } finally { |
| 243 | + blobStorageServiceLogger.detachAppender(logAppender); |
| 244 | + } |
| 245 | + } |
| 246 | + |
145 | 247 | @Test |
146 | 248 | void testStorePatientInBlobStoragePayloadUriNull() { |
147 | 249 | PatientReportingEvaluationStatus status = new PatientReportingEvaluationStatus(); |
|
0 commit comments