Skip to content

Commit

Permalink
[bdd-engine] Add ability to load ExamplesTable bodies from variable
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst committed Sep 8, 2021
1 parent c6ab617 commit 423da69
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019-2021 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.jbehave.core.model;

import java.util.Optional;

import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.model.ExamplesTable.TablePropertiesQueue;
import org.jbehave.core.steps.ParameterConverters;
import org.vividus.bdd.steps.VariableResolver;

public class VariableResolvingTableParsers extends TableParsers
{
private final VariableResolver variableResolver;

public VariableResolvingTableParsers(VariableResolver variableResolver, Keywords keywords,
ParameterConverters parameterConverters, Optional<String> defaultNullPlaceholder)
{
super(keywords, parameterConverters, defaultNullPlaceholder);
this.variableResolver = variableResolver;
}

@Override
public TablePropertiesQueue parseProperties(String tableAsString)
{
return super.parseProperties((String) variableResolver.resolve(tableAsString));
}
}
16 changes: 3 additions & 13 deletions vividus-bdd-engine/src/main/java/org/vividus/bdd/StoryLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,25 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.FilenameUtils;
import org.jbehave.core.io.LoadFromClasspath;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.vividus.bdd.examples.IExamplesTableLoader;
import org.vividus.bdd.steps.VariableResolver;

public class StoryLoader extends LoadFromClasspath
{
private final VariableResolver variableResolver;
private IExamplesTableLoader examplesTableLoader;
private ResourcePatternResolver resourcePatternResolver;

public StoryLoader(VariableResolver variableResolver)
{
super(StandardCharsets.UTF_8);
this.variableResolver = variableResolver;
}

@Override
public String loadResourceAsText(String resourcePath)
{
String path = (String) variableResolver.resolve(resourcePath);
if ("table".equals(FilenameUtils.getExtension(path)))
if ("table".equals(FilenameUtils.getExtension(resourcePath)))
{
return examplesTableLoader.loadExamplesTable(path);
return examplesTableLoader.loadExamplesTable(resourcePath);
}
return super.loadResourceAsText(path);
return super.loadResourceAsText(resourcePath);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jbehave.core.model.Story;
import org.jbehave.core.model.TableTransformers;
import org.jbehave.core.model.TableTransformers.TableTransformer;
import org.jbehave.core.model.VariableResolvingTableParsers;
import org.jbehave.core.parsers.RegexStoryParser;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.reporters.ViewGenerator;
Expand Down Expand Up @@ -67,6 +68,8 @@ public void init() throws IOException
useParameterControls(parameterControls);
useParameterConverters(new ParameterConvertersDecorator(this, variableResolver, expressionAdaptor)
.addConverters(customConverters));
tableParsers = new VariableResolvingTableParsers(variableResolver, keywords(), parameterConverters(),
Optional.empty());
useStoryParser(new RegexStoryParser(examplesTableFactory()));
TableTransformers transformers = tableTransformers();
customTableTransformers.forEach(transformers::useTransformer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@
import org.springframework.core.io.support.ResourcePatternResolver;
import org.vividus.bdd.examples.IExamplesTableLoader;
import org.vividus.bdd.resource.ResourceLoadException;
import org.vividus.bdd.steps.VariableResolver;

@ExtendWith(MockitoExtension.class)
class StoryLoaderTests
{
@Mock private VariableResolver variableResolver;
@Mock private ResourcePatternResolver resourcePatternResolver;
@Mock private IExamplesTableLoader examplesTableLoader;
@InjectMocks private StoryLoader storyLoader;
Expand All @@ -59,20 +57,17 @@ void beforeEach()
@Test
void testLoadResourceAsText()
{
var preProcessedStoryPath = "${unit}test.story";
var storyPath = "unittest.story";
when(variableResolver.resolve(preProcessedStoryPath)).thenReturn(storyPath);
Resource resource = resourceLoader.getResource(storyPath);
when(resourcePatternResolver.getResource(storyPath)).thenReturn(resource);
String actual = storyLoader.loadResourceAsText(preProcessedStoryPath);
String actual = storyLoader.loadResourceAsText(storyPath);
assertEquals("unittest", actual.trim());
}

@Test
void shouldThrowResourceLoadException() throws IOException
{
var resourcePath = "resourcePath";
when(variableResolver.resolve(resourcePath)).thenReturn(resourcePath);
Resource resource = mock(Resource.class);
when(resourcePatternResolver.getResource(resourcePath)).thenReturn(resource);
ResourceLoadException ioException = new ResourceLoadException("Resource IOException");
Expand All @@ -96,7 +91,6 @@ void testLoadExamplesTableAsText()
{
var tablePath = "unittest.table";
var tableContent = "tableContent";
when(variableResolver.resolve(tablePath)).thenReturn(tablePath);
when(examplesTableLoader.loadExamplesTable(tablePath)).thenReturn(tableContent);
String actual = storyLoader.loadResourceAsText(tablePath);
assertEquals(tableContent, actual);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
Expand All @@ -44,6 +45,7 @@
import org.jbehave.core.model.ExamplesTableFactory;
import org.jbehave.core.model.Story;
import org.jbehave.core.model.TableTransformers.TableTransformer;
import org.jbehave.core.model.VariableResolvingTableParsers;
import org.jbehave.core.parsers.RegexStoryParser;
import org.jbehave.core.reporters.ViewGenerator;
import org.jbehave.core.steps.DelegatingStepMonitor;
Expand Down Expand Up @@ -120,6 +122,14 @@ void testInit() throws IOException
constructedMocks.put(ParameterConvertersDecorator.class, mock);

when(mock.addConverters(parameterConverterList)).thenReturn(mock);
});
MockedConstruction<VariableResolvingTableParsers> ignoredParsers = mockConstruction(
VariableResolvingTableParsers.class, (mock, context) -> {
assertEquals(1, context.getCount());
assertEquals(List.of(variableResolver, constructedMocks.get(Keywords.class),
constructedMocks.get(ParameterConvertersDecorator.class), Optional.empty()),
context.arguments());
constructedMocks.put(VariableResolvingTableParsers.class, mock);
}))
{
StoryControls storyControls = mock(StoryControls.class);
Expand All @@ -140,6 +150,8 @@ void testInit() throws IOException
(RegexStoryParser) constructedMocks.get(RegexStoryParser.class));
ordered.verify(configuration).useStoryControls(storyControls);

assertSame(constructedMocks.get(VariableResolvingTableParsers.class), configuration.tableParsers());

verifyStepMonitor(stepMonitor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ bdd.batch-1.meta-filters=groovy: !skip && epic != 'vividus-plugin-ssh' && !local
bdd.batch-1.threads=5
bdd.batch-1.story-execution-timeout=PT10M
bdd.variables.batch-1.locale=ca
bdd.variables.batch-1.filtered-examples-table={transformer=FILTERING, byColumnNames=column3}\
/data/for-filtering-transformer.table

bdd.story-loader.batch-2.resource-location=story/integration
bdd.story-loader.batch-2.resource-include-patterns=Batch-level variables and meta-filters.story
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ Examples:
/data/for-filtering-transformer.table


Scenario: Verify FILTERING transformer with byColumnNames parameter loaded from variable
Then `<column3>` is equal to `C`
Examples:
${filtered-examples-table}


Scenario: Verify RESOLVING_EXPRESSIONS_EAGERLY transformer
When I initialize story variable `table` with values:
/data/with-replacing-transformer.table
Expand Down

0 comments on commit 423da69

Please sign in to comment.