diff --git a/time-machine/src/main/kotlin/de/bund/digitalservice/ris/norms/timemachine/core/ModificationApplier.kt b/time-machine/src/main/kotlin/de/bund/digitalservice/ris/norms/timemachine/core/ModificationApplier.kt index f4c547d0d..9ce72ac5a 100644 --- a/time-machine/src/main/kotlin/de/bund/digitalservice/ris/norms/timemachine/core/ModificationApplier.kt +++ b/time-machine/src/main/kotlin/de/bund/digitalservice/ris/norms/timemachine/core/ModificationApplier.kt @@ -12,8 +12,10 @@ fun applyModification(amendingLaw: Document, targetLaw: Document): Document { val modification = getFirstModification(amendingLaw) requireNotNull(modification) { "Amending law does not include any modification" } - val eId = getReferenceEid(modification) - requireNotNull(eId) { "Modification has no target reference" } + val eli = findHrefInModification(modification) + requireNotNull(eli) { "Could not find href in modification" } + + val eId = extractLokaleKomponente(eli) val elementToModify = findElementToModify(eId, amendedLaw) requireNotNull(elementToModify) { "Could not find element to modify with eId: '$eId'" } @@ -34,10 +36,10 @@ fun applyModification(amendingLaw: Document, targetLaw: Document): Document { fun getFirstModification(amendingLaw: Document) = getNode("//*[local-name()='mod']", amendingLaw) -fun getReferenceEid(modification: Node): String? { - val href = getNode("//*[local-name()='ref']/@href", modification)?.nodeValue - return href?.split("/")?.takeLast(2)?.get(0) -} +fun findHrefInModification(modification: Node): String? = + getNode("//*[local-name()='ref']/@href", modification)?.nodeValue + +fun extractLokaleKomponente(eli: String) = eli.split("/").takeLast(2)[0] fun findElementToModify(eId: String, amendedLaw: Document) = getNode("//*[@eId='$eId']", amendedLaw) diff --git a/time-machine/src/test/kotlin/unit/de/bund/digitalservice/ris/norms/timemachine/core/ModificationApplierTest.kt b/time-machine/src/test/kotlin/unit/de/bund/digitalservice/ris/norms/timemachine/core/ModificationApplierTest.kt index c19b04473..4bbf51907 100644 --- a/time-machine/src/test/kotlin/unit/de/bund/digitalservice/ris/norms/timemachine/core/ModificationApplierTest.kt +++ b/time-machine/src/test/kotlin/unit/de/bund/digitalservice/ris/norms/timemachine/core/ModificationApplierTest.kt @@ -62,20 +62,6 @@ class ModificationApplierTest { .hasMessage("Amending law does not include any modification") } - @Test - fun `it throws an exception if the modification does not include a reference with an eId`() { - val amendingLaw = - """ - - Change paragraph 2 - - """ - - assertThatThrownBy { applyModification(amendingLaw.asXml(), anyTargetLaw.asXml()) } - .isInstanceOf(IllegalArgumentException::class.java) - .hasMessage("Modification has no target reference") - } - @Test fun `it throws an exception if there is no element with the eId to modify`() { val amendingLaw = @@ -98,6 +84,63 @@ class ModificationApplierTest { .isInstanceOf(IllegalArgumentException::class.java) .hasMessage("Could not find element to modify with eId: 'none'") } + + @Test + fun `it throws an exception if the text to be replaced can't be found`() { + val amendingLaw = + """ + + + In paragraph 2 . + + + """ + + assertThatThrownBy { applyModification(amendingLaw.asXml(), anyTargetLaw.asXml()) } + .isInstanceOf(IllegalArgumentException::class.java) + .hasMessage("Could not find text that should be replaced") + } + + @Test + fun `it throws an exception if the replacement text can't be found`() { + val amendingLaw = + """ + + + In paragraph 2 replace old with. + + + """ + + assertThatThrownBy { applyModification(amendingLaw.asXml(), anyTargetLaw.asXml()) } + .isInstanceOf(IllegalArgumentException::class.java) + .hasMessage("Could not find replacement text") + } + + @Test + fun `it throws an exception if no href is found in modification`() { + val amendingLaw = + """ + + + + + """ + + assertThatThrownBy { applyModification(amendingLaw.asXml(), anyTargetLaw.asXml()) } + .isInstanceOf(IllegalArgumentException::class.java) + .hasMessage("Could not find href in modification") + } + + @Test + fun `it gets the lokale Komponente from an ELI`() { + val eli = + "eli/bund/bgbl-1/1964/s593/1964-08-05/1/deu/regelungstext-1/para-20_abs-1_untergl-1_listenelem-2_inhalt-1_text-1/9-34.xml" + + val result = extractLokaleKomponente(eli) + + assertThat(result).isEqualTo("para-20_abs-1_untergl-1_listenelem-2_inhalt-1_text-1") + } } private const val anyAmendingLaw =