Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Jan 31, 2025
1 parent 0782490 commit d78c531
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 51 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=5.3.6-SNAPSHOT
version=5.4.0-SNAPSHOT
description=EPAM Report portal. Cucumber JVM version [6.0.0; ) adapter
cucumber_version=7.20.1
junit_version=5.6.3
Expand Down
67 changes: 28 additions & 39 deletions src/main/java/com/epam/reportportal/cucumber/AbstractReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public abstract class AbstractReporter implements ConcurrentEventListener {

protected static final URI WORKING_DIRECTORY = new File(System.getProperty("user.dir")).toURI();
protected static final String METHOD_OPENING_BRACKET = "(";
protected static final String HOOK_ = "Hook: ";
protected static final String DOCSTRING_DECORATOR = "\n\"\"\"\n";
private static final String ERROR_FORMAT = "Error:\n%s";

Expand Down Expand Up @@ -346,16 +345,14 @@ protected void afterScenario(TestCaseFinished event) {
}

/**
* Generate a step name based on its type (Before Hook / Regular / etc.)
* Generate a step name.
*
* @param testStep Cucumber's TestStep object
* @return a step name
*/
@Nullable
protected String getStepName(@Nonnull TestStep testStep) {
return testStep instanceof HookTestStep ?
HOOK_ + ((HookTestStep) testStep).getHookType().toString() :
((PickleStepTestStep) testStep).getStep().getText();
protected String getStepName(@Nonnull PickleStepTestStep testStep) {
return testStep.getStep().getText();
}

/**
Expand Down Expand Up @@ -392,7 +389,8 @@ protected TestCaseIdEntry getTestCaseId(@Nonnull TestStep testStep, @Nullable St
* @return a Request to ReportPortal
*/
@Nonnull
protected StartTestItemRQ buildStartStepRequest(@Nonnull TestStep testStep, @Nullable String stepPrefix, @Nullable String keyword) {
protected StartTestItemRQ buildStartStepRequest(@Nonnull PickleStepTestStep testStep, @Nullable String stepPrefix,
@Nullable String keyword) {
StartTestItemRQ rq = new StartTestItemRQ();
rq.setName(Utils.buildName(stepPrefix, keyword, getStepName(testStep)));
rq.setDescription(buildMultilineArgument(testStep));
Expand Down Expand Up @@ -427,24 +425,21 @@ private void addToTree(@Nonnull TestCase scenario, @Nullable String text, @Nulla
* Start Cucumber step
*
* @param testCase Cucumber's TestCase object
* @param testStep a cucumber step object
* @param step a cucumber step object
*/
protected void beforeStep(@Nonnull TestCase testCase, @Nonnull TestStep testStep) {
protected void beforeStep(@Nonnull TestCase testCase, @Nonnull PickleStepTestStep step) {
execute(
testCase, (f, s) -> {
if (testStep instanceof PickleStepTestStep) {
PickleStepTestStep step = (PickleStepTestStep) testStep;
String stepPrefix = step.getStep().getLocation().getLine() < s.getLine() ? BACKGROUND_PREFIX : null;
StartTestItemRQ rq = buildStartStepRequest(testStep, stepPrefix, step.getStep().getKeyword());
Maybe<String> stepId = startStep(s.getId(), rq);
if (rq.isHasStats()) {
descriptionsMap.put(stepId, ofNullable(rq.getDescription()).orElse(StringUtils.EMPTY));
}
s.setStepId(stepId);
String stepText = step.getStep().getText();
if (getLaunch().getParameters().isCallbackReportingEnabled()) {
addToTree(testCase, stepText, stepId);
}
String stepPrefix = step.getStep().getLocation().getLine() < s.getLine() ? BACKGROUND_PREFIX : null;
StartTestItemRQ rq = buildStartStepRequest(step, stepPrefix, step.getStep().getKeyword());
Maybe<String> stepId = startStep(s.getId(), rq);
if (rq.isHasStats()) {
descriptionsMap.put(stepId, ofNullable(rq.getDescription()).orElse(StringUtils.EMPTY));
}
s.setStepId(stepId);
String stepText = step.getStep().getText();
if (getLaunch().getParameters().isCallbackReportingEnabled()) {
addToTree(testCase, stepText, stepId);
}
}
);
Expand All @@ -458,7 +453,7 @@ protected void beforeStep(@Nonnull TestCase testCase, @Nonnull TestStep testStep
* @param result Step result
*/
@SuppressWarnings("unused")
protected void afterStep(@Nonnull TestCase testCase, @Nonnull TestStep testStep, @Nonnull Result result) {
protected void afterStep(@Nonnull TestCase testCase, @Nonnull PickleStepTestStep testStep, @Nonnull Result result) {
execute(
testCase, (f, s) -> {
reportResult(result, null);
Expand Down Expand Up @@ -538,11 +533,6 @@ protected void beforeHooks(@Nonnull TestCase testCase, @Nonnull HookTestStep tes
);
}

private void removeFromTree(TestCase testCase, String text) {
retrieveLeaf(testCase.getUri(), testCase.getLocation().getLine(), itemTree).ifPresent(scenarioLeaf -> scenarioLeaf.getChildItems()
.remove(createKey(text)));
}

/**
* Called when before/after-hooks are finished
*
Expand All @@ -556,11 +546,6 @@ protected void afterHooks(@Nonnull TestCase testCase, @Nonnull HookTestStep step
reportResult(result, (isBefore(step) ? "Before" : "After") + " hook: " + step.getCodeLocation());
finishTestItem(s.getHookId(), mapItemStatus(result.getStatus()));
s.setHookId(Maybe.empty());
if (step.getHookType() == HookType.AFTER_STEP) {
if (step instanceof PickleStepTestStep) {
removeFromTree(testCase, ((PickleStepTestStep) step).getStep().getText());
}
}
}
);
}
Expand Down Expand Up @@ -892,18 +877,22 @@ protected void handleTestStepStarted(@Nonnull TestStepStarted event) {
TestCase testCase = event.getTestCase();
if (testStep instanceof HookTestStep) {
beforeHooks(testCase, (HookTestStep) testStep);
} else if (testStep instanceof PickleStepTestStep) {
beforeStep(testCase, (PickleStepTestStep) testStep);
} else {
beforeStep(testCase, testStep);
LOGGER.warn("Unable to start unknown step type: {}", testStep.getClass().getSimpleName());
}
}

protected void handleTestStepFinished(@Nonnull TestStepFinished event) {
if (event.getTestStep() instanceof HookTestStep) {
TestCase testCase = event.getTestCase();
HookTestStep hookTestStep = (HookTestStep) event.getTestStep();
afterHooks(testCase, hookTestStep, event.getResult());
TestStep testStep = event.getTestStep();
TestCase testCase = event.getTestCase();
if (testStep instanceof HookTestStep) {
afterHooks(testCase, (HookTestStep) testStep, event.getResult());
} else if (testStep instanceof PickleStepTestStep) {
afterStep(testCase, (PickleStepTestStep) testStep, event.getResult());
} else {
afterStep(event.getTestCase(), event.getTestStep(), event.getResult());
LOGGER.warn("Unable to finish unknown step type: {}", testStep.getClass().getSimpleName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import com.epam.reportportal.utils.MemoizingSupplier;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import io.cucumber.plugin.event.HookTestStep;
import io.cucumber.plugin.event.PickleStepTestStep;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestStep;
import io.reactivex.Maybe;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -63,14 +63,15 @@ protected void beforeLaunch() {

@Override
@Nonnull
protected StartTestItemRQ buildStartStepRequest(@Nonnull TestStep testStep, @Nullable String stepPrefix, @Nullable String keyword) {
protected StartTestItemRQ buildStartStepRequest(@Nonnull PickleStepTestStep testStep, @Nullable String stepPrefix,
@Nullable String keyword) {
StartTestItemRQ rq = super.buildStartStepRequest(testStep, stepPrefix, keyword);
rq.setHasStats(false);
return rq;
}

@Override
protected void beforeStep(@Nonnull TestCase testCase, @Nonnull TestStep testStep) {
protected void beforeStep(@Nonnull TestCase testCase, @Nonnull PickleStepTestStep testStep) {
super.beforeStep(testCase, testStep);
String description = buildMultilineArgument(testStep).trim();
if (!description.isEmpty()) {
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/com/epam/reportportal/cucumber/HooksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,28 @@ public class HooksTest {
@CucumberOptions(features = "src/test/resources/features/DummyScenario.feature", glue = {
"com.epam.reportportal.cucumber.integration.hooks.step" }, plugin = { "pretty",
"com.epam.reportportal.cucumber.integration.TestStepReporter" })
public static class StepHooksReporter extends AbstractTestNGCucumberTests {
public static class StepHooksReporterTest extends AbstractTestNGCucumberTests {

}

@CucumberOptions(features = "src/test/resources/features/DummyScenario.feature", glue = {
"com.epam.reportportal.cucumber.integration.hooks.scenario" }, plugin = { "pretty",
"com.epam.reportportal.cucumber.integration.TestStepReporter" })
public static class ScenarioHooksReporter extends AbstractTestNGCucumberTests {
public static class ScenarioHooksReporterTest extends AbstractTestNGCucumberTests {

}

@CucumberOptions(features = "src/test/resources/features/DummyScenario.feature", glue = {
"com.epam.reportportal.cucumber.integration.hooks.all" }, plugin = { "pretty",
"com.epam.reportportal.cucumber.integration.TestStepReporter" })
public static class AllHooksReporter extends AbstractTestNGCucumberTests {
public static class AllHooksReporterTest extends AbstractTestNGCucumberTests {

}

@CucumberOptions(features = "src/test/resources/features/DummyScenario.feature", glue = {
"com.epam.reportportal.cucumber.integration.feature" }, plugin = { "pretty",
"com.epam.reportportal.cucumber.integration.TestStepReporter" })
public static class NoHooksReporter extends AbstractTestNGCucumberTests {
public static class NoHooksReporterTest extends AbstractTestNGCucumberTests {

}

Expand Down Expand Up @@ -96,7 +96,7 @@ public void tearDown() {
@Test
@SuppressWarnings("unchecked")
public void verify_before_after_step_reported_in_steps() {
TestUtils.runTests(StepHooksReporter.class);
TestUtils.runTests(StepHooksReporterTest.class);

verify(client, times(1)).startTestItem(any());
verify(client, times(1)).startTestItem(same(suiteId), any());
Expand All @@ -107,7 +107,7 @@ public void verify_before_after_step_reported_in_steps() {
@Test
@SuppressWarnings("unchecked")
public void verify_before_after_scenario_reported_in_steps() {
TestUtils.runTests(ScenarioHooksReporter.class);
TestUtils.runTests(ScenarioHooksReporterTest.class);

verify(client, times(1)).startTestItem(any());
verify(client, times(1)).startTestItem(same(suiteId), any());
Expand All @@ -118,7 +118,7 @@ public void verify_before_after_scenario_reported_in_steps() {
@Test
@SuppressWarnings("unchecked")
public void verify_before_after_all_reported_in_steps() {
TestUtils.runTests(AllHooksReporter.class);
TestUtils.runTests(AllHooksReporterTest.class);

verify(client, times(1)).startTestItem(any());
verify(client, times(1)).startTestItem(same(suiteId), any());
Expand All @@ -131,7 +131,7 @@ public void verify_before_after_all_reported_in_steps() {
@Test
@SuppressWarnings("unchecked")
public void verify_before_after_not_reported_in_steps() {
TestUtils.runTests(NoHooksReporter.class);
TestUtils.runTests(NoHooksReporterTest.class);

verify(client, times(1)).startTestItem(any());
verify(client, times(1)).startTestItem(same(suiteId), any());
Expand Down

0 comments on commit d78c531

Please sign in to comment.