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

JBEHAVE-1604 Add ability to load chains of external resources #78

Merged
merged 1 commit into from
Nov 6, 2024
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 @@ -372,7 +372,7 @@ public String asString() {
for (TableProperties properties : tablePropertiesQueue) {
String propertiesAsString = properties.getPropertiesAsString();
if (!propertiesAsString.isEmpty()) {
sb.append("{").append(propertiesAsString).append("}").append(lastTableProperties().getRowSeparator());
sb.append(properties.asString()).append(lastTableProperties().getRowSeparator());
}
}
sb.append(ExamplesTableStringBuilder.buildExamplesTableString(lastTableProperties(), getHeaders(),
Expand Down Expand Up @@ -476,6 +476,10 @@ private String decoratePropertyValue(String value, Decorator decorator) {
}
}

public String asString() {
return String.format("{%s}", propertiesAsString);
}

@SuppressWarnings("unchecked")
public <T> T getMandatoryNonBlankProperty(String propertyName, Type type) {
String propertyValue = properties.getProperty(propertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.apache.commons.lang3.Validate.isTrue;

import java.util.Deque;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.jbehave.core.configuration.Configuration;
Expand Down Expand Up @@ -94,7 +95,7 @@ public ExamplesTable createExamplesTable(String input) {
String tableAsString = tablePropertiesQueue.getTable().trim();
Deque<TableProperties> properties = tablePropertiesQueue.getProperties();

if (!isTable(tableAsString, properties.peekFirst()) && !tableAsString.isEmpty()) {
if (isExternal(tableAsString, properties.peekFirst())) {
checkResourceProperties(input, properties.peekFirst());
String loadedTable = resourceLoader.loadResourceAsText(tableAsString.trim());
tablePropertiesQueue = tableParsers.parseProperties(loadedTable);
Expand All @@ -105,6 +106,16 @@ public ExamplesTable createExamplesTable(String input) {
target.addLast(outerProperties);
}

String table = tablePropertiesQueue.getTable();
if (isExternal(tablePropertiesQueue.getTable(), headProperties)) {
table = target.stream().map(TableProperties::asString)
.collect(Collectors.joining(
System.lineSeparator(),
"",
System.lineSeparator() + table));
return createExamplesTable(table);
}

boolean hasTransformers = target.getFirst().getTransformer() != null;
if (hasTransformers) {
loadedTable = TableTransformersExecutor.applyTransformers(tableTransformers,
Expand All @@ -128,6 +139,10 @@ public ExamplesTable createExamplesTable(String input) {
tableTransformers, tableTransformerMonitor);
}

private boolean isExternal(String tableAsString, TableProperties properties) {
return !isTable(tableAsString, properties) && !tableAsString.isEmpty();
}

private void checkResourceProperties(String table, TableProperties properties) {
isTrue(keywords.examplesTableHeaderSeparator().equals(properties.getHeaderSeparator())
&& keywords.examplesTableValueSeparator().equals(properties.getValueSeparator())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,25 @@ void shouldLoadTableFromPathAndPreserveSeparators() {
ensureRowContentIs(rows, 2, asList("STK1", "15.0", "16.0", "ON"));
}

@Test
void shouldLoadTableFromAnotherTableContainingTableReference() {
// Given
ExamplesTableFactory factory = new ExamplesTableFactory(new LoadFromClasspath(), new TableTransformers());

// When
ExamplesTable table = factory.createExamplesTable("{transformer=REPLACING, replacing=STK1, replacement=STK2}"
+ System.lineSeparator() + "{transformer=REPLACING, replacing=price, replacement=cost}"
+ System.lineSeparator() + "data-reference.table");

// Then
assertThat(table.getHeaders(), equalTo(asList("symbol", "threshold", "cost", "status")));
List<Map<String, String>> rows = table.getRows();
assertThat(rows.size(), equalTo(3));
ensureRowContentIs(rows, 0, asList("STK2", "17.0", "5.0", "OFF"));
ensureRowContentIs(rows, 1, asList("STK2", "17.0", "10.0", "OFF"));
ensureRowContentIs(rows, 2, asList("STK2", "17.0", "16.0", "ON"));
}

private void ensureRowContentIs(List<Map<String, String>> rows, int row, List<String> expected) {
assertThat(new ArrayList<>(rows.get(row).values()), equalTo(expected));
}
Expand Down
2 changes: 2 additions & 0 deletions jbehave-core/src/test/resources/data-reference.table
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{transformer=REPLACING, replacing=15.0, replacement=17.0}
data.table
Loading