Skip to content

Commit

Permalink
Feat(time-machine): add some tests for higher condition coverage
Browse files Browse the repository at this point in the history
NDISC-60
  • Loading branch information
SebastianRossa committed Dec 15, 2023
1 parent c7caa97 commit ed342dd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'" }
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
"""
<akn:body>
<akn:mod>Change paragraph 2</akn:mod>
</akn:body>
"""

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 =
Expand All @@ -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 =
"""
<akn:body>
<akn:mod>
In <akn:ref href="any">paragraph 2</akn:ref> .
</akn:mod>
</akn:body>
"""

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 =
"""
<akn:body>
<akn:mod>
In <akn:ref href="any">paragraph 2</akn:ref> replace <akn:quotedText>old</akn:quotedText> with.
</akn:mod>
</akn:body>
"""

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 =
"""
<akn:body>
<akn:mod>
</akn:mod>
</akn:body>
"""

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 =
Expand Down

0 comments on commit ed342dd

Please sign in to comment.