|
| 1 | +package de.medizininformatikinitiative.torch.service; |
| 2 | + |
| 3 | +import de.medizininformatikinitiative.torch.management.StructureDefinitionHandler; |
| 4 | +import de.medizininformatikinitiative.torch.model.consent.PatientBatchWithConsent; |
| 5 | +import de.medizininformatikinitiative.torch.model.crtdl.annotated.AnnotatedAttribute; |
| 6 | +import de.medizininformatikinitiative.torch.model.crtdl.annotated.AnnotatedAttributeGroup; |
| 7 | +import de.medizininformatikinitiative.torch.model.management.PatientResourceBundle; |
| 8 | +import de.medizininformatikinitiative.torch.model.management.ResourceGroup; |
| 9 | +import de.medizininformatikinitiative.torch.util.ProfileMustHaveChecker; |
| 10 | +import org.hl7.fhir.r4.model.Observation; |
| 11 | +import org.hl7.fhir.r4.model.Reference; |
| 12 | +import org.junit.jupiter.api.Nested; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | +import org.mockito.InjectMocks; |
| 16 | +import org.mockito.Mock; |
| 17 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 18 | +import reactor.core.publisher.Flux; |
| 19 | +import reactor.test.StepVerifier; |
| 20 | + |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Optional; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.mockito.ArgumentMatchers.any; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | + |
| 30 | +@ExtendWith(MockitoExtension.class) |
| 31 | +class DirectResourceLoaderTest { |
| 32 | + |
| 33 | + |
| 34 | + @Mock |
| 35 | + DataStore dataStore; |
| 36 | + @Mock |
| 37 | + StructureDefinitionHandler structureDefinitionHandler; |
| 38 | + @Mock |
| 39 | + ProfileMustHaveChecker profileMustHaveChecker; |
| 40 | + @InjectMocks |
| 41 | + DirectResourceLoader directResourceLoader; |
| 42 | + |
| 43 | + |
| 44 | + @Nested |
| 45 | + class ProcessPatientAttributeGroups { |
| 46 | + |
| 47 | + @Test |
| 48 | + void testIgnoresEmptyFlux() { |
| 49 | + |
| 50 | + var attribute = new AnnotatedAttribute("Observation.name", "Observation.name", "Observation.name", false); |
| 51 | + var attributeGroup = new AnnotatedAttributeGroup("test", "groupRef", List.of(attribute), List.of(), null); |
| 52 | + |
| 53 | + var patientBundle = new PatientResourceBundle("1"); |
| 54 | + var batchWithConsent = PatientBatchWithConsent.fromList(List.of(patientBundle)); |
| 55 | + var safeSet = new HashSet<>(List.of("1")); |
| 56 | + |
| 57 | + when(structureDefinitionHandler.getResourceType("groupRef")).thenReturn("Observation"); |
| 58 | + when(dataStore.search(any(), any())).thenAnswer(invocation -> Flux.empty()); |
| 59 | + |
| 60 | + var result = directResourceLoader.processPatientAttributeGroups( |
| 61 | + List.of(attributeGroup), |
| 62 | + batchWithConsent, |
| 63 | + safeSet |
| 64 | + ); |
| 65 | + |
| 66 | + StepVerifier.create(result) |
| 67 | + .assertNext(res -> { |
| 68 | + assertThat(res.bundles()).containsKey("1"); |
| 69 | + assertThat(res.get("1").bundle().cache()).doesNotContainKey("Observation/xyz"); |
| 70 | + }) |
| 71 | + .verifyComplete(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testIgnoresEmptyResource() { |
| 76 | + |
| 77 | + var attribute = new AnnotatedAttribute("Observation.name", "Observation.name", "Observation.name", false); |
| 78 | + var attributeGroup = new AnnotatedAttributeGroup("test", "groupRef", List.of(attribute), List.of(), null); |
| 79 | + |
| 80 | + var patientBundle = new PatientResourceBundle("1"); |
| 81 | + var batchWithConsent = PatientBatchWithConsent.fromList(List.of(patientBundle)); |
| 82 | + var safeSet = new HashSet<>(List.of("1")); |
| 83 | + |
| 84 | + |
| 85 | + when(structureDefinitionHandler.getResourceType("groupRef")).thenReturn("Observation"); |
| 86 | + when(dataStore.search(any(), any())).thenAnswer(invocation -> Flux.just(new Observation())); |
| 87 | + |
| 88 | + var result = directResourceLoader.processPatientAttributeGroups( |
| 89 | + List.of(attributeGroup), |
| 90 | + batchWithConsent, |
| 91 | + safeSet |
| 92 | + ); |
| 93 | + |
| 94 | + StepVerifier.create(result) |
| 95 | + .assertNext(res -> { |
| 96 | + assertThat(res.bundles()).containsKey("1"); |
| 97 | + assertThat(res.get("1").bundle().cache()).doesNotContainKey("Observation/xyz"); |
| 98 | + }) |
| 99 | + .verifyComplete(); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + @Test |
| 104 | + void testIgnoresObservationOfUnknownPatient() { |
| 105 | + |
| 106 | + var attribute = new AnnotatedAttribute("Observation.name", "Observation.name", "Observation.name", false); |
| 107 | + var attributeGroup = new AnnotatedAttributeGroup("test", "groupRef", List.of(attribute), List.of(), null); |
| 108 | + |
| 109 | + var patientBundle = new PatientResourceBundle("1"); |
| 110 | + var batchWithConsent = PatientBatchWithConsent.fromList(List.of(patientBundle)); |
| 111 | + var safeSet = new HashSet<>(List.of("1")); |
| 112 | + |
| 113 | + Observation observation = new Observation(); |
| 114 | + observation.setId("Observation/xyz"); |
| 115 | + observation.setSubject(new Reference("Patient/2")); |
| 116 | + |
| 117 | + when(structureDefinitionHandler.getResourceType("groupRef")).thenReturn("Observation"); |
| 118 | + when(dataStore.search(any(), any())).thenAnswer(invocation -> Flux.just(observation)); |
| 119 | + |
| 120 | + var result = directResourceLoader.processPatientAttributeGroups( |
| 121 | + List.of(attributeGroup), |
| 122 | + batchWithConsent, |
| 123 | + safeSet |
| 124 | + ); |
| 125 | + |
| 126 | + StepVerifier.create(result) |
| 127 | + .assertNext(res -> { |
| 128 | + assertThat(res.bundles()).containsKey("1"); |
| 129 | + assertThat(res.get("1").bundle().cache()).doesNotContainKey("Observation/xyz"); |
| 130 | + }) |
| 131 | + .verifyComplete(); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void testIgnoresResourceWithoutPatientReference() { |
| 136 | + // Arrange |
| 137 | + var attribute = new AnnotatedAttribute("Observation.name", "Observation.name", "Observation.name", false); |
| 138 | + var attributeGroup = new AnnotatedAttributeGroup("test", "groupRef", List.of(attribute), List.of(), null); |
| 139 | + var patientBundle = new PatientResourceBundle("1"); |
| 140 | + var batchWithConsent = PatientBatchWithConsent.fromList(List.of(patientBundle)); |
| 141 | + var safeSet = new HashSet<>(List.of("1")); |
| 142 | + |
| 143 | + Observation observation = new Observation(); |
| 144 | + observation.setId("xyz"); |
| 145 | + |
| 146 | + when(structureDefinitionHandler.getResourceType("groupRef")).thenReturn("Observation"); |
| 147 | + when(dataStore.search(any(), any())).thenAnswer(invocation -> Flux.just(observation)); |
| 148 | + |
| 149 | + var result = directResourceLoader.processPatientAttributeGroups( |
| 150 | + List.of(attributeGroup), |
| 151 | + batchWithConsent, |
| 152 | + safeSet |
| 153 | + ); |
| 154 | + |
| 155 | + StepVerifier.create(result) |
| 156 | + .assertNext(res -> { |
| 157 | + assertThat(res.bundles()).containsKey("1"); |
| 158 | + assertThat(res.get("1").bundle().cache()).doesNotContainKey("Observation/xyz"); |
| 159 | + }) |
| 160 | + .verifyComplete(); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + @Test |
| 165 | + void testStoresObservationWithInvalidMustHave() { |
| 166 | + // Arrange |
| 167 | + var attribute = new AnnotatedAttribute("Observation.name", "Observation.name", "Observation.name", false); |
| 168 | + var attributeGroup = new AnnotatedAttributeGroup("test", "groupRef", List.of(attribute), List.of(), null); |
| 169 | + var patientBundle = new PatientResourceBundle("1"); |
| 170 | + var batchWithConsent = PatientBatchWithConsent.fromList(List.of(patientBundle)); |
| 171 | + var safeSet = new HashSet<>(List.of("1")); |
| 172 | + |
| 173 | + Observation observation = new Observation(); |
| 174 | + observation.setId("xyz"); |
| 175 | + observation.setSubject(new Reference("Patient/1")); |
| 176 | + |
| 177 | + when(structureDefinitionHandler.getResourceType("groupRef")).thenReturn("Observation"); |
| 178 | + when(dataStore.search(any(), any())).thenAnswer(invocation -> Flux.just(observation)); |
| 179 | + when(profileMustHaveChecker.fulfilled(observation, attributeGroup)).thenReturn(false); |
| 180 | + |
| 181 | + var result = directResourceLoader.processPatientAttributeGroups( |
| 182 | + List.of(attributeGroup), |
| 183 | + batchWithConsent, |
| 184 | + safeSet |
| 185 | + ); |
| 186 | + |
| 187 | + StepVerifier.create(result) |
| 188 | + .assertNext(processedBatch -> { |
| 189 | + assertThat(processedBatch).isNotNull(); |
| 190 | + assertThat(processedBatch.patientBatch().ids()).containsExactly("1"); |
| 191 | + |
| 192 | + // Example check for modified bundle cache |
| 193 | + assertThat(processedBatch.get("1").bundle().cache()) |
| 194 | + .isEqualTo(Map.of("Observation/xyz", Optional.of(observation))); |
| 195 | + assertThat(processedBatch.get("1").bundle().getValidResourceGroups()).doesNotContain( |
| 196 | + new ResourceGroup("Observation/xyz", "test")); |
| 197 | + }) |
| 198 | + .verifyComplete(); |
| 199 | + } |
| 200 | + |
| 201 | + |
| 202 | + @Test |
| 203 | + void testStoresObservationWithKnownPatient() { |
| 204 | + // Arrange |
| 205 | + var attribute = new AnnotatedAttribute("Observation.name", "Observation.name", "Observation.name", false); |
| 206 | + var attributeGroup = new AnnotatedAttributeGroup("test", "groupRef", List.of(attribute), List.of(), null); |
| 207 | + var patientBundle = new PatientResourceBundle("1"); |
| 208 | + var batchWithConsent = PatientBatchWithConsent.fromList(List.of(patientBundle)); |
| 209 | + var safeSet = new HashSet<>(List.of("1")); |
| 210 | + |
| 211 | + Observation observation = new Observation(); |
| 212 | + observation.setId("xyz"); |
| 213 | + observation.setSubject(new Reference("Patient/1")); |
| 214 | + |
| 215 | + when(structureDefinitionHandler.getResourceType("groupRef")).thenReturn("Observation"); |
| 216 | + when(dataStore.search(any(), any())).thenAnswer(invocation -> Flux.just(observation)); |
| 217 | + when(profileMustHaveChecker.fulfilled(observation, attributeGroup)).thenReturn(true); |
| 218 | + |
| 219 | + var result = directResourceLoader.processPatientAttributeGroups( |
| 220 | + List.of(attributeGroup), |
| 221 | + batchWithConsent, |
| 222 | + safeSet |
| 223 | + ); |
| 224 | + |
| 225 | + StepVerifier.create(result) |
| 226 | + .assertNext(processedBatch -> { |
| 227 | + assertThat(processedBatch).isNotNull(); |
| 228 | + assertThat(processedBatch.patientBatch().ids()).containsExactly("1"); |
| 229 | + |
| 230 | + // Example check for modified bundle cache |
| 231 | + assertThat(processedBatch.get("1").bundle().cache()) |
| 232 | + .isEqualTo(Map.of("Observation/xyz", Optional.of(observation))); |
| 233 | + assertThat(processedBatch.get("1").bundle().getValidResourceGroups()).containsExactly( |
| 234 | + new ResourceGroup("Observation/xyz", "test")); |
| 235 | + }) |
| 236 | + .verifyComplete(); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments