Skip to content

Commit 79031e7

Browse files
authored
adding tests for json fhir convertion (#1012)
1 parent 50492e9 commit 79031e7

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

service/src/test/java/uk/nhs/adaptors/gp2gp/common/service/FhirParseServiceTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
import org.mockito.junit.jupiter.MockitoExtension;
1313
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
1414
import uk.nhs.adaptors.gp2gp.common.configuration.ObjectMapperBean;
15+
import uk.nhs.adaptors.gp2gp.common.exception.FhirValidationException;
1516
import uk.nhs.adaptors.gp2gp.ehr.EhrResendController;
1617
import java.util.List;
1718
import 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

Comments
 (0)