Skip to content

Commit

Permalink
[engine] Fix logging of deprecated steps without parameters inside co…
Browse files Browse the repository at this point in the history
…mposite steps (#4682)

Co-authored-by: draker94 <noreply@github.com>
  • Loading branch information
draker94 and web-flow authored Dec 21, 2023
1 parent 13a0174 commit c35b389
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Comparator;
import java.util.Formatter;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -45,8 +46,8 @@ public DeprecatedStepNotificationFactory(RunTestContext runTestContext, Configur
protected String createDeprecatedStepNotification(String versionToRemove, String replacementFormatPattern)
{
String runningDeprecatedStep = runTestContext.getRunningStory().getRunningSteps().getFirst();
Matcher stepMatcher = getStepMatcher(runningDeprecatedStep);
String[] stepParams = getParametersFromStep(stepMatcher);
Optional<Matcher> stepMatcher = getStepMatcher(runningDeprecatedStep);
String[] stepParams = stepMatcher.map(this::getParametersFromStep).orElseGet(() -> new String[0]);

String notification;
try (Formatter newStep = new Formatter())
Expand All @@ -59,7 +60,7 @@ protected String createDeprecatedStepNotification(String versionToRemove, String
return notification;
}

private Matcher getStepMatcher(String rawDeprecatedStep)
private Optional<Matcher> getStepMatcher(String rawDeprecatedStep)
{
List<Matcher> matchers = new ArrayList<>();
Keywords keywords = configuration.keywords();
Expand All @@ -73,7 +74,7 @@ private Matcher getStepMatcher(String rawDeprecatedStep)
matchers.add(matcher);
}
}
return matchers.stream().max(Comparator.comparing(Matcher::groupCount)).orElse(null);
return matchers.stream().max(Comparator.comparing(Matcher::groupCount));
}

private String[] getParametersFromStep(Matcher stepMatcher)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.Keywords;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -42,6 +43,9 @@
@ExtendWith(MockitoExtension.class)
class DeprecatedStepNotificationFactoryTests
{
private static final String DEPRECATION_MSG_FORMAT =
"The step: \"%s\" is deprecated and will be removed in VIVIDUS %s. Use step: \"%s\"";

private static final String REMOVE_VERSION = "0.7.0";

private static final String NOT_PARAMETERIZED_STEP_VALUE = "step without parameters deprecated";
Expand Down Expand Up @@ -93,13 +97,26 @@ void shouldGetNotificationForRunningDeprecatedStep(String deprecatedStepValue, S
when(collectingStepPatternsMonitor.getRegisteredStepPatterns()).thenReturn(stepPatternsCache);

mockRunTestContext(deprecatedStep);
String expectedNotification = String.format(
"The step: \"%s\" is deprecated and will be removed in VIVIDUS %s. Use step: \"%s\"", deprecatedStep,
REMOVE_VERSION, expectedActualStep);
String expectedNotification = String.format(DEPRECATION_MSG_FORMAT, deprecatedStep, REMOVE_VERSION,
expectedActualStep);
assertEquals(expectedNotification, deprecatedStepNotificationFactory
.createDeprecatedStepNotification(REMOVE_VERSION, formatPattern));
}

@Test
void shouldGetNotificationForRunningDeprecatedStepWithoutPattern()
{
Map<String, Pattern> stepPatternsCache = new ConcurrentHashMap<>();
when(configuration.keywords()).thenReturn(new Keywords());
when(collectingStepPatternsMonitor.getRegisteredStepPatterns()).thenReturn(stepPatternsCache);

mockRunTestContext(NOT_PARAMETERIZED_STEP_DEPR);
String expectedNotification = String.format(DEPRECATION_MSG_FORMAT, NOT_PARAMETERIZED_STEP_DEPR, REMOVE_VERSION,
NOT_PARAMETERIZED_STEP_ACTUAL);
assertEquals(expectedNotification, deprecatedStepNotificationFactory
.createDeprecatedStepNotification(REMOVE_VERSION, NOT_PARAMETERIZED_STEP_ACTUAL));
}

private void mockRunTestContext(String deprecatedStep)
{
var runningStory = mock(RunningStory.class);
Expand Down

0 comments on commit c35b389

Please sign in to comment.