diff options
| author | Paul-Christian Volkmer | 2024-09-21 22:10:24 +0200 |
|---|---|---|
| committer | Paul-Christian Volkmer | 2024-09-21 22:10:24 +0200 |
| commit | cc27edc544cec1b892e7c224aec9e6e42342aa39 (patch) | |
| tree | 3036b92f84a707d769782d63c2b018166623abf5 /src/test/java/dev/dnpm/analyzer | |
| parent | 93215825f5c8aec0912d562b544f370cffe9cda7 (diff) | |
refactor: use package name following Java guidelines
Diffstat (limited to 'src/test/java/dev/dnpm/analyzer')
5 files changed, 580 insertions, 0 deletions
diff --git a/src/test/java/dev/dnpm/analyzer/AnalyzerUtilsTest.java b/src/test/java/dev/dnpm/analyzer/AnalyzerUtilsTest.java new file mode 100644 index 0000000..2d51bbc --- /dev/null +++ b/src/test/java/dev/dnpm/analyzer/AnalyzerUtilsTest.java @@ -0,0 +1,137 @@ +package dev.dnpm.analyzer; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; + +class AnalyzerUtilsTest { + + private final Map<String, Object> input = Map.of("value1", 1, "valueA", "A", "valueTrue", true); + + private static Set<TestTypeData> testTypeData() { + return Set.of( + new TestTypeData("value1", Integer.class).withExpectedResult(true), + new TestTypeData("valueA", String.class).withExpectedResult(true), + new TestTypeData("valueTrue", Boolean.class).withExpectedResult(true), + + new TestTypeData("value1", String.class).withExpectedResult(false), + new TestTypeData("valueA", Boolean.class).withExpectedResult(false), + new TestTypeData("valueTrue", Integer.class).withExpectedResult(false), + + new TestTypeData("value1", Boolean.class).withExpectedResult(false), + new TestTypeData("valueA", Integer.class).withExpectedResult(false), + new TestTypeData("valueTrue", String.class).withExpectedResult(false) + ); + } + + @ParameterizedTest + @MethodSource("testTypeData") + void testShouldReturnExpectedResultForTypedCheck(TestTypeData testData) { + var actual = AnalyzerUtils.requiredValuePresent(input, testData.key, testData.type); + assertThat(actual).isEqualTo(testData.result); + } + + private static Set<TestMatchData> testMatchData() { + return Set.of( + new TestMatchData("value1", "[\\d]").withExpectedResult(true), + new TestMatchData("valueA", "[A-Z]").withExpectedResult(true), + + new TestMatchData("value1", "[A-Z]").withExpectedResult(false), + new TestMatchData("valueA", "[a-z]").withExpectedResult(false), + new TestMatchData("valueA", "[\\d]").withExpectedResult(false) + ); + } + + @ParameterizedTest + @MethodSource("testMatchData") + void testShouldReturnExpectedResultForMatchCheck(TestMatchData testData) { + var actual = AnalyzerUtils.requiredValueMatches(input, testData.key, testData.regexp); + assertThat(actual).isEqualTo(testData.result); + } + + @Test + void testShouldCheckIfInputValueIsIdNumber() { + assertThat(AnalyzerUtils.requiredValueIsId(Map.of("value", 0), "value")).isFalse(); + assertThat(AnalyzerUtils.requiredValueIsId(Map.of("value", "ABC"), "value")).isFalse(); + assertThat(AnalyzerUtils.requiredValueIsId(Map.of("value", 1234), "value")).isTrue(); + } + + @Test + void testShouldReturnInputValueAsOptional() { + assertThat(AnalyzerUtils.getRequiredValue(Map.of("value", 1234), "value", Integer.class)).isEqualTo(Optional.of(1234)); + assertThat(AnalyzerUtils.getRequiredValue(Map.of("value", "ABC"), "value", String.class)).isEqualTo(Optional.of("ABC")); + + assertThat(AnalyzerUtils.getRequiredValue(Map.of("value", 1234), "value1", Integer.class)).isEmpty(); + assertThat(AnalyzerUtils.getRequiredValue(Map.of("value", "ABC"), "value1", String.class)).isEmpty(); + assertThat(AnalyzerUtils.getRequiredValue(Map.of("value", 1234), "value", String.class)).isEmpty(); + assertThat(AnalyzerUtils.getRequiredValue(Map.of("value", "ABC"), "value", Boolean.class)).isEmpty(); + } + + @Test + void testShouldReturnInputIdAsOptional() { + assertThat(AnalyzerUtils.getRequiredId(Map.of("value", 1234), "value")).isEqualTo(Optional.of(1234)); + + assertThat(AnalyzerUtils.getRequiredId(Map.of("value", 1234), "value1")).isEmpty(); + assertThat(AnalyzerUtils.getRequiredId(Map.of("value", "ABC"), "value")).isEmpty(); + assertThat(AnalyzerUtils.getRequiredId(Map.of("value", 0), "value")).isEmpty(); + } + + @Test + void testShouldReturnInputValueMatchingAsOptional() { + assertThat(AnalyzerUtils.getRequiredValueMatching(Map.of("value", 1234), "value", "[\\d]+")).isEqualTo(Optional.of("1234")); + assertThat(AnalyzerUtils.getRequiredValueMatching(Map.of("value", "ABC"), "value", "[A-Z]+")).isEqualTo(Optional.of("ABC")); + + assertThat(AnalyzerUtils.getRequiredValueMatching(Map.of("value", "ABC"), "value1", "[A-Z]+")).isEmpty(); + } + + private static class TestTypeData { + public final String key; + public final Class<?> type; + + public boolean result; + + public TestTypeData(String key, Class<?> type) { + this.key = key; + this.type = type; + } + + public TestTypeData withExpectedResult(boolean result) { + this.result = result; + return this; + } + + @Override + public String toString() { + return String.format("key: '%s', type: %s, result: %s", key, type.getSimpleName(), result); + } + } + + private static class TestMatchData { + public final String key; + public final String regexp; + + public boolean result; + + public TestMatchData(String key, String regexp) { + this.key = key; + this.regexp = regexp; + } + + public TestMatchData withExpectedResult(boolean result) { + this.result = result; + return this; + } + + @Override + public String toString() { + return String.format("key: '%s', regexp: '%s', result: %s", key, regexp, result); + } + } + +} diff --git a/src/test/java/dev/dnpm/analyzer/EinzelempfehlungAnalyzerTest.java b/src/test/java/dev/dnpm/analyzer/EinzelempfehlungAnalyzerTest.java new file mode 100644 index 0000000..fbc1d92 --- /dev/null +++ b/src/test/java/dev/dnpm/analyzer/EinzelempfehlungAnalyzerTest.java @@ -0,0 +1,103 @@ +package dev.dnpm.analyzer; + +import dev.dnpm.security.PermissionType; +import dev.dnpm.security.PersonPoolBasedPermissionEvaluator; +import dev.dnpm.services.StudienService; +import dev.dnpm.services.molekulargenetik.MolekulargenetikFormService; +import de.itc.onkostar.api.IOnkostarApi; +import de.itc.onkostar.api.Procedure; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class EinzelempfehlungAnalyzerTest { + + private IOnkostarApi onkostarApi; + + private StudienService studienService; + + private MolekulargenetikFormService molekulargenetikFormService; + + private PersonPoolBasedPermissionEvaluator permissionEvaluator; + + private EinzelempfehlungAnalyzer analyzer; + + @BeforeEach + void setup( + @Mock IOnkostarApi onkostarApi, + @Mock StudienService studienService, + @Mock MolekulargenetikFormService molekulargenetikFormService, + @Mock PersonPoolBasedPermissionEvaluator permissionEvaluator + ) { + this.onkostarApi = onkostarApi; + this.studienService = studienService; + this.molekulargenetikFormService = molekulargenetikFormService; + this.permissionEvaluator = permissionEvaluator; + this.analyzer = new EinzelempfehlungAnalyzer(onkostarApi, studienService, molekulargenetikFormService, permissionEvaluator); + } + + @Test + void testShouldRequestVariantsFromMolekulargenetikFormService() { + doAnswer(invocationOnMock -> new Procedure(this.onkostarApi)).when(onkostarApi).getProcedure(anyInt()); + when(this.permissionEvaluator.hasPermission(any(), any(Procedure.class), any(PermissionType.class))) + .thenReturn(true); + + analyzer.getVariants(Map.of("id", 123)); + verify(molekulargenetikFormService, times(1)).getVariants(any(Procedure.class)); + } + + @Test + void shouldRequestAllStudienForEmptyQueryString() { + var input = Map.of("q", (Object) " "); + this.analyzer.getStudien(input); + + verify(studienService, times(1)).findActive(); + } + + @Test + void shouldRequestActiveStudienForEmptyInputMap() { + var input = new HashMap<String, Object>(); + this.analyzer.getStudien(input); + + verify(studienService, times(1)).findActive(); + } + + @Test + void shouldRequestFilteredActiveStudien() { + var input = Map.of("q", (Object) "NCT-123"); + this.analyzer.getStudien(input); + + var captor = ArgumentCaptor.forClass(String.class); + verify(studienService, times(1)).findActiveByQuery(captor.capture()); + assertThat(captor.getValue()).isEqualTo("NCT-123"); + } + + @Test + void shouldRequestActiveStudien() { + var input = Map.of("q", (Object) ""); + this.analyzer.getStudien(input); + + verify(studienService, times(1)).findActive(); + } + + @Test + void shouldRequestAllFilteredtudien() { + var input = Map.of("q", (Object) "NCT-123"); + this.analyzer.getStudien(input); + + var captor = ArgumentCaptor.forClass(String.class); + verify(studienService, times(1)).findActiveByQuery(captor.capture()); + assertThat(captor.getValue()).isEqualTo("NCT-123"); + } + +} diff --git a/src/test/java/dev/dnpm/analyzer/FollowUpAnalyzerTest.java b/src/test/java/dev/dnpm/analyzer/FollowUpAnalyzerTest.java new file mode 100644 index 0000000..cfe5427 --- /dev/null +++ b/src/test/java/dev/dnpm/analyzer/FollowUpAnalyzerTest.java @@ -0,0 +1,80 @@ +package dev.dnpm.analyzer; + +import de.itc.onkostar.api.IOnkostarApi; +import de.itc.onkostar.api.Item; +import de.itc.onkostar.api.Procedure; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class FollowUpAnalyzerTest { + + private IOnkostarApi onkostarApi; + + private FollowUpAnalyzer followUpAnalyzer; + + @BeforeEach + void setUp( + @Mock IOnkostarApi onkostarApi + ) { + this.onkostarApi = onkostarApi; + this.followUpAnalyzer = new FollowUpAnalyzer(onkostarApi); + } + + @Test + void shouldBacklinkEinzelempfehlungUsingOnkostarApi() throws Exception { + var einzelempfehlung = new Procedure(onkostarApi); + einzelempfehlung.setId(1234); + + when(onkostarApi.getProcedure(anyInt())).thenReturn(einzelempfehlung); + + var procedure = new Procedure(onkostarApi); + procedure.setId(1000); + procedure.setValue("LinkTherapieempfehlung", new Item("LinkTherapieempfehlung", 1234)); + + followUpAnalyzer.analyze(procedure, null); + + var procedureIdCaptor = ArgumentCaptor.forClass(Integer.class); + verify(onkostarApi, times(1)).getProcedure(procedureIdCaptor.capture()); + assertThat(procedureIdCaptor.getValue()).isEqualTo(einzelempfehlung.getId()); + + var procedureCaptor = ArgumentCaptor.forClass(Procedure.class); + verify(onkostarApi, times(1)).saveProcedure(procedureCaptor.capture()); + assertThat(procedureCaptor.getValue()).isNotNull(); + assertThat(procedureCaptor.getValue().getId()).isEqualTo(einzelempfehlung.getId()); + } + + @Test + void shouldNotBacklinkEinzelempfehlungIfNoEinzelempfehlungSelected() throws Exception { + var procedure = new Procedure(onkostarApi); + procedure.setId(1000); + + followUpAnalyzer.analyze(procedure, null); + + verify(onkostarApi, times(0)).getProcedure(anyInt()); + verify(onkostarApi, times(0)).saveProcedure(any(Procedure.class)); + } + + @Test + void shouldNotBacklinkEinzelempfehlungIfEinzelempfehlungDoesNotExist() throws Exception { + var procedure = new Procedure(onkostarApi); + procedure.setId(1000); + procedure.setValue("LinkTherapieempfehlung", new Item("LinkTherapieempfehlung", 1234)); + + followUpAnalyzer.analyze(procedure, null); + + var procedureIdCaptor = ArgumentCaptor.forClass(Integer.class); + verify(onkostarApi, times(1)).getProcedure(procedureIdCaptor.capture()); + assertThat(procedureIdCaptor.getValue()).isEqualTo(1234); + + verify(onkostarApi, times(0)).saveProcedure(any(Procedure.class)); + } + +} diff --git a/src/test/java/dev/dnpm/analyzer/TherapieMitEcogAnalyzerTest.java b/src/test/java/dev/dnpm/analyzer/TherapieMitEcogAnalyzerTest.java new file mode 100644 index 0000000..a37ac06 --- /dev/null +++ b/src/test/java/dev/dnpm/analyzer/TherapieMitEcogAnalyzerTest.java @@ -0,0 +1,164 @@ +package dev.dnpm.analyzer; + +import dev.dnpm.dto.EcogStatusWithDate; +import dev.dnpm.services.strahlentherapie.StrahlentherapieService; +import dev.dnpm.services.systemtherapie.SystemtherapieService; +import de.itc.onkostar.api.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.Date; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class TherapieMitEcogAnalyzerTest { + + private IOnkostarApi onkostarApi; + + private StrahlentherapieService strahlentherapieService; + + private SystemtherapieService systemtherapieService; + + private TherapieMitEcogAnalyzer therapieMitEcogAnalyzer; + + private Disease dummyDisease(int id, Date diagnosisDate) { + var disease = new Disease(onkostarApi); + disease.setId(id); + disease.setDiagnosisDate(diagnosisDate); + return disease; + } + + private Date daysPassed(int days) { + return Date.from(Instant.now().minus(days, ChronoUnit.DAYS)); + } + + @BeforeEach + void setUp( + @Mock IOnkostarApi onkostarApi, + @Mock StrahlentherapieService strahlentherapieService, + @Mock SystemtherapieService systemtherapieService + ) { + this.onkostarApi = onkostarApi; + this.strahlentherapieService = strahlentherapieService; + this.systemtherapieService = systemtherapieService; + this.therapieMitEcogAnalyzer = new TherapieMitEcogAnalyzer(onkostarApi, strahlentherapieService, systemtherapieService); + } + + @Test + void shouldInsertNewEcogStatus() throws Exception { + final var diagnosisDate = daysPassed(7); + final var ecogDate = daysPassed(1); + final var procedureDate = daysPassed(1); + + doAnswer(invocationOnMock -> List.of(new EcogStatusWithDate(ecogDate, "0"))) + .when(systemtherapieService).ecogStatus(any(Patient.class)); + + var patient = new Patient(onkostarApi); + patient.setId(1); + + var procedure = new Procedure(onkostarApi); + procedure.setId(1000); + procedure.setStartDate(procedureDate); + procedure.setEditState(ProcedureEditStateType.COMPLETED); + procedure.setPatientId(1); + procedure.setPatient(patient); + procedure.setValue("ECOGvorTherapie", new Item("ECOGvorTherapie", 1)); + + doAnswer(invocationOnMock -> List.of(dummyDisease(42, diagnosisDate))).when(this.onkostarApi).getDiseasesByPatientId(anyInt()); + + doAnswer(invocationOnMock -> List.of(procedure)).when(onkostarApi).getProceduresForDiseaseByForm(anyInt(), anyString()); + + therapieMitEcogAnalyzer.analyze(procedure, dummyDisease(10, diagnosisDate)); + + var idCaptor = ArgumentCaptor.forClass(Integer.class); + var formNameCaptor = ArgumentCaptor.forClass(String.class); + verify(onkostarApi, times(1)).getProceduresForDiseaseByForm(idCaptor.capture(), formNameCaptor.capture()); + assertThat(idCaptor.getValue()).isEqualTo(42); + assertThat(formNameCaptor.getValue()).isEqualTo("DNPM Klinik/Anamnese"); + + verify(onkostarApi, times(1)).saveProcedure(any(Procedure.class), anyBoolean()); + } + + @Test + void shouldNotModifyEcogStatusIfNoCompletedSystemTherapy() throws Exception { + final var diagnosisDate = daysPassed(7); + final var procedureDate = daysPassed(1); + + doAnswer(invocationOnMock -> List.of()) + .when(systemtherapieService).ecogStatus(any(Patient.class)); + + var patient = new Patient(onkostarApi); + patient.setId(1); + + var procedure = new Procedure(onkostarApi); + procedure.setId(1000); + procedure.setStartDate(procedureDate); + procedure.setEditState(ProcedureEditStateType.COMPLETED); + procedure.setPatientId(1); + procedure.setPatient(patient); + procedure.setValue("ECOGvorTherapie", new Item("ECOGvorTherapie", 1)); + + therapieMitEcogAnalyzer.analyze(procedure, dummyDisease(10, diagnosisDate)); + + verify(onkostarApi, times(0)).getProceduresForDiseaseByForm(anyInt(), anyString()); + verify(onkostarApi, times(0)).saveProcedure(any(Procedure.class), anyBoolean()); + } + + @Test + void shouldNotIncludeEcogStatusBeforeDiagnosisDate() throws Exception { + final var diagnosisDate = daysPassed(7); + final var ecogDate = daysPassed(28); + final var procedureDate = daysPassed(1); + + doAnswer(invocationOnMock -> List.of(new EcogStatusWithDate(ecogDate, "0"))) + .when(systemtherapieService).ecogStatus(any(Patient.class)); + + var patient = new Patient(onkostarApi); + patient.setId(1); + + var procedure = new Procedure(onkostarApi); + procedure.setId(1000); + procedure.setStartDate(procedureDate); + procedure.setEditState(ProcedureEditStateType.COMPLETED); + procedure.setPatientId(1); + procedure.setPatient(patient); + procedure.setValue("ECOGvorTherapie", new Item("ECOGvorTherapie", 1)); + + therapieMitEcogAnalyzer.analyze(procedure, dummyDisease(10, diagnosisDate)); + + verify(onkostarApi, times(0)).getProceduresForDiseaseByForm(anyInt(), anyString()); + verify(onkostarApi, times(0)).saveProcedure(any(Procedure.class), anyBoolean()); + } + + @Test + void shouldRequestEcogFromStrahlentherapieAndSystemtherapie() { + final var diagnosisDate = daysPassed(7); + final var procedureDate = daysPassed(1); + + var patient = new Patient(onkostarApi); + patient.setId(1); + + var procedure = new Procedure(onkostarApi); + procedure.setId(1000); + procedure.setStartDate(procedureDate); + procedure.setEditState(ProcedureEditStateType.COMPLETED); + procedure.setPatientId(1); + procedure.setPatient(patient); + procedure.setValue("ECOGvorTherapie", new Item("ECOGvorTherapie", 1)); + + therapieMitEcogAnalyzer.analyze(procedure, dummyDisease(10, diagnosisDate)); + + verify(strahlentherapieService, times(1)).ecogStatus(any()); + verify(systemtherapieService, times(1)).ecogStatus(any()); + } + +} diff --git a/src/test/java/dev/dnpm/analyzer/TherapieplanAnalyzerTest.java b/src/test/java/dev/dnpm/analyzer/TherapieplanAnalyzerTest.java new file mode 100644 index 0000000..2f22e37 --- /dev/null +++ b/src/test/java/dev/dnpm/analyzer/TherapieplanAnalyzerTest.java @@ -0,0 +1,96 @@ +package dev.dnpm.analyzer; + +import dev.dnpm.security.DelegatingDataBasedPermissionEvaluator; +import dev.dnpm.security.PermissionType; +import dev.dnpm.services.FormService; +import dev.dnpm.services.mtb.MtbService; +import dev.dnpm.services.therapieplan.MultipleMtbTherapieplanService; +import dev.dnpm.services.therapieplan.TherapieplanService; +import dev.dnpm.services.therapieplan.TherapieplanServiceFactory; +import de.itc.onkostar.api.IOnkostarApi; +import de.itc.onkostar.api.Item; +import de.itc.onkostar.api.Procedure; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class TherapieplanAnalyzerTest { + + @Mock + private IOnkostarApi onkostarApi; + + @Mock + private FormService formService; + + @Mock + private TherapieplanServiceFactory therapieplanServiceFactory; + + @Mock + private TherapieplanService therapieplanService; + + @Mock + private MtbService mtbService; + + @Mock + private DelegatingDataBasedPermissionEvaluator permissionEvaluator; + + private TherapieplanAnalyzer therapieplanAnalyzer; + + @BeforeEach + void setUp() { + this.therapieplanAnalyzer = new TherapieplanAnalyzer(therapieplanServiceFactory, mtbService, permissionEvaluator); + } + + @Test + void shouldRunServiceMethodsOnAnalyzeCalled() { + when(this.therapieplanServiceFactory.currentUsableInstance()) + .thenReturn(new MultipleMtbTherapieplanService(onkostarApi, formService)); + + this.therapieplanAnalyzer.analyze(new Procedure(onkostarApi), null); + + verify(this.therapieplanServiceFactory, times(1)).currentUsableInstance(); + } + + @Test + void shouldRequestProtokollauszug() { + doAnswer(invocationOnMock -> { + var procedure = new Procedure(onkostarApi); + procedure.setValue("referstemtb", new Item("referstemtb", 2345)); + return List.of(procedure); + }).when(this.therapieplanService).findReferencedMtbs(anyInt()); + + when(this.therapieplanServiceFactory.currentUsableInstance()) + .thenReturn(therapieplanService); + + when(this.permissionEvaluator.hasPermission(any(), anyInt(), anyString(), any(PermissionType.class))).thenReturn(true); + + var input = Map.of("id", (Object) 1234); + this.therapieplanAnalyzer.getProtokollauszug(input); + + var captor = ArgumentCaptor.forClass(List.class); + verify(mtbService, times(1)).getProtocol(captor.capture()); + assertThat(captor.getValue()).hasSize(1); + } + + @Test + void shouldNotRequestProtokollauszugDueToNoPermission() { + when(this.permissionEvaluator.hasPermission(any(), anyInt(), anyString(), any(PermissionType.class))) + .thenReturn(false); + + var input = Map.of("id", (Object) 1234); + this.therapieplanAnalyzer.getProtokollauszug(input); + + verify(mtbService, times(0)).getProtocol(anyList()); + } + +} |
