|
| 1 | +/* |
| 2 | + * Copyright 2026 EPAM Systems |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.epam.reportportal.cucumber; |
| 18 | + |
| 19 | +import com.epam.reportportal.cucumber.integration.TestScenarioReporter; |
| 20 | +import com.epam.reportportal.cucumber.integration.feature.RetrySteps; |
| 21 | +import com.epam.reportportal.cucumber.integration.testng.RetryAnalyzer; |
| 22 | +import com.epam.reportportal.cucumber.integration.util.TestUtils; |
| 23 | +import com.epam.reportportal.listeners.ListenerParameters; |
| 24 | +import com.epam.reportportal.service.ReportPortal; |
| 25 | +import com.epam.reportportal.service.ReportPortalClient; |
| 26 | +import com.epam.reportportal.util.test.CommonUtils; |
| 27 | +import com.epam.ta.reportportal.ws.model.StartTestItemRQ; |
| 28 | +import io.cucumber.testng.AbstractTestNGCucumberTests; |
| 29 | +import io.cucumber.testng.CucumberOptions; |
| 30 | +import org.apache.commons.lang3.tuple.Pair; |
| 31 | +import org.junit.jupiter.api.AfterEach; |
| 32 | +import org.junit.jupiter.api.BeforeEach; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.mockito.ArgumentCaptor; |
| 35 | +import org.testng.IAnnotationTransformer; |
| 36 | +import org.testng.annotations.ITestAnnotation; |
| 37 | + |
| 38 | +import java.lang.reflect.Constructor; |
| 39 | +import java.lang.reflect.Method; |
| 40 | +import java.util.List; |
| 41 | +import java.util.concurrent.ExecutorService; |
| 42 | +import java.util.concurrent.Executors; |
| 43 | +import java.util.stream.Collectors; |
| 44 | +import java.util.stream.IntStream; |
| 45 | +import java.util.stream.Stream; |
| 46 | + |
| 47 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 48 | +import static org.hamcrest.Matchers.*; |
| 49 | +import static org.mockito.ArgumentMatchers.same; |
| 50 | +import static org.mockito.Mockito.*; |
| 51 | + |
| 52 | +public class TestNgRetryDetectionTest { |
| 53 | + public static class AnnotationTransformer implements IAnnotationTransformer { |
| 54 | + @Override |
| 55 | + public void transform(ITestAnnotation annotation, Class testClass, Constructor constructor, Method method) { |
| 56 | + annotation.setRetryAnalyzer(RetryAnalyzer.class); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @CucumberOptions(features = "src/test/resources/features/TestNgRetry.feature", glue = { |
| 61 | + "com.epam.reportportal.cucumber.integration.feature" }, plugin = { |
| 62 | + "com.epam.reportportal.cucumber.integration.TestScenarioReporter" }) |
| 63 | + public static class RetryCucumberTest extends AbstractTestNGCucumberTests { |
| 64 | + } |
| 65 | + |
| 66 | + private final String launchId = CommonUtils.namedId("launch_"); |
| 67 | + private final String suiteId = CommonUtils.namedId("feature_"); |
| 68 | + private final List<String> testIds = Stream.generate(() -> CommonUtils.namedId("scenario_")).limit(2).collect(Collectors.toList()); |
| 69 | + private final List<String> stepIds = Stream.generate(() -> CommonUtils.namedId("step_")).limit(4).collect(Collectors.toList()); |
| 70 | + private final List<Pair<String, List<String>>> steps = IntStream.range(0, testIds.size()) |
| 71 | + .mapToObj(i -> Pair.of(testIds.get(i), stepIds.subList(i * 2, i * 2 + 2))) |
| 72 | + .collect(Collectors.toList()); |
| 73 | + |
| 74 | + private final ListenerParameters params = TestUtils.standardParameters(); |
| 75 | + private final ReportPortalClient client = mock(ReportPortalClient.class); |
| 76 | + private final ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 77 | + private final ReportPortal reportPortal = ReportPortal.create(client, params, executorService); |
| 78 | + |
| 79 | + @BeforeEach |
| 80 | + public void setup() { |
| 81 | + TestUtils.mockLaunch(client, launchId, suiteId, steps); |
| 82 | + TestUtils.mockLogging(client); |
| 83 | + TestScenarioReporter.RP.set(reportPortal); |
| 84 | + RetrySteps.reset(); |
| 85 | + } |
| 86 | + |
| 87 | + @AfterEach |
| 88 | + public void tearDown() { |
| 89 | + CommonUtils.shutdownExecutorService(executorService); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void verify_retry_flags_and_links() { |
| 94 | + TestUtils.runTestsWithListener(AnnotationTransformer.class, RetryCucumberTest.class); |
| 95 | + |
| 96 | + ArgumentCaptor<StartTestItemRQ> startScenarioCaptor = ArgumentCaptor.forClass(StartTestItemRQ.class); |
| 97 | + verify(client, times(2)).startTestItem(same(suiteId), startScenarioCaptor.capture()); |
| 98 | + |
| 99 | + List<StartTestItemRQ> scenarioStarts = startScenarioCaptor.getAllValues(); |
| 100 | + assertThat(scenarioStarts, hasSize(2)); |
| 101 | + |
| 102 | + StartTestItemRQ firstAttempt = scenarioStarts.get(0); |
| 103 | + assertThat(firstAttempt.isRetry(), nullValue()); |
| 104 | + assertThat(firstAttempt.getRetryOf(), nullValue()); |
| 105 | + |
| 106 | + StartTestItemRQ secondAttempt = scenarioStarts.get(1); |
| 107 | + assertThat(secondAttempt.isRetry(), equalTo(true)); |
| 108 | + assertThat(secondAttempt.getRetryOf(), equalTo(testIds.get(0))); |
| 109 | + } |
| 110 | +} |
0 commit comments