summaryrefslogtreecommitdiff
path: root/src/test/kotlin/dev/dnpm/etl/processor/pseudonym/ExtensionsTest.kt
blob: abc17c6ab121509a9c1062010c4493bbd546574d (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * This file is part of ETL-Processor
 *
 * Copyright (c) 2023       Comprehensive Cancer Center Mainfranken
 * Copyright (c) 2025-2026  Paul-Christian Volkmer, Datenintegrationszentrum Philipps-Universität Marburg and Contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package dev.dnpm.etl.processor.pseudonym

import ca.uhn.fhir.context.FhirContext
import dev.dnpm.etl.processor.config.AppConfigProperties
import dev.dnpm.etl.processor.config.GIcsConfigProperties
import dev.dnpm.etl.processor.config.JacksonConfig
import dev.dnpm.etl.processor.consent.MtbFileConsentService
import dev.dnpm.etl.processor.services.ConsentProcessor
import dev.dnpm.etl.processor.services.ConsentProcessorTest
import dev.pcvolkmer.mv64e.mtb.*
import org.assertj.core.api.Assertions.assertThat
import org.hl7.fhir.r4.model.Bundle
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.anyValueClass
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.whenever
import org.springframework.core.io.ClassPathResource
import tools.jackson.databind.json.JsonMapper
import java.time.Instant
import java.util.*

@ExtendWith(MockitoExtension::class)
class ExtensionsTest {
    fun getJsonMapper(): JsonMapper {
        return JacksonConfig().jsonMapper()
    }

    @Nested
    inner class UsingDnpmV2Datamodel {

        val FAKE_MTB_FILE_PATH = "mv64e-mtb-fake-patient.json"
        val CLEAN_PATIENT_ID = "644bae7a-56f6-4ee8-b02f-c532e65af5b1"

        private fun fakeMtbFile(): Mtb {
            val mtbFile = ClassPathResource(FAKE_MTB_FILE_PATH).inputStream
            return getJsonMapper().readValue(mtbFile, Mtb::class.java)
        }

        private fun Mtb.serialized(): String {
            return getJsonMapper().writeValueAsString(this)
        }

        @Test
        fun shouldNotContainCleanPatientId(@Mock pseudonymizeService: PseudonymizeService) {
            doAnswer {
                it.arguments[0]
                "PSEUDO-ID"
            }
                .whenever(pseudonymizeService)
                .patientPseudonym(anyValueClass())

            val mtbFile = fakeMtbFile()
            mtbFile.ensureMetaDataIsInitialized()
            addConsentData(mtbFile)

            mtbFile.pseudonymizeWith(pseudonymizeService)

            assertThat(mtbFile.patient.id).isEqualTo("PSEUDO-ID")
            assertThat(mtbFile.serialized()).doesNotContain(CLEAN_PATIENT_ID)
        }

        private fun addConsentData(mtbFile: Mtb) {
            val gIcsConfigProperties = GIcsConfigProperties("", "", "")
            val appConfigProperties = AppConfigProperties(emptyList())

            val bundle = Bundle()
            val dummyConsent = ConsentProcessorTest.getDummyGenomDeConsent()
            dummyConsent.patient.reference = "Patient/$CLEAN_PATIENT_ID"
            bundle.addEntry().resource = dummyConsent

            ConsentProcessor(
                appConfigProperties,
                gIcsConfigProperties,
                JacksonConfig().jsonMapper(),
                FhirContext.forR4(),
                MtbFileConsentService(),
            )
                .embedBroadConsentResources(mtbFile, bundle)
        }

        @Test
        fun shouldNotThrowExceptionOnNullValues(@Mock pseudonymizeService: PseudonymizeService) {
            doAnswer {
                it.arguments[0]
                "PSEUDO-ID"
            }
                .whenever(pseudonymizeService)
                .patientPseudonym(anyValueClass())

            doAnswer { "TESTDOMAIN" }.whenever(pseudonymizeService).prefix()

            val mtbFile =
                Mtb().apply {
                    this.patient =
                        Patient().apply {
                            this.id = "PID"
                            this.birthDate = Date.from(Instant.now())
                            this.gender = GenderCoding().apply { this.code = GenderCodingCode.MALE }
                        }
                    this.episodesOfCare =
                        listOf(
                            MtbEpisodeOfCare().apply {
                                this.id = "1"
                                this.patient = Reference().apply { this.id = "PID" }
                                this.period = PeriodDate().apply { this.start = Date.from(Instant.now()) }
                            }
                        )
                }

            mtbFile.pseudonymizeWith(pseudonymizeService)
            mtbFile.anonymizeContentWith(pseudonymizeService)

            assertThat(mtbFile.episodesOfCare).hasSize(1)
            assertThat(mtbFile.episodesOfCare.map { it.id }).isNotNull
        }

        @Test
        fun shouldNotContainAnyUuidAfterRehashingOfIds(@Mock pseudonymizeService: PseudonymizeService) {
            doAnswer {
                it.arguments[0]
                "PSEUDO-ID"
            }
                .whenever(pseudonymizeService)
                .patientPseudonym(anyValueClass())

            doAnswer { "TESTDOMAIN" }.whenever(pseudonymizeService).prefix()

            val mtbFile = fakeMtbFile()

            /** replace hex values with random long, so our test does not match false positives */
            mtbFile.ngsReports.forEach { report ->
                report.results.simpleVariants.forEach { simpleVariant ->
                    simpleVariant.externalIds.forEach { extIdValue ->
                        extIdValue.value = Math.random().toLong().toString()
                    }
                }
            }
            mtbFile.ngsReports.forEach { report ->
                report.results.rnaFusions.forEach { simpleVariant ->
                    simpleVariant.externalIds.forEach { extIdValue ->
                        extIdValue.value = Math.random().toLong().toString()
                    }
                    simpleVariant.fusionPartner3Prime?.transcriptId?.value = Math.random().toLong().toString()
                    simpleVariant.fusionPartner5Prime?.transcriptId?.value = Math.random().toLong().toString()
                    simpleVariant.externalIds?.forEach { it?.value = Math.random().toLong().toString() }
                }
            }

            mtbFile.pseudonymizeWith(pseudonymizeService)
            mtbFile.anonymizeContentWith(pseudonymizeService)

            val pattern =
                "\"[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\"".toRegex().toPattern()
            val input = mtbFile.serialized()
            val matcher = pattern.matcher(input)

            assertThrows<IllegalStateException> {
                matcher.find()
                val posSt = "check at pos: " + matcher.start().toString() + ", " + matcher.end()
                println(posSt + " with " + matcher.group())
            }
                .also { assertThat(it.message).isEqualTo("No match found") }
        }
    }

    @Test
    fun shouldUseSameAnonymIdForDiagnosisAndDiagnosisReferences(
        @Mock pseudonymizeService: PseudonymizeService
    ) {

        doAnswer {
            it.arguments[0]
            "PSEUDO-ID"
        }
            .whenever(pseudonymizeService)
            .patientPseudonym(anyValueClass())

        doAnswer { "TESTDOMAIN" }.whenever(pseudonymizeService).prefix()

        val mtbFile =
            Mtb().apply {
                this.patient =
                    Patient().apply {
                        this.id = "PID"
                        this.birthDate = Date.from(Instant.now())
                        this.gender = GenderCoding().apply { this.code = GenderCodingCode.MALE }
                    }
                this.diagnoses = listOf(MtbDiagnosis().apply { this.id = "Diagnosis-1" })
                this.episodesOfCare =
                    listOf(
                        MtbEpisodeOfCare().apply {
                            this.id = "Episode-1"
                            this.diagnoses = listOf(Reference().apply { this.id = "Diagnosis-1" })
                        }
                    )
                this.guidelineTherapies =
                    listOf(
                        MtbSystemicTherapy().apply {
                            this.id = "Systemic-Therapy-1"
                            this.reason = Reference().apply { this.id = "Diagnosis-1" }
                        }
                    )
                this.guidelineProcedures =
                    listOf(
                        OncoProcedure().apply {
                            this.id = "Onco-Procedure-1"
                            this.reason = Reference().apply { this.id = "Diagnosis-1" }
                        }
                    )
                this.specimens =
                    listOf(
                        TumorSpecimen().apply {
                            this.id = "Specimen-1"
                            this.diagnosis = Reference().apply { this.id = "Diagnosis-1" }
                        }
                    )
            }

        mtbFile.pseudonymizeWith(pseudonymizeService)
        mtbFile.anonymizeContentWith(pseudonymizeService)

        assertThat(mtbFile.diagnoses.first().id)
            .isEqualTo(mtbFile.episodesOfCare.first().diagnoses.first().id)
        assertThat(mtbFile.diagnoses.first().id).isEqualTo(mtbFile.guidelineTherapies.first().reason.id)
        assertThat(mtbFile.diagnoses.first().id)
            .isEqualTo(mtbFile.guidelineProcedures.first().reason.id)
        assertThat(mtbFile.diagnoses.first().id).isEqualTo(mtbFile.specimens.first().diagnosis.id)
    }

    @Test
    fun shouldNotThrowAnyExceptionOnMissingMsiId(@Mock pseudonymizeService: PseudonymizeService) {

        doAnswer {
            it.arguments[0]
            "PSEUDO-ID"
        }
            .whenever(pseudonymizeService)
            .patientPseudonym(anyValueClass())

        doAnswer { "TESTDOMAIN" }.whenever(pseudonymizeService).prefix()

        val mtbFile =
            Mtb().apply {
                this.patient =
                    Patient().apply {
                        this.id = "PID"
                        this.birthDate = Date.from(Instant.now())
                        this.gender = GenderCoding().apply { this.code = GenderCodingCode.MALE }
                    }
                this.msiFindings =
                    listOf(
                        null,
                        Msi.builder().id("1").build(),
                        Msi.builder().build(),
                        Msi.builder().specimen(null).build(),
                        Msi.builder().specimen(Reference.builder().build()).build(),
                    )
            }

        mtbFile.pseudonymizeWith(pseudonymizeService)
        mtbFile.anonymizeContentWith(pseudonymizeService)

        assertThat(mtbFile.msiFindings).isNotNull
        assertThat(mtbFile.msiFindings[1])
            .satisfiesAnyOf(
                { assertThat(it.id).isNull() },
                { assertThat(it.id).isEqualTo("TESTDOMAIN44e20a53bbbf9f3ae39626d05df7014dcd77d6098") },
            )
    }
}