From 812d57c3fc9bf0371680e19e93b639f14fd9e5cd Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Fri, 30 Aug 2024 17:47:20 +0200 Subject: [PATCH] fix: check if Pat-ID is reasonable value (#90) * test: add test for existing method This includes simple refac by returning early if match has been found * feat: add configuration for patient id pattern * test: default pattern is used w/o config * docs: use config entry as usage example * test: add test data for old PatIDs in Erlangen with first digit "G" * chore: update logging message This updates the logging message shown if the input does not match the pattern. Co-authored-by: chgl * config: update config entry to lowercase kebab-case This also re enables this config option with a default value. Co-authored-by: chgl * chore: change @Value to match lowercase kebab case Co-authored-by: chgl * docs: change inline comments in unit tests Co-authored-by: chgl --------- Co-authored-by: chgl --- .../obdstofhir/mapper/ObdsToFhirMapper.java | 21 ++++-- src/main/resources/application.yml | 1 + .../mapper/ObdsToFhirIntegrationTest.java | 66 +++++++++++++++++++ .../mapper/ObdsToFhirMapperTests.java | 16 +++++ 4 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsToFhirIntegrationTest.java diff --git a/src/main/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsToFhirMapper.java b/src/main/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsToFhirMapper.java index 0261bdb7..a730939a 100644 --- a/src/main/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsToFhirMapper.java +++ b/src/main/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsToFhirMapper.java @@ -18,13 +18,25 @@ import org.miracum.streams.ume.obdstofhir.model.MeldungExportList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.util.StringUtils; public abstract class ObdsToFhirMapper { protected final FhirProperties fhirProperties; + static Pattern localPatientIdPattern = Pattern.compile("[^0]\\d{8}"); private static final Logger log = LoggerFactory.getLogger(ObdsToFhirMapper.class); + @Value("${app.patient-id-pattern:[^0]\\d{8}}") + void setStringPattern(String value) { + try { + ObdsToFhirMapper.localPatientIdPattern = Pattern.compile(value); + } catch (Exception e) { + log.error("Not a valid patient ID pattern: {}. Use valid RegExp instead.", value); + throw e; + } + } + protected ObdsToFhirMapper(final FhirProperties fhirProperties) { this.fhirProperties = fhirProperties; } @@ -62,16 +74,13 @@ protected String computeResourceIdFromIdentifier(Identifier identifier) { } protected static String convertId(String id) { - Pattern pattern = Pattern.compile("[^0]\\d{8}"); - Matcher matcher = pattern.matcher(id); - var convertedId = ""; + Matcher matcher = ObdsToFhirMapper.localPatientIdPattern.matcher(id); if (matcher.find()) { - convertedId = matcher.group(); + return matcher.group(); } else { - log.warn("Identifier to convert does not have 9 digits without leading '0': {}", id); + log.warn("Identifier to convert does not match pattern: {}", matcher.pattern().toString()); return id; } - return convertedId; } public static List prioritiseLatestMeldungExports( diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 3ddb7c79..205de856 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,6 +1,7 @@ app: version: 2.1.3 enableCheckDigitConv: ${CHECK_DIGIT_CONVERSION:false} + patient-id-pattern: "[^0]\\d{8}" fhir: extensions: diff --git a/src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsToFhirIntegrationTest.java b/src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsToFhirIntegrationTest.java new file mode 100644 index 00000000..d4ef821b --- /dev/null +++ b/src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsToFhirIntegrationTest.java @@ -0,0 +1,66 @@ +package org.miracum.streams.ume.obdstofhir.mapper; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.miracum.streams.ume.obdstofhir.FhirProperties; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +import org.springframework.stereotype.Component; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@Import({ObdsTestMapper.class}) +@MockBean(FhirProperties.class) +public class ObdsToFhirIntegrationTest { + + @Autowired ObdsTestMapper mapper; + + @Nested + @TestPropertySource(properties = {"app.patient-id-pattern=\\\\w*"}) + class AllWordCharactersAllowedPattern { + + @ParameterizedTest + @CsvSource({ + "12345,12345", + "123456789,123456789", + "1234567890,1234567890", + "1234567891,1234567891", + "0000012345,0000012345" + }) + void applyLocalPatientIdPattern(String input, String output) { + var actual = ObdsToFhirMapper.convertId(input); + assertThat(actual).isEqualTo(output); + } + } + + @Nested + class UseDefaultPatternWithoutConfig { + + @ParameterizedTest + @CsvSource({ + "12345,12345", + "123456789,123456789", + "1234567890,123456789", // Max 9 digits, remove last digit '0' + "1234567891,123456789", // Max 9 digits, remove last digit '1' + "0000012345,0000012345", // Not mathching pattern - keep as is + }) + void applyDefaultPattern(String input, String output) { + var actual = ObdsToFhirMapper.convertId(input); + assertThat(actual).isEqualTo(output); + } + } +} + +@Component +class ObdsTestMapper extends ObdsToFhirMapper { + + protected ObdsTestMapper(FhirProperties fhirProperties) { + super(fhirProperties); + } +} diff --git a/src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsToFhirMapperTests.java b/src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsToFhirMapperTests.java index 2c0f0612..0767d8d7 100644 --- a/src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsToFhirMapperTests.java +++ b/src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsToFhirMapperTests.java @@ -99,4 +99,20 @@ void convertObdsDateToDateTimeTypeShouldThrowExceptionOnUnparsableDateString() { ObdsToFhirMapper.convertObdsDateToDateTimeType("some shiny day somewere"); }); } + + @ParameterizedTest + @CsvSource({ + "12345,12345", + "123456789,123456789", + "1234567890,123456789", // Max 9 digits, remove last digit '0' + "1234567891,123456789", // Max 9 digits, remove last digit '1' + "0000012345,0000012345", // Not mathching pattern - keep as is + "G1234,G1234", // Not mathching pattern - keep as is + "G123456,G123456", // Not matching pattern - keep as is + "G123456789,G12345678", // return Non-zero and 8 digits + }) + void convertPatientIdWithDefaultPattern(String input, String output) { + var actual = ObdsToFhirMapper.convertId(input); + assertThat(actual).isEqualTo(output); + } }