1212import org .mockito .junit .jupiter .MockitoExtension ;
1313import org .springframework .http .converter .json .Jackson2ObjectMapperBuilder ;
1414import uk .nhs .adaptors .gp2gp .common .configuration .ObjectMapperBean ;
15+ import uk .nhs .adaptors .gp2gp .common .exception .FhirValidationException ;
1516import uk .nhs .adaptors .gp2gp .ehr .EhrResendController ;
1617import java .util .List ;
1718import static org .junit .jupiter .api .Assertions .assertEquals ;
19+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
20+ import static org .junit .jupiter .api .Assertions .assertThrows ;
21+ import static org .junit .jupiter .api .Assertions .assertTrue ;
1822
1923
2024@ ExtendWith (MockitoExtension .class )
@@ -24,6 +28,7 @@ class FhirParseServiceTest {
2428 private static final String OPERATION_OUTCOME_URL = "https://fhir.nhs.uk/STU3/StructureDefinition/GPConnect-OperationOutcome-1" ;
2529 private OperationOutcome operationOutcome ;
2630 private ObjectMapper objectMapper ;
31+ private FhirParseService fhirParseService ;
2732
2833 @ BeforeEach
2934 void setUp () {
@@ -36,11 +41,11 @@ void setUp() {
3641 OperationOutcome .IssueSeverity .ERROR ,
3742 details ,
3843 diagnostics );
44+ fhirParseService = new FhirParseService ();
3945 }
4046
4147 @ Test
4248 void ableToEncodeOperationOutcomeToJson () throws JsonProcessingException {
43- FhirParseService fhirParseService = new FhirParseService ();
4449
4550 String convertedToJsonOperationOutcome = fhirParseService .encodeToJson (operationOutcome );
4651
@@ -53,6 +58,30 @@ void ableToEncodeOperationOutcomeToJson() throws JsonProcessingException {
5358 assertEquals (OPERATION_OUTCOME_URL , operationOutcomeUrl );
5459 }
5560
61+ @ Test
62+ void shouldEncodeResourceToPrettyPrintedJson () {
63+ String convertedOperationlOutput = fhirParseService .encodeToJson (operationOutcome );
64+
65+ assertNotNull (convertedOperationlOutput );
66+ assertTrue (convertedOperationlOutput .contains ("\n " ), "OperationalOutcome should contain line breaks (PrettyPrint)" );
67+ assertTrue (convertedOperationlOutput .contains (" " ), "OperationalOutcome should contain indentation (PrettyPrint)" );
68+ }
69+
70+ @ Test
71+ void shouldHandleNullResourceGracefully () {
72+ assertThrows (NullPointerException .class , () -> fhirParseService .encodeToJson (null ));
73+ }
74+
75+ @ Test
76+ void shouldThrowFhirValidationExceptionForInvalidInput () {
77+
78+ String invalidResourceString = "Invalid FHIR Resource" ;
79+
80+ var exception = assertThrows (FhirValidationException .class , () -> fhirParseService .parseResource (invalidResourceString ,
81+ OperationOutcome .class ));
82+ assertNotNull (exception .getMessage ());
83+ }
84+
5685 private static CodeableConcept getCodeableConcept () {
5786 var details = new CodeableConcept ();
5887 var codeableConceptCoding = new Coding ();
0 commit comments