Skip to content
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: check if Pat-ID is reasonable value #90

Merged
merged 10 commits into from
Aug 30, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
pcvolkmer marked this conversation as resolved.
Show resolved Hide resolved
} 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<MeldungExport> prioritiseLatestMeldungExports(
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
app:
version: 2.1.3
enableCheckDigitConv: ${CHECK_DIGIT_CONVERSION:false}
patient-id-pattern: "[^0]\\d{8}"

fhir:
extensions:
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading