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

Release #225

Merged
merged 2 commits into from
Dec 19, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/com/epam/reportportal/utils/ParameterUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -154,4 +152,18 @@ public static <T> List<ParameterResource> 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<ParameterResource> parameters) {
List<List<String>> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand All @@ -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<List<String>> table) {
StringBuilder result = new StringBuilder();
int tableLength = table.stream().mapToInt(List::size).max().orElse(-1);
List<Iterator<String>> iterList = table.stream().map(List::iterator).collect(Collectors.toList());
List<Integer> 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<String> 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();
}
}
22 changes: 22 additions & 0 deletions src/test/java/com/epam/reportportal/utils/ParameterUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,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) {
}
}
Expand All @@ -65,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) {
}

Expand Down Expand Up @@ -235,4 +240,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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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"));
Expand All @@ -36,4 +47,16 @@ public void toMarkdownScript() {
assertThat("Incorrect markdown prefix", asCode("groovy", "hello"), equalTo("!!!MARKDOWN_MODE!!!```groovy\nhello\n```"));
}

}

@Test
public void test_format_data_table() {
List<List<String>> 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));
}
}