Skip to content

Commit

Permalink
Remove amended by filter from LoadElementsByTypeUseCase
Browse files Browse the repository at this point in the history
RISDEV-0000
  • Loading branch information
malte-laukoetter committed Feb 14, 2025
1 parent 2642811 commit 7c1847f
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public ResponseEntity<List<ElementResponseSchema>> getElementList(
@RequestParam final Optional<DokumentExpressionEli> amendedBy
) {
List<ElementResponseSchema> elements = loadElementsByTypeUseCase
.loadElementsByType(
new LoadElementsByTypeUseCase.Query(eli, Arrays.asList(type), amendedBy.orElse(null))
)
.loadElementsByType(new LoadElementsByTypeUseCase.Query(eli, Arrays.asList(type)))
.stream()
.map(ElementResponseMapper::fromElementNode)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import de.bund.digitalservice.ris.norms.domain.entity.eli.DokumentExpressionEli;
import de.bund.digitalservice.ris.norms.utils.exceptions.NormsAppException;
import java.util.List;
import javax.annotation.Nullable;
import org.w3c.dom.Node;

/** Use case for getting a list of elements of certain types as {@link Node} from a dokument. */
public interface LoadElementsByTypeUseCase {
/**
* Load the list of elements of certain types from the dokument. Elements can additionally be filtered
* to include only those touched by a specific amending command.
* Load the list of elements of certain types from the dokument.
*
* @return List of elements from the dokument
* @param query Query used for identifying the elements
Expand All @@ -24,18 +22,8 @@ public interface LoadElementsByTypeUseCase {
* @param eli The ELI used to identify the dokument
* @param elementType The types of the elements. While this is a list of strings, only certain
* values are allowed. Check {@link ElementService.ElementType} for the supported types.
* @param amendedBy EId of an amending command. If provided, filters the list to include only
* elements touched by that amending command.
*/
record Query(
DokumentExpressionEli eli,
List<String> elementType,
@Nullable DokumentExpressionEli amendedBy
) {
public Query(DokumentExpressionEli eli, List<String> elementType) {
this(eli, elementType, null);
}
}
record Query(DokumentExpressionEli eli, List<String> elementType) {}

/** Indicates that at least one of the requested types is not supported. */
class UnsupportedElementTypeException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@
import de.bund.digitalservice.ris.norms.application.exception.RegelungstextNotFoundException;
import de.bund.digitalservice.ris.norms.application.port.input.*;
import de.bund.digitalservice.ris.norms.application.port.output.LoadRegelungstextPort;
import de.bund.digitalservice.ris.norms.domain.entity.Analysis;
import de.bund.digitalservice.ris.norms.domain.entity.EId;
import de.bund.digitalservice.ris.norms.domain.entity.Href;
import de.bund.digitalservice.ris.norms.domain.entity.Norm;
import de.bund.digitalservice.ris.norms.domain.entity.TextualMod;
import de.bund.digitalservice.ris.norms.domain.entity.eli.DokumentExpressionEli;
import de.bund.digitalservice.ris.norms.utils.NodeParser;
import de.bund.digitalservice.ris.norms.utils.XmlMapper;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;
import org.springframework.stereotype.Service;
import org.w3c.dom.Node;

Expand Down Expand Up @@ -120,52 +112,13 @@ public List<Node> loadElementsByType(LoadElementsByTypeUseCase.Query query) {
.loadRegelungstext(new LoadRegelungstextPort.Command(query.eli()))
.orElseThrow(() -> new RegelungstextNotFoundException(query.eli().toString()));

// Source EIDs from passive mods
final var passiveModsDestinationEids = getDestinationEidsFromPassiveMods(
regelungstext
.getMeta()
.getAnalysis()
.map(Analysis::getPassiveModifications)
.orElse(Collections.emptyList()),
query.amendedBy()
);

return NodeParser
.getNodesFromExpression(combinedXPaths, regelungstext.getDocument())
.stream()
.filter(element -> { // filter by "amendedBy")
// no amending law -> all elements are fine
if (query.amendedBy() == null) return true;

var eId = EId.fromMandatoryNode(element).value();
return passiveModsDestinationEids.stream().anyMatch(modEid -> modEid.contains(eId));
})
.toList();
}

private String getXPathForEid(String eid) {
return String.format("//*[@eId='%s']", eid);
}

private List<String> getDestinationEidsFromPassiveMods(
List<TextualMod> mods,
@Nullable DokumentExpressionEli amendedBy
) {
return mods
.stream()
.filter(passiveMod -> {
if (amendedBy == null) return true;

return passiveMod
.getSourceHref()
.flatMap(Href::getExpressionEli)
.orElseThrow()
.equals(amendedBy);
})
.map(TextualMod::getDestinationHref)
.flatMap(Optional::stream)
.map(Href::getEId)
.flatMap(Optional::stream)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,7 @@ void itReturnsOnlyTheElementsMatchingTheGivenAmendingLaw() throws Exception {
DokumentExpressionEli.fromString(
"eli/bund/bgbl-1/1990/s2954/2022-12-19/1/deu/regelungstext-1"
),
eq(List.of("preface", "preamble", "article", "conclusions")),
DokumentExpressionEli.fromString(
"eli/bund/bgbl-1/2017/s815/1995-03-15/1/deu/regelungstext-1"
)
eq(List.of("preface", "preamble", "article", "conclusions"))
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,65 +384,6 @@ void throwsWhenNormDoesNotExist() {
assertThatThrownBy(() -> service.loadElementsByType(query))
.isInstanceOf(RegelungstextNotFoundException.class);
}

@Test
void filtersReturnedElementsByAmendingNorm() {
// Given
var regelungstext = Fixtures.loadRegelungstextFromDisk(
"NormWithPassiveModificationsInDifferentArticles.xml"
);
var eli = regelungstext.getExpressionEli();
when(loadRegelungstextPort.loadRegelungstext(new LoadRegelungstextPort.Command(eli)))
.thenReturn(Optional.of(regelungstext));

// When
var elements = service.loadElementsByType(
new LoadElementsByTypeUseCase.Query(
eli,
List.of("preface", "preamble", "article", "conclusions"),
DokumentExpressionEli.fromString(
"eli/bund/bgbl-1/2017/s815/1995-03-15/1/deu/regelungstext-1"
)
)
);

// Then
assertThat(elements).hasSize(1);
assertThat(elements.getFirst().getNodeName()).isEqualTo("akn:article");
}

@Test
void returnsEmptyListIfNoElementIsAffectedByTheAmendingNorm() {
// Given
var regelungstext = Fixtures.loadRegelungstextFromDisk(
"NormWithMultiplePassiveModifications.xml"
);
var eli = regelungstext.getExpressionEli();
when(
loadRegelungstextPort.loadRegelungstext(
new LoadRegelungstextPort.Command(
DokumentExpressionEli.fromString(
"eli/bund/bgbl-1/1990/s2954/2022-12-19/1/deu/regelungstext-1"
)
)
)
)
.thenReturn(Optional.of(regelungstext));

// When
var elements = service.loadElementsByType(
new LoadElementsByTypeUseCase.Query(
eli,
List.of("preface", "preamble", "article", "conclusions"),
DokumentExpressionEli.fromString(
"eli/bund/bgbl-1/1000/1/1000-01-01/1/deu/regelungstext-1"
)
)
);

// Then
assertThat(elements).isEmpty();
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,62 +472,5 @@ void itReturnsEntriesWithBookPartChapterTitleSubtitleSectionAndSubsectionInforma
)
.andExpect(jsonPath("$[6].type").value("subtitle"));
}

@Test
void itReturnsAnEmptyListIfNoElementIsAffectedByTheGivenAmendingLaw() throws Exception {
// Given
dokumentRepository.save(
DokumentMapper.mapToDto(
Fixtures.loadRegelungstextFromDisk("NormWithMultiplePassiveModifications.xml")
)
);
dokumentRepository.save(
DokumentMapper.mapToDto(
Fixtures.loadRegelungstextFromDisk("NormWithPrefacePreambleAndConclusions.xml")
)
);

var url =
"/api/v1/norms/eli/bund/bgbl-1/2017/s419/2017-03-15/1/deu/regelungstext-1/elements" +
"?type=preface" +
"&type=preamble" +
"&type=article" +
"&type=conclusions" +
"&amendedBy=eli/bund/bgbl-1/2017/s419/2017-03-15/1/deu/regelungstext-1"; // amending norm eli

// When
mockMvc
.perform(get(url))
// Then
.andExpect(status().isOk())
.andExpect(jsonPath("$[0]").doesNotExist());
}

@Test
void itReturnsOnlyTheElementsMatchingTheGivenAmendingLaw() throws Exception {
dokumentRepository.save(
DokumentMapper.mapToDto(
Fixtures.loadRegelungstextFromDisk("NormWithPassiveModificationsInDifferentArticles.xml")
)
);

var url =
"/api/v1/norms/eli/bund/bgbl-1/1990/s2954/2022-12-19/1/deu/regelungstext-1/elements" +
"?type=preface" +
"&type=preamble" +
"&type=article" +
"&type=conclusions" +
"&amendedBy=eli/bund/bgbl-1/2017/s815/1995-03-15/1/deu/regelungstext-1"; // second

// When
mockMvc
.perform(get(url))
// Then
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].eid").exists())
.andExpect(jsonPath("$[0].title").exists())
.andExpect(jsonPath("$[0].type").exists())
.andExpect(jsonPath("$[1]").doesNotExist());
}
}
}

0 comments on commit 7c1847f

Please sign in to comment.