Skip to content

Commit

Permalink
adding tests for json fhir convertion (#1012)
Browse files Browse the repository at this point in the history
  • Loading branch information
ole4ryb authored Nov 27, 2024
1 parent 50492e9 commit 79031e7
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import uk.nhs.adaptors.gp2gp.common.configuration.ObjectMapperBean;
import uk.nhs.adaptors.gp2gp.common.exception.FhirValidationException;
import uk.nhs.adaptors.gp2gp.ehr.EhrResendController;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;


@ExtendWith(MockitoExtension.class)
Expand All @@ -24,6 +28,7 @@ class FhirParseServiceTest {
private static final String OPERATION_OUTCOME_URL = "https://fhir.nhs.uk/STU3/StructureDefinition/GPConnect-OperationOutcome-1";
private OperationOutcome operationOutcome;
private ObjectMapper objectMapper;
private FhirParseService fhirParseService;

@BeforeEach
void setUp() {
Expand All @@ -36,11 +41,11 @@ void setUp() {
OperationOutcome.IssueSeverity.ERROR,
details,
diagnostics);
fhirParseService = new FhirParseService();
}

@Test
void ableToEncodeOperationOutcomeToJson() throws JsonProcessingException {
FhirParseService fhirParseService = new FhirParseService();

String convertedToJsonOperationOutcome = fhirParseService.encodeToJson(operationOutcome);

Expand All @@ -53,6 +58,30 @@ void ableToEncodeOperationOutcomeToJson() throws JsonProcessingException {
assertEquals(OPERATION_OUTCOME_URL, operationOutcomeUrl);
}

@Test
void shouldEncodeResourceToPrettyPrintedJson() {
String convertedOperationlOutput = fhirParseService.encodeToJson(operationOutcome);

assertNotNull(convertedOperationlOutput);
assertTrue(convertedOperationlOutput.contains("\n"), "OperationalOutcome should contain line breaks (PrettyPrint)");
assertTrue(convertedOperationlOutput.contains(" "), "OperationalOutcome should contain indentation (PrettyPrint)");
}

@Test
void shouldHandleNullResourceGracefully() {
assertThrows(NullPointerException.class, () -> fhirParseService.encodeToJson(null));
}

@Test
void shouldThrowFhirValidationExceptionForInvalidInput() {

String invalidResourceString = "Invalid FHIR Resource";

var exception = assertThrows(FhirValidationException.class, () -> fhirParseService.parseResource(invalidResourceString,
OperationOutcome.class));
assertNotNull(exception.getMessage());
}

private static CodeableConcept getCodeableConcept() {
var details = new CodeableConcept();
var codeableConceptCoding = new Coding();
Expand Down

0 comments on commit 79031e7

Please sign in to comment.