@@ -169,31 +169,27 @@ private boolean hasObservationsWithoutSpecimen(List<Observation> observations) {
169169 * For correct display in EMIS, any observation without a specimen must be assigned a dummy specimen.
170170 */
171171 private List <Observation > assignDummySpecimensToObservationsWithNoSpecimen (
172- List <Observation > observations , List <Specimen > specimens ) {
172+ List <Observation > observations , List <Specimen > specimens ) {
173173
174174 List <Observation > filingComments = getFilingComments (observations );
175- observations = new ArrayList <>(stripFilingComments (observations ));
175+ List < Observation > nonFilingObservations = new ArrayList <>(stripFilingComments (observations ));
176176
177- if (!hasObservationsWithoutSpecimen (observations )) {
178- observations .addAll (filingComments );
179- return observations ;
180- }
181-
182- // The assumption was made that all test results without a specimen will have the same dummy specimen referenced
183- Specimen dummySpecimen = specimens .stream ()
177+ if (hasObservationsWithoutSpecimen (nonFilingObservations )) {
178+ Specimen dummySpecimen = specimens .stream ()
184179 .filter (specimen -> specimen .getId ().contains (NOT_PRESENT_SPECIMEN_ID_PREFIX ))
185- .toList ().getFirst ();
180+ .findFirst ()
181+ .orElseThrow (() -> new IllegalStateException (
182+ "No dummy specimen found with prefix: " + NOT_PRESENT_SPECIMEN_ID_PREFIX ));
186183
187- Reference dummySpecimenReference = new Reference (dummySpecimen .getId ());
184+ Reference dummySpecimenReference = new Reference (dummySpecimen .getId ());
188185
189- for (Observation observation : observations ) {
190- if (!observation .hasSpecimen () && !isFilingComment (observation )) {
191- observation .setSpecimen (dummySpecimenReference );
192- }
186+ nonFilingObservations .stream ()
187+ .filter (obs -> !obs .hasSpecimen ())
188+ .forEach (obs -> obs .setSpecimen (dummySpecimenReference ));
193189 }
194190
195- observations .addAll (filingComments );
196- return observations ;
191+ nonFilingObservations .addAll (filingComments );
192+ return nonFilingObservations ;
197193 }
198194
199195 private Specimen generateDummySpecimen (DiagnosticReport diagnosticReport ) {
0 commit comments