2020import com .fasterxml .jackson .databind .ObjectMapper ;
2121import java .time .Duration ;
2222import java .util .List ;
23- import java .util .concurrent .atomic .AtomicBoolean ;
23+ import java .util .concurrent .atomic .AtomicInteger ;
2424import org .hl7 .fhir .r4 .model .Bundle ;
2525import org .junit .jupiter .api .BeforeEach ;
2626import org .junit .jupiter .api .Test ;
@@ -37,6 +37,9 @@ class DefaultTransferProcessRunnerTest {
3737 private static final String PATIENT_IDENTIFIER_2 = "patient-142391" ;
3838 private static final ConsentedPatient PATIENT_2 =
3939 new ConsentedPatient (PATIENT_IDENTIFIER_2 , "system" );
40+ private static final String PATIENT_IDENTIFIER_3 = "patient-293847" ;
41+ private static final ConsentedPatient PATIENT_3 =
42+ new ConsentedPatient (PATIENT_IDENTIFIER_3 , "system" );
4043
4144 private DefaultTransferProcessRunner runner ;
4245
@@ -180,7 +183,7 @@ void errorInDataSelectorSkipsBundleAndContinues() {
180183 new TransferProcessDefinition (
181184 "test" ,
182185 rawConfig ,
183- pids -> fromIterable (List .of (PATIENT , PATIENT_2 )),
186+ pids -> fromIterable (List .of (PATIENT , PATIENT_2 , PATIENT_3 )),
184187 errorOnSecond (new Bundle ()),
185188 b -> just (new TransportBundle (new Bundle (), "transferId" )),
186189 b -> just (new Result ()));
@@ -192,15 +195,15 @@ void errorInDataSelectorSkipsBundleAndContinues() {
192195 }
193196
194197 private static DataSelector errorOnSecond (Bundle bundle ) {
195- var first = new AtomicBoolean ( true );
198+ var callCount = new AtomicInteger ( 0 );
196199 return p ->
197- first . getAndSet ( false )
200+ callCount . getAndIncrement () == 1
198201 ? Flux .error (new RuntimeException ("Cannot select data" ))
199202 : Flux .just (bundle ).map (b -> new ConsentedPatientBundle (b , p ));
200203 }
201204
202205 private void completedWithErrors (TransferProcessStatus r ) {
203- assertThat (r .sentBundles ()).isEqualTo (1 );
206+ assertThat (r .sentBundles ()).isEqualTo (2 );
204207 assertThat (r .skippedBundles ()).isEqualTo (1 );
205208 assertThat (r .phase ()).isEqualTo (Phase .COMPLETED_WITH_ERROR );
206209 }
@@ -211,7 +214,7 @@ void errorInDeidentificatorSkipsBundleAndContinues() {
211214 new TransferProcessDefinition (
212215 "test" ,
213216 rawConfig ,
214- pids -> fromIterable (List .of (PATIENT , PATIENT_2 )),
217+ pids -> fromIterable (List .of (PATIENT , PATIENT_2 , PATIENT_3 )),
215218 p -> fromIterable (List .of (new ConsentedPatientBundle (new Bundle (), p ))),
216219 errorOnSecond (new TransportBundle (new Bundle (), "transferId" )),
217220 b -> just (new Result ()));
@@ -223,11 +226,11 @@ void errorInDeidentificatorSkipsBundleAndContinues() {
223226 }
224227
225228 private static Deidentificator errorOnSecond (TransportBundle bundle ) {
226- var first = new AtomicBoolean ( true );
229+ var callCount = new AtomicInteger ( 0 );
227230 return b ->
228- first . getAndSet ( false )
229- ? just ( bundle )
230- : Mono . error ( new RuntimeException ( "Cannot deidentify bundle" ) );
231+ callCount . getAndIncrement () == 1
232+ ? Mono . error ( new RuntimeException ( "Cannot deidentify bundle" ) )
233+ : just ( bundle );
231234 }
232235
233236 @ Test
@@ -236,7 +239,7 @@ void errorInBundleSenderSkipsBundleAndContinues() {
236239 new TransferProcessDefinition (
237240 "test" ,
238241 rawConfig ,
239- pids -> fromIterable (List .of (PATIENT , DefaultTransferProcessRunnerTest . PATIENT_2 )),
242+ pids -> fromIterable (List .of (PATIENT , PATIENT_2 , PATIENT_3 )),
240243 p -> fromIterable (List .of (new ConsentedPatientBundle (new Bundle (), p ))),
241244 b -> just (new TransportBundle (new Bundle (), "transferId" )),
242245 errorOnSecond (new Result ()));
@@ -248,11 +251,112 @@ void errorInBundleSenderSkipsBundleAndContinues() {
248251 }
249252
250253 private static BundleSender errorOnSecond (Result result ) {
251- var first = new AtomicBoolean ( true );
254+ var callCount = new AtomicInteger ( 0 );
252255 return b ->
253- first .getAndSet (false )
254- ? just (result )
255- : Mono .error (new RuntimeException ("Cannot send bundle" ));
256+ callCount .getAndIncrement () == 1
257+ ? Mono .error (new RuntimeException ("Cannot send bundle" ))
258+ : just (result );
259+ }
260+
261+ @ Test
262+ void errorInDataSelectorRecordsFailedPatient () {
263+ var process =
264+ new TransferProcessDefinition (
265+ "test" ,
266+ rawConfig ,
267+ pids -> fromIterable (List .of (PATIENT , PATIENT_2 , PATIENT_3 )),
268+ errorOnSecond (new Bundle ()),
269+ b -> just (new TransportBundle (new Bundle (), "transferId" )),
270+ b -> just (new Result ()));
271+
272+ var processId = runner .start (process , List .of ());
273+ waitForCompletion (processId );
274+
275+ create (runner .status (processId ))
276+ .assertNext (
277+ r -> {
278+ assertThat (r .phase ()).isEqualTo (Phase .COMPLETED_WITH_ERROR );
279+ assertThat (r .sentBundles ()).isEqualTo (2 );
280+ assertThat (r .failedPatients ()).hasSize (1 );
281+ assertThat (r .failedPatients ().getFirst ().patientId ())
282+ .isEqualTo (PATIENT_IDENTIFIER_2 );
283+ assertThat (r .failedPatients ().getFirst ().errorMessage ())
284+ .isEqualTo ("Cannot select data" );
285+ })
286+ .verifyComplete ();
287+ }
288+
289+ @ Test
290+ void errorInDeidentificatorRecordsFailedPatient () {
291+ var process =
292+ new TransferProcessDefinition (
293+ "test" ,
294+ rawConfig ,
295+ pids -> fromIterable (List .of (PATIENT , PATIENT_2 , PATIENT_3 )),
296+ p -> fromIterable (List .of (new ConsentedPatientBundle (new Bundle (), p ))),
297+ errorOnSecond (new TransportBundle (new Bundle (), "transferId" )),
298+ b -> just (new Result ()));
299+
300+ var processId = runner .start (process , List .of ());
301+ waitForCompletion (processId );
302+
303+ create (runner .status (processId ))
304+ .assertNext (
305+ r -> {
306+ assertThat (r .phase ()).isEqualTo (Phase .COMPLETED_WITH_ERROR );
307+ assertThat (r .sentBundles ()).isEqualTo (2 );
308+ assertThat (r .failedPatients ()).hasSize (1 );
309+ assertThat (r .failedPatients ().getFirst ().errorMessage ())
310+ .isEqualTo ("Cannot deidentify bundle" );
311+ })
312+ .verifyComplete ();
313+ }
314+
315+ @ Test
316+ void errorInBundleSenderRecordsFailedPatient () {
317+ var process =
318+ new TransferProcessDefinition (
319+ "test" ,
320+ rawConfig ,
321+ pids -> fromIterable (List .of (PATIENT , PATIENT_2 , PATIENT_3 )),
322+ p -> fromIterable (List .of (new ConsentedPatientBundle (new Bundle (), p ))),
323+ b -> just (new TransportBundle (new Bundle (), "transferId" )),
324+ errorOnSecond (new Result ()));
325+
326+ var processId = runner .start (process , List .of ());
327+ waitForCompletion (processId );
328+
329+ create (runner .status (processId ))
330+ .assertNext (
331+ r -> {
332+ assertThat (r .phase ()).isEqualTo (Phase .COMPLETED_WITH_ERROR );
333+ assertThat (r .sentBundles ()).isEqualTo (2 );
334+ assertThat (r .failedPatients ()).hasSize (1 );
335+ assertThat (r .failedPatients ().getFirst ().errorMessage ())
336+ .isEqualTo ("Cannot send bundle" );
337+ })
338+ .verifyComplete ();
339+ }
340+
341+ @ Test
342+ void successfulTransferHasNoFailedPatients () {
343+ var process =
344+ new TransferProcessDefinition (
345+ "test" ,
346+ rawConfig ,
347+ pids -> fromIterable (List .of (PATIENT )),
348+ p -> fromIterable (List .of (new ConsentedPatientBundle (new Bundle (), PATIENT ))),
349+ b -> just (new TransportBundle (new Bundle (), "transferId" )),
350+ b -> just (new Result ()));
351+
352+ var processId = runner .start (process , List .of ());
353+ create (runner .status (processId ))
354+ .assertNext (
355+ r -> {
356+ assertThat (r .phase ()).isEqualTo (Phase .COMPLETED );
357+ assertThat (r .failedPatients ()).isEmpty ();
358+ })
359+ .verifyComplete ();
256360 }
257361
258362 @ Test
0 commit comments