Skip to content

Commit

Permalink
Add scenario parameters reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Dec 18, 2023
1 parent fbb4433 commit 7d0e6d1
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import com.epam.reportportal.service.item.TestCaseIdEntry;
import com.epam.reportportal.utils.AttributeParser;
import com.epam.reportportal.utils.MemoizingSupplier;
import com.epam.reportportal.utils.ParameterUtils;
import com.epam.reportportal.utils.TestCaseIdUtils;
import com.epam.reportportal.utils.properties.SystemAttributesExtractor;
import com.epam.ta.reportportal.ws.model.FinishExecutionRQ;
import com.epam.ta.reportportal.ws.model.FinishTestItemRQ;
import com.epam.ta.reportportal.ws.model.ParameterResource;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import com.epam.ta.reportportal.ws.model.attribute.ItemAttributesRQ;
import com.epam.ta.reportportal.ws.model.launch.StartLaunchRQ;
Expand All @@ -41,7 +43,6 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -56,9 +57,9 @@ public class ReportPortalPublisher {
public static final String EXAMPLE_CODE_REFERENCE_PATTERN = "%s/[EXAMPLE:%s%s]";

private static final Logger LOGGER = LoggerFactory.getLogger(ReportPortalPublisher.class);
private final ConcurrentHashMap<String, Maybe<String>> featureIdMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Maybe<String>> scenarioIdMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Maybe<String>, Long> stepStartTimeMap = new ConcurrentHashMap<>();
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> stepId;

private final MemoizingSupplier<Launch> launch;
Expand Down Expand Up @@ -191,12 +192,12 @@ protected StartTestItemRQ buildStartTestItemRq(@Nonnull String name, @Nonnull Da
/**
* Customize start step test item event/request
*
* @param name item's name
* @param startTime item's start time
* @param step Karate's Step class instance
* @return request to ReportPortal
*/
protected StartTestItemRQ buildStartStepRq(@Nonnull String name, @Nonnull Date startTime) {
StartTestItemRQ rq = buildStartTestItemRq(name, startTime, ItemType.STEP);
protected StartTestItemRQ buildStartStepRq(@Nonnull Step step) {
String stepName = step.getPrefix() + " " + step.getText();
StartTestItemRQ rq = buildStartTestItemRq(stepName, getStepStartTime(stepStartTimeMap, stepId), ItemType.STEP);
rq.setHasStats(false);
return rq;
}
Expand Down Expand Up @@ -228,6 +229,19 @@ protected StartTestItemRQ buildStartFeatureRq(@Nonnull FeatureResult featureResu
return rq;
}

@Nullable
private List<ParameterResource> getParameters(@Nonnull Scenario scenario) {
if (scenario.getExampleIndex() < 0) {
return null;
}
return scenario.getExampleData().entrySet().stream().map(e -> {
ParameterResource parameterResource = new ParameterResource();
parameterResource.setKey(e.getKey());
parameterResource.setValue(ofNullable(e.getValue()).map(Object::toString).orElse(ParameterUtils.NULL_VALUE));
return parameterResource;
}).collect(Collectors.toList());
}

/**
* Build ReportPortal request for start Scenario event
*
Expand All @@ -242,6 +256,7 @@ protected StartTestItemRQ buildStartScenarioRq(@Nonnull ScenarioResult scenarioR
rq.setCodeRef(getCodeRef(scenario));
rq.setTestCaseId(ofNullable(getTestCaseId(scenario)).map(TestCaseIdEntry::getId).orElse(null));
rq.setAttributes(toAttributes(scenario.getTags()));
rq.setParameters(getParameters(scenario));
return rq;
}

Expand Down Expand Up @@ -338,8 +353,7 @@ public void finishScenario(ScenarioResult scenarioResult) {
* @param scenarioResult scenario result
*/
public void startStep(StepResult stepResult, ScenarioResult scenarioResult) {
String stepName = stepResult.getStep().getPrefix() + " " + stepResult.getStep().getText();
StartTestItemRQ rq = buildStartStepRq(stepName, getStepStartTime(stepStartTimeMap, stepId));
StartTestItemRQ rq = buildStartStepRq(stepResult.getStep());
stepId = launch.get().startTestItem(scenarioIdMap.get(scenarioResult.getScenario().getName()), rq);
stepStartTimeMap.put(stepId, rq.getStartTime().getTime());
}
Expand Down Expand Up @@ -441,7 +455,7 @@ private String getLogLevel(String status) {
* @param stepId step ID.
* @return step new startTime in Date format.
*/
private Date getStepStartTime(ConcurrentHashMap<Maybe<String>, Long> stepStartTimeMap, Maybe<String> stepId) {
private Date getStepStartTime(Map<Maybe<String>, Long> stepStartTimeMap, Maybe<String> stepId) {
long currentStepStartTime = Calendar.getInstance().getTime().getTime();

if (!stepStartTimeMap.keySet().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.epam.reportportal.karate.parameters;

import com.epam.reportportal.karate.utils.TestUtils;
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.ParameterResource;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
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.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.Mockito.*;

public class ExamplesScenarioParametersTest {
private final String featureId = CommonUtils.namedId("feature_");
private final List<String> exampleIds = Stream.generate(() -> CommonUtils.namedId("example_")).limit(2)
.collect(Collectors.toList());
private final List<Pair<String, List<String>>> stepIds = exampleIds.stream()
.map(e -> Pair.of(e, Stream.generate(() -> CommonUtils.namedId("step_"))
.limit(2).collect(Collectors.toList())))
.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, stepIds);
mockBatchLogging(client);
}

@Test
public void test_examples_scenario_parameters() {
var results = TestUtils.runAsReport(rp, "classpath:feature/examples.feature");
assertThat(results.getFailCount(), equalTo(0));

ArgumentCaptor<StartTestItemRQ> captor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(1)).startTestItem(captor.capture());
verify(client, times(2)).startTestItem(same(featureId), captor.capture());
verify(client, times(2)).startTestItem(same(exampleIds.get(0)), captor.capture());
verify(client, times(2)).startTestItem(same(exampleIds.get(1)), captor.capture());

List<StartTestItemRQ> items = captor.getAllValues();
assertThat(items, hasSize(7));

StartTestItemRQ firstScenarioRq = items.get(1);
StartTestItemRQ secondScenarioRq = items.get(2);
List<ParameterResource> firstParameters = firstScenarioRq.getParameters();
List<ParameterResource> secondParameters = secondScenarioRq.getParameters();
assertThat(firstParameters, hasSize(3));
assertThat(secondParameters, hasSize(3));

assertThat(
firstParameters.stream().map(p -> p.getKey() + ":" + p.getValue()).collect(Collectors.toSet()),
allOf(hasItem("vara:2"), hasItem("varb:2"), hasItem("result:4"))
);
assertThat(
secondParameters.stream().map(p -> p.getKey() + ":" + p.getValue()).collect(Collectors.toSet()),
allOf(hasItem("vara:1"), hasItem("varb:2"), hasItem("result:3"))
);
}
}

0 comments on commit 7d0e6d1

Please sign in to comment.