summaryrefslogtreecommitdiff
path: root/src/test/java/dev/dnpm/oshelper/analyzer/TherapieMitEcogAnalyzerTest.java
blob: c1f0b2836682291554acfb56b770f9e42c2095b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * This file is part of onkostar-plugin-dnpm
 *
 * Copyright (C) 2023-2026 the original author or authors.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package dev.dnpm.oshelper.analyzer;

import dev.dnpm.oshelper.dto.EcogStatusWithDate;
import dev.dnpm.oshelper.services.strahlentherapie.StrahlentherapieService;
import dev.dnpm.oshelper.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());
    }

}