Skip to content

Commit

Permalink
Background logic update
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Dec 21, 2023
1 parent 226962f commit a7c5bda
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import static org.apache.commons.lang3.StringUtils.isNotBlank;

public class ReportPortalPublisher {
public static final String BACKGROUND_PREFIX = "BACKGROUND: ";
public static final String SCENARIO_CODE_REFERENCE_PATTERN = "%s/[SCENARIO:%s]";
public static final String EXAMPLE_CODE_REFERENCE_PATTERN = "%s/[EXAMPLE:%s%s]";
public static final String VARIABLE_PATTERN =
Expand All @@ -66,6 +65,7 @@ public class ReportPortalPublisher {
private final Map<String, Maybe<String>> featureIdMap = new HashMap<>();
private final Map<String, Maybe<String>> scenarioIdMap = new HashMap<>();
private final Map<Maybe<String>, Long> stepStartTimeMap = new HashMap<>();
private Maybe<String> backgroundId;
private Maybe<String> stepId;

private final MemoizingSupplier<Launch> launch;
Expand Down Expand Up @@ -307,6 +307,7 @@ public void finishFeature(FeatureResult featureResult) {
featureResult.isFailed() ? ItemStatus.FAILED : ItemStatus.PASSED);
//noinspection ReactiveStreamsUnusedPublisher
launch.get().finishTestItem(featureIdMap.remove(featureResult.getCallNameForReport()), rq);
backgroundId = null;
}

/**
Expand Down Expand Up @@ -339,6 +340,14 @@ public void finishScenario(ScenarioResult scenarioResult) {
launch.get().finishTestItem(removedScenarioId, rq);
}

@Nonnull
@SuppressWarnings("unused")
protected StartTestItemRQ buildStartBackgroundRq(@Nonnull StepResult stepResult, @Nonnull ScenarioResult scenarioResult) {
StartTestItemRQ rq = buildStartTestItemRq(Background.KEYWORD, Calendar.getInstance().getTime(), ItemType.STEP);
rq.setHasStats(false);
return rq;
}

/**
* Customize start step test item event/request
*
Expand All @@ -350,9 +359,6 @@ public void finishScenario(ScenarioResult scenarioResult) {
protected StartTestItemRQ buildStartStepRq(@Nonnull StepResult stepResult, @Nonnull ScenarioResult scenarioResult) {
Step step = stepResult.getStep();
String stepName = step.getPrefix() + " " + step.getText();
if (step.isBackground()) {
stepName = BACKGROUND_PREFIX + stepName;
}
StartTestItemRQ rq = buildStartTestItemRq(stepName, getStepStartTime(stepStartTimeMap, stepId), ItemType.STEP);
rq.setHasStats(false);
if (step.isOutline()) {
Expand Down Expand Up @@ -382,8 +388,20 @@ protected StartTestItemRQ buildStartStepRq(@Nonnull StepResult stepResult, @Nonn
* @param scenarioResult scenario result
*/
public void startStep(StepResult stepResult, ScenarioResult scenarioResult) {
if (stepResult.getStep().isBackground()) {
backgroundId = ofNullable(backgroundId).orElseGet(() -> {
StartTestItemRQ backgroundRq = buildStartBackgroundRq(stepResult, scenarioResult);
return launch.get().startTestItem(scenarioIdMap.get(scenarioResult.getScenario().getName()), backgroundRq);
});
} else {
backgroundId = null;
}
StartTestItemRQ stepRq = buildStartStepRq(stepResult, scenarioResult);
stepId = launch.get().startTestItem(scenarioIdMap.get(scenarioResult.getScenario().getName()), stepRq);
stepId = launch.get()
.startTestItem(
backgroundId != null ? backgroundId : scenarioIdMap.get(scenarioResult.getScenario().getName()),
stepRq
);
stepStartTimeMap.put(stepId, stepRq.getStartTime().getTime());
ofNullable(stepRq.getParameters())
.filter(params -> !params.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
package com.epam.reportportal.karate.background;

import com.epam.reportportal.karate.utils.TestUtils;
import com.epam.reportportal.listeners.ItemType;
import com.epam.reportportal.service.ReportPortal;
import com.epam.reportportal.service.ReportPortalClient;
import com.epam.reportportal.util.test.CommonUtils;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import com.intuit.karate.core.Background;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.epam.reportportal.karate.utils.TestUtils.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.*;

public class BackgroundTest {
private final String featureId = CommonUtils.namedId("feature_");
private final String scenarioId = CommonUtils.namedId("scenario_");
private final List<String> stepIds = Stream.generate(() -> CommonUtils.namedId("step_"))
.limit(3).collect(Collectors.toList());
private final List<Pair<String, String>> nestedStepIds = stepIds.stream()
.map(id -> Pair.of(id, CommonUtils.namedId("nested_step_"))).collect(Collectors.toList());

private final ReportPortalClient client = mock(ReportPortalClient.class);
private final ReportPortal rp = ReportPortal.create(client, standardParameters(), testExecutor());

@BeforeEach
public void setupMock() {
mockLaunch(client, null, featureId, scenarioId, stepIds);
mockNestedSteps(client, nestedStepIds);
mockBatchLogging(client);
}

Expand All @@ -45,15 +51,26 @@ public void test_background_steps() {
verify(client, times(1)).startTestItem(same(featureId), captor.capture());
ArgumentCaptor<StartTestItemRQ> stepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(3)).startTestItem(same(scenarioId), stepCaptor.capture());
ArgumentCaptor<StartTestItemRQ> nestedStepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client).startTestItem(startsWith("step_"), nestedStepCaptor.capture());

List<StartTestItemRQ> items = captor.getAllValues();
assertThat(items, hasSize(2));
List<StartTestItemRQ> steps = stepCaptor.getAllValues();
assertThat(steps, hasSize(3));

Set<String> stepNames = steps.stream().map(StartTestItemRQ::getName).collect(Collectors.toSet());
List<StartTestItemRQ> backgroundSteps = steps.stream()
.filter(s -> s.getName().startsWith(Background.KEYWORD)).collect(Collectors.toList());
assertThat(backgroundSteps, hasSize(1));
StartTestItemRQ backgroundStep = backgroundSteps.get(0);
assertThat(backgroundStep.getName(), equalTo(Background.KEYWORD)); // No name for Background in Karate
assertThat(backgroundStep.isHasStats(), equalTo(Boolean.FALSE));
assertThat(backgroundStep.getStartTime(), notNullValue());
assertThat(backgroundStep.getType(), equalTo(ItemType.STEP.name()));

assertThat(stepNames, allOf(hasItem("BACKGROUND: Given def four = 4"), hasItem("When def actualFour = 2 * 2"),
hasItem("Then assert actualFour == four")));
List<StartTestItemRQ> nestedSteps = nestedStepCaptor.getAllValues();
assertThat(nestedSteps, hasSize(1));
StartTestItemRQ nestedStep = nestedSteps.get(0);
assertThat(nestedStep.getName(), equalTo("Given def four = 4"));
}
}

0 comments on commit a7c5bda

Please sign in to comment.