Skip to content

Commit

Permalink
Merge pull request #37 from reportportal/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
HardNorth authored Sep 14, 2024
2 parents 37f211e + 9bd04d4 commit e0242ca
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 28 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
java-version: '11'

- name: Build with Gradle
timeout-minutes: 30
run: ./gradlew build

- name: Codecov upload
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## [Unreleased]
### Fixed
- Issue [#32](https://github.com/reportportal/agent-java-karate/issues/32): Handling of calling scenario in the same Feature file, by @HardNorth

## [5.1.0]
### Changed
Expand Down
55 changes: 29 additions & 26 deletions src/main/java/com/epam/reportportal/karate/ReportPortalHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,30 +162,34 @@ protected StartTestItemRQ buildStartFeatureRq(@Nonnull FeatureRuntime fr) {
return rq;
}

private String getFeatureNameForReport(FeatureRuntime fr) {
int callDepth = ofNullable(fr.caller).map(c -> c.depth).orElse(0);
return callDepth + ":" + fr.featureCall.feature.getNameForReport();
}

@Override
public boolean beforeFeature(FeatureRuntime fr) {
StartTestItemRQ rq = buildStartFeatureRq(fr);
featureIdMap.computeIfAbsent(fr.featureCall.feature.getNameForReport(),
f -> new MemoizingSupplier<>(() -> {
if(fr.caller == null || fr.caller.depth == 0) {
return launch.get().startTestItem(rq);
} else {
Maybe<String> scenarioId = scenarioIdMap.get(fr.caller.parentRuntime.scenario.getUniqueId());
if (scenarioId == null) {
LOGGER.error("ERROR: Trying to post unspecified scenario.");
return launch.get().startTestItem(rq);
}
rq.setType(ItemType.STEP.name());
rq.setHasStats(false);
rq.setName(getInnerFeatureName(rq.getName()));
Maybe<String> itemId = launch.get().startTestItem(scenarioId, rq);
innerFeatures.add(itemId);
if (StringUtils.isNotBlank(rq.getDescription())) {
ReportPortalUtils.sendLog(itemId, rq.getDescription(), LogLevel.INFO, rq.getStartTime());
}
return itemId;
}
}));
featureIdMap.computeIfAbsent(getFeatureNameForReport(fr), f -> new MemoizingSupplier<>(() -> {
if (ofNullable(fr.caller).map(c -> c.depth).orElse(0) == 0) {
return launch.get().startTestItem(rq);
} else {
Maybe<String> scenarioId = scenarioIdMap.get(fr.caller.parentRuntime.scenario.getUniqueId());
if (scenarioId == null) {
LOGGER.error("ERROR: Trying to post unspecified scenario.");
return launch.get().startTestItem(rq);
}
rq.setType(ItemType.STEP.name());
rq.setHasStats(false);
rq.setName(getInnerFeatureName(rq.getName()));
Maybe<String> itemId = launch.get().startTestItem(scenarioId, rq);
innerFeatures.add(itemId);
if (StringUtils.isNotBlank(rq.getDescription())) {
ReportPortalUtils.sendLog(itemId, rq.getDescription(), LogLevel.INFO, rq.getStartTime());
}
return itemId;
}
}));
return true;
}

Expand All @@ -202,7 +206,7 @@ protected FinishTestItemRQ buildFinishFeatureRq(@Nonnull FeatureRuntime fr) {

@Override
public void afterFeature(FeatureRuntime fr) {
Optional<Maybe<String>> optionalId = ofNullable(featureIdMap.remove(fr.featureCall.feature.getNameForReport())).map(Supplier::get);
Optional<Maybe<String>> optionalId = ofNullable(featureIdMap.remove(getFeatureNameForReport(fr))).map(Supplier::get);
if (optionalId.isEmpty()) {
LOGGER.error("ERROR: Trying to finish unspecified feature.");
}
Expand All @@ -222,8 +226,7 @@ public void afterFeature(FeatureRuntime fr) {
@Nonnull
protected StartTestItemRQ buildStartScenarioRq(@Nonnull ScenarioRuntime sr) {
StartTestItemRQ rq = ReportPortalUtils.buildStartScenarioRq(sr.result);
ofNullable(featureIdMap.get(sr.featureRuntime.featureCall.feature.getNameForReport()))
.map(Supplier::get)
ofNullable(featureIdMap.get(getFeatureNameForReport(sr.featureRuntime))).map(Supplier::get)
.map(featureId -> innerFeatures.contains(featureId) ? featureId : null)
.ifPresent(featureId -> {
rq.setType(ItemType.STEP.name());
Expand All @@ -236,8 +239,8 @@ protected StartTestItemRQ buildStartScenarioRq(@Nonnull ScenarioRuntime sr) {
@Override
public boolean beforeScenario(ScenarioRuntime sr) {
StartTestItemRQ rq = buildStartScenarioRq(sr);
Optional<Maybe<String>> optionalId = ofNullable(featureIdMap.get(sr.featureRuntime.featureCall.feature.getNameForReport()))
.map(Supplier::get);
Optional<Maybe<String>> optionalId = ofNullable(featureIdMap.get(getFeatureNameForReport(sr.featureRuntime))).map(
Supplier::get);
if (optionalId.isEmpty()) {
LOGGER.error("ERROR: Trying to post unspecified feature.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2024 EPAM Systems
*
* 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 com.epam.reportportal.karate.call;

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.StartTestItemRQ;
import com.intuit.karate.Results;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.mockito.ArgumentCaptor;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
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.Mockito.*;

public class CallTheSameFeatureWithParametersHookTest {
private static final String TEST_FEATURE = "classpath:feature/call_same_feature.feature";
private final String featureId = CommonUtils.namedId("feature_");
private final String scenarioId = CommonUtils.namedId("scenario_");
private final String innerFeatureId = CommonUtils.namedId("feature_step_");
private final List<String> stepIds = Arrays.asList(CommonUtils.namedId("step_"), CommonUtils.namedId("step_"),
CommonUtils.namedId("step_"), innerFeatureId);
private final String innerScenarioId = CommonUtils.namedId("scenario_step_");
private final List<String> innerStepIds = Stream.generate(() -> CommonUtils.namedId("inner_step_"))
.limit(4)
.collect(Collectors.toList());

private final List<Pair<String, Collection<Pair<String, List<String>>>>> features = Stream.of(Pair.of(featureId,
(Collection<Pair<String, List<String>>>) Collections.singletonList(Pair.of(scenarioId, stepIds))
))
.collect(Collectors.toList());
private final List<Pair<String, String>> nestedSteps = Stream.concat(
Stream.of(Pair.of(innerFeatureId, innerScenarioId)),
innerStepIds.stream().map(id -> Pair.of(innerScenarioId, id))
).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);
mockFeatures(client, features);
mockNestedSteps(client, nestedSteps);
mockBatchLogging(client);
}

@Test
@Timeout(value = 30, threadMode = Timeout.ThreadMode.SEPARATE_THREAD)
public void test_call_feature_with_parameters_hook_reporting() {
Results results = TestUtils.runAsHook(rp, TEST_FEATURE);
assertThat(results.getFailCount(), equalTo(0));

ArgumentCaptor<StartTestItemRQ> featureCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(1)).startTestItem(featureCaptor.capture());
ArgumentCaptor<StartTestItemRQ> scenarioCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client).startTestItem(same(featureId), scenarioCaptor.capture());
ArgumentCaptor<StartTestItemRQ> stepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(4)).startTestItem(same(scenarioId), stepCaptor.capture());
ArgumentCaptor<StartTestItemRQ> innerScenarioCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client).startTestItem(same(innerFeatureId), innerScenarioCaptor.capture());
ArgumentCaptor<StartTestItemRQ> innerStepCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class);
verify(client, times(4)).startTestItem(same(innerScenarioId), innerStepCaptor.capture());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.epam.reportportal.karate.description;
package com.epam.reportportal.karate.call;

import com.epam.reportportal.karate.utils.TestUtils;
import com.epam.reportportal.service.ReportPortal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.epam.reportportal.karate.description;
package com.epam.reportportal.karate.call;

import com.epam.reportportal.karate.utils.TestUtils;
import com.epam.reportportal.service.ReportPortal;
Expand Down
13 changes: 13 additions & 0 deletions src/test/resources/feature/call_same_feature.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: the very basic test to run by Karate

@ignore @execute-test
Scenario: Power of two
* def expected = expectedValue
* def powValue = testValue
* def actual = powValue * powValue
* assert actual == expected

Scenario: Verify math
* def expectedValue = 4
* def testValue = 2
* call read('@execute-test') { expectedValue: #(expectedValue), testValue: #(testValue) }

0 comments on commit e0242ca

Please sign in to comment.