From 883cc649bae5850ccbe1588008bb273eab55ca3c Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Tue, 19 Dec 2023 15:30:28 +0300 Subject: [PATCH 1/2] Parameter format methods --- CHANGELOG.md | 2 + .../reportportal/utils/ParameterUtils.java | 20 +++++-- .../utils/markdown/MarkdownUtils.java | 59 ++++++++++++++++++- .../utils/ParameterUtilsTest.java | 18 ++++++ .../utils/markdown/MarkdownUtilsTest.java | 25 +++++++- 5 files changed, 116 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45c19909..ca45e4de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## [Unreleased] +### Added +- `MarkdownUtils.formatDataTable` and `ParameterUtils.formatParametersAsTable` methods to support parameter reporting for BDD frameworks, by @HardNorth ## [5.1.25] ### Changed diff --git a/src/main/java/com/epam/reportportal/utils/ParameterUtils.java b/src/main/java/com/epam/reportportal/utils/ParameterUtils.java index a9783ce8..544f9dd3 100644 --- a/src/main/java/com/epam/reportportal/utils/ParameterUtils.java +++ b/src/main/java/com/epam/reportportal/utils/ParameterUtils.java @@ -17,6 +17,7 @@ package com.epam.reportportal.utils; import com.epam.reportportal.annotations.ParameterKey; +import com.epam.reportportal.utils.markdown.MarkdownUtils; import com.epam.ta.reportportal.ws.model.ParameterResource; import org.apache.commons.lang3.tuple.Pair; @@ -25,10 +26,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Executable; import java.lang.reflect.Parameter; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -154,4 +152,18 @@ public static List getParameters(@Nullable final String c .findAny()); }).map(m -> ParameterUtils.getParameters(m, paramValues.orElse(null))).orElse(getParameters(parameters)); } + + /** + * Format parameters as Markdown table. + * + * @param parameters list of parameters + * @return text representation of parameter table + */ + @Nonnull + public static String formatParametersAsTable(@Nonnull List parameters) { + List> tableList = new ArrayList<>(); + tableList.add(parameters.stream().map(ParameterResource::getKey).collect(Collectors.toList())); + tableList.add(parameters.stream().map(ParameterResource::getValue).collect(Collectors.toList())); + return MarkdownUtils.formatDataTable(tableList); + } } diff --git a/src/main/java/com/epam/reportportal/utils/markdown/MarkdownUtils.java b/src/main/java/com/epam/reportportal/utils/markdown/MarkdownUtils.java index 431ef5a6..a4db048e 100644 --- a/src/main/java/com/epam/reportportal/utils/markdown/MarkdownUtils.java +++ b/src/main/java/com/epam/reportportal/utils/markdown/MarkdownUtils.java @@ -15,6 +15,12 @@ */ package com.epam.reportportal.utils.markdown; +import javax.annotation.Nonnull; +import java.util.Iterator; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + import static java.util.Optional.ofNullable; /** @@ -26,6 +32,10 @@ public class MarkdownUtils { public static final String MARKDOWN_MODE = "!!!MARKDOWN_MODE!!!"; private static final char NEW_LINE = '\n'; + public static final String ONE_SPACE = "\u00A0"; + public static final String TABLE_INDENT = "\u00A0\u00A0\u00A0\u00A0"; + public static final String TABLE_COLUMN_SEPARATOR = "|"; + public static final String TABLE_ROW_SEPARATOR = "-"; /** * Adds special prefix to make log message being processed as markdown @@ -45,9 +55,52 @@ public static String asMarkdown(String message) { * @return Message to be sent to ReportPortal */ public static String asCode(String language, String script) { - return MARKDOWN_MODE + - "```" + ofNullable(language).orElse("") + NEW_LINE + + return asMarkdown("```" + ofNullable(language).orElse("") + NEW_LINE + script + NEW_LINE + - "```"; + "```"); + } + + /** + * Converts a table represented as List of Lists to a formatted table string + * + * @param table a table object + * @return string representation of the table + */ + @Nonnull + public static String formatDataTable(@Nonnull final List> table) { + StringBuilder result = new StringBuilder(); + int tableLength = table.stream().mapToInt(List::size).max().orElse(-1); + List> iterList = table.stream().map(List::iterator).collect(Collectors.toList()); + List colSizes = IntStream.range(0, tableLength) + .mapToObj(n -> iterList.stream().filter(Iterator::hasNext).map(Iterator::next).collect(Collectors.toList())) + .map(col -> col.stream().mapToInt(String::length).max().orElse(0)) + .collect(Collectors.toList()); + + boolean header = true; + for (List row : table) { + result.append(TABLE_INDENT).append(TABLE_COLUMN_SEPARATOR); + for (int i = 0; i < row.size(); i++) { + String cell = row.get(i); + int maxSize = colSizes.get(i) - cell.length() + 2; + int lSpace = maxSize / 2; + int rSpace = maxSize - lSpace; + IntStream.range(0, lSpace).forEach(j -> result.append(ONE_SPACE)); + result.append(cell); + IntStream.range(0, rSpace).forEach(j -> result.append(ONE_SPACE)); + result.append(TABLE_COLUMN_SEPARATOR); + } + if (header) { + header = false; + result.append(NEW_LINE); + result.append(TABLE_INDENT).append(TABLE_COLUMN_SEPARATOR); + for (int i = 0; i < row.size(); i++) { + int maxSize = colSizes.get(i) + 2; + IntStream.range(0, maxSize).forEach(j -> result.append(TABLE_ROW_SEPARATOR)); + result.append(TABLE_COLUMN_SEPARATOR); + } + } + result.append(NEW_LINE); + } + return result.toString().trim(); } } diff --git a/src/test/java/com/epam/reportportal/utils/ParameterUtilsTest.java b/src/test/java/com/epam/reportportal/utils/ParameterUtilsTest.java index d46bc4e3..54864e18 100644 --- a/src/test/java/com/epam/reportportal/utils/ParameterUtilsTest.java +++ b/src/test/java/com/epam/reportportal/utils/ParameterUtilsTest.java @@ -18,6 +18,7 @@ import com.epam.reportportal.annotations.ParameterKey; import com.epam.reportportal.service.item.TestCaseIdEntry; +import com.epam.reportportal.utils.markdown.MarkdownUtilsTest; import com.epam.ta.reportportal.ws.model.ParameterResource; import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.api.Test; @@ -235,4 +236,21 @@ public void test_non_existent_codref() { assertThat(p.getValue(), equalTo(v == null ? "NULL" : v.toString())); }); } + + @Test + public void test_parameters_format() { + ParameterResource varA = new ParameterResource(); + ParameterResource varB = new ParameterResource(); + ParameterResource result = new ParameterResource(); + + varA.setKey("var_a"); + varB.setKey("var_b"); + result.setKey("result"); + varA.setValue("2"); + varB.setValue("2"); + result.setValue("4"); + + assertThat(ParameterUtils.formatParametersAsTable(Arrays.asList(varA, varB, result)), + equalTo(MarkdownUtilsTest.ONE_ROW_EXPECTED_TABLE)); + } } diff --git a/src/test/java/com/epam/reportportal/utils/markdown/MarkdownUtilsTest.java b/src/test/java/com/epam/reportportal/utils/markdown/MarkdownUtilsTest.java index 4f092e9c..d9cc2e17 100644 --- a/src/test/java/com/epam/reportportal/utils/markdown/MarkdownUtilsTest.java +++ b/src/test/java/com/epam/reportportal/utils/markdown/MarkdownUtilsTest.java @@ -17,7 +17,11 @@ import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.List; + import static com.epam.reportportal.utils.markdown.MarkdownUtils.asCode; +import static com.epam.reportportal.utils.markdown.MarkdownUtils.formatDataTable; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -26,6 +30,13 @@ */ public class MarkdownUtilsTest { + public static final String ONE_ROW_EXPECTED_TABLE = "\u00A0\u00A0\u00A0\u00A0|\u00A0var_a\u00A0|\u00A0var_b\u00A0|\u00A0result\u00A0|\n" + + "\u00A0\u00A0\u00A0\u00A0|-------|-------|--------|\n" + + "\u00A0\u00A0\u00A0\u00A0|\u00A0\u00A0\u00A02\u00A0\u00A0\u00A0|\u00A0\u00A0\u00A02\u00A0\u00A0\u00A0|\u00A0\u00A0\u00A04\u00A0\u00A0\u00A0\u00A0|"; + + public static final String TWO_ROWS_EXPECTED_TABLE = ONE_ROW_EXPECTED_TABLE + "\n" + + "\u00A0\u00A0\u00A0\u00A0|\u00A0\u00A0\u00A01\u00A0\u00A0\u00A0|\u00A0\u00A0\u00A02\u00A0\u00A0\u00A0|\u00A0\u00A0\u00A03\u00A0\u00A0\u00A0\u00A0|"; + @Test public void asMarkdown() { assertThat("Incorrect markdown prefix", MarkdownUtils.asMarkdown("hello"), equalTo("!!!MARKDOWN_MODE!!!hello")); @@ -36,4 +47,16 @@ public void toMarkdownScript() { assertThat("Incorrect markdown prefix", asCode("groovy", "hello"), equalTo("!!!MARKDOWN_MODE!!!```groovy\nhello\n```")); } -} \ No newline at end of file + + @Test + public void test_format_data_table() { + List> table = + Arrays.asList( + Arrays.asList("var_a", "var_b", "result"), + Arrays.asList("2", "2", "4"), + Arrays.asList("1", "2", "3") + ); + + assertThat(formatDataTable(table), equalTo(TWO_ROWS_EXPECTED_TABLE)); + } +} From 77aeac69323004fd12b7a05936e45fe5b0a9a757 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Tue, 19 Dec 2023 15:32:13 +0300 Subject: [PATCH 2/2] Suppress some warnings in tests --- .../java/com/epam/reportportal/utils/ParameterUtilsTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/java/com/epam/reportportal/utils/ParameterUtilsTest.java b/src/test/java/com/epam/reportportal/utils/ParameterUtilsTest.java index 54864e18..ef1523f2 100644 --- a/src/test/java/com/epam/reportportal/utils/ParameterUtilsTest.java +++ b/src/test/java/com/epam/reportportal/utils/ParameterUtilsTest.java @@ -39,11 +39,13 @@ public class ParameterUtilsTest { public static class ParameterUtilsTestObject { + @SuppressWarnings("unused") public ParameterUtilsTestObject(String string, TestCaseIdEntry id, int number) { } } public static class ParameterUtilsTestObjectKey { + @SuppressWarnings("unused") public ParameterUtilsTestObjectKey(String string, TestCaseIdEntry id, @ParameterKey("index") int number) { } } @@ -66,9 +68,11 @@ public ParameterUtilsTestObjectKey(String string, TestCaseIdEntry id, @Parameter .findAny() .orElse(null); + @SuppressWarnings("unused") public void test(String string, TestCaseIdEntry id, int number) { } + @SuppressWarnings("unused") public void testKey(String string, @ParameterKey("id") TestCaseIdEntry id, int number) { }