Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kulgg committed Jan 9, 2024
1 parent 1c9f42b commit 5895605
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@

import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.MutableDataSet;
import org.jsoup.Jsoup;

public class MarkdownToTextConverter {
String extractText(String markdown) {
public String extractText(String markdown) {
var options = new MutableDataSet();
Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
Node document = parser.parse(markdown);
String html = renderer.render(document);
var parser = Parser.builder(options).build();
var renderer = HtmlRenderer.builder(options).build();
var document = parser.parse(markdown);
var html = renderer.render(document);
return Jsoup.parse(html).text();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package de.sovity.edc.ext.wrapper.api.common.mappers.utils;

public class TextUtils {
String abbreviate(String text, int maxCharacters) {
public String abbreviate(String text, int maxCharacters) {
if (text == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,11 @@ private JsonObject getPrivateProperties(JsonObject assetJsonLd) {
}

private String buildShortDescription(String description) {
if (description != null) {
return textUtils.abbreviate(markdownToTextConverter.extractText(description), 300);
if (description == null) {
return null;
}
return null;

var text = markdownToTextConverter.extractText(description);
return textUtils.abbreviate(text, 300);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void test_abbreviate_null() {
String text = null;

// act
var actual = textUtils.abbreviate(text, 300);
var actual = textUtils.abbreviate(text, 1);

// assert
assertThat(actual).isEqualTo(null);
Expand All @@ -28,10 +28,10 @@ void test_abbreviate_null() {
@Test
void test_abbreviate_emptyString() {
// arrange
String text = "";
var text = "";

// act
var actual = textUtils.abbreviate(text, 300);
var actual = textUtils.abbreviate(text, 1);

// assert
assertThat(actual).isEqualTo("");
Expand All @@ -40,26 +40,24 @@ void test_abbreviate_emptyString() {
@Test
void test_abbreviate_lengthLessThanMaxCharacters() {
// arrange
String text = "Hello";
var text = "a";

// act
var actual = textUtils.abbreviate(text, 300);
var actual = textUtils.abbreviate(text, 2);

// assert
assertThat(actual).isEqualTo("Hello");
assertThat(actual).isEqualTo("a");
}

@Test
void test_abbreviate_lengthLongerThanMaxCharacters() {
// arrange
var tmp = "0123456789";
var text = tmp.repeat(40); // 400 length String
var expected = tmp.repeat(30);
var text = "aa";

// act
var actual = textUtils.abbreviate(text, 300);
var actual = textUtils.abbreviate(text, 1);

// assert
assertThat(actual).isEqualTo(expected);
assertThat(actual).isEqualTo("a");
}
}

0 comments on commit 5895605

Please sign in to comment.