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-delta #1013

Merged
merged 2 commits into from
Feb 19, 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 @@ -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,50 +23,21 @@ 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)
)
);
}

/**
* Retrieves the XML preview of a regelungstext.
*
* @param previewRequestSchema The Request schema for rendering a regelungstext. It includes the regelungstext xml
* and any additional regelungstexts that should be used instead of the saved once for rendering the
* regelungstext.
* @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.
* @deprecated
*/
@PostMapping(consumes = { APPLICATION_JSON_VALUE }, produces = { APPLICATION_XML_VALUE })
@Deprecated(forRemoval = true)
public ResponseEntity<String> getXMLPreview(
@RequestBody final PreviewRequestSchema previewRequestSchema,
@RequestParam Optional<Instant> atIsoDate
) {
return ResponseEntity.ok(previewRequestSchema.getRegelungstext());
}
}

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
106 changes: 2 additions & 104 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,77 +58,10 @@ describe("useNormRenderHtml", () => {
method: "POST",
headers: expect.objectContaining({
Accept: "text/html",
"Content-Type": "application/json",
"Content-Type": "application/xml",
}),
body: expect.stringContaining(xml),
}),
)
})
})

describe("useNormRenderXml", () => {
beforeEach(() => {
vi.resetModules()
vi.resetAllMocks()
})

it("calls the API without date", async () => {
const responseBody = `<?xml version="1.0" encoding="UTF-8"?><law>applied-passive-modification</law>`
const fetchSpy = vi
.spyOn(global, "fetch")
.mockResolvedValueOnce(new Response(responseBody))

const xml = "<law></law>"
const { useNormRenderXml } = await import("./useNormRender")

const { data, isFinished } = useNormRenderXml(xml)
await vi.waitUntil(() => isFinished.value)
expect(data.value).toBe(responseBody)

expect(fetchSpy).toHaveBeenCalledWith(
"/api/v1/renderings",
expect.objectContaining({
method: "POST",
headers: expect.objectContaining({
Accept: "application/xml",
"Content-Type": "application/json",
}),
body: expect.stringContaining(xml),
}),
)
})

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

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

expect(fetchSpy).toHaveBeenCalledWith(
"/api/v1/renderings?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 { useNormRenderXml } = await import("./useNormRender")

const { isFinished } = useNormRenderXml("<law></law>", undefined, [
"<xml>other-norm</xml>",
])
await vi.waitUntil(() => isFinished.value)

expect(fetchSpy).toHaveBeenCalledWith(
"/api/v1/renderings",
expect.objectContaining({
body: expect.stringMatching("<xml>other-norm</xml>"),
}),
)
})
})
60 changes: 2 additions & 58 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,70 +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),
})),
)
}

/**
* Composable for rendering the XML of a norm as XML.
*
* @param normXml XML of the norm that should be rendered
* @param customNorms The XMLs of norms which are referenced by the norm (e.g. in passiveModifications) and should be used instead of the data stored.
* @param at Passive modifications coming into effect before this date should be applied before rendering the HTML
*/
export function useNormRenderXml(
normXml: MaybeRefOrGetter<string | undefined>,
at?: MaybeRefOrGetter<Date | undefined>,
customNorms?: MaybeRefOrGetter<string[] | undefined>,
): UseFetchReturn<string> {
return useApiFetch<string>(
computed(() => {
if (!toValue(normXml)) return INVALID_URL

const searchParams = new URLSearchParams()
const atValue = toValue(at)
if (atValue) {
searchParams.set("atIsoDate", atValue.toISOString())
}

return (
"renderings" +
(searchParams.size > 0 ? "?" : "") +
searchParams.toString()
)
}),
{
headers: {
Accept: "application/xml",
"Content-Type": "application/json",
},
},
{
refetch: true,
},
).post(
computed(() => ({
regelungstext: toValue(normXml),
customRegelungstexte: toValue(customNorms),
})),
)
).post(normXml)
}
Loading