Skip to content

Commit 102bb2e

Browse files
committed
Fix Steps bubbling in assertj aspect
1 parent 5dc185e commit 102bb2e

4 files changed

Lines changed: 93 additions & 18 deletions

File tree

allure-assertj/src/main/java/io/qameta/allure/assertj/AllureAspectJ.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
import org.aspectj.lang.annotation.Aspect;
2828
import org.aspectj.lang.annotation.Before;
2929
import org.aspectj.lang.annotation.Pointcut;
30-
import org.aspectj.lang.reflect.MethodSignature;
3130
import org.slf4j.Logger;
3231
import org.slf4j.LoggerFactory;
3332

33+
import java.util.Optional;
3434
import java.util.UUID;
3535
import java.util.stream.Collectors;
3636
import java.util.stream.Stream;
@@ -88,12 +88,9 @@ public void logAssertCreation(final JoinPoint joinPoint) {
8888

8989
@Before("anyAssert()")
9090
public void stepStart(final JoinPoint joinPoint) {
91-
final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
92-
9391
final String uuid = UUID.randomUUID().toString();
94-
final String name = joinPoint.getArgs().length > 0
95-
? String.format("%s \'%s\'", methodSignature.getName(), arrayToString(joinPoint.getArgs()))
96-
: methodSignature.getName();
92+
final String name = getStepName(joinPoint);
93+
if (isStartBubbling(name)) return;
9794

9895
final StepResult result = new StepResult()
9996
.setName(name);
@@ -102,15 +99,21 @@ public void stepStart(final JoinPoint joinPoint) {
10299
}
103100

104101
@AfterThrowing(pointcut = "anyAssert()", throwing = "e")
105-
public void stepFailed(final Throwable e) {
102+
public void stepFailed(final JoinPoint joinPoin, final Throwable e) {
103+
final String name = getStepName(joinPoin);
104+
if (isEndBubbling(name)) return;
105+
106106
getLifecycle().updateStep(s -> s
107107
.setStatus(getStatus(e).orElse(Status.BROKEN))
108108
.setStatusDetails(getStatusDetails(e).orElse(null)));
109109
getLifecycle().stopStep();
110110
}
111111

112112
@AfterReturning(pointcut = "anyAssert()")
113-
public void stepStop() {
113+
public void stepStop(final JoinPoint joinPoin) {
114+
final String name = getStepName(joinPoin);
115+
if (isEndBubbling(name)) return;
116+
114117
getLifecycle().updateStep(s -> s.setStatus(Status.PASSED));
115118
getLifecycle().stopStep();
116119
}
@@ -133,4 +136,24 @@ private static String arrayToString(final Object... array) {
133136
.map(ObjectUtils::toString)
134137
.collect(Collectors.joining(" "));
135138
}
139+
140+
private String getStepName(JoinPoint joinPoint) {
141+
return joinPoint.getArgs().length > 0
142+
? String.format("%s \'%s\'", joinPoint.getSignature().getName(), arrayToString(joinPoint.getArgs()))
143+
: joinPoint.getSignature().getName();
144+
}
145+
146+
private boolean isStartBubbling(String stepName) {
147+
final Optional<StepResult> lastResult = getLifecycle().getCurrentStepResult();
148+
if (!lastResult.isPresent()) return false;
149+
if (lastResult.get().getStop() != null) return false;
150+
return stepName.equals(lastResult.get().getName());
151+
}
152+
153+
private boolean isEndBubbling(String stepName) {
154+
final Optional<StepResult> lastResult = getLifecycle().getCurrentStepResult();
155+
if (!lastResult.isPresent()) return true;
156+
if (lastResult.get().getStop() != null) return true;
157+
return !stepName.equals(lastResult.get().getName());
158+
}
136159
}

allure-assertj/src/test/java/io/qameta/allure/assertj/AllureAspectJTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424

2525
import java.nio.charset.StandardCharsets;
2626
import java.util.Arrays;
27+
import java.util.List;
28+
import java.util.Map;
2729

2830
import static io.qameta.allure.test.RunUtils.runWithinTestContext;
2931
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.assertj.core.api.Assertions.assertThatList;
33+
import static org.assertj.core.api.MapAssert.assertThatMap;
3034

3135
/**
3236
* @author charlie (Dmitry Baev).
@@ -121,4 +125,27 @@ void softAssertions() {
121125
.extracting(StepResult::getName)
122126
.contains("as 'Test description []'", "isEqualTo '26'");
123127
}
128+
129+
@Test
130+
void preventListAssertionsBubblingTest() {
131+
final AllureResults results = runWithinTestContext(
132+
() -> assertThatList(List.of("value1")).isEqualTo(List.of("value1", "value2")),
133+
AllureAspectJ::setLifecycle);
134+
135+
assertThat(results.getTestResults()).isNotEmpty();
136+
assertThat(results.getTestResults().get(0).getSteps()).hasSize(2);
137+
assertThat(results.getTestResults().get(0).getSteps().get(1).getSteps()).isEmpty();
138+
}
139+
140+
@Test
141+
void preventMapAssertionsBubblingTest() {
142+
final AllureResults results = runWithinTestContext(
143+
() -> assertThatMap(Map.of("key1", "value1"))
144+
.isEqualTo(Map.of("key1", "value1", "key2", "value2")),
145+
AllureAspectJ::setLifecycle);
146+
147+
assertThat(results.getTestResults()).isNotEmpty();
148+
assertThat(results.getTestResults().get(0).getSteps()).hasSize(2);
149+
assertThat(results.getTestResults().get(0).getSteps().get(1).getSteps()).isEmpty();
150+
}
124151
}

allure-java-commons/src/main/java/io/qameta/allure/AllureLifecycle.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,19 @@ public Optional<String> getCurrentTestCaseOrStep() {
299299
return threadContext.getCurrent();
300300
}
301301

302+
/**
303+
* Return optional current step result object.
304+
*
305+
* @return the optional wrapper of the current StepResult object.
306+
*/
307+
public Optional<StepResult> getCurrentStepResult() {
308+
final Optional<String> currentUuid = threadContext.getCurrent();
309+
if (!currentUuid.isPresent()) {
310+
return Optional.empty();
311+
}
312+
return storage.getStep(currentUuid.get());
313+
}
314+
302315
/**
303316
* Sets specified test case uuid as current. Note that
304317
* test case with such uuid should be created and existed in storage, otherwise

allure-java-commons/src/test/java/io/qameta/allure/AllureLifecycleTest.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515
*/
1616
package io.qameta.allure;
1717

18+
import io.qameta.allure.model.*;
1819
import io.qameta.allure.model.Attachment;
19-
import io.qameta.allure.model.FixtureResult;
20-
import io.qameta.allure.model.StepResult;
21-
import io.qameta.allure.model.TestResult;
22-
import io.qameta.allure.model.TestResultContainer;
2320
import io.qameta.allure.test.AllureResults;
2421
import io.qameta.allure.test.RunUtils;
2522
import org.junit.jupiter.api.BeforeEach;
@@ -30,23 +27,20 @@
3027
import java.io.ByteArrayInputStream;
3128
import java.io.InputStream;
3229
import java.nio.charset.StandardCharsets;
33-
import java.util.ArrayList;
34-
import java.util.Collection;
35-
import java.util.List;
36-
import java.util.Map;
37-
import java.util.Objects;
38-
import java.util.Optional;
30+
import java.util.*;
3931
import java.util.concurrent.Callable;
4032
import java.util.concurrent.CompletableFuture;
4133
import java.util.concurrent.CopyOnWriteArrayList;
4234
import java.util.concurrent.ExecutorService;
4335
import java.util.concurrent.Executors;
4436
import java.util.concurrent.Future;
4537
import java.util.concurrent.TimeUnit;
38+
import java.util.concurrent.atomic.AtomicReference;
4639
import java.util.function.Supplier;
4740
import java.util.stream.Collectors;
4841

4942
import static io.qameta.allure.Allure.addStreamAttachmentAsync;
43+
import static io.qameta.allure.test.RunUtils.runWithinTestContext;
5044
import static io.qameta.allure.test.TestData.randomId;
5145
import static io.qameta.allure.test.TestData.randomName;
5246
import static io.qameta.allure.test.TestData.randomString;
@@ -488,4 +482,22 @@ public Void call() {
488482
return null;
489483
}
490484
}
485+
486+
@Test
487+
void getCurrentStepResultTest() {
488+
final AtomicReference<AllureLifecycle> lifecycle = new AtomicReference<>();
489+
runWithinTestContext(() -> {
490+
assertThat(lifecycle.get().getCurrentStepResult()).isEmpty();
491+
492+
final StepResult testStepResult = new StepResult();
493+
testStepResult.setName("test step");
494+
testStepResult.setStatus(Status.PASSED);
495+
496+
lifecycle.get().startStep(UUID.randomUUID().toString(), testStepResult);
497+
assertThat(lifecycle.get().getCurrentStepResult()).isPresent().isEqualTo(testStepResult);
498+
499+
lifecycle.get().stopStep();
500+
assertThat(lifecycle.get().getCurrentStepResult()).isEmpty();
501+
}, lifecycle::set);
502+
}
491503
}

0 commit comments

Comments
 (0)