Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove time-machine from norm render endpoint #994

Merged
merged 4 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public ResponseEntity<String> getArticleRender(
@PathVariable final String eid,
@RequestParam Optional<Instant> atIsoDate
) {
var query = new LoadArticleHtmlUseCase.Query(eli, eid, atIsoDate.orElse(null));
var query = new LoadArticleHtmlUseCase.Query(eli, eid);
var articleHtml = loadArticleHtmlUseCase.loadArticleHtml(query);
return ResponseEntity.ok(articleHtml);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@ public class ElementController {

private final LoadElementUseCase loadElementUseCase;
private final LoadElementHtmlUseCase loadElementHtmlUseCase;
private final LoadElementHtmlAtDateUseCase loadElementHtmlAtDateUseCase;
private final LoadElementsByTypeUseCase loadElementsByTypeUseCase;

public ElementController(
LoadElementUseCase loadElementUseCase,
LoadElementHtmlUseCase loadElementHtmlUseCase,
LoadElementHtmlAtDateUseCase loadElementHtmlAtDateUseCase,
LoadElementsByTypeUseCase loadElementsByTypeUseCase
) {
this.loadElementUseCase = loadElementUseCase;
this.loadElementHtmlUseCase = loadElementHtmlUseCase;
this.loadElementHtmlAtDateUseCase = loadElementHtmlAtDateUseCase;
this.loadElementsByTypeUseCase = loadElementsByTypeUseCase;
}

Expand All @@ -52,15 +49,9 @@ public ResponseEntity<String> getElementHtmlPreview(
@PathVariable final String eid,
@RequestParam Optional<Instant> atIsoDate
) {
var elementHtml = atIsoDate
.map(date ->
loadElementHtmlAtDateUseCase.loadElementHtmlAtDate(
new LoadElementHtmlAtDateUseCase.Query(eli, eid, date)
)
)
.orElseGet(() ->
loadElementHtmlUseCase.loadElementHtml(new LoadElementHtmlUseCase.Query(eli, eid))
);
var elementHtml = loadElementHtmlUseCase.loadElementHtml(
new LoadElementHtmlUseCase.Query(eli, eid)
);

return ResponseEntity.ok(elementHtml);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import de.bund.digitalservice.ris.norms.adapter.input.restapi.schema.UpdateModsResponseSchema;
import de.bund.digitalservice.ris.norms.application.port.input.*;
import de.bund.digitalservice.ris.norms.domain.entity.eli.DokumentExpressionEli;
import de.bund.digitalservice.ris.norms.utils.XmlMapper;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import java.time.Instant;
Expand All @@ -35,26 +34,20 @@
public class NormExpressionController {

private final LoadNormUseCase loadNormUseCase;
private final LoadRegelungstextUseCase loadRegelungstextUseCase;
private final LoadRegelungstextXmlUseCase loadRegelungstextXmlUseCase;
private final UpdateRegelungstextXmlUseCase updateRegelungstextXmlUseCase;
private final TransformLegalDocMlToHtmlUseCase transformLegalDocMlToHtmlUseCase;
private final ApplyPassiveModificationsUseCase applyPassiveModificationsUseCase;

public NormExpressionController(
LoadNormUseCase loadNormUseCase,
LoadRegelungstextUseCase loadRegelungstextUseCase,
LoadRegelungstextXmlUseCase loadRegelungstextXmlUseCase,
UpdateRegelungstextXmlUseCase updateRegelungstextXmlUseCase,
TransformLegalDocMlToHtmlUseCase transformLegalDocMlToHtmlUseCase,
ApplyPassiveModificationsUseCase applyPassiveModificationsUseCase
TransformLegalDocMlToHtmlUseCase transformLegalDocMlToHtmlUseCase
) {
this.loadNormUseCase = loadNormUseCase;
this.loadRegelungstextUseCase = loadRegelungstextUseCase;
this.loadRegelungstextXmlUseCase = loadRegelungstextXmlUseCase;
this.updateRegelungstextXmlUseCase = updateRegelungstextXmlUseCase;
this.transformLegalDocMlToHtmlUseCase = transformLegalDocMlToHtmlUseCase;
this.applyPassiveModificationsUseCase = applyPassiveModificationsUseCase;
}

/**
Expand Down Expand Up @@ -103,26 +96,6 @@ public ResponseEntity<String> getNormRender(
@RequestParam(defaultValue = "false") boolean showMetadata,
@RequestParam Optional<Instant> atIsoDate
) {
if (atIsoDate.isPresent()) {
var regelungstext = loadRegelungstextUseCase.loadRegelungstext(
new LoadRegelungstextUseCase.Query(eli)
);
regelungstext =
applyPassiveModificationsUseCase.applyPassiveModifications(
new ApplyPassiveModificationsUseCase.Query(regelungstext, atIsoDate.get())
);

return ResponseEntity.ok(
this.transformLegalDocMlToHtmlUseCase.transformLegalDocMlToHtml(
new TransformLegalDocMlToHtmlUseCase.Query(
XmlMapper.toString(regelungstext.getDocument()),
showMetadata,
false
)
)
);
}

var normXml = loadRegelungstextXmlUseCase.loadRegelungstextXml(
new LoadRegelungstextXmlUseCase.Query(eli)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package de.bund.digitalservice.ris.norms.application.port.input;

import de.bund.digitalservice.ris.norms.domain.entity.eli.DokumentExpressionEli;
import java.time.Instant;
import org.springframework.lang.Nullable;

/** Use case for loading a norm's article */
public interface LoadArticleHtmlUseCase {
Expand All @@ -19,12 +17,6 @@ public interface LoadArticleHtmlUseCase {
*
* @param eli ELI of the norm to load the article from
* @param eid EID of the article to load from the norm
* @param atIsoDate (Optional) Apply the norm's passive mods up to this date before loading the
* article
*/
record Query(DokumentExpressionEli eli, String eid, @Nullable Instant atIsoDate) {
public Query(DokumentExpressionEli eli, String eid) {
this(eli, eid, null);
}
}
record Query(DokumentExpressionEli eli, String eid) {}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ public String loadArticleHtml(final LoadArticleHtmlUseCase.Query query) {
.loadRegelungstext(new LoadRegelungstextPort.Command(query.eli()))
.orElseThrow(() -> new NormNotFoundException(query.eli().toString()));

if (query.atIsoDate() != null) {
regelungstext =
timeMachineService.applyPassiveModifications(
new ApplyPassiveModificationsUseCase.Query(regelungstext, query.atIsoDate())
);
}

return regelungstext
.getArticles()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@
*/
@Service
public class ElementService
implements
LoadElementUseCase,
LoadElementHtmlUseCase,
LoadElementHtmlAtDateUseCase,
LoadElementsByTypeUseCase {
implements LoadElementUseCase, LoadElementHtmlUseCase, LoadElementsByTypeUseCase {

private final LoadRegelungstextPort loadRegelungstextPort;
private final XsltTransformationService xsltTransformationService;
private final TimeMachineService timeMachineService;

/** The types of elements that can be retrieved from a norm. */
public enum ElementType {
Expand Down Expand Up @@ -81,12 +76,10 @@ public static ElementType fromLabel(String label) {

public ElementService(
LoadRegelungstextPort loadRegelungstextPort,
XsltTransformationService xsltTransformationService,
TimeMachineService timeMachineService
XsltTransformationService xsltTransformationService
) {
this.loadRegelungstextPort = loadRegelungstextPort;
this.xsltTransformationService = xsltTransformationService;
this.timeMachineService = timeMachineService;
}

@Override
Expand All @@ -113,26 +106,6 @@ public String loadElementHtml(final LoadElementHtmlUseCase.Query query) {
);
}

@Override
public String loadElementHtmlAtDate(final LoadElementHtmlAtDateUseCase.Query query) {
var regelungstext = loadRegelungstextPort
.loadRegelungstext(new LoadRegelungstextPort.Command(query.eli()))
.orElseThrow(() -> new RegelungstextNotFoundException(query.eli().toString()));

regelungstext =
timeMachineService.applyPassiveModifications(
new ApplyPassiveModificationsUseCase.Query(regelungstext, query.atDate())
);

final var element = NodeParser
.getElementFromExpression(getXPathForEid(query.eid()), regelungstext.getDocument())
.orElseThrow(() -> new ElementNotFoundException(query.eli().toString(), query.eid()));

return xsltTransformationService.transformLegalDocMlToHtml(
new TransformLegalDocMlToHtmlUseCase.Query(XmlMapper.toString(element), false, false)
);
}

@Override
public List<Node> loadElementsByType(LoadElementsByTypeUseCase.Query query) {
// No need to do anything if no types are requested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ class ElementControllerTest {
@MockitoBean
private LoadElementsByTypeUseCase loadElementsByTypeUseCase;

@MockitoBean
private LoadElementHtmlAtDateUseCase loadElementHtmlAtDateUseCase;

@Nested
class getElementHtmlPreview {

Expand Down Expand Up @@ -75,31 +72,6 @@ void itThrowsXmlProcessingException() throws Exception {
);
}

@Test
void itThrowsXmlProcessingExceptionWithPassedDate() throws Exception {
when(loadElementHtmlAtDateUseCase.loadElementHtmlAtDate(any()))
.thenThrow(new XmlProcessingException("Error message", null));
mockMvc
.perform(
get(
"/api/v1/norms/eli/bund/bgbl-1/2017/s419/2017-03-15/1/deu/regelungstext-1/elements/hauptteil-1_art-1?atIsoDate=2024-04-03T00:00:00.000Z"
)
.accept(MediaType.TEXT_HTML)
)
// then
.andExpect(status().isInternalServerError())
.andExpect(jsonPath("type").value("/errors/xml-processing-error"))
.andExpect(jsonPath("title").value("XML processing error"))
.andExpect(jsonPath("status").value(500))
.andExpect(jsonPath("detail").value("Error message"))
.andExpect(
jsonPath("instance")
.value(
"/api/v1/norms/eli/bund/bgbl-1/2017/s419/2017-03-15/1/deu/regelungstext-1/elements/hauptteil-1_art-1"
)
);
}

@Test
void returnsHtmlRendering() throws Exception {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import de.bund.digitalservice.ris.norms.application.exception.NormNotFoundException;
import de.bund.digitalservice.ris.norms.application.port.input.*;
import de.bund.digitalservice.ris.norms.config.SecurityConfig;
import de.bund.digitalservice.ris.norms.domain.entity.Fixtures;
import de.bund.digitalservice.ris.norms.domain.entity.Norm;
import de.bund.digitalservice.ris.norms.domain.entity.Regelungstext;
import de.bund.digitalservice.ris.norms.domain.entity.eli.NormExpressionEli;
Expand Down Expand Up @@ -59,9 +58,6 @@ class NormExpressionControllerTest {
@MockitoBean
private TransformLegalDocMlToHtmlUseCase transformLegalDocMlToHtmlUseCase;

@MockitoBean
private ApplyPassiveModificationsUseCase applyPassiveModificationsUseCase;

@Nested
class getNorm {

Expand Down Expand Up @@ -241,30 +237,6 @@ void itCallsNormServiceAndReturnsNormRenderWithMetadata() throws Exception {
argThat(query -> query.xml().equals(xml) && query.showMetadata())
);
}

@Test
void itCallsNormServiceAndReturnsNormRenderWithAtIsoDate() throws Exception {
// Given
final String eli = "eli/bund/bgbl-1/1990/s2954/2022-12-19/1/deu/regelungstext-1";
final String html = "<div></div>";

when(loadNormUseCase.loadNorm(any())).thenReturn(Fixtures.loadNormFromDisk("SimpleNorm.xml"));
when(transformLegalDocMlToHtmlUseCase.transformLegalDocMlToHtml(any())).thenReturn(html);
when(applyPassiveModificationsUseCase.applyPassiveModifications(any()))
.thenReturn(Fixtures.loadRegelungstextFromDisk("SimpleNorm.xml"));

// When // Then
mockMvc
.perform(
get("/api/v1/norms/{eli}?atIsoDate=2024-04-03T00:00:00.000Z", eli)
.accept(MediaType.TEXT_HTML)
)
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
.andExpect(content().string(html));

verify(applyPassiveModificationsUseCase, times(1)).applyPassiveModifications(any());
}
}

@Nested
Expand Down
Loading