Skip to content
Snippets Groups Projects
Commit e598f4ca authored by OZG-Cloud Team's avatar OZG-Cloud Team
Browse files

OZG-7077 implement comments from code review

parent 0a1e5fdc
No related branches found
No related tags found
No related merge requests found
......@@ -58,14 +58,14 @@ public class FormDataHtmlCleaner {
return value;
}
Object parseHtml(String html) {
String parseHtml(String html) {
var outputSettings = new Document.OutputSettings(); // keep new lines
outputSettings.prettyPrint(false);
var htmlText = Jsoup.clean(modifyInputHtml(html), "", Safelist.none(), outputSettings);
var htmlText = Jsoup.clean(replaceNewLines(html), "", Safelist.none(), outputSettings);
return HtmlUtils.htmlUnescape(htmlText);
}
String modifyInputHtml(String html) {
private String replaceNewLines(String html) {
return html.replace("\\\\n", "\n");
}
}
/*
* Copyright (C) 2023 Das Land Schleswig-Holstein vertreten durch den
* Ministerpräsidenten des Landes Schleswig-Holstein
* Staatskanzlei
* Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
*
* Lizenziert unter der EUPL, Version 1.2 oder - sobald
* diese von der Europäischen Kommission genehmigt wurden -
* Folgeversionen der EUPL ("Lizenz");
* Sie dürfen dieses Werk ausschließlich gemäß
* dieser Lizenz nutzen.
* Eine Kopie der Lizenz finden Sie hier:
*
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* Sofern nicht durch anwendbare Rechtsvorschriften
* gefordert oder in schriftlicher Form vereinbart, wird
* die unter der Lizenz verbreitete Software "so wie sie
* ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
* ausdrücklich oder stillschweigend - verbreitet.
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
package de.ozgcloud.eingang.formcycle;
import static org.assertj.core.api.Assertions.*;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Spy;
import de.ozgcloud.eingang.common.formdata.FormDataTestFactory;
class FormDataHtmlCleanerITCase {
@Spy
@InjectMocks
private FormDataHtmlCleaner cleaner;
static final String KEY_LABEL = "label";
static final String KEY_VALUE = "value";
static final Map<String, Object> FORM_DATA_MAP = Map.of("tf1", Map.of(
KEY_LABEL, "<p><em>Label</em></p>",
KEY_VALUE, "<i>Value</i>"),
"fs1", Map.of(
KEY_LABEL, "Key",
KEY_VALUE, Map.of(
"tf3", Map.of(
KEY_LABEL, "<p><s>Label</s></p>",
KEY_VALUE, "ein Text mit <html><body><h1>Hello</h1><body><html>")),
"tf4", Map.of(
KEY_LABEL, "<p><span style=\"background-color:#1abc9c;\">Wichtig</span></p>",
KEY_VALUE, "Text"),
"ed1", Map.of(
KEY_LABEL,
"<ol>\n\t<li><em><strong><u>ganz</u></strong></em></li>\n\t<li><span style=\"color:#e74c3c;"
+ "\">bunt</span></li>\n</ol>",
KEY_VALUE, "TExt\nmit\n Leerzeichen\nund\n Umbrüchen"
)));
static final Map<String, Object> EXPECTED_MAP = Map.of("tf1", Map.of(
KEY_LABEL, "Label",
KEY_VALUE, "Value"),
"fs1", Map.of(
KEY_LABEL, "Key",
KEY_VALUE, Map.of(
"tf3", Map.of(
KEY_LABEL, "Label",
KEY_VALUE, "ein Text mit Hello")),
"tf4", Map.of(
KEY_LABEL, "Wichtig",
KEY_VALUE, "Text"),
"ed1", Map.of(
KEY_LABEL,
"\n\tganz\n\tbunt\n",
KEY_VALUE, "TExt\nmit\n Leerzeichen\nund\n Umbrüchen"
)));
@Test
void shouldCleanHtml() {
var result = cleaner.clean(FormDataTestFactory.createBuilder().formData(FORM_DATA_MAP).build());
assertThat(result.getFormData()).usingRecursiveComparison().isEqualTo(EXPECTED_MAP);
}
}
\ No newline at end of file
......@@ -175,81 +175,31 @@ class FormDataHtmlCleanerTest {
}
@Nested
class TestHtmlCleaner {
static final String KEY_LABEL = "label";
static final String KEY_VALUE = "value";
static final Map<String, Object> FORM_DATA_MAP = Map.of("tf1", Map.of(
KEY_LABEL, "<p><em>Label</em></p>",
KEY_VALUE, "<i>Value</i>"),
"fs1", Map.of(
KEY_LABEL, "Key",
KEY_VALUE, Map.of(
"tf3", Map.of(
KEY_LABEL, "<p><s>Label</s></p>",
KEY_VALUE, "ein Text mit <html><body><h1>Hello</h1><body><html>")),
"tf4", Map.of(
KEY_LABEL, "<p><span style=\"background-color:#1abc9c;\">Wichtig</span></p>",
KEY_VALUE, "Text"),
"ed1", Map.of(
KEY_LABEL,
"<ol>\n\t<li><em><strong><u>ganz</u></strong></em></li>\n\t<li><span style=\"color:#e74c3c;"
+ "\">bunt</span></li>\n</ol>",
KEY_VALUE, "TExt\nmit\n Leerzeichen\nund\n Umbrüchen"
)));
static final Map<String, Object> EXPECTED_MAP = Map.of("tf1", Map.of(
KEY_LABEL, "Label",
KEY_VALUE, "Value"),
"fs1", Map.of(
KEY_LABEL, "Key",
KEY_VALUE, Map.of(
"tf3", Map.of(
KEY_LABEL, "Label",
KEY_VALUE, "ein Text mit Hello")),
"tf4", Map.of(
KEY_LABEL, "Wichtig",
KEY_VALUE, "Text"),
"ed1", Map.of(
KEY_LABEL,
"\n\tganz\n\tbunt\n",
KEY_VALUE, "TExt\nmit\n Leerzeichen\nund\n Umbrüchen"
)));
class TestParseHtml {
@Test
void shouldCleanHtml() {
var result = cleaner.clean(FormData.builder().formData(FORM_DATA_MAP).build());
void shouldParseHtml() {
var result = cleaner.parseHtml("<html><body><h1>Hello</h1><body><html>");
assertThat(result.getFormData()).usingRecursiveComparison().isEqualTo(EXPECTED_MAP);
assertThat(result).isEqualTo("Hello");
}
@DisplayName("should decode german characters")
@ParameterizedTest(name = "{0} => {2}")
@CsvSource({ "&Auml;, &Auml;-Wert, Ä, Ä-Wert",
"&Ouml;, &Ouml;-Wert, Ö, Ö-Wert",
"&Uuml;, &Uuml;-Wert, Ü, Ü-Wert",
"&szlig;, Label mit &szlig;, ß, Label mit ß" })
void shouldDecodeUmlaut(String srcLabel, String srcValue, String expectedLabel, String expectedValue) {
var sourceMap = Map.<String, Object>of("tf1", Map.of(KEY_LABEL, srcLabel, KEY_VALUE, srcValue));
var expectedMap = Map.of("tf1", Map.of(KEY_LABEL, expectedLabel, KEY_VALUE, expectedValue));
@CsvSource({ "&Auml;, Ä", "&Ouml;, Ö", "&Uuml;, Ü", "&szlig;, ß" })
void shouldDecodeUmlaut(String srcValue, String expectedValue) {
var result = cleaner.parseHtml(srcValue);
var result = cleaner.cleanFormData(sourceMap);
assertThat(result).isEqualTo(expectedMap);
assertThat(result).isEqualTo(expectedValue);
}
@DisplayName("should decode special characters")
@ParameterizedTest(name = "{0} => {1}")
@CsvSource(value = { "&amp;, &",
"&lt;, <,",
"&gt;, >",
"&quot;, \"",
"&#39;, '" }, quoteCharacter = '`')
@CsvSource(value = { "&amp;, &", "&lt;, <,", "&gt;, >", "&quot;, \"", "&#39;, '" }, quoteCharacter = '`')
void shouldDecodeAmpersand(String src, String expected) {
var result = cleaner.cleanFormData(Map.of(KEY_LABEL, src));
var result = cleaner.parseHtml(src);
assertThat(result).isEqualTo(Map.of(KEY_LABEL, expected));
assertThat(result).isEqualTo(expected);
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment