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 ;
7- import static org .mockito .Mockito .when ;
6+ import static org .junit . jupiter . api . Assertions . assertThrows ;
7+ import static org .mockito .Mockito .* ;
88
99import java .time .Instant ;
1010import java .time .temporal .ChronoUnit ;
11+ import java .util .Arrays ;
12+ import java .util .Collections ;
1113
1214import org .hl7 .fhir .dstu3 .model .Bundle ;
1315import org .junit .jupiter .api .Test ;
1921
2022import uk .nhs .adaptors .gp2gp .common .service .RandomIdGeneratorService ;
2123import uk .nhs .adaptors .gp2gp .common .service .TimestampService ;
24+ import uk .nhs .adaptors .gp2gp .ehr .exception .EhrValidationException ;
2225import uk .nhs .adaptors .gp2gp .gpc .GetGpcStructuredTaskDefinition ;
2326
2427@ ExtendWith (MockitoExtension .class )
@@ -50,6 +53,8 @@ class EhrExtractMapperTest {
5053
5154 @ Test
5255 void When_NhsOverrideNumberProvided_Expect_OverrideToBeUsed () {
56+ when (nonConsultationResourceMapper .mapRemainingResourcesToEhrCompositions (any ()))
57+ .thenReturn (Arrays .asList ("not" , "empty" ));
5358 ReflectionTestUtils .setField (ehrExtractMapper , OVERRIDE_NHS_NUMBER , OVERRIDE_NHS_NUMBER_VALUE );
5459 when (agentDirectoryMapper .mapEHRFolderToAgentDirectory (any (Bundle .class ), eq (OVERRIDE_NHS_NUMBER_VALUE )))
5560 .thenReturn (OVERRIDE_NHS_NUMBER_VALUE );
@@ -66,6 +71,8 @@ void When_NhsOverrideNumberProvided_Expect_OverrideToBeUsed() {
6671
6772 @ Test
6873 void When_NhsOverrideNumberIsBlank_Expect_ActualNhsNumberIsUsed () {
74+ when (nonConsultationResourceMapper .mapRemainingResourcesToEhrCompositions (any ()))
75+ .thenReturn (Arrays .asList ("not" , "empty" ));
6976 when (agentDirectoryMapper .mapEHRFolderToAgentDirectory (any (Bundle .class ), eq (NHS_NUMBER )))
7077 .thenReturn (NHS_NUMBER );
7178 when (timestampService .now ()).thenReturn (Instant .now ().truncatedTo (ChronoUnit .MILLIS ));
@@ -79,6 +86,27 @@ void When_NhsOverrideNumberIsBlank_Expect_ActualNhsNumberIsUsed() {
7986 assertThat (parameters .getAgentDirectory ()).isEqualTo (NHS_NUMBER );
8087 }
8188
89+ @ Test
90+ void When_BundleHasNoMappableContent_Expect_EhrValidationExceptionIsThrown () {
91+ when (timestampService .now ()).thenReturn (Instant .now ().truncatedTo (ChronoUnit .MILLIS ));
92+
93+ var taskDef = GetGpcStructuredTaskDefinition .builder ()
94+ .nhsNumber (NHS_NUMBER )
95+ .build ();
96+ var bundle = mock (Bundle .class );
97+
98+ lenient ().when (nonConsultationResourceMapper .mapRemainingResourcesToEhrCompositions (any (Bundle .class )))
99+ .thenReturn (Collections .emptyList ());
100+
101+ EhrValidationException thrown = assertThrows (EhrValidationException .class , () ->
102+ ehrExtractMapper .mapBundleToEhrFhirExtractParams (taskDef , bundle )
103+ );
104+
105+ assertThat (thrown .getMessage ()).isEqualTo ("couldn't extract EHR Extract: empty structured access record." );
106+ }
107+
108+
109+
82110 @ Test
83111 void When_BuildEhrCompositionForSkeletonEhrExtract_Expect_ExpectedComponentBuilt () {
84112 var documentId = "documentId" ;
0 commit comments