-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add default implementation of reference validator #176
Open
pcvolkmer
wants to merge
1
commit into
bzkf:beta
Choose a base branch
from
CCC-MF:feat_referencevalidation
base: beta
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 30 additions & 0 deletions
30
src/main/java/org/miracum/streams/ume/obdstofhir/mapper/mii/Mapper.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,30 @@ | ||
package org.miracum.streams.ume.obdstofhir.mapper.mii; | ||
|
||
import java.util.Objects; | ||
import org.apache.commons.lang3.Validate; | ||
import org.hl7.fhir.r4.model.Enumerations.ResourceType; | ||
import org.hl7.fhir.r4.model.Reference; | ||
|
||
public interface Mapper<S, D> { | ||
|
||
/** | ||
* Default implementation of reference validation. This does not check the existance of the | ||
* referenced resource! | ||
* | ||
* @param reference The reference to be validated | ||
* @param resourceType The required resource type of the reference | ||
* @return Will return `true` if reference is usable | ||
* @throws NullPointerException if reference is null | ||
* @throws IllegalArgumentException if reference is not of required reesource type. | ||
*/ | ||
default boolean verifyReference(Reference reference, ResourceType resourceType) | ||
throws NullPointerException, IllegalArgumentException { | ||
Objects.requireNonNull( | ||
reference, String.format("Reference to %s must not be null", resourceType.toString())); | ||
Validate.isTrue( | ||
Objects.equals(reference.getReferenceElement().getResourceType(), resourceType.toCode()), | ||
String.format("The reference should point to a %s resource", resourceType.toString())); | ||
|
||
return true; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package org.miracum.streams.ume.obdstofhir.mapper.mii; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import ca.uhn.fhir.context.FhirContext; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
|
@@ -12,6 +13,7 @@ | |
import org.approvaltests.Approvals; | ||
import org.hl7.fhir.r4.model.Reference; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.miracum.streams.ume.obdstofhir.FhirProperties; | ||
|
@@ -60,4 +62,64 @@ void map_withGivenObds_shouldCreateValidProcedure(String sourceFile) throws IOEx | |
Approvals.verify( | ||
fhirJson, Approvals.NAMES.withParameters(sourceFile).forFile().withExtension(".fhir.json")); | ||
} | ||
|
||
@Test | ||
void shouldNotVerifyNullReferences() throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Würde es sich statt in jedem Mapper-Tests das nochmal zu prüfen sich anbieten lieber nur das verifyReference in isolation zu testen? |
||
final var resource = this.getClass().getClassLoader().getResource("obds3/Testpatient_1.xml"); | ||
assertThat(resource).isNotNull(); | ||
|
||
final var xmlMapper = | ||
XmlMapper.builder() | ||
.defaultUseWrapper(false) | ||
.addModule(new JakartaXmlBindAnnotationModule()) | ||
.addModule(new Jdk8Module()) | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
.build(); | ||
|
||
final var obds = xmlMapper.readValue(resource.openStream(), OBDS.class); | ||
|
||
var obdsPatient = obds.getMengePatient().getPatient().getFirst(); | ||
|
||
var wrongPatient = (Reference) null; | ||
var stMeldung = | ||
obdsPatient.getMengeMeldung().getMeldung().stream() | ||
.filter(m -> m.getST() != null) | ||
.findFirst() | ||
.get(); | ||
|
||
var ex = | ||
assertThrows(NullPointerException.class, () -> sut.map(stMeldung.getST(), wrongPatient)); | ||
assertThat(ex).hasMessage("Reference to PATIENT must not be null"); | ||
} | ||
|
||
@Test | ||
void shouldNotVerifyWrongReferencesType() throws Exception { | ||
final var resource = this.getClass().getClassLoader().getResource("obds3/Testpatient_1.xml"); | ||
assertThat(resource).isNotNull(); | ||
|
||
final var xmlMapper = | ||
XmlMapper.builder() | ||
.defaultUseWrapper(false) | ||
.addModule(new JakartaXmlBindAnnotationModule()) | ||
.addModule(new Jdk8Module()) | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
.build(); | ||
|
||
final var obds = xmlMapper.readValue(resource.openStream(), OBDS.class); | ||
|
||
var obdsPatient = obds.getMengePatient().getPatient().getFirst(); | ||
|
||
var wrongPatient = new Reference("Procedure/any"); | ||
var stMeldung = | ||
obdsPatient.getMengeMeldung().getMeldung().stream() | ||
.filter(m -> m.getST() != null) | ||
.findFirst() | ||
.get(); | ||
|
||
var ex = | ||
assertThrows( | ||
IllegalArgumentException.class, () -> sut.map(stMeldung.getST(), wrongPatient)); | ||
|
||
assertThat(ex).hasMessage("The reference should point to a PATIENT resource"); | ||
} | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
könnte das auch eine static methode in einer util klasse sein ?
oder was ist der vorteil vom implements.... ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finde ich auch einen guten Vorschlag! Ich glaub das mit dem interface stammt noch aus der früheren Idee einer generischen Mapper-Schnittstelle. Man kann es auch als Methode in den ObdsToFhirMapper einbauen - auch wenn der so langsam zu einer kleinen Müllhalde an helper/util-Methoden wird.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Könnte auch eine static method sein. Ich hatte es nur als default impl in einem Interface umgesetzt, da im November einmal die Frage aufkam, ob wir nicht eine Schnittstelle für alle Mapper definieren wollen.