33import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
44import static org .mockito .ArgumentMatchers .any ;
55import static org .mockito .ArgumentMatchers .eq ;
6- import static org .mockito . Mockito . mock ;
6+ import static org .junit . jupiter . api . Assertions . assertThrows ;
77import static org .mockito .Mockito .when ;
8+ import static org .mockito .Mockito .mock ;
9+
810
911import java .time .Instant ;
1012import java .time .temporal .ChronoUnit ;
13+ import java .util .Arrays ;
14+ import java .util .Collections ;
1115
1216import org .hl7 .fhir .dstu3 .model .Bundle ;
1317import org .junit .jupiter .api .Test ;
1923
2024import uk .nhs .adaptors .gp2gp .common .service .RandomIdGeneratorService ;
2125import uk .nhs .adaptors .gp2gp .common .service .TimestampService ;
26+ import uk .nhs .adaptors .gp2gp .ehr .exception .EhrValidationException ;
2227import uk .nhs .adaptors .gp2gp .gpc .GetGpcStructuredTaskDefinition ;
2328
2429@ ExtendWith (MockitoExtension .class )
@@ -50,35 +55,60 @@ class EhrExtractMapperTest {
5055
5156 @ Test
5257 void When_NhsOverrideNumberProvided_Expect_OverrideToBeUsed () {
58+ when (nonConsultationResourceMapper .mapRemainingResourcesToEhrCompositions (any ()))
59+ .thenReturn (Arrays .asList ("not" , "empty" ));
5360 ReflectionTestUtils .setField (ehrExtractMapper , OVERRIDE_NHS_NUMBER , OVERRIDE_NHS_NUMBER_VALUE );
5461 when (agentDirectoryMapper .mapEHRFolderToAgentDirectory (any (Bundle .class ), eq (OVERRIDE_NHS_NUMBER_VALUE )))
55- .thenReturn (OVERRIDE_NHS_NUMBER_VALUE );
62+ .thenReturn (OVERRIDE_NHS_NUMBER_VALUE );
5663 when (timestampService .now ()).thenReturn (Instant .now ().truncatedTo (ChronoUnit .MILLIS ));
5764 when (messageContext .getEffectiveTime ()).thenReturn (ehrFolderEffectiveTime );
5865
5966 var taskDef = GetGpcStructuredTaskDefinition .builder ()
60- .nhsNumber (NHS_NUMBER )
61- .build ();
67+ .nhsNumber (NHS_NUMBER )
68+ .build ();
6269 var bundle = mock (Bundle .class );
6370 var parameters = ehrExtractMapper .mapBundleToEhrFhirExtractParams (taskDef , bundle );
6471 assertThat (parameters .getAgentDirectory ()).isEqualTo (OVERRIDE_NHS_NUMBER_VALUE );
6572 }
6673
6774 @ Test
6875 void When_NhsOverrideNumberIsBlank_Expect_ActualNhsNumberIsUsed () {
76+ when (nonConsultationResourceMapper .mapRemainingResourcesToEhrCompositions (any ()))
77+ .thenReturn (Arrays .asList ("not" , "empty" ));
6978 when (agentDirectoryMapper .mapEHRFolderToAgentDirectory (any (Bundle .class ), eq (NHS_NUMBER )))
70- .thenReturn (NHS_NUMBER );
79+ .thenReturn (NHS_NUMBER );
7180 when (timestampService .now ()).thenReturn (Instant .now ().truncatedTo (ChronoUnit .MILLIS ));
7281 when (messageContext .getEffectiveTime ()).thenReturn (ehrFolderEffectiveTime );
7382
7483 var taskDef = GetGpcStructuredTaskDefinition .builder ()
75- .nhsNumber (NHS_NUMBER )
76- .build ();
84+ .nhsNumber (NHS_NUMBER )
85+ .build ();
7786 var bundle = mock (Bundle .class );
7887 var parameters = ehrExtractMapper .mapBundleToEhrFhirExtractParams (taskDef , bundle );
7988 assertThat (parameters .getAgentDirectory ()).isEqualTo (NHS_NUMBER );
8089 }
8190
91+ @ Test
92+ void When_BundleHasNoMappableContent_Expect_EhrValidationExceptionIsThrown () {
93+ when (timestampService .now ()).thenReturn (Instant .now ().truncatedTo (ChronoUnit .MILLIS ));
94+
95+ var taskDef = GetGpcStructuredTaskDefinition .builder ()
96+ .nhsNumber (NHS_NUMBER )
97+ .build ();
98+ var bundle = mock (Bundle .class );
99+
100+ when (nonConsultationResourceMapper .mapRemainingResourcesToEhrCompositions (any (Bundle .class )))
101+ .thenReturn (Collections .emptyList ());
102+
103+ EhrValidationException thrown = assertThrows (EhrValidationException .class , () ->
104+ ehrExtractMapper .mapBundleToEhrFhirExtractParams (taskDef , bundle )
105+ );
106+
107+ assertThat (thrown .getMessage ()).isEqualTo ("could not extract EHR Extract: empty structured access record." );
108+ }
109+
110+
111+
82112 @ Test
83113 void When_BuildEhrCompositionForSkeletonEhrExtract_Expect_ExpectedComponentBuilt () {
84114 var documentId = "documentId" ;
@@ -125,4 +155,4 @@ void When_BuildEhrCompositionForSkeletonEhrExtract_Expect_ExpectedComponentBuilt
125155
126156 assertThat (actual ).isEqualTo (expected );
127157 }
128- }
158+ }
0 commit comments