Skip to content

Commit

Permalink
Simplify rendering endpoint
Browse files Browse the repository at this point in the history
RISDEV-0000
  • Loading branch information
malte-laukoetter committed Feb 18, 2025
1 parent 406101e commit a300182
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import static org.springframework.http.MediaType.*;

import de.bund.digitalservice.ris.norms.adapter.input.restapi.schema.PreviewRequestSchema;
import de.bund.digitalservice.ris.norms.application.port.input.TransformLegalDocMlToHtmlUseCase;
import java.time.Instant;
import java.util.Optional;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -26,29 +23,20 @@ public RenderingController(TransformLegalDocMlToHtmlUseCase transformLegalDocMlT
/**
* Retrieves the HTML rendering of a norm.
*
* @param previewRequestSchema The Request schema for rendering a norm. It includes the norm xml
* and any additional norms that should be used instead of the saved once for rendering the
* norm.
* @param xml The xml that should be rendered.
* @param showMetadata A boolean indicating whether metadata should be included in the rendering.
* @param snippet A boolean indicating whether the XML passed is just a snippet.
* @param atIsoDate ISO date string indicating which modifications should be applied before the
* HTML gets rendered and returned. If no date is provided the current date is used.
* @return A {@link ResponseEntity} containing the HTML rendering of the law document.
*/
@PostMapping(consumes = { APPLICATION_JSON_VALUE }, produces = { TEXT_HTML_VALUE })
@PostMapping(consumes = { APPLICATION_XML_VALUE }, produces = { TEXT_HTML_VALUE })
public ResponseEntity<String> getHtmlPreview(
@RequestBody final PreviewRequestSchema previewRequestSchema,
@RequestBody final String xml,
@RequestParam(defaultValue = "false") boolean showMetadata,
@RequestParam(defaultValue = "false") boolean snippet,
@RequestParam Optional<Instant> atIsoDate
@RequestParam(defaultValue = "false") boolean snippet
) {
return ResponseEntity.ok(
this.transformLegalDocMlToHtmlUseCase.transformLegalDocMlToHtml(
new TransformLegalDocMlToHtmlUseCase.Query(
previewRequestSchema.getRegelungstext(),
showMetadata,
snippet
)
new TransformLegalDocMlToHtmlUseCase.Query(xml, showMetadata, snippet)
)
);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,8 @@ void itThrowsXmlProcessingException() throws Exception {
post("/api/v1/renderings")
.accept(MediaType.TEXT_HTML)
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"regelungstext": "<law>original-law</law>"
}
"""
)
.contentType(MediaType.APPLICATION_XML)
.content("<law>original-law</law>")
)
.andExpect(status().isInternalServerError())
.andExpect(jsonPath("type").value("/errors/xml-processing-error"))
Expand All @@ -77,15 +70,8 @@ void getHtmlPreviewWithShowMetadataTrue() throws Exception {
.queryParam("showMetadata", "true")
.accept(MediaType.TEXT_HTML)
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"regelungstext": "<law>original-law</law>"
}
"""
)
.contentType(MediaType.APPLICATION_XML)
.content("<law>original-law</law>")
)
.andExpect(status().isOk())
.andExpect(content().string("<html></html>"));
Expand All @@ -107,14 +93,8 @@ void getHtmlPreviewWithShowMetadataFalse() throws Exception {
.queryParam("showMetadata", "false")
.accept(MediaType.TEXT_HTML)
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"regelungstext": "<law>original-law</law>"
}
"""
)
.contentType(MediaType.APPLICATION_XML)
.content("<law>original-law</law>")
)
.andExpect(status().isOk())
.andExpect(content().string("<html></html>"));
Expand All @@ -135,14 +115,8 @@ void getHtmlPreviewWithSnippetTrue() throws Exception {
.queryParam("snippet", "true")
.accept(MediaType.TEXT_HTML)
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"regelungstext": "<law>original-law</law>"
}
"""
)
.contentType(MediaType.APPLICATION_XML)
.content("<law>original-law</law>")
)
.andExpect(status().isOk())
.andExpect(content().string("<html></html>"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.google.gson.JsonObject;
import de.bund.digitalservice.ris.norms.adapter.output.database.repository.DokumentRepository;
import de.bund.digitalservice.ris.norms.domain.entity.Fixtures;
import de.bund.digitalservice.ris.norms.integration.BaseIntegrationTest;
Expand Down Expand Up @@ -35,20 +34,12 @@ class getHtmlPreview {

@Test
void itReturnsRender() throws Exception {
// Given
var jsonPayload = new JsonObject();
jsonPayload.addProperty(
"regelungstext",
Fixtures.loadTextFromDisk("NormWithPassiveModifications.xml")
);

// When // Then
mockMvc
.perform(
post("/api/v1/renderings?atIsoDate=2024-01-01T00:00:00.0Z")
.accept(MediaType.TEXT_HTML)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonPayload.toString())
.contentType(MediaType.APPLICATION_XML)
.content(Fixtures.loadTextFromDisk("NormWithPassiveModifications.xml"))
)
.andExpect(status().isOk())
.andExpect(
Expand Down
39 changes: 2 additions & 37 deletions frontend/src/composables/useNormRender.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,48 +33,13 @@ describe("useNormRenderHtml", () => {
method: "POST",
headers: expect.objectContaining({
Accept: "text/html",
"Content-Type": "application/json",
"Content-Type": "application/xml",
}),
body: expect.stringContaining(xml),
}),
)
})

it("support setting a date", async () => {
const fetchSpy = vi.spyOn(global, "fetch").mockResolvedValue(new Response())

const { useNormRenderHtml } = await import("./useNormRender")
const { isFinished } = useNormRenderHtml("<law></law>", {
showMetadata: true,
at: new Date(Date.UTC(2023, 0, 1)),
})
await vi.waitUntil(() => isFinished.value)

expect(fetchSpy).toHaveBeenCalledWith(
"/api/v1/renderings?showMetadata=true&atIsoDate=2023-01-01T00%3A00%3A00.000Z",
expect.anything(),
)
})

it("support setting custom norms", async () => {
const fetchSpy = vi.spyOn(global, "fetch").mockResolvedValue(new Response())

const { useNormRenderHtml } = await import("./useNormRender")

const { isFinished } = useNormRenderHtml("<law></law>", {
showMetadata: true,
customNorms: ["<xml>other-norm</xml>"],
})
await vi.waitUntil(() => isFinished.value)

expect(fetchSpy).toHaveBeenCalledWith(
"/api/v1/renderings?showMetadata=true",
expect.objectContaining({
body: expect.stringMatching("<xml>other-norm</xml>"),
}),
)
})

it("calls the API with snippet set to true", async () => {
const fetchSpy = vi
.spyOn(global, "fetch")
Expand All @@ -93,7 +58,7 @@ describe("useNormRenderHtml", () => {
method: "POST",
headers: expect.objectContaining({
Accept: "text/html",
"Content-Type": "application/json",
"Content-Type": "application/xml",
}),
body: expect.stringContaining(xml),
}),
Expand Down
15 changes: 2 additions & 13 deletions frontend/src/composables/useNormRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ export function useNormRenderHtml(
showMetadata?: MaybeRefOrGetter<boolean>
/** If the XML sent is only a snippet of a norm */
snippet?: MaybeRefOrGetter<boolean>
/** Passive modifications coming into effect before this date should be applied before rendering the HTML */
at?: MaybeRefOrGetter<Date | undefined>
/** The XMLs of norms which are referenced by the norm (e.g. in passiveModifications) and should be used instead of the data stored. */
customNorms?: MaybeRefOrGetter<string[] | undefined>
},
): UseFetchReturn<string> {
return useApiFetch<string>(
Expand All @@ -31,25 +27,18 @@ export function useNormRenderHtml(
searchParams.set("showMetadata", showMetadataVal ? "true" : "false")
const snippetVal = toValue(options?.snippet)
if (snippetVal) searchParams.set("snippet", snippetVal ? "true" : "false")
const atVal = toValue(options?.at)
if (atVal) searchParams.set("atIsoDate", atVal.toISOString())

const queryString = searchParams.toString()
return queryString ? `renderings?${queryString}` : "renderings"
}),
{
headers: {
Accept: "text/html",
"Content-Type": "application/json",
"Content-Type": "application/xml",
},
},
{
refetch: true,
},
).post(
computed(() => ({
regelungstext: toValue(normXml),
customRegelungstexte: toValue(options?.customNorms),
})),
)
).post(normXml)
}

0 comments on commit a300182

Please sign in to comment.