Skip to content

Commit 2b95c8a

Browse files
committed
tests for enforcing strict error handling while converting json
1 parent 79031e7 commit 2b95c8a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

service/src/main/java/uk/nhs/adaptors/gp2gp/common/service/FhirParseService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public String encodeToJson(IBaseResource resource) {
2626

2727
private IParser prepareParser() {
2828
FhirContext ctx = FhirContext.forDstu3();
29-
ctx.newJsonParser();
3029
ctx.setParserErrorHandler(new StrictErrorHandler());
3130
return ctx.newJsonParser();
3231
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package uk.nhs.adaptors.gp2gp.common.service;
22

3+
import ca.uhn.fhir.parser.DataFormatException;
34
import com.fasterxml.jackson.core.JsonProcessingException;
45
import com.fasterxml.jackson.databind.JsonNode;
56
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -44,6 +45,32 @@ void setUp() {
4445
fhirParseService = new FhirParseService();
4546
}
4647

48+
@Test
49+
void shouldThrowValidationExceptionForInvalidJsonDiagnosticsField() {
50+
51+
String invalidJson = "{\n"
52+
+ " \"resourceType\": \"OperationOutcome\",\n"
53+
+ " \"meta\": {\n"
54+
+ " \"profile\": [ \"https://fhir.nhs.uk/STU3/StructureDefinition/GPConnect-OperationOutcome-1\" ]\n"
55+
+ " },\n"
56+
+ " \"issue\": [ {\n"
57+
+ " \"severity\": \"error\",\n"
58+
+ " \"code\": \"value\",\n"
59+
+ " \"details\": {\n"
60+
+ " \"coding\": [ {\n"
61+
+ " \"system\": \"http://fhir.nhs.net/ValueSet/gpconnect-error-or-warning-code-1\",\n"
62+
+ " \"code\": \"INVALID_IDENTIFIER_VALUE\"\n"
63+
+ " } ]\n"
64+
+ " },\n"
65+
+ " \"diagnosticos\": \"Provide a conversationId that exists and retry the operation\"\n"
66+
+ " } ]\n"
67+
+ "}";
68+
69+
assertThrows(FhirValidationException.class, () -> {
70+
fhirParseService.parseResource(invalidJson, OperationOutcome.class);
71+
});
72+
}
73+
4774
@Test
4875
void ableToEncodeOperationOutcomeToJson() throws JsonProcessingException {
4976

0 commit comments

Comments
 (0)