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

[vividus] Introduce INDEXING transformer #3385

Merged
merged 1 commit into from
Nov 24, 2022
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
45 changes: 45 additions & 0 deletions docs/modules/commons/pages/table-transformers.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,48 @@ Examples:
----
{transformer=CARTESIAN_PRODUCT, tables=\{transformer=REPEATING\, times=100\};\{transformer=FROM_CSV\, csvPath=/data/csv.csv\}}
----

== INDEXING

`INDEXING` transformer adds a column with the rows indices in the specified order. Indices are zero-based

[cols="1,3", options="header"]
|===
|Parameter|Description

|`order`|The mandatory indedxing order: `ASCENDING` or `DESCENDING`.
|===

.ASCENDING order example
[source,gherkin]
----
{transformer=INDEXING, order=ASCENDING}
|key |
|value |
|value2|
----

.ASCENDING order resulting table
[source,gherkin]
----
|key |index|
|value |0 |
ikalinin1 marked this conversation as resolved.
Show resolved Hide resolved
|value2|1 |
----

.DESCENDING order example
[source,gherkin]
----
{transformer=INDEXING, order=DESCENDING}
|key |
|value |
|value2|
----

.DESCENDING order resulting table
[source,gherkin]
----
|key |index|
|value |1 |
|value2|0 |
----
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,21 @@ When I initialize scenario variable `sitemapTransformerTable` with values:
{transformer=FROM_SITEMAP, siteMapRelativeUrl=/index.html, column=sitemapUrl, ignoreErrors=true}
Then `${sitemapTransformerTable}` is equal to table:
|sitemapUrl |

Scenario: Verify INDEXING transformer ASCENDING order
Then `<index>` is = `<expected>`
Examples:
{transformer=INDEXING, order=ASCENDING}
|expected|
|0 |
|1 |
|2 |

Scenario: Verify INDEXING transformer DESCENDING order
Then `<index>` is = `<expected>`
Examples:
{transformer=INDEXING, order=DESCENDING}
|expected|
|2 |
|1 |
|0 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.transformer;

import java.util.List;
import java.util.function.IntUnaryOperator;

import org.apache.commons.lang3.Validate;
import org.jbehave.core.model.ExamplesTable.TableProperties;
import org.jbehave.core.model.ExamplesTable.TableRows;
import org.jbehave.core.model.TableParsers;
import org.jbehave.core.model.TableTransformers.TableTransformer;
import org.vividus.util.ExamplesTableProcessor;

public class IndexingTableTransformer implements TableTransformer
{
private static final String INDEX = "index";

@Override
public String transform(String tableAsString, TableParsers tableParsers, TableProperties properties)
{
TableRows tableRows = tableParsers.parseRows(tableAsString, properties);
List<String> headers = tableRows.getHeaders();
Validate.isTrue(!headers.contains(INDEX),
"Unable to add column with row indices to the table, because it has `index` column.");
Order order = properties.getMandatoryNonBlankProperty("order", Order.class);
List<List<String>> rows = tableRows.getRows();
IntUnaryOperator indexer = order.getIndexer(rows);
for (int i = 0; i < rows.size(); i++)
{
rows.get(i).add(Integer.toString(indexer.applyAsInt(i)));
}
headers.add(INDEX);
return ExamplesTableProcessor.buildExamplesTable(headers, rows, properties);
}

private enum Order
{
ASCENDING
{
@Override
IntUnaryOperator getIndexer(List<?> rows)
{
return i -> i;
}
},
DESCENDING
{
@Override
IntUnaryOperator getIndexer(List<?> rows)
{
int size = rows.size();
return i -> size - i - 1;
}
};

abstract IntUnaryOperator getIndexer(List<?> rows);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<bean id="CARTESIAN_PRODUCT" class="org.vividus.transformer.CartesianProductTableTransformer" />
<bean id="DISTINCTING" class="org.vividus.transformer.DistinctingTableTransformer" />
<bean id="FILTERING" class="org.vividus.transformer.FilteringTableTransformer" />
<bean id="INDEXING" class="org.vividus.transformer.IndexingTableTransformer" />
<bean id="ITERATING" class="org.vividus.transformer.IteratingTableTransformer" />
<bean id="JOINING" class="org.vividus.transformer.JoiningTableTransformer" />
<bean id="MERGING" class="org.vividus.transformer.MergingTableTransformer" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.transformer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.model.ExamplesTable.TableProperties;
import org.jbehave.core.model.TableParsers;
import org.jbehave.core.steps.ParameterConverters;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class IndexingTableTransformerTests
{
@ParameterizedTest
@CsvSource({
"order=ASCENDING, '|k|index|\n|value|0|\n|value2|1|'",
"order=DESCENDING, '|k|index|\n|value|1|\n|value2|0|'"
})
void shouldAddIndexColumnAccordingWithOrder(String properties, String expectedTable)
{
var transformer = new IndexingTableTransformer();
var converters = new ParameterConverters();
var tableProperties = new TableProperties(properties, new Keywords(), converters);

assertEquals(expectedTable, transformer.transform("|k|\n|value|\n|value2|",
new TableParsers(new ParameterConverters()), tableProperties));
}

@Test
void shouldThrowAnExceptionIfIndexColumnIsInTheTable()
{
var transformer = new IndexingTableTransformer();
var converters = new ParameterConverters();
var tableProperties = new TableProperties("", new Keywords(), converters);
var tableParsers = new TableParsers(new ParameterConverters());

var iae = assertThrows(IllegalArgumentException.class, () -> transformer.transform("|index|\n|v|",
tableParsers, tableProperties));
assertEquals("Unable to add column with row indices to the table, because it has `index` column.",
iae.getMessage());
}
}