Skip to content

Commit 99e174f

Browse files
committed
see which tests break
1 parent a0babeb commit 99e174f

File tree

2 files changed

+92
-17
lines changed

2 files changed

+92
-17
lines changed

hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4ValidateTest.java

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.hl7.fhir.r4.model.IdType;
6161
import org.hl7.fhir.r4.model.IntegerType;
6262
import org.hl7.fhir.r4.model.Location;
63+
import org.hl7.fhir.r4.model.Medication;
6364
import org.hl7.fhir.r4.model.Narrative;
6465
import org.hl7.fhir.r4.model.Observation;
6566
import org.hl7.fhir.r4.model.Observation.ObservationStatus;
@@ -171,18 +172,20 @@ public void testValidateCodeInValueSetWithUnknownCodeSystem_FailValidation() {
171172
oo = validateAndReturnOutcome(obs);
172173
String encoded = encode(oo);
173174
ourLog.info(encoded);
174-
assertThat(oo.getIssueFirstRep().getDiagnostics()).as(encoded).isEqualTo("No issues detected during validation");
175+
assertThat(oo.getIssue()).hasSize(1);
176+
assertThat(oo.getIssueFirstRep().getDiagnostics()).as(encoded).isEqualTo("CodeSystem is unknown and can't be validated: http://cs for 'http://cs#code1'");
177+
assertThat(oo.getIssueFirstRep().getSeverity()).as(encoded).isEqualTo(OperationOutcome.IssueSeverity.ERROR);
175178

176179
// Invalid code
177180
obs.setValue(new Quantity().setSystem("http://cs").setCode("code99").setValue(123));
178181
oo = validateAndReturnOutcome(obs);
179182
encoded = encode(oo);
180183
ourLog.info(encoded);
181-
assertThat(oo.getIssue().size()).as(encoded).isEqualTo(1);
182-
assertThat(oo.getIssue().get(0).getDiagnostics()).contains("provided (http://cs#code99) was not found in the value set");
183-
assertThat(oo.getIssue().get(0).getDiagnostics()).contains("Unknown code 'http://cs#code99' for in-memory expansion of ValueSet 'http://vs'");
184-
assertThat(oo.getIssueFirstRep().getSeverity()).as(encoded).isEqualTo(OperationOutcome.IssueSeverity.ERROR);
185-
184+
assertThat(oo.getIssue().size()).as(encoded).isEqualTo(2);
185+
assertThat(oo.getIssueFirstRep().getDiagnostics()).as(encoded).isEqualTo("CodeSystem is unknown and can't be validated: http://cs for 'http://cs#code99'");
186+
assertThat(oo.getIssue().get(1).getDiagnostics()).contains("provided (http://cs#code99) was not found in the value set");
187+
assertThat(oo.getIssue().get(1).getDiagnostics()).contains("Unknown code 'http://cs#code99' for in-memory expansion of ValueSet 'http://vs'");
188+
assertThat(oo.getIssue().get(1).getSeverity()).as(encoded).isEqualTo(OperationOutcome.IssueSeverity.ERROR);
186189
}
187190

188191
@Test
@@ -274,20 +277,22 @@ public void testValidateCodeInEnumeratedValueSetWithUnknownCodeSystem_Error() {
274277

275278
// Valid code
276279
obs.setValue(new Quantity().setSystem("http://cs").setCode("code1").setValue(123));
277-
oo = validateAndReturnOutcome(obs, false);
280+
//the code system is unknown, we should expect an error for that, but no error related to the code not being in the value set, since it is valid
281+
oo = validateAndReturnOutcome(obs, true);
278282
encoded = encode(oo);
279283
ourLog.info(encoded);
280284
assertThat(oo.getIssue()).hasSize(1);
281-
assertThat(oo.getIssueFirstRep().getDiagnostics()).contains("No issues detected during validation");
282-
assertEquals(OperationOutcome.IssueSeverity.INFORMATION, oo.getIssueFirstRep().getSeverity());
285+
assertThat(oo.getIssueFirstRep().getDiagnostics()).contains("CodeSystem is unknown and can't be validated: http://cs for 'http://cs#code1'");
286+
assertEquals(OperationOutcome.IssueSeverity.ERROR, oo.getIssueFirstRep().getSeverity());
283287

284288
// Invalid code
285289
obs.setValue(new Quantity().setSystem("http://cs").setCode("code99").setValue(123));
286290
oo = validateAndReturnOutcome(obs, true);
287291
encoded = encode(oo);
288292
ourLog.info(encoded);
289-
assertThat(oo.getIssue()).hasSize(1);
290-
assertThat(oo.getIssue().get(0).getDiagnostics()).contains("provided (http://cs#code99) was not found in the value set");
293+
assertThat(oo.getIssue()).hasSize(2);
294+
assertThat(oo.getIssue().get(0).getDiagnostics()).contains("CodeSystem is unknown and can't be validated: http://cs for 'http://cs#code99'");
295+
assertThat(oo.getIssue().get(1).getDiagnostics()).contains("provided (http://cs#code99) was not found in the value set");
291296
assertEquals(OperationOutcome.IssueSeverity.ERROR, oo.getIssueFirstRep().getSeverity());
292297
}
293298

@@ -687,7 +692,8 @@ public void testValidateCode_InMemoryExpansionAgainstHugeValueSet() throws Excep
687692
obs.getText().setStatus(Narrative.NarrativeStatus.GENERATED);
688693
obs.getCode().getCodingFirstRep().setSystem("http://foo").setCode("CODE3").setDisplay("Display 3");
689694
oo = validateAndReturnOutcome(obs);
690-
assertThat(oo.getIssueFirstRep().getDiagnostics()).as(encode(oo)).isEqualTo("None of the codings provided are in the value set 'ValueSet[http://example.com/fhir/ValueSet/observation-vitalsignresult]' (http://example.com/fhir/ValueSet/observation-vitalsignresult), and a coding from this value set is required) (codes = http://foo#CODE3)");
695+
assertThat(oo.getIssue().get(0).getDiagnostics()).as(encode(oo)).isEqualTo("CodeSystem is unknown and can't be validated: http://foo for 'http://foo#CODE3'");
696+
assertThat(oo.getIssue().get(1).getDiagnostics()).as(encode(oo)).isEqualTo("None of the codings provided are in the value set 'ValueSet[http://example.com/fhir/ValueSet/observation-vitalsignresult]' (http://example.com/fhir/ValueSet/observation-vitalsignresult), and a coding from this value set is required) (codes = http://foo#CODE3)");
691697

692698
// Code that exists but isn't in the valueset
693699
obs.getText().setStatus(Narrative.NarrativeStatus.GENERATED);
@@ -726,6 +732,9 @@ public void testValidateCode_InMemoryExpansionAgainstHugeValueSet() throws Excep
726732
@ParameterizedTest
727733
@MethodSource("paramsUnknownCodeSystemBindingStrengths")
728734
void testValidateCode_unknownCodeSystem_returnsCorrectSeverityForDifferentBindingStrengths(Enumerations.BindingStrength theStructureDefinitionStrength, int theExpectedNumIssues, String theExpectedSeverity, String theExpectedDiagnosticsMessage) throws Exception {
735+
736+
myUnknownCodeSystemWarningValidationSupport.setNonExistentCodeSystemSeverity(IValidationSupport.IssueSeverity.INFORMATION);
737+
729738
// Given
730739
myStorageSettings.setPreExpandValueSets(true);
731740

@@ -756,6 +765,8 @@ void testValidateCode_unknownCodeSystem_returnsCorrectSeverityForDifferentBindin
756765
// When
757766
oo = validateAndReturnOutcome(org);
758767

768+
String encoded = encode(oo);
769+
ourLog.info("HERE:" + encoded);
759770
// Then
760771
assertThat(oo.getIssue()).hasSize(theExpectedNumIssues);
761772
assertThat(oo.getIssueFirstRep().getSeverity().getDisplay()).isEqualTo(theExpectedSeverity);
@@ -1110,7 +1121,8 @@ public void testValidateWithFragmentCodeSystem_WithDirectBinding() throws IOExce
11101121
outcome = (OperationOutcome) myObservationDao.validate(obs, null, null, null, ValidationModeEnum.CREATE, "http://example.com/structuredefinition", mySrd).getOperationOutcome();
11111122
assertHasErrors(outcome);
11121123
ourLog.debug("Outcome: {}", myFhirContext.newJsonParser().setPrettyPrint(true).encodeResourceToString(outcome));
1113-
assertThat(outcome.getIssueFirstRep().getDiagnostics()).contains("None of the codings provided are in the value set 'MessageCategory'");
1124+
assertThat(outcome.getIssue().get(0).getDiagnostics()).contains("CodeSystem is unknown and can't be validated: http://example.com/foo-foo for 'http://example.com/foo-foo#some-code'");
1125+
assertThat(outcome.getIssue().get(1).getDiagnostics()).contains("None of the codings provided are in the value set 'MessageCategory'");
11141126
assertEquals(OperationOutcome.IssueSeverity.ERROR, outcome.getIssueFirstRep().getSeverity());
11151127
}
11161128

@@ -1194,7 +1206,8 @@ public void testValidateCode_PreExpansionAgainstHugeValueSet() throws Exception
11941206
obs.getText().setStatus(Narrative.NarrativeStatus.GENERATED);
11951207
obs.getCode().getCodingFirstRep().setSystem("http://foo").setCode("CODE3").setDisplay("Display 3");
11961208
oo = validateAndReturnOutcome(obs);
1197-
assertThat(oo.getIssue().get(1).getDiagnostics()).as(encode(oo)).isEqualTo("None of the codings provided are in the value set 'ValueSet[http://example.com/fhir/ValueSet/observation-vitalsignresult]' (http://example.com/fhir/ValueSet/observation-vitalsignresult), and a coding from this value set is required) (codes = http://foo#CODE3)");
1209+
assertThat(oo.getIssue().get(1).getDiagnostics()).as(encode(oo)).contains("CodeSystem is unknown and can't be validated: http://foo for 'http://foo#CODE3'");
1210+
assertThat(oo.getIssue().get(2).getDiagnostics()).as(encode(oo)).isEqualTo("None of the codings provided are in the value set 'ValueSet[http://example.com/fhir/ValueSet/observation-vitalsignresult]' (http://example.com/fhir/ValueSet/observation-vitalsignresult), and a coding from this value set is required) (codes = http://foo#CODE3)");
11981211

11991212
// Code that exists but isn't in the valueset
12001213
obs.getText().setStatus(Narrative.NarrativeStatus.GENERATED);
@@ -1217,13 +1230,15 @@ public void testValidateCode_PreExpansionAgainstHugeValueSet() throws Exception
12171230
@Test
12181231
public void testValidate_ValidationSupportThrowsException() {
12191232
IValidationSupport validationSupport = mock(IValidationSupport.class);
1233+
when(validationSupport.isValueSetSupported(any(), any())).thenReturn(true);
12201234
when(validationSupport.validateCodeInValueSet(any(), any(), any(), any(), any(), any())).thenAnswer(t -> {
12211235
// This will fail with a constraint error
12221236
try {
12231237
myResourceTableDao.save(new ResourceTable());
12241238
myResourceTableDao.flush();
12251239
} catch (Exception e) {
12261240
ourLog.info("Hit expected exception: {}", e.toString());
1241+
throw e;
12271242
}
12281243
return null;
12291244
});
@@ -1245,8 +1260,11 @@ public void testValidate_ValidationSupportThrowsException() {
12451260

12461261
// Valid code
12471262
obs.getText().setStatus(Narrative.NarrativeStatus.GENERATED);
1248-
obs.getCode().getCodingFirstRep().setSystem("http://loinc.org").setCode("CODE3").setDisplay("Display 3");
1263+
obs.getCode().getCodingFirstRep().setSystem("http://loinc.org").setCode("100022-3").setDisplay("Display 3");
12491264
oo = validateAndReturnOutcome(obs);
1265+
1266+
String encoded = encode(oo);
1267+
ourLog.info("HERE:" + encoded);
12501268
assertThat(oo.getIssueFirstRep().getDiagnostics()).as(encode(oo)).isEqualTo("No issues detected during validation");
12511269

12521270
} finally {
@@ -2219,7 +2237,10 @@ public void testValidateCodeInUnknownCodeSystemWithRequiredBinding() throws IOEx
22192237
OperationOutcome oo = (OperationOutcome) result.getOperationOutcome();
22202238
assertHasErrors(oo);
22212239
ourLog.debug(myFhirContext.newJsonParser().setPrettyPrint(true).encodeResourceToString(oo));
2222-
assertThat(oo.getIssueFirstRep().getDiagnostics()).contains("None of the codings provided are in the value set 'Condition Clinical Status Codes' (http://hl7.org/fhir/ValueSet/condition-clinical|4.0.1), and a coding from this value set is required) (codes = http://terminology.hl7.org/CodeSystem/condition-clinical/wrong-system#notrealcode)");
2240+
assertThat(oo.getIssue().get(0).getDiagnostics()).contains("CodeSystem is unknown and can't be validated: http://terminology.hl7.org/CodeSystem/condition-clinical/wrong-system for 'http://terminology.hl7.org/CodeSystem/condition-clinical/wrong-system#notrealcode");
2241+
assertThat(oo.getIssue().get(0).getSeverity()).isEqualTo(OperationOutcome.IssueSeverity.ERROR);
2242+
assertThat(oo.getIssue().get(1).getDiagnostics()).contains("None of the codings provided are in the value set 'Condition Clinical Status Codes' (http://hl7.org/fhir/ValueSet/condition-clinical|4.0.1), and a coding from this value set is required) (codes = http://terminology.hl7.org/CodeSystem/condition-clinical/wrong-system#notrealcode)");
2243+
assertThat(oo.getIssue().get(1).getSeverity()).isEqualTo(OperationOutcome.IssueSeverity.ERROR);
22232244
}
22242245

22252246
private IBaseResource findResourceByIdInBundle(Bundle vss, String name) {
@@ -2494,4 +2515,58 @@ public void testValidateObservationWithVitalSignsLoincCode() {
24942515
assertHasNoErrors(oo);
24952516
}
24962517

2518+
@Test
2519+
public void testValidateCodeInValueSetWithUnknownCodeSystem_whenWarningSeverityConfigured_mustRaiseWarningAndSucceed() {
2520+
myUnknownCodeSystemWarningValidationSupport.setNonExistentCodeSystemSeverity(IValidationSupport.IssueSeverity.WARNING);
2521+
2522+
Medication medication = new Medication();
2523+
medication.setCode(new CodeableConcept().addCoding(new Coding("http://test123.org", "golden", "xxx")));
2524+
//medication.setCode(new CodeableConcept().addCoding(new Coding("http://hl7.org/fhir/universal-tag", "golden", "xxx")));
2525+
//medication.setCode(new CodeableConcept().addCoding(new Coding("http://snomed.info/sct", "00559407", "xxx")));
2526+
medication.getText().setDiv(new XhtmlNode().setValue("<div>AA</div>")).setStatus(Narrative.NarrativeStatus.GENERATED);
2527+
2528+
OperationOutcome oo;
2529+
String encoded;
2530+
2531+
// execute
2532+
oo = validateAndReturnOutcome(medication);
2533+
2534+
// verify
2535+
encoded = encode(oo);
2536+
ourLog.info(encoded);
2537+
assertThat(oo.getIssueFirstRep().getSeverity()).isEqualTo(OperationOutcome.IssueSeverity.WARNING);
2538+
assertThat(oo.getIssueFirstRep().getDiagnostics()).contains("CodeSystem is unknown and can't be validated: http://test123.org for 'http://test123.org#golden'");
2539+
}
2540+
2541+
2542+
@Test
2543+
// @CsvSource({"ERROR", "WARNING"})
2544+
// public void testValidateCodeInValueSetWithUnknownCodeSystem_whenDefaultSeverityConfigured_mustRaiseErrorAndFail(String theIssueSeverity) {
2545+
public void testValidateCodeInValueSetWithUnknownCodeSystem_whenDefaultSeverityConfigured_mustRaiseErrorAndFail() {
2546+
myUnknownCodeSystemWarningValidationSupport.setNonExistentCodeSystemSeverity(IValidationSupport.IssueSeverity.ERROR);
2547+
2548+
Medication medication = new Medication();
2549+
2550+
//medication.getMeta().addTag(new Coding("http://hl7.org/fhir/universal-tag", "golden", "xxx"));
2551+
//medication.getMeta().addTag(new Coding("http://test123.org", "golden", "xxx"));
2552+
medication.setCode(new CodeableConcept().addCoding(new Coding("http://test123.org", "golden", "xxx")));
2553+
//medication.setCode(new CodeableConcept().addCoding(new Coding("http://hl7.org/fhir/universal-tag", "golden", "xxx")));
2554+
//medication.setCode(new CodeableConcept().addCoding(new Coding("http://snomed.info/scts", "00559407", "xxx")));
2555+
medication.getText().setDiv(new XhtmlNode().setValue("<div>AA</div>")).setStatus(Narrative.NarrativeStatus.GENERATED);
2556+
2557+
OperationOutcome oo;
2558+
String encoded;
2559+
2560+
// execute
2561+
oo = validateAndReturnOutcome(medication);
2562+
2563+
// verify
2564+
encoded = encode(oo);
2565+
ourLog.info("HERE:" + encoded);
2566+
assertThat(oo.getIssueFirstRep().getSeverity()).isEqualTo(OperationOutcome.IssueSeverity.ERROR);
2567+
assertThat(oo.getIssueFirstRep().getDiagnostics()).contains("CodeSystem is unknown and can't be validated: https//test123.org for 'http://test123.org#10000'");
2568+
}
2569+
2570+
24972571
}
2572+

hapi-fhir-validation/src/main/java/org/hl7/fhir/common/hapi/validation/support/UnknownCodeSystemWarningValidationSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private boolean allowNonExistentCodeSystems() {
126126
switch (myNonExistentCodeSystemSeverity) {
127127
case ERROR:
128128
case FATAL:
129-
return false;
129+
return true;
130130
case WARNING:
131131
case INFORMATION:
132132
return true;

0 commit comments

Comments
 (0)