-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automerge Pull-Request for Release 2.5.0
- Loading branch information
Showing
30 changed files
with
2,033 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
commons/src/main/java/de/gematik/refv/commons/validation/BundleValidationModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright (c) 2022-2024 gematik GmbH | ||
Licensed under the Apache License, Version 2.0 (the License); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an 'AS IS' BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package de.gematik.refv.commons.validation; | ||
|
||
import ca.uhn.fhir.validation.IValidationContext; | ||
import ca.uhn.fhir.validation.IValidatorModule; | ||
import ca.uhn.fhir.validation.ResultSeverityEnum; | ||
import ca.uhn.fhir.validation.SingleValidationMessage; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.hl7.fhir.instance.model.api.IBaseResource; | ||
import org.hl7.fhir.r4.model.Bundle; | ||
import org.hl7.fhir.utilities.Utilities; | ||
|
||
import java.text.MessageFormat; | ||
|
||
/** | ||
* The module is a backport of fullUrl and ID match validation introduced by a <a href="https://github.com/hapifhir/org.hl7.fhir.core/commit/4c6a318749d164466d2bf31918a3969f239c4ca8">version of Java Core Validator</a>, which is later than the one used at the moment. | ||
* However, the original implementation behaves incorrectly for fullUrls being URNs (cf. BundleValidationModuleTests.validateResourceWithValidUrnFullUrlAndId). | ||
* Once the original implementation is fixed, this module can be completely removed. | ||
*/ | ||
class BundleValidationModule implements IValidatorModule { | ||
|
||
private static final String FULLURL_AND_ID_MISMATCH_CODE = "BUNDLE_ENTRY_URL_MATCHES_TYPE_ID"; | ||
private static final String MESSAGE_TEMPLATE = "The fullUrl ''{0}'' looks like a RESTful server URL, so it must end with the correct type and id (/{1}/{2})"; | ||
|
||
@Override | ||
public void validateResource(IValidationContext<IBaseResource> iValidationContext) { | ||
var resource = iValidationContext.getResource(); | ||
if(!(resource instanceof Bundle)) | ||
return; | ||
|
||
var bundle = (Bundle)resource; | ||
var i = 0; | ||
for(var entry: bundle.getEntry()) { | ||
String fullUrl = entry.getFullUrl(); | ||
String resourceType = entry.getResource().fhirType(); | ||
String id = entry.getResource().getIdPart(); | ||
|
||
if(StringUtils.isBlank(fullUrl) || StringUtils.isBlank(id) || !Utilities.isURL(fullUrl)) | ||
continue; | ||
|
||
if(!fullUrl.endsWith("/"+ resourceType + "/" + id)) { | ||
SingleValidationMessage message = new SingleValidationMessage(); | ||
message.setSeverity(ResultSeverityEnum.WARNING); | ||
message.setMessage(MessageFormat.format(MESSAGE_TEMPLATE, fullUrl, resourceType, id)); | ||
message.setMessageId(FULLURL_AND_ID_MISMATCH_CODE); | ||
message.setLocationString(MessageFormat.format("Bundle.entry[{0}]", i)); | ||
iValidationContext.addValidationMessage(message); | ||
} | ||
|
||
i++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
commons/src/test/java/de/gematik/refv/commons/validation/BundleValidationModuleTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Copyright (c) 2022-2024 gematik GmbH | ||
Licensed under the Apache License, Version 2.0 (the License); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an 'AS IS' BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package de.gematik.refv.commons.validation; | ||
|
||
import ca.uhn.fhir.context.FhirContext; | ||
import ca.uhn.fhir.validation.IValidationContext; | ||
import ca.uhn.fhir.validation.ResultSeverityEnum; | ||
import ca.uhn.fhir.validation.ValidationContext; | ||
import org.hl7.fhir.instance.model.api.IBaseResource; | ||
import org.hl7.fhir.r4.model.Bundle; | ||
import org.hl7.fhir.r4.model.Patient; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class BundleValidationModuleTests { | ||
|
||
|
||
@CsvSource({ | ||
"http://example.com/Patient/123,123", | ||
"urn:test:blabla,123", // Non-URL fullUrl | ||
"Patient/456,123", // relative fullUrl | ||
",123", // empty fullUrl | ||
"http://example.com/Patient/123,", // empty id | ||
}) | ||
@ParameterizedTest | ||
void validateResourceWithValidFullUrlAndId(String fullUrl, String id) { | ||
Bundle bundle = new Bundle(); | ||
Bundle.BundleEntryComponent entry = new Bundle.BundleEntryComponent(); | ||
entry.setFullUrl(fullUrl); | ||
entry.setResource(new Patient().setId(id)); | ||
bundle.addEntry(entry); | ||
|
||
IValidationContext<IBaseResource> context = createValidationContext(bundle); | ||
new BundleValidationModule().validateResource(context); | ||
|
||
assertTrue(context.toResult().getMessages().isEmpty(), "No validation messages should be present for valid fullUrl and id: " + context.toResult().toString()); | ||
} | ||
|
||
@Test | ||
void validateResourceWithFullUrlNotEndingWithId() { | ||
Bundle bundle = new Bundle(); | ||
Bundle.BundleEntryComponent entry = new Bundle.BundleEntryComponent(); | ||
entry.setFullUrl("http://example.com/Patient/456"); | ||
entry.setResource(new Patient().setId("123")); | ||
bundle.addEntry(entry); | ||
|
||
IValidationContext<IBaseResource> context = createValidationContext(bundle); | ||
new BundleValidationModule().validateResource(context); | ||
|
||
assertEquals(1, context.toResult().getMessages().size(), "One validation message should be present for fullUrl not ending with id"); | ||
assertEquals(ResultSeverityEnum.WARNING, context.toResult().getMessages().get(0).getSeverity(), "Validation message should be a warning"); | ||
} | ||
|
||
@Test | ||
void validateResourceWithNonBundleResource() { | ||
Patient patient = new Patient(); | ||
IValidationContext<IBaseResource> context = createValidationContext(patient); | ||
new BundleValidationModule().validateResource(context); | ||
|
||
assertTrue(context.toResult().getMessages().isEmpty(), "No validation messages should be present for non-bundle resource"); | ||
} | ||
|
||
private IValidationContext<IBaseResource> createValidationContext(IBaseResource resource) { | ||
return ValidationContext.forResource(FhirContext.forR4Cached(), resource, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.